forked from stepcode/stepcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexact.c
More file actions
488 lines (436 loc) · 13.8 KB
/
lexact.c
File metadata and controls
488 lines (436 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* This software was developed by U.S. Government employees as part of
* their official duties and is not subject to copyright.
*
* $Log: lexact.c,v $
* Revision 1.10 1997/09/24 20:05:38 dar
* scanner went into infinite loop with unmatched single quote at eol
*
* Revision 1.10 1997/09/24 15:56:35 libes
* scanner went into infinite loop with unmatched single quote at eol
*
* Revision 1.9 1997/01/21 20:07:11 dar
* made C++ compatible
*
* Revision 1.8 1997/01/21 19:51:14 libes
* add POSIX portability
*
* Revision 1.7 1995/04/05 13:55:40 clark
* CADDETC preval
*
* Revision 1.6 1994/11/29 20:55:34 clark
* fix inline comment bug
*
* Revision 1.5 1994/11/22 18:32:39 clark
* Part 11 IS; group reference
*
* Revision 1.4 1994/11/10 19:20:03 clark
* Update to IS
*
* Revision 1.3 1994/05/11 19:51:24 libes
* numerous fixes
*
* Revision 1.2 1993/10/15 18:48:48 libes
* CADDETC certified
*
* Revision 1.7 1993/02/22 21:48:00 libes
* added decl for strdup
*
* Revision 1.6 1993/01/19 22:44:17 libes
* *** empty log message ***
*
* Revision 1.5 1992/08/27 23:40:58 libes
* remove connection between scanner and rest of EXPRESSinitializations
*
* Revision 1.4 1992/08/18 17:13:43 libes
* rm'd extraneous error messages
*
* Revision 1.3 1992/06/08 18:06:57 libes
* prettied up interface to print_objects_when_running
*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "sc_memmgr.h"
#include "express/lexact.h"
#include "express/linklist.h"
#include "stack.h"
#include "express/hash.h"
#include "express/express.h"
#include "express/dict.h"
#include "express/alloc.h"
#include "token_type.h"
#include "expparse.h"
#include "expscan.h"
extern YYSTYPE yylval;
Scan_Buffer SCAN_buffers[SCAN_NESTING_DEPTH];
int SCAN_current_buffer = 0;
char * SCANcurrent;
extern int yylineno;
#define SCAN_COMMENT_LENGTH 256
static char last_comment_[256] = "";
static char * last_comment = 0;
/* keyword lookup table */
static Hash_Table keyword_dictionary;
static struct keyword_entry {
char * key;
int token;
} keywords[] = {
{ "ABS", TOK_BUILTIN_FUNCTION },
{ "ABSTRACT", TOK_ABSTRACT },
{ "ACOS", TOK_BUILTIN_FUNCTION },
{ "AGGREGATE", TOK_AGGREGATE },
{ "ALIAS", TOK_ALIAS },
{ "AND", TOK_AND },
{ "ANDOR", TOK_ANDOR },
{ "ARRAY", TOK_ARRAY },
{ "AS", TOK_AS },
{ "ASIN", TOK_BUILTIN_FUNCTION },
{ "ATAN", TOK_BUILTIN_FUNCTION },
{ "BAG", TOK_BAG },
{ "BEGIN", TOK_BEGIN },
{ "BINARY", TOK_BINARY },
{ "BLENGTH", TOK_BUILTIN_FUNCTION },
{ "BOOLEAN", TOK_BOOLEAN },
{ "BY", TOK_BY },
{ "CASE", TOK_CASE },
{ "CONST_E", TOK_E },
{ "CONSTANT", TOK_CONSTANT },
{ "COS", TOK_BUILTIN_FUNCTION },
{ "DERIVE", TOK_DERIVE },
{ "DIV", TOK_DIV },
{ "ELSE", TOK_ELSE },
{ "END", TOK_END },
{ "END_ALIAS", TOK_END_ALIAS },
{ "END_CASE", TOK_END_CASE },
{ "END_CONSTANT", TOK_END_CONSTANT },
{ "END_ENTITY", TOK_END_ENTITY },
{ "END_FUNCTION", TOK_END_FUNCTION },
{ "END_IF", TOK_END_IF },
{ "END_LOCAL", TOK_END_LOCAL },
{ "END_PROCEDURE", TOK_END_PROCEDURE },
{ "END_REPEAT", TOK_END_REPEAT },
{ "END_RULE", TOK_END_RULE },
{ "END_SCHEMA", TOK_END_SCHEMA },
{ "END_TYPE", TOK_END_TYPE },
{ "ENTITY", TOK_ENTITY },
{ "ENUMERATION", TOK_ENUMERATION },
{ "ESCAPE", TOK_ESCAPE },
{ "EXISTS", TOK_BUILTIN_FUNCTION },
{ "EXP", TOK_BUILTIN_FUNCTION },
{ "FALSE", TOK_LOGICAL_LITERAL },
{ "FIXED", TOK_FIXED },
{ "FOR", TOK_FOR },
{ "FORMAT", TOK_BUILTIN_FUNCTION },
{ "FROM", TOK_FROM },
{ "FUNCTION", TOK_FUNCTION },
{ "GENERIC", TOK_GENERIC },
{ "HIBOUND", TOK_BUILTIN_FUNCTION },
{ "HIINDEX", TOK_BUILTIN_FUNCTION },
{ "IF", TOK_IF },
{ "IN", TOK_IN },
{ "INCLUDE", TOK_INCLUDE },
{ "INSERT", TOK_BUILTIN_PROCEDURE },
{ "INTEGER", TOK_INTEGER },
{ "INVERSE", TOK_INVERSE },
{ "LENGTH", TOK_BUILTIN_FUNCTION },
{ "LIKE", TOK_LIKE },
{ "LIST", TOK_LIST },
{ "LOBOUND", TOK_BUILTIN_FUNCTION },
{ "LOCAL", TOK_LOCAL },
{ "LOG", TOK_BUILTIN_FUNCTION },
{ "LOG10", TOK_BUILTIN_FUNCTION },
{ "LOG2", TOK_BUILTIN_FUNCTION },
{ "LOGICAL", TOK_LOGICAL },
{ "LOINDEX", TOK_BUILTIN_FUNCTION },
{ "MOD", TOK_MOD },
{ "NOT", TOK_NOT },
{ "NUMBER", TOK_NUMBER },
{ "NVL", TOK_BUILTIN_FUNCTION },
{ "ODD", TOK_BUILTIN_FUNCTION },
{ "OF", TOK_OF },
{ "ONEOF", TOK_ONEOF },
{ "OPTIONAL", TOK_OPTIONAL },
{ "OR", TOK_OR },
{ "OTHERWISE", TOK_OTHERWISE },
{ "PI", TOK_PI },
{ "PROCEDURE", TOK_PROCEDURE },
{ "QUERY", TOK_QUERY },
{ "REAL", TOK_REAL },
{ "REFERENCE", TOK_REFERENCE },
{ "REMOVE", TOK_BUILTIN_PROCEDURE },
{ "REPEAT", TOK_REPEAT },
{ "RETURN", TOK_RETURN },
{ "ROLESOF", TOK_BUILTIN_FUNCTION },
{ "RULE", TOK_RULE },
{ "SCHEMA", TOK_SCHEMA },
{ "SELECT", TOK_SELECT },
{ "SELF", TOK_SELF },
{ "SET", TOK_SET },
{ "SIN", TOK_BUILTIN_FUNCTION },
{ "SIZEOF", TOK_BUILTIN_FUNCTION },
{ "SKIP", TOK_SKIP },
{ "SQRT", TOK_BUILTIN_FUNCTION },
{ "STRING", TOK_STRING },
{ "SUBTYPE", TOK_SUBTYPE },
{ "SUPERTYPE", TOK_SUPERTYPE },
{ "TAN", TOK_BUILTIN_FUNCTION },
{ "THEN", TOK_THEN },
{ "TO", TOK_TO },
{ "TRUE", TOK_LOGICAL_LITERAL },
{ "TYPE", TOK_TYPE },
{ "TYPEOF", TOK_BUILTIN_FUNCTION },
{ "UNIQUE", TOK_UNIQUE },
{ "UNKNOWN", TOK_LOGICAL_LITERAL },
{ "UNTIL", TOK_UNTIL },
{ "USE", TOK_USE },
{ "USEDIN", TOK_BUILTIN_FUNCTION },
{ "VALUE", TOK_BUILTIN_FUNCTION },
{ "VALUE_IN", TOK_BUILTIN_FUNCTION },
{ "VALUE_UNIQUE", TOK_BUILTIN_FUNCTION },
{ "VAR", TOK_VAR },
{ "WHERE", TOK_WHERE },
{ "WHILE", TOK_WHILE },
{ "XOR", TOK_XOR },
{ 0, 0}
};
static void SCANpush_buffer( char * filename, FILE * fp ) {
SCANbuffer.savedPos = SCANcurrent;
SCANbuffer.lineno = yylineno;
yylineno = 1;
++SCAN_current_buffer;
#ifdef keep_nul
SCANbuffer.numRead = 0;
#else
*( SCANcurrent = SCANbuffer.text ) = '\0';
#endif
SCANbuffer.readEof = false;
SCANbuffer.file = fp;
SCANbuffer.filename = current_filename = filename;
}
static void SCANpop_buffer() {
if( SCANbuffer.file != NULL ) {
fclose( SCANbuffer.file );
}
--SCAN_current_buffer;
SCANcurrent = SCANbuffer.savedPos;
yylineno = SCANbuffer.lineno + 1; /* DEL */
current_filename = SCANbuffer.filename;
}
void SCANinitialize( void ) {
struct keyword_entry * k;
keyword_dictionary = HASHcreate( 100 ); /* not exact */
for( k = keywords; k->key; k++ ) {
DICTdefine( keyword_dictionary, k->key, k, NULL, OBJ_UNKNOWN );
/* not "unknown", but certainly won't be looked up by type! */
}
}
/** Clean up the Scan module */
void SCANcleanup( void ) {
}
int SCANprocess_real_literal( const char * yytext ) {
sscanf( yytext, "%lf", &( yylval.rVal ) );
return TOK_REAL_LITERAL;
}
int SCANprocess_integer_literal( const char * yytext ) {
sscanf( yytext, "%d", &( yylval.iVal ) );
return TOK_INTEGER_LITERAL;
}
int SCANprocess_binary_literal( const char * yytext ) {
yylval.binary = SCANstrdup( yytext + 1 ); /* drop '%' prefix */
return TOK_BINARY_LITERAL;
}
int SCANprocess_logical_literal( char * string ) {
switch( string[0] ) {
case 'T':
yylval.logical = Ltrue;
break;
case 'F':
yylval.logical = Lfalse;
break;
default:
yylval.logical = Lunknown;
break;
/* default will actually be triggered by 'UNKNOWN' keyword */
}
sc_free( string );
return TOK_LOGICAL_LITERAL;
}
int SCANprocess_identifier_or_keyword( const char * yytext ) {
char * test_string, * dest;
const char * src;
struct keyword_entry * k;
int len;
/* make uppercase copy */
len = strlen( yytext );
dest = test_string = ( char * )sc_malloc( len + 1 );
for( src = yytext; *src; src++, dest++ ) {
*dest = ( islower( *src ) ? toupper( *src ) : *src );
}
*dest = '\0';
/* check for language keywords */
k = ( struct keyword_entry * )DICTlookup( keyword_dictionary, test_string );
if( k ) {
switch( k->token ) {
case TOK_BUILTIN_FUNCTION:
case TOK_BUILTIN_PROCEDURE:
break;
case TOK_LOGICAL_LITERAL:
return SCANprocess_logical_literal( test_string );
default:
sc_free( test_string );
return k->token;
}
}
/* now we have an identifier token */
yylval.symbol = SYMBOLcreate( test_string, yylineno, current_filename );
if( k ) {
/* built-in function/procedure */
return( k->token );
} else {
/* plain identifier */
/* translate back to lower-case */
SCANlowerize( test_string );
return TOK_IDENTIFIER;
}
}
int SCANprocess_string( const char * yytext ) {
char * s, *d; /* source, destination */
/* strip off quotes */
yylval.string = SCANstrdup( yytext + 1 ); /* remove 1st single quote */
/* change pairs of quotes to single quotes */
for( s = d = yylval.string; *s; ) {
if( *s != '\'' ) {
*d++ = *s++;
} else if( 0 == strncmp( s, "''", 2 ) ) {
*d++ = '\'';
s += 2;
} else if( *s == '\'' ) {
/* trailing quote */
*s = '\0';
/* if string was unterminated, there will be no */
/* quote to remove in which case the scanner has */
/* already complained about it */
}
}
*d = '\0';
return TOK_STRING_LITERAL;
}
int SCANprocess_encoded_string( const char * yytext ) {
char * s; /* source */
int count;
/* strip off quotes */
yylval.string = SCANstrdup( yytext + 1 ); /* remove 1st double quote */
s = strrchr( yylval.string, '"' );
if( s ) {
*s = '\0'; /* remove last double quote */
}
/* if string was unterminated, there will be no quote to remove */
/* in which case the scanner has already complained about it */
count = 0;
for( s = yylval.string; *s; s++, count++ ) {
if( !isxdigit( *s ) ) {
ERRORreport_with_line( ENCODED_STRING_BAD_DIGIT, yylineno, *s );
}
}
if( 0 != ( count % 8 ) ) {
ERRORreport_with_line( ENCODED_STRING_BAD_COUNT, yylineno, count );
}
return TOK_STRING_LITERAL_ENCODED;
}
int SCANprocess_semicolon( const char * yytext, int commentp ) {
if( commentp ) {
strcpy( last_comment_, strchr( yytext, '-' ) );
yylval.string = last_comment_;
} else {
yylval.string = last_comment;
}
if( last_comment ) {
last_comment = 0;
}
return TOK_SEMICOLON;
}
void SCANsave_comment( const char * yytext ) {
strncpy( last_comment_ , yytext, SCAN_COMMENT_LENGTH - 1 );
last_comment = last_comment_;
}
bool SCANread( void ) {
int numRead;
bool done;
do {
/* this loop is guaranteed to terminate, since buffer[0] is on yyin */
while( SCANbuffer.file == NULL ) {
SCANpop_buffer();
if( SCANtext_ready ) {
return true;
}
}
/* now we have a file buffer */
/* check for more stuff already buffered */
if( SCANtext_ready ) {
return true;
}
/* check whether we've seen eof on this file */
if( !SCANbuffer.readEof ) {
numRead = fread( SCANbuffer.text, sizeof( char ),
SCAN_BUFFER_SIZE, SCANbuffer.file );
if( numRead < SCAN_BUFFER_SIZE ) {
SCANbuffer.readEof = true;
}
#ifdef keep_nul
SCANbuffer.numRead = numRead;
#else
SCANbuffer.text[numRead] = '\0';
#endif
SCANcurrent = SCANbuffer.text;
}
if( !( done = SCANtext_ready ) ) {
if( SCAN_current_buffer == 0 ) {
done = true;
fclose( SCANbuffer.file ); /* close yyin */
SCANbuffer.file = NULL;
} else {
SCANpop_buffer();
}
}
} while( !done );
return SCANtext_ready;
}
void SCANinclude_file( char * filename ) {
extern int print_objects_while_running;
FILE * fp;
if( ( fp = fopen( filename, "r" ) ) == NULL ) {
ERRORreport_with_line( INCLUDE_FILE, yylineno );
} else {
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
fprintf( stderr, "parse: including %s at line %d of %s\n",
filename, yylineno, SCANbuffer.filename );
}
SCANpush_buffer( filename, fp );
}
}
void SCANlowerize( char * s ) {
for( ; *s; s++ ) {
if( isupper( *s ) ) {
*s = tolower( *s );
}
}
}
void SCANupperize( char * s ) {
for( ; *s; s++ ) {
if( islower( *s ) ) {
*s = toupper( *s );
}
}
}
char * SCANstrdup( const char * s ) {
char * s2 = ( char * )sc_malloc( strlen( s ) + 1 );
if( !s2 ) {
return 0;
}
strcpy( s2, s );
return s2;
}
long SCANtell() {
return yylineno;
}