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