1 | %{ |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | /* $Id: scanner.l,v 1.1 1999-11-23 21:30:22 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 "stype.h" |
---|
13 | #include "grammar.h" |
---|
14 | |
---|
15 | /*#define DEBUG 1*/ |
---|
16 | |
---|
17 | # define YYLP_ERR_NONE 0 |
---|
18 | # define YYLP_DEF_BR2 1 |
---|
19 | # define YYLP_BODY_BR2 2 |
---|
20 | # define YYLP_BODY_BR3 3 |
---|
21 | # define YYLP_BODY_TMBR2 4 |
---|
22 | # define YYLP_BODY_TMBR3 5 |
---|
23 | # define YYLP_EX_BR2 6 |
---|
24 | # define YYLP_EX_BR3 7 |
---|
25 | # define YYLP_BAD_CHAR 8 |
---|
26 | # define YYLP_MISSQUOT 9 |
---|
27 | # define YYLP_MISS_BR1 10 |
---|
28 | # define YYLP_MISS_BR2 11 |
---|
29 | # define YYLP_MISS_BR3 12 |
---|
30 | |
---|
31 | int offset = 0; |
---|
32 | int yylineno = 1; |
---|
33 | int do_return = 0; |
---|
34 | |
---|
35 | int state_level = -1; |
---|
36 | int state_max = 0; |
---|
37 | char *yylp_buffer_start; |
---|
38 | |
---|
39 | extern int sectnum; |
---|
40 | extern moddef module_def; |
---|
41 | |
---|
42 | struct _states { |
---|
43 | char name[32]; |
---|
44 | int state; |
---|
45 | } * old_states = NULL; |
---|
46 | /* int *old_states = NULL;*/ |
---|
47 | |
---|
48 | int current_pos(int i); |
---|
49 | int read_string(char **buffer, long *start, long end); |
---|
50 | int libread(FILE* f, char* buf, int max_size); |
---|
51 | add_action(char *new_text); |
---|
52 | |
---|
53 | void push_state(int state, int new_state, char *name); |
---|
54 | void switch_state(int state, int new_state, char *name); |
---|
55 | void pop_state(); |
---|
56 | extern int init_modgen(moddef *module_def, char *filename); |
---|
57 | |
---|
58 | static char * dupyytext() |
---|
59 | { |
---|
60 | if (yyleng>0) yytext[yyleng-1] = '\0'; |
---|
61 | return strdup((char *)yytext); |
---|
62 | } |
---|
63 | |
---|
64 | # undef YY_DECL |
---|
65 | # define YY_DECL int yylex(YYSTYPE* lvalp) |
---|
66 | |
---|
67 | # undef YY_INPUT |
---|
68 | # define YY_INPUT(buf,result,max_size) \ |
---|
69 | if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \ |
---|
70 | && ferror( yyin ) ) \ |
---|
71 | YY_FATAL_ERROR( "read in flex scanner failed" ); |
---|
72 | |
---|
73 | # undef yywrap |
---|
74 | extern "C" |
---|
75 | { |
---|
76 | int yywrap(); |
---|
77 | } |
---|
78 | |
---|
79 | #define ACTION_ECHO add_action( yytext ) |
---|
80 | |
---|
81 | %} |
---|
82 | |
---|
83 | digit [0-9] |
---|
84 | letter [@a-zA-Z\'] |
---|
85 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
86 | fname ({letter}({letter}*{digit}*_*.)*|_) |
---|
87 | letters ({letter}|{digit}|[_./#%^*:,]) |
---|
88 | string ({letters}*) |
---|
89 | comment [\/][\/] |
---|
90 | dolar [$] |
---|
91 | symbols [~!@#$%^&*()_+-={}\\\|\[\];:,<.>/\?\' \n\~\`\r] |
---|
92 | aletters ({letter}|{digit}|{symbols}|{dolar}|{escquote}) |
---|
93 | strings ({aletters}*) |
---|
94 | quote [\"] |
---|
95 | escquote (\\\") |
---|
96 | taborspace [ \t] |
---|
97 | tos ({taborspace}*) |
---|
98 | eq (=|{tos}+=|=+{tos}|{tos}+=+{tos}) |
---|
99 | tnl ([ \t\n]*) |
---|
100 | col (;|{tos}+;) |
---|
101 | eqnl ([ \t\n]*+=[ \t\n]*) |
---|
102 | begincode (%+\{) |
---|
103 | endcode (\n+%+[}]+\n) |
---|
104 | |
---|
105 | WS [[:blank:]]+ |
---|
106 | NL \r?\n |
---|
107 | EQ (([{WS}]+=[{WS}]+)|=) |
---|
108 | |
---|
109 | NAME ([[:alpha:]_][[:alnum:]_-]*) |
---|
110 | FILENAME ([[:alpha:]_][[:alnum:]_-]*\.(c|cc|h)) |
---|
111 | |
---|
112 | /* %start START */ |
---|
113 | |
---|
114 | %x preamble |
---|
115 | %x CODEBLOCK |
---|
116 | %x CODEBLOCK2 |
---|
117 | %x module |
---|
118 | %x COMMENT |
---|
119 | %x STRING |
---|
120 | %x SECT2 |
---|
121 | %x SECT3 |
---|
122 | %x PROCDECL |
---|
123 | |
---|
124 | %x pdef |
---|
125 | %x procopt |
---|
126 | %x procdef |
---|
127 | %x ctext |
---|
128 | %x cstring |
---|
129 | |
---|
130 | %% |
---|
131 | static int brace1 = 0; /* { } */ |
---|
132 | static int brace2 = 0; /* ( ) */ |
---|
133 | static int brace3 = 0; /* [ ] */ |
---|
134 | static int quote = 0; /* " */ |
---|
135 | |
---|
136 | <COMMENT,STRING,INITIAL><<EOF>> { |
---|
137 | printf( "EOF encountered inside an action\n"); |
---|
138 | printf("ERRRRROOOOORRR\n"); |
---|
139 | } |
---|
140 | |
---|
141 | <INITIAL>{ |
---|
142 | ^{WS} push_state(YYSTATE, CODEBLOCK, "CODEBLOCK"); |
---|
143 | ^"/*" ACTION_ECHO; push_state(YYSTATE, COMMENT, "COMMENT"); |
---|
144 | ^"//".*{NL} ++yylineno; ACTION_ECHO; |
---|
145 | ^"%{".*{NL} { |
---|
146 | yylineno++; |
---|
147 | push_state(YYSTATE, CODEBLOCK, "CODEBLOCK"); |
---|
148 | fprintf(module_def.fmtfp, "#line %d \"%s\"\n", |
---|
149 | yylineno, module_def.filename); |
---|
150 | } |
---|
151 | {WS} /* discard */ |
---|
152 | {NL} { yylineno++; } |
---|
153 | |
---|
154 | {eq} { return '='; } |
---|
155 | ";" { return ';'; } |
---|
156 | "," { return ','; } |
---|
157 | |
---|
158 | "\"" { do_return++; push_state(YYSTATE, STRING, "string"); } |
---|
159 | ^"%%".* { |
---|
160 | sectnum = 2; |
---|
161 | printf("Goto section %d\n", sectnum); |
---|
162 | push_state(YYSTATE, SECT2, "SECT2"); |
---|
163 | /*line_directive_out( (FILE *) 0, 1 );*/ |
---|
164 | return SECTEND; |
---|
165 | } |
---|
166 | ^{NAME} { |
---|
167 | lvalp->name = strdup(yytext); |
---|
168 | return NAME; |
---|
169 | } |
---|
170 | {FILENAME} { |
---|
171 | lvalp->name = strdup(yytext); |
---|
172 | return FILENAME; |
---|
173 | } |
---|
174 | } |
---|
175 | <CODEBLOCK>{ |
---|
176 | ^"%}".*{NL} { |
---|
177 | char * s, *t; |
---|
178 | |
---|
179 | yylineno++; |
---|
180 | pop_state(); |
---|
181 | s = t = lvalp->name = dupyytext(); |
---|
182 | while (*yytext) |
---|
183 | { |
---|
184 | if (*yytext == '\\') yytext++; |
---|
185 | *s++ = *yytext++; |
---|
186 | } |
---|
187 | if(s-t>2 && *(s-1)=='}' && *(s-2)=='%') *(s-2)='\0'; |
---|
188 | return MCCODETOK; |
---|
189 | } |
---|
190 | {NL} { yylineno++; yymore(); } |
---|
191 | . { yymore(); } |
---|
192 | } |
---|
193 | |
---|
194 | <STRING>{ |
---|
195 | {NL} { yylineno++; yymore(); } |
---|
196 | [^\"] { yymore(); } |
---|
197 | "\\\\" { yymore(); } |
---|
198 | "\\\"" { yymore(); } |
---|
199 | ("\"") { |
---|
200 | char * s, *t; |
---|
201 | pop_state(); |
---|
202 | if(do_return) { |
---|
203 | s = t = lvalp->name = dupyytext(); |
---|
204 | while (*yytext) |
---|
205 | { |
---|
206 | if (*yytext == '\\') yytext++; |
---|
207 | *s++ = *yytext++; |
---|
208 | } |
---|
209 | *s++ = *yytext++; |
---|
210 | do_return = 0; |
---|
211 | return MSTRINGTOK; |
---|
212 | } else { |
---|
213 | do_return = 0; |
---|
214 | yymore(); |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | <COMMENT>{ |
---|
220 | "*/" /*ACTION_ECHO;*/ pop_state(); yymore(); |
---|
221 | "*" /*ACTION_ECHO;*/ yymore(); |
---|
222 | [^*\n]+ ++yylineno; yymore(); /*ACTION_ECHO;*/ |
---|
223 | [^*\n]*{NL} ++yylineno; yymore(); /*ACTION_ECHO;*/ |
---|
224 | } |
---|
225 | |
---|
226 | <SECT2>{ |
---|
227 | ^"%%".* { |
---|
228 | sectnum = 3; |
---|
229 | printf("Goto section %d\n", sectnum); |
---|
230 | switch_state(YYSTATE, SECT3, "SECT3"); |
---|
231 | /*yyterminate();*/ |
---|
232 | /*line_directive_out( (FILE *) 0, 1 );*/ |
---|
233 | return SECT2END; |
---|
234 | } |
---|
235 | {NL} { yylineno++; } |
---|
236 | {WS} /* ignore */ |
---|
237 | ; /* ignore */ |
---|
238 | "/*" ACTION_ECHO; push_state(YYSTATE, COMMENT, "COMMENT"); |
---|
239 | "//".*{NL} ++yylineno; ACTION_ECHO; |
---|
240 | proc+{WS} { |
---|
241 | brace1 = 0; /* { */ |
---|
242 | brace2 = 0; /* ( */ |
---|
243 | brace3 = 0; /* [ */ |
---|
244 | push_state(YYSTATE, PROCDECL, "PROCDECL"); |
---|
245 | return PROCDECLTOK; |
---|
246 | } |
---|
247 | . { printf("'%s' ", yytext); } |
---|
248 | |
---|
249 | } |
---|
250 | |
---|
251 | <SECT3>{ |
---|
252 | {NL} { yylineno++; } |
---|
253 | .*(\n?) ECHO; |
---|
254 | <<EOF>> { sectnum = 0; printf("XXXX\n"); |
---|
255 | return SECT3END; |
---|
256 | /*yyterminate();*/ } |
---|
257 | } |
---|
258 | |
---|
259 | <PROCDECL>{ |
---|
260 | "{" { brace1++; |
---|
261 | switch_state(YYSTATE, CODEBLOCK2, "CODEBLOCK2"); |
---|
262 | return '{'; |
---|
263 | } |
---|
264 | {NAME} { |
---|
265 | int i,tok; |
---|
266 | printf("VAR: %s\n", yytext); |
---|
267 | lvalp->name = strdup(yytext); |
---|
268 | i = IsCmd(lvalp->name, tok); |
---|
269 | printf("Res=%d, %d\n", i, tok); |
---|
270 | if(i) return VARTYPETOK; |
---|
271 | else return NAME; |
---|
272 | } |
---|
273 | {NL} { yylineno++; } |
---|
274 | . { } |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | <CODEBLOCK2>{ |
---|
279 | "/*" push_state(YYSTATE, COMMENT, "COMMENT"); yymore(); |
---|
280 | "//".*{NL} ++yylineno; yymore(); |
---|
281 | "\"" { push_state(YYSTATE, STRING, "string"); yymore();} |
---|
282 | "{" { brace1++; yymore();} |
---|
283 | "}" { brace1--; |
---|
284 | if(brace1<=0) { |
---|
285 | char * s, *t; |
---|
286 | |
---|
287 | yylineno++; |
---|
288 | pop_state(); |
---|
289 | s = t = lvalp->name = dupyytext(); |
---|
290 | while (*yytext) |
---|
291 | { |
---|
292 | if (*yytext == '\\') yytext++; |
---|
293 | *s++ = *yytext++; |
---|
294 | } |
---|
295 | if(s-t>2 && *(s-1)=='}' && *(s-2)=='%') *(s-2)='\0'; |
---|
296 | printf("BRACE DOWN=%d\n", brace1); |
---|
297 | return MCODETOK; |
---|
298 | return PROCEND; |
---|
299 | } else yymore(); |
---|
300 | } |
---|
301 | {NL} { yylineno++; yymore(); } |
---|
302 | . { yymore(); } |
---|
303 | } |
---|
304 | |
---|
305 | %% |
---|
306 | |
---|
307 | extern "C" { |
---|
308 | int yywrap() { |
---|
309 | /*printf("yywrap()\n");*/ |
---|
310 | return 1; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | int libread(FILE* f, char* buf, int max_size) |
---|
315 | { int rc; |
---|
316 | |
---|
317 | offset = ftell(f); |
---|
318 | rc = fread( buf, 1, max_size, f ); |
---|
319 | #if YYLPDEBUG >2 |
---|
320 | printf("fread: %d of %d\n", rc, max_size); |
---|
321 | #endif |
---|
322 | yylp_buffer_start = buf; |
---|
323 | return rc; |
---|
324 | } |
---|
325 | |
---|
326 | void switch_state(int state, int new_state, char *name) |
---|
327 | { |
---|
328 | #if DEBUG |
---|
329 | printf("====>SWITCH to new state %d/%d l=%d, [%s] at %d\n", |
---|
330 | state, new_state, state_level, name, yylineno); |
---|
331 | #endif |
---|
332 | strncpy(old_states[state_level].name, name, |
---|
333 | sizeof(old_states[state_level].name)); |
---|
334 | BEGIN(new_state); |
---|
335 | } |
---|
336 | |
---|
337 | void push_state(int state, int new_state, char *name) |
---|
338 | { |
---|
339 | state_level++; |
---|
340 | #if DEBUG |
---|
341 | printf("====>PUSH to new state %d/%d l=%d, [%s] at %d\n", |
---|
342 | state, new_state, state_level, name, yylineno); |
---|
343 | #endif |
---|
344 | if(state_level>=state_max) { |
---|
345 | state_max++; |
---|
346 | if(old_states == NULL) |
---|
347 | old_states = (struct _states *)malloc(sizeof(struct _states)); |
---|
348 | else { |
---|
349 | old_states = (struct _states *)realloc(old_states, |
---|
350 | state_max*sizeof(struct _states)); |
---|
351 | } |
---|
352 | } |
---|
353 | old_states[state_level].state = state; |
---|
354 | strncpy(old_states[state_level].name, name, |
---|
355 | sizeof(old_states[state_level].name)); |
---|
356 | BEGIN(new_state); |
---|
357 | } |
---|
358 | |
---|
359 | void pop_state() |
---|
360 | { |
---|
361 | #if DEBUG |
---|
362 | printf("====>Back to old state %d, l=%d [%s] at %d\n", |
---|
363 | old_states[state_level].state, |
---|
364 | state_level, |
---|
365 | (state_level>0) ? old_states[state_level-1].name : "INITIAL", |
---|
366 | yylineno); |
---|
367 | #endif |
---|
368 | if(state_level<0) return; |
---|
369 | BEGIN(old_states[state_level].state); |
---|
370 | state_level--; |
---|
371 | if(state_level<0) state_level = -1; |
---|
372 | } |
---|
373 | |
---|
374 | add_action(char *new_text) |
---|
375 | { |
---|
376 | |
---|
377 | printf("%s", new_text); |
---|
378 | } |
---|