1 | %{ |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | /* $Id: scanner.l,v 1.4 1999-03-31 22:03:23 krueger Exp $ */ |
---|
6 | #include <stdio.h> |
---|
7 | #include <string.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <ctype.h> |
---|
10 | |
---|
11 | #include "modgen.h" |
---|
12 | #include <mod2.h> |
---|
13 | #include <febase.h> |
---|
14 | #include <grammar.h> |
---|
15 | #include <ipid.h> |
---|
16 | #include <ipshell.h> |
---|
17 | #include <mmemory.h> |
---|
18 | #include <structs.h> |
---|
19 | #include <subexpr.h> |
---|
20 | #include <tok.h> |
---|
21 | |
---|
22 | #define DEBUG 0 |
---|
23 | |
---|
24 | # define YYLP_ERR_NONE 0 |
---|
25 | # define YYLP_DEF_BR2 1 |
---|
26 | # define YYLP_BODY_BR2 2 |
---|
27 | # define YYLP_BODY_BR3 3 |
---|
28 | # define YYLP_BODY_TMBR2 4 |
---|
29 | # define YYLP_BODY_TMBR3 5 |
---|
30 | # define YYLP_EX_BR2 6 |
---|
31 | # define YYLP_EX_BR3 7 |
---|
32 | # define YYLP_BAD_CHAR 8 |
---|
33 | # define YYLP_MISSQUOT 9 |
---|
34 | # define YYLP_MISS_BR1 10 |
---|
35 | # define YYLP_MISS_BR2 11 |
---|
36 | # define YYLP_MISS_BR3 12 |
---|
37 | |
---|
38 | const char sNoName[]="_"; |
---|
39 | int brace1 = 0; /* { } */ |
---|
40 | int brace2 = 0; /* ( ) */ |
---|
41 | int brace3 = 0; /* [ ] */ |
---|
42 | int quote = 0; /* " */ |
---|
43 | int offset = 0; |
---|
44 | |
---|
45 | char my_yylinebuf[80]; |
---|
46 | int *old_states = NULL; |
---|
47 | int state_level = -1; |
---|
48 | int state_max = 0; |
---|
49 | int yylineno = 1; |
---|
50 | int tok; |
---|
51 | int myynest = -1; |
---|
52 | int traceit = 0; |
---|
53 | moddef module_def; |
---|
54 | int yylplineno = 1; |
---|
55 | int yylp_errno = 0; |
---|
56 | |
---|
57 | long C_start; |
---|
58 | long C_end; |
---|
59 | char c_codetype = 0; |
---|
60 | #define C_CODE_NONE 0 |
---|
61 | #define C_CODE_PROC 1 |
---|
62 | #define C_CODE_MAIN 2 |
---|
63 | |
---|
64 | char string_type = 0; |
---|
65 | #define STRING_NONE 0 |
---|
66 | #define STRING_INFO 1 |
---|
67 | #define STRING_VERS 2 |
---|
68 | long string_start = 0; |
---|
69 | long string_end = 0; |
---|
70 | |
---|
71 | char *yylp_buffer_start; |
---|
72 | |
---|
73 | int libread(FILE* f, char* buf, int max_size); |
---|
74 | int current_pos(int i); |
---|
75 | int read_string(char **buffer, long *start, long end); |
---|
76 | |
---|
77 | void push_state(int state, int new_state); |
---|
78 | void pop_state(); |
---|
79 | |
---|
80 | # undef YY_INPUT |
---|
81 | # define YY_INPUT(buf,result,max_size) \ |
---|
82 | if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \ |
---|
83 | && ferror( yyin ) ) \ |
---|
84 | YY_FATAL_ERROR( "read in flex scanner failed" ); |
---|
85 | |
---|
86 | |
---|
87 | extern "C" |
---|
88 | { |
---|
89 | int yywrap(); |
---|
90 | } |
---|
91 | |
---|
92 | %} |
---|
93 | |
---|
94 | digit [0-9] |
---|
95 | letter [@a-zA-Z\'] |
---|
96 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
97 | fname ({letter}({letter}*{digit}*_*.)*|_) |
---|
98 | letters ({letter}|{digit}|[_./#%^*:,]) |
---|
99 | string ({letters}*) |
---|
100 | comment [\/][\/] |
---|
101 | dolar [$] |
---|
102 | symbols [~!@#$%^&*()_+-={}\\\|\[\];:,<.>/\?\' \n\~\`\r] |
---|
103 | aletters ({letter}|{digit}|{symbols}|{dolar}|{escquote}) |
---|
104 | strings ({aletters}*) |
---|
105 | quote [\"] |
---|
106 | escquote (\\\") |
---|
107 | taborspace [ \t] |
---|
108 | tos ({taborspace}*) |
---|
109 | eq (=|{tos}+=|=+{tos}|{tos}+=+{tos}) |
---|
110 | tnl ([ \t\n]*) |
---|
111 | col (;|{tos}+;) |
---|
112 | eqnl ([ \t\n]*+=[ \t\n]*) |
---|
113 | |
---|
114 | /* %start START */ |
---|
115 | |
---|
116 | %x pdef |
---|
117 | %x comment |
---|
118 | %x procdef |
---|
119 | %x ctext |
---|
120 | %x string |
---|
121 | %x cstring |
---|
122 | |
---|
123 | %% |
---|
124 | (\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { } |
---|
125 | \/\/* { push_state(YYSTATE, comment); } |
---|
126 | |
---|
127 | (module+{eqnl}+{quote}+{name}+{quote}) { |
---|
128 | char *buff = (char *)malloc(yyleng+4); |
---|
129 | memset(buff, '\0', yyleng+4); |
---|
130 | sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); |
---|
131 | module_def.name = (char *)malloc(strlen(buff)+1); |
---|
132 | memset(module_def.name, '\0', strlen(buff)+1); |
---|
133 | memcpy(module_def.name, buff, strlen(buff)); |
---|
134 | strcat(buff, ".cc"); |
---|
135 | Add2files(&module_def, buff); |
---|
136 | free(buff); |
---|
137 | } |
---|
138 | |
---|
139 | (version+{eqnl}+{quote}+{strings}+{quote}) { |
---|
140 | yyless(4); |
---|
141 | string_type = STRING_VERS; |
---|
142 | push_state(YYSTATE, cstring); |
---|
143 | } |
---|
144 | |
---|
145 | (helpfile+{eqnl}+{quote}+{fname}+{quote}) { |
---|
146 | char *buff = (char *)malloc(yyleng+1); |
---|
147 | memset(buff, '\0', yyleng+1); |
---|
148 | sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff); |
---|
149 | module_def.helpfile = (char *)malloc(strlen(buff)+1); |
---|
150 | memset(module_def.helpfile, '\0', strlen(buff)+1); |
---|
151 | memcpy(module_def.helpfile, buff, strlen(buff)); |
---|
152 | printf("==>HELP:'%s'\n", module_def.helpfile); |
---|
153 | free(buff); |
---|
154 | } |
---|
155 | |
---|
156 | (cxxsource+{eqnl}+{fname}) { |
---|
157 | char *buff = (char *)malloc(yyleng+1); |
---|
158 | memset(buff, '\0', yyleng+1); |
---|
159 | sscanf( yytext, "%*[^=]=%s", buff); |
---|
160 | Add2files(&module_def,buff); |
---|
161 | free(buff); |
---|
162 | } |
---|
163 | |
---|
164 | (info+{eqnl}+{quote}+{strings}+{quote}) { |
---|
165 | yyless(4); |
---|
166 | string_type = STRING_INFO; |
---|
167 | push_state(YYSTATE, cstring); |
---|
168 | } |
---|
169 | |
---|
170 | <cstring>{quote} { quote++; push_state(YYSTATE, string); |
---|
171 | string_start = current_pos(yyleng); } |
---|
172 | <cstring>\n { yylplineno++; } |
---|
173 | <cstring>{col} { |
---|
174 | switch(string_type) { |
---|
175 | case STRING_INFO: |
---|
176 | read_string(&module_def.info, &string_start, string_end); |
---|
177 | break; |
---|
178 | |
---|
179 | case STRING_VERS: |
---|
180 | read_string(&module_def.version, &string_start, string_end); |
---|
181 | make_version(module_def.version, &module_def); |
---|
182 | break; |
---|
183 | } |
---|
184 | string_type = STRING_NONE; |
---|
185 | pop_state(); } |
---|
186 | <cstring>. { } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | (proc+{tos}+{name})|({tos}+proc+{tos}+{name}) { |
---|
191 | char *proc = (char *)malloc(yyleng+1); |
---|
192 | memset(proc, '\0', yyleng+1); |
---|
193 | push_state(YYSTATE, pdef); |
---|
194 | sscanf( yytext, "%*[^p]proc %s", proc); |
---|
195 | if(strlen(proc)<1) sscanf( yytext, "proc %s", proc); |
---|
196 | Add2proclist(&module_def, proc, "none", "NONE", NONE); |
---|
197 | free(proc); |
---|
198 | } |
---|
199 | |
---|
200 | (proc+{tos}+{name}+{tos}+{name})|({tos}+proc+{tos}+{name}+{tos}+{name}) { |
---|
201 | char *proc = (char *)malloc(yyleng+1); |
---|
202 | char *ret_val = (char *)malloc(yyleng+1); |
---|
203 | memset(proc, '\0', yyleng+1); |
---|
204 | memset(ret_val, '\0', yyleng+1); |
---|
205 | |
---|
206 | char n2[32]; |
---|
207 | int cmd; |
---|
208 | memset(n2, '\0', 32); |
---|
209 | push_state(YYSTATE, pdef); |
---|
210 | sscanf( yytext, "%*[^p]proc %s %s", ret_val, proc); |
---|
211 | if(strlen(proc)<1) sscanf( yytext, "proc %s %s", ret_val, proc); |
---|
212 | printf("'%s'\n", yytext); |
---|
213 | printf("R: proc '%s' '%s'\n", ret_val, proc); |
---|
214 | cmd = IsCmd(ret_val, tok); |
---|
215 | if(cmd!=0) |
---|
216 | Add2proclist(&module_def, proc, ret_val, |
---|
217 | decl2str(tok,n2), tok); |
---|
218 | else { |
---|
219 | printf("proc '%s': Invalid return parameter %s.\n", |
---|
220 | proc, ret_val); |
---|
221 | Add2proclist(&module_def, proc, "none", |
---|
222 | "NONE", NONE); |
---|
223 | } |
---|
224 | free(proc); |
---|
225 | free(ret_val); |
---|
226 | } |
---|
227 | |
---|
228 | <pdef>[ \t] { } |
---|
229 | <pdef>"(" { } |
---|
230 | <pdef>{name}+, { |
---|
231 | char param[256], n2[32]; |
---|
232 | int cmd; |
---|
233 | memset(n2, '\0', 32); |
---|
234 | memset(param, '\0', 256); |
---|
235 | sscanf( yytext, "%[^,],", param); |
---|
236 | cmd = IsCmd(param, tok); |
---|
237 | if(cmd!=0)AddParam(&module_def, |
---|
238 | param, decl2str(tok,n2), tok); |
---|
239 | else printf("proc '%s': Invalid parameter %s.\n", |
---|
240 | module_def.procs[module_def.proccnt-1].procname, |
---|
241 | param); |
---|
242 | } |
---|
243 | <pdef>{name}+")"+{tnl}+"{" { |
---|
244 | char param[256], n2[32]; |
---|
245 | int cmd; |
---|
246 | BEGIN(procdef); |
---|
247 | memset(param, '\0', 256); |
---|
248 | memset(n2, '\0', 32); |
---|
249 | sscanf( yytext, "%[^)])", param); |
---|
250 | cmd = IsCmd(param, tok); |
---|
251 | if(cmd!=0)AddParam(&module_def, |
---|
252 | param, decl2str(tok,n2), tok); |
---|
253 | else printf("proc '%s': Invalid parameter %s.\n", |
---|
254 | module_def.procs[module_def.proccnt-1].procname, |
---|
255 | param); |
---|
256 | } |
---|
257 | <pdef>")"+{tnl}+"{" { |
---|
258 | BEGIN(procdef); |
---|
259 | } |
---|
260 | |
---|
261 | <pdef>"{" { |
---|
262 | BEGIN(procdef); |
---|
263 | } |
---|
264 | |
---|
265 | <pdef>(")"+{eqnl}+{name})|({eq}+{name}) { |
---|
266 | char funcname[256]; |
---|
267 | pop_state(); |
---|
268 | sscanf( yytext, "%*[^=]=%s", funcname); |
---|
269 | if(strlen(funcname)<=0) |
---|
270 | sscanf( yytext, "=%s", funcname); |
---|
271 | free(module_def.procs[module_def.proccnt-1].funcname); |
---|
272 | module_def.procs[module_def.proccnt-1].funcname = |
---|
273 | (char *)malloc(strlen(funcname)+1); |
---|
274 | memset(module_def.procs[module_def.proccnt-1].funcname, |
---|
275 | '\0', strlen(funcname)+1); |
---|
276 | memcpy(module_def.procs[module_def.proccnt-1].funcname, |
---|
277 | funcname, strlen(funcname)); |
---|
278 | } |
---|
279 | |
---|
280 | <pdef>({name}+")"+{eqnl}+{name}) { |
---|
281 | char param[256], n2[32],funcname[256]; |
---|
282 | int cmd; |
---|
283 | pop_state(); |
---|
284 | memset(param, '\0', 256); |
---|
285 | memset(n2, '\0', 32); |
---|
286 | memset(funcname, '\0', 256); |
---|
287 | sscanf( yytext, "%[^)])%*[^=]=%s", param, funcname); |
---|
288 | if(strlen(funcname)<=0) |
---|
289 | sscanf( yytext, "%[^)])=%s", param, funcname); |
---|
290 | free(module_def.procs[module_def.proccnt-1].funcname); |
---|
291 | module_def.procs[module_def.proccnt-1].funcname = |
---|
292 | (char *)malloc(strlen(funcname)+1); |
---|
293 | memset(module_def.procs[module_def.proccnt-1].funcname, |
---|
294 | '\0', strlen(funcname)+1); |
---|
295 | memcpy(module_def.procs[module_def.proccnt-1].funcname, |
---|
296 | funcname, strlen(funcname)); |
---|
297 | cmd = IsCmd(param, tok); |
---|
298 | if(cmd!=0)AddParam(&module_def, |
---|
299 | param, decl2str(tok,n2), tok); |
---|
300 | else printf("proc '%s': Invalid parameter %s.\n", |
---|
301 | module_def.procs[module_def.proccnt-1].procname, param); |
---|
302 | } |
---|
303 | |
---|
304 | <pdef>. { } |
---|
305 | |
---|
306 | <procdef>"}" { pop_state(); } |
---|
307 | <procdef>RETURN+{eqnl}+{name}+{col} { |
---|
308 | printf("Return:\n");} |
---|
309 | <procdef>function+{eqnl}+{name}+{col} { |
---|
310 | char *funcname = (char *)malloc(yyleng+1); |
---|
311 | memset(funcname, '\0', yyleng+1); |
---|
312 | sscanf( yytext, "%*[^=]=%[^;];", funcname); |
---|
313 | if(strlen(funcname)<=0) |
---|
314 | sscanf( yytext, "=%[^;];", funcname); |
---|
315 | free(module_def.procs[module_def.proccnt-1].funcname); |
---|
316 | module_def.procs[module_def.proccnt-1].funcname = |
---|
317 | (char *)malloc(strlen(funcname)+ 1); |
---|
318 | memset(module_def.procs[module_def.proccnt-1].funcname, |
---|
319 | '\0', strlen(funcname)+1); |
---|
320 | memcpy(module_def.procs[module_def.proccnt-1].funcname, |
---|
321 | funcname, strlen(funcname)); |
---|
322 | } |
---|
323 | |
---|
324 | <procdef>checkring+{col} { |
---|
325 | printf("Do checkring\n"); } |
---|
326 | <procdef>C+{eqnl}+"{" { |
---|
327 | push_state(YYSTATE, ctext); |
---|
328 | C_start=current_pos(yyleng); |
---|
329 | yyless(yyleng-1); |
---|
330 | c_codetype = C_CODE_PROC; |
---|
331 | } |
---|
332 | <procdef>. { printf("%s", yytext); } |
---|
333 | |
---|
334 | <comment>\*\/ { pop_state(); } |
---|
335 | <comment>\n { yylineno++; } |
---|
336 | <comment>. { } |
---|
337 | |
---|
338 | <ctext>({comment}[^\n]*) { } |
---|
339 | <ctext>\/\/* { push_state(YYSTATE, comment); } |
---|
340 | <ctext>{quote} { quote++; push_state(YYSTATE, string); } |
---|
341 | <ctext>"{" { brace1++; } |
---|
342 | <ctext>"}" { |
---|
343 | brace1--; |
---|
344 | if(brace2>0) { |
---|
345 | yylp_errno = YYLP_BODY_BR2; |
---|
346 | return(1); |
---|
347 | } |
---|
348 | if(brace3>0) { |
---|
349 | yylp_errno = YYLP_BODY_BR3; |
---|
350 | return(1); |
---|
351 | } |
---|
352 | if(brace1<=0) { |
---|
353 | C_end = current_pos(yyleng)-1; |
---|
354 | printf("Ctext: %d-%d\n", C_start, C_end); |
---|
355 | switch(c_codetype) { |
---|
356 | case C_CODE_PROC: |
---|
357 | read_string(&(module_def.procs[module_def.proccnt-1].c_code), |
---|
358 | &C_start, C_end); |
---|
359 | break; |
---|
360 | |
---|
361 | case C_CODE_MAIN: |
---|
362 | break; |
---|
363 | } |
---|
364 | c_codetype = C_CODE_NONE; |
---|
365 | pop_state(); |
---|
366 | } |
---|
367 | } |
---|
368 | <ctext>"(" { brace2++; } |
---|
369 | <ctext>")" { brace2--; |
---|
370 | if(brace2<0) { |
---|
371 | yylp_errno = YYLP_BODY_TMBR2; |
---|
372 | return(1); |
---|
373 | } |
---|
374 | } |
---|
375 | <ctext>"[" { brace3++; } |
---|
376 | <ctext>"]" { brace3--; |
---|
377 | if(brace3<0) { |
---|
378 | yylp_errno = YYLP_BODY_TMBR3; |
---|
379 | return(1); |
---|
380 | } |
---|
381 | } |
---|
382 | <ctext>\n { yylineno++; } |
---|
383 | <ctext>. { } |
---|
384 | |
---|
385 | <string>{quote} { quote--; string_end = current_pos(yyleng)-1; pop_state(); } |
---|
386 | <string>(\\\\)|(\\\") { } |
---|
387 | <string>\n { yylplineno++; } |
---|
388 | <string>. { } |
---|
389 | |
---|
390 | |
---|
391 | C+{eqnl}+"{" { push_state(YYSTATE, ctext); |
---|
392 | C_start=current_pos(yyleng); |
---|
393 | yyless(yyleng-1); |
---|
394 | } |
---|
395 | \n { yylineno++; } |
---|
396 | \r { } |
---|
397 | . { } |
---|
398 | |
---|
399 | %% |
---|
400 | |
---|
401 | extern "C" { |
---|
402 | int yywrap() { |
---|
403 | //printf("yywrap()\n"); |
---|
404 | return 1; |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | int libread(FILE* f, char* buf, int max_size) |
---|
409 | { int rc; |
---|
410 | |
---|
411 | offset = ftell(f); |
---|
412 | rc = fread( buf, 1, max_size, f ); |
---|
413 | #if YYLPDEBUG >2 |
---|
414 | printf("fread: %d of %d\n", rc, max_size); |
---|
415 | #endif |
---|
416 | yylp_buffer_start = buf; |
---|
417 | return rc; |
---|
418 | } |
---|
419 | |
---|
420 | int current_pos(int i) |
---|
421 | { |
---|
422 | return(i+offset+(int)(yytext-yylp_buffer_start)); |
---|
423 | } |
---|
424 | |
---|
425 | int read_string(char **p, long *start, long end) |
---|
426 | { |
---|
427 | char *buffer; |
---|
428 | |
---|
429 | if(*start > 0 ) { |
---|
430 | long len = end - *start; |
---|
431 | if(len>=0) { |
---|
432 | long cur = ftell(yyin); |
---|
433 | buffer = (char *)malloc(len+1); |
---|
434 | memset(buffer, '\0', len+1); |
---|
435 | fseek(yyin, *start, SEEK_SET); |
---|
436 | fread(buffer, 1, len, yyin); |
---|
437 | fseek(yyin, cur, SEEK_SET); |
---|
438 | *p = buffer; |
---|
439 | } |
---|
440 | *start = 0; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | void push_state(int state, int new_state) |
---|
445 | { |
---|
446 | state_level++; |
---|
447 | #if DEBUG |
---|
448 | printf("====>PUSH to new state %d/%d l=%d\n", state, new_state, state_level); |
---|
449 | #endif |
---|
450 | if(state_level>=state_max) { |
---|
451 | state_max++; |
---|
452 | if(old_states == NULL) |
---|
453 | old_states = (int *)malloc(sizeof(int)); |
---|
454 | else { |
---|
455 | old_states = (int *)realloc(old_states, state_max*sizeof(int)); |
---|
456 | } |
---|
457 | } |
---|
458 | old_states[state_level] = state; |
---|
459 | BEGIN(new_state); |
---|
460 | } |
---|
461 | |
---|
462 | void pop_state() |
---|
463 | { |
---|
464 | #if DEBUG |
---|
465 | printf("====>Back to old state %d, l=%d\n", old_states[state_level], |
---|
466 | state_level); |
---|
467 | #endif |
---|
468 | BEGIN(old_states[state_level]); |
---|
469 | state_level--; |
---|
470 | if(state_level<0) state_level = -1; |
---|
471 | } |
---|
472 | |
---|
473 | main( int argc, char *argv[] ) |
---|
474 | { |
---|
475 | ++argv, --argc; /* skip over program name */ |
---|
476 | if ( argc > 0 ) |
---|
477 | yyin = fopen( argv[0], "rb" ); |
---|
478 | else |
---|
479 | yyin = stdin; |
---|
480 | module_def.name=NULL; |
---|
481 | module_def.version=NULL; |
---|
482 | module_def.info=NULL; |
---|
483 | module_def.helpfile=NULL; |
---|
484 | module_def.procs=NULL; |
---|
485 | module_def.proccnt = 0; |
---|
486 | module_def.files = NULL; |
---|
487 | module_def.filecnt = 0; |
---|
488 | yylex(); |
---|
489 | PrintProclist(&module_def); |
---|
490 | generate_mod(&module_def); |
---|
491 | mod_create_makefile(&module_def); |
---|
492 | |
---|
493 | } |
---|