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