1 | %{ |
---|
2 | /**************************************** |
---|
3 | * Computer Algebra System SINGULAR * |
---|
4 | ****************************************/ |
---|
5 | /* $Id: scanner.l,v 1.15 1999-03-19 17:42:33 obachman Exp $ */ |
---|
6 | #include <stdio.h> |
---|
7 | #include <string.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <ctype.h> |
---|
10 | |
---|
11 | #include "mod2.h" |
---|
12 | #include "tok.h" |
---|
13 | #include "stype.h" |
---|
14 | #include "ipshell.h" |
---|
15 | #include "mmemory.h" |
---|
16 | #include "febase.h" |
---|
17 | |
---|
18 | int feReadLine(char* b, int l); |
---|
19 | #define ALLOC(a) Alloc((a)) |
---|
20 | int yylineno = 0; |
---|
21 | static int blocknest = 0; |
---|
22 | extern char * yytext; |
---|
23 | //extern unsigned char * yytext; |
---|
24 | extern int yyleng; |
---|
25 | extern BOOLEAN noringvars; |
---|
26 | extern int inerror; |
---|
27 | |
---|
28 | static char * dupyytext() |
---|
29 | { |
---|
30 | if (yyleng>0) yytext[yyleng-1] = '\0'; |
---|
31 | return mstrdup((char *)yytext); |
---|
32 | } |
---|
33 | |
---|
34 | static char * dupyytextNL() |
---|
35 | { |
---|
36 | int i = yyleng;//strlen((char *)yytext); |
---|
37 | char * rc = (char*)AllocL( 3 + i ); |
---|
38 | if (i>0) |
---|
39 | { |
---|
40 | strncpy( rc, (char *)yytext, i-1 ); |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | i++; |
---|
45 | } |
---|
46 | rc[i-1] = '\n'; |
---|
47 | rc[i] = '\n'; |
---|
48 | rc[i+1] = '\0'; |
---|
49 | return rc; |
---|
50 | } |
---|
51 | |
---|
52 | #undef YY_DECL |
---|
53 | #define YY_DECL int yylex(YYSTYPE* lvalp) |
---|
54 | |
---|
55 | #undef yywrap |
---|
56 | extern "C" { |
---|
57 | int yywrap() { return exitVoice(); } |
---|
58 | } |
---|
59 | |
---|
60 | #undef YY_INPUT |
---|
61 | #define YY_INPUT(buf,result,max_size) \ |
---|
62 | result = feReadLine( (char *) (buf), (max_size) ) |
---|
63 | |
---|
64 | #undef YY_USER_ACTION |
---|
65 | #define YY_USER_ACTION \ |
---|
66 | if ((inerror==1)&&(*yytext>=' '))\ |
---|
67 | { Print(" skipping text from `%s`",yytext);inerror=2; } |
---|
68 | |
---|
69 | %} |
---|
70 | |
---|
71 | digit [0-9] |
---|
72 | letter [@a-zA-Z\'] |
---|
73 | integer {digit}+ |
---|
74 | monom {letter}+{digit}* |
---|
75 | rgvars ({digit}+[/])*{digit}+{monom}+ |
---|
76 | realnum {digit}+"."{digit}+("e"[+-]{digit}+)? |
---|
77 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
78 | parname # |
---|
79 | |
---|
80 | /* %start START */ |
---|
81 | %option always-interactive |
---|
82 | |
---|
83 | %x string |
---|
84 | %x block |
---|
85 | %x blockstr |
---|
86 | %x brace |
---|
87 | %x bracestr |
---|
88 | %x bracket |
---|
89 | %x asstring |
---|
90 | |
---|
91 | %% |
---|
92 | \/\/[^\n]* { } |
---|
93 | ^#![^\n]* { } |
---|
94 | "/*" { |
---|
95 | yy_noeof=noeof_comment; |
---|
96 | loop |
---|
97 | { |
---|
98 | register int c; |
---|
99 | while ( (c = yyinput()) != '*' && c != EOF ); |
---|
100 | if ( c == '*' ) |
---|
101 | { |
---|
102 | while ( (c = yyinput()) == '*' ); |
---|
103 | if ( c == '/' ) break; /* found the end */ |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | break; |
---|
108 | } |
---|
109 | } |
---|
110 | yy_noeof=0; |
---|
111 | } |
---|
112 | pause[ \t\n]*[\.;] { fePause(); } |
---|
113 | while { prompt_char='.'; |
---|
114 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
115 | return WHILE_CMD;} |
---|
116 | for { prompt_char='.'; |
---|
117 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
118 | return FOR_CMD;} |
---|
119 | |
---|
120 | ("help"|"?")[ \t\n]* { yy_noeof = noeof_asstring; |
---|
121 | BEGIN(asstring); |
---|
122 | return HELP_CMD; |
---|
123 | } |
---|
124 | |
---|
125 | proc[ \t]+{name}[ \t]*\( { |
---|
126 | char c; char *cp; |
---|
127 | lvalp->name = mstrdup(iiProcName((char *)yytext,c,cp)); |
---|
128 | yy_noeof = noeof_procname; |
---|
129 | blocknest = 1; |
---|
130 | BEGIN(brace); |
---|
131 | return PROC_DEF; |
---|
132 | } |
---|
133 | <asstring>[^;\n]+ { |
---|
134 | lvalp->name = mstrdup((char *)yytext); |
---|
135 | yy_noeof = 0; BEGIN(INITIAL); |
---|
136 | return STRINGTOK; |
---|
137 | } |
---|
138 | <asstring>; { |
---|
139 | yy_noeof = 0; BEGIN(INITIAL); |
---|
140 | return *yytext; |
---|
141 | } |
---|
142 | |
---|
143 | <brace>"\"" { |
---|
144 | yy_noeof = noeof_string; |
---|
145 | BEGIN(bracestr); |
---|
146 | yymore(); |
---|
147 | } |
---|
148 | <brace>"(" { if (blocknest++) yymore(); } |
---|
149 | <brace>[^;\(\)] { if (blocknest) yymore(); } |
---|
150 | <brace>";" { |
---|
151 | if (blocknest) |
---|
152 | { |
---|
153 | lvalp->name = dupyytext(); |
---|
154 | return STRINGTOK; |
---|
155 | } |
---|
156 | } |
---|
157 | <brace>")" { |
---|
158 | if (--blocknest <= 0) |
---|
159 | { |
---|
160 | yy_noeof = 0; |
---|
161 | BEGIN(INITIAL); |
---|
162 | lvalp->name = dupyytext(); |
---|
163 | return STRINGTOK; |
---|
164 | } |
---|
165 | yymore(); |
---|
166 | } |
---|
167 | <bracestr>"\"" { |
---|
168 | yy_noeof = noeof_brace; |
---|
169 | BEGIN(brace); |
---|
170 | yymore(); |
---|
171 | } |
---|
172 | <bracestr>[^\"] { yymore(); } |
---|
173 | <bracket>"(" { return '('; } |
---|
174 | <bracket>"," { return ','; } |
---|
175 | <bracket>[ \t\n]* { ; } |
---|
176 | <bracket>[^\(\), \t\n]* { |
---|
177 | lvalp->name = mstrdup((char *)yytext); |
---|
178 | return STRINGTOK; |
---|
179 | } |
---|
180 | <bracket>\"[^\"]*\" { |
---|
181 | lvalp->name = mstrdup((char *)yytext); |
---|
182 | return STRINGTOK; |
---|
183 | } |
---|
184 | <bracket>")" { |
---|
185 | yy_noeof = 0; BEGIN(INITIAL); |
---|
186 | return ')'; |
---|
187 | } |
---|
188 | |
---|
189 | "{" { |
---|
190 | yy_blocklineno = yylineno; |
---|
191 | blocknest = 1; |
---|
192 | yy_noeof = noeof_block; |
---|
193 | BEGIN(block); |
---|
194 | } |
---|
195 | <block>"\"" { |
---|
196 | yy_noeof = noeof_string; |
---|
197 | BEGIN(blockstr); |
---|
198 | yymore(); |
---|
199 | } |
---|
200 | <blockstr>[^\"] { yymore(); } |
---|
201 | <blockstr>"\\\\" { yymore(); } |
---|
202 | <blockstr>"\\\"" { yymore(); } |
---|
203 | <blockstr>"\"" { |
---|
204 | yy_noeof = noeof_block; |
---|
205 | BEGIN(block); |
---|
206 | yymore(); |
---|
207 | } |
---|
208 | <block>[^\{\}\"]* { yymore(); } |
---|
209 | <block>\/\/[^\n]* { yymore(); } |
---|
210 | <block>"{" { blocknest++; yymore(); } |
---|
211 | <block>"}" { |
---|
212 | if (--blocknest <= 0) |
---|
213 | { |
---|
214 | BEGIN(INITIAL); |
---|
215 | yy_noeof = 0; |
---|
216 | lvalp->name = dupyytextNL(); |
---|
217 | return BLOCKTOK; |
---|
218 | } |
---|
219 | yymore(); |
---|
220 | } |
---|
221 | "\"" { BEGIN(string); yy_noeof = noeof_string;} |
---|
222 | ~ { return SYS_BREAK; } |
---|
223 | <string>[^\"] { yymore(); } |
---|
224 | <string>"\\\\" { yymore(); } |
---|
225 | <string>"\\\"" { yymore(); } |
---|
226 | <string>"\"" { |
---|
227 | char * s; |
---|
228 | yy_noeof = 0; |
---|
229 | BEGIN(INITIAL); |
---|
230 | s = lvalp->name = dupyytext(); |
---|
231 | while (*yytext) |
---|
232 | { |
---|
233 | if (*yytext == '\\') yytext++; |
---|
234 | *s++ = *yytext++; |
---|
235 | } |
---|
236 | *s++ = *yytext++; |
---|
237 | return STRINGTOK; |
---|
238 | } |
---|
239 | |
---|
240 | [ \t\r\n] /* skip whitespace */ |
---|
241 | ".." { return DOTDOT; } |
---|
242 | "::" { return COLONCOLON; } |
---|
243 | "--" { return MINUSMINUS; } |
---|
244 | "++" { return PLUSPLUS ; } |
---|
245 | "==" { return EQUAL_EQUAL; } |
---|
246 | "&&" { return '&'; } |
---|
247 | "||" { return '|'; } |
---|
248 | "<=" { return LE; } |
---|
249 | ">=" { return GE; } |
---|
250 | "!" { return NOT; } |
---|
251 | "!=" { return NOTEQUAL; } |
---|
252 | "<>" { return NOTEQUAL; } |
---|
253 | "**" { return '^'; } |
---|
254 | \\ { return '\\'; } |
---|
255 | newline { |
---|
256 | lvalp->name = mstrdup("\n"); |
---|
257 | return STRINGTOK; |
---|
258 | } |
---|
259 | {integer} { |
---|
260 | lvalp->name = (char *)yytext; |
---|
261 | if ((currRing!=NULL) |
---|
262 | && (strlen(lvalp->name)>MAX_INT_LEN)) |
---|
263 | { |
---|
264 | lvalp->name = mstrdup((char *)yytext); |
---|
265 | return RINGVAR; |
---|
266 | } |
---|
267 | else |
---|
268 | return INT_CONST; |
---|
269 | } |
---|
270 | {integer}\/{integer} { |
---|
271 | lvalp->name = mstrdup((char *)yytext); |
---|
272 | return RINGVAR; |
---|
273 | } |
---|
274 | \$ { |
---|
275 | m2_end(-1); |
---|
276 | } |
---|
277 | (quit|exit)[ \t\n]*[\.;]? { |
---|
278 | #ifdef MM_STAT |
---|
279 | mmStat(-500); |
---|
280 | #endif |
---|
281 | #ifdef MDEBUG |
---|
282 | #ifdef MLIST |
---|
283 | mmTestList(0); |
---|
284 | #endif |
---|
285 | #endif |
---|
286 | m2_end(0); |
---|
287 | } |
---|
288 | |
---|
289 | {rgvars}|{realnum} { |
---|
290 | lvalp->name = mstrdup((char *)yytext); |
---|
291 | return RINGVAR; |
---|
292 | } |
---|
293 | |
---|
294 | ({parname}|{name}) { |
---|
295 | /* {name} */ |
---|
296 | int rc=0; |
---|
297 | if (yytext[strlen((char *)yytext)-1] == '\n') |
---|
298 | { |
---|
299 | yytext[strlen((char *)yytext)-1] = '\0'; |
---|
300 | } |
---|
301 | if (yyleng > 1) |
---|
302 | { |
---|
303 | rc = IsCmd((char *)yytext,lvalp->i); |
---|
304 | if (rc) return rc; |
---|
305 | } |
---|
306 | lvalp->name = mstrdup((char *)yytext); |
---|
307 | return UNKNOWN_IDENT; |
---|
308 | } |
---|
309 | |
---|
310 | . { |
---|
311 | /*if (*yytext == '\n') REJECT;*/ |
---|
312 | lvalp->i = *yytext; /* token has own value */ |
---|
313 | return *yytext; |
---|
314 | } |
---|
315 | %% |
---|
316 | |
---|
317 | void * myynewbuffer() |
---|
318 | { |
---|
319 | void * oldb = yy_current_buffer; |
---|
320 | yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE)); |
---|
321 | return oldb; |
---|
322 | } |
---|
323 | |
---|
324 | void myyoldbuffer(void * oldb) |
---|
325 | { |
---|
326 | yy_delete_buffer(yy_current_buffer); |
---|
327 | yy_switch_to_buffer((YY_BUFFER_STATE)oldb); |
---|
328 | //yy_flush_buffer((YY_BUFFER_STATE)oldb); |
---|
329 | } |
---|