source: git/Singular/scanner.ll @ 5476e83

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