source: git/Singular/scanner.l @ f5a3a23

spielwiese
Last change on this file since f5a3a23 was b1dfaf, checked in by Frank Seelisch <seelisch@…>, 14 years ago
patch from Kai (checked for problems under Windows OS: no problems) git-svn-id: file:///usr/local/Singular/svn/trunk@13210 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.1 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id$ */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <ctype.h>
10
11#include <kernel/mod2.h>
12#include <omalloc/omalloc.h>
13#include <Singular/tok.h>
14#include <Singular/stype.h>
15#include <Singular/ipshell.h>
16#include <kernel/febase.h>
17
18int feReadLine(char* b, int l);
19#define ALLOC(a) omAlloc((a))
20#ifndef NEW_FLEX
21int yylineno  = 0;
22#endif /* NEW_LEX */
23int blocknest = 0;
24extern char * yytext;
25//extern unsigned char * yytext;
26extern int yyleng;
27extern int inerror;
28
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
32static void* my_malloc(size_t size)
33{
34  void* addr = omAlloc(size);
35  omMarkAsStaticAddr(addr);
36  return addr;
37}
38
39static 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}
45static void my_free(void* addr)
46{
47  omFree(addr);
48}
49#undef malloc
50#define malloc my_malloc
51#undef realloc
52#define realloc my_realloc
53#undef free
54#define free my_free
55static char * dupyytext()
56{
57  char* s;
58  if (yyleng>0) yytext[yyleng-1] = '\0';
59  s = omStrDup((char *)yytext);
60  omMarkAsStaticAddr(s);
61  return s;
62}
63
64static char * dupyytextNL()
65{
66  int i = yyleng;//strlen((char *)yytext);
67  char * rc = (char*)omAlloc( 3 + i );
68  omMarkAsStaticAddr(rc);
69  if (i>0)
70  {
71    strncpy( rc, (char *)yytext, i-1 );
72  }
73  else
74  {
75    i++;
76  }
77  rc[i-1] = '\n';
78  rc[i] = '\n';
79  rc[i+1] = '\0';
80  return rc;
81}
82
83  #undef YY_DECL
84  #define YY_DECL int yylex(YYSTYPE* lvalp)
85
86  #undef yywrap
87  extern "C" {
88  int yywrap() { return exitVoice(); }
89  }
90
91  #undef YY_INPUT
92  #define YY_INPUT(buf,result,max_size) \
93          result = feReadLine( (char *) (buf), (max_size) )
94
95  #undef YY_USER_ACTION
96  #define YY_USER_ACTION \
97          if ((inerror==1)&&(*yytext>=' '))\
98          { Print("   skipping text from `%s`",yytext);inerror=2; }
99
100%}
101
102digit          [0-9]
103letter         [@a-zA-Z\']
104integer        {digit}+
105monom          {letter}+{digit}*
106rgvars         ({digit}+[/])*{digit}+{monom}+
107realnum        {digit}*"."{digit}+("e"[+-]{digit}+)?
108name           ({letter}({letter}*{digit}*_*)*|_)
109parname        #
110
111/* %start START */
112%option always-interactive
113
114%x string
115%x block
116%x blockstr
117%x brace
118%x bracestr
119%x bracket
120%x asstring
121
122%%
123\/\/[^\n]*               { }
124^#![^\n]*                { }
125"/*"                     {
126                           yy_noeof=noeof_comment;
127                           loop
128                           {
129                             register int c;
130                             while ( (c = yyinput()) != '*' && c != EOF );
131                             if ( c == '*' )
132                             {
133                               while ( (c = yyinput()) == '*' );
134                               if ( c == '/' ) break; /* found the end */
135                             }
136                             else
137                             {
138                               break;
139                             }
140                           }
141                           yy_noeof=0;
142                         }
143while                    { prompt_char='.';
144                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
145                           return WHILE_CMD;}
146for                      { prompt_char='.';
147                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
148                           return FOR_CMD;}
149
150("help"|"?")[ \t\n]*     { yy_noeof = noeof_asstring;
151                           BEGIN(asstring);
152                           return HELP_CMD;
153                         }
154
155example[ \t\n]*          { yy_noeof = noeof_asstring;
156                           BEGIN(asstring);
157                           return EXAMPLE_CMD;
158                         }
159
160proc[ \t]+{name}[ \t]*\( {
161                           char c; char *cp;
162                           lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp));
163                           yy_noeof = noeof_procname;
164                           blocknest = 1;
165                           BEGIN(brace);
166                           return PROC_DEF;
167                         }
168<asstring>[^;\n]+        {
169                           lvalp->name = omStrDup((char *)yytext);
170                           yy_noeof = 0; BEGIN(INITIAL);
171                           return STRINGTOK;
172                         }
173<asstring>;              {
174                           yy_noeof = 0; BEGIN(INITIAL);
175                           return *yytext;
176                         }
177
178<brace>"\""              {
179                           yy_noeof = noeof_string;
180                           BEGIN(bracestr);
181                           yymore();
182                         }
183<brace>"("               { if (blocknest++) yymore(); }
184<brace>[^;\(\)]          { if (blocknest) yymore(); }
185<brace>";"               {
186                           if (blocknest)
187                           {
188                             lvalp->name = dupyytext();
189                             return STRINGTOK;
190                           }
191                         }
192<brace>")"               {
193                           if (--blocknest <= 0)
194                           {
195                             yy_noeof = 0;
196                             BEGIN(INITIAL);
197                             lvalp->name = dupyytext();
198                             return STRINGTOK;
199                           }
200                           yymore();
201                         }
202<bracestr>"\""           {
203                           yy_noeof = noeof_brace;
204                           BEGIN(brace);
205                           yymore();
206                         }
207<bracestr>[^\"]          { yymore(); }
208<bracket>"("             { return '('; }
209<bracket>","             { return ','; }
210<bracket>[ \t\n]*        { ; }
211<bracket>[^\(\), \t\n]*  {
212                           lvalp->name = omStrDup((char *)yytext);
213                           return STRINGTOK;
214                         }
215<bracket>\"[^\"]*\"      {
216                           lvalp->name = omStrDup((char *)yytext);
217                           return STRINGTOK;
218                         }
219<bracket>")"             {
220                           yy_noeof = 0; BEGIN(INITIAL);
221                           return ')';
222                         }
223
224"{"                      {
225                           yy_blocklineno = yylineno;
226                           blocknest = 1;
227                           yy_noeof = noeof_block;
228                           BEGIN(block);
229                         }
230<block>"\""              {
231                           yy_noeof = noeof_string;
232                           BEGIN(blockstr);
233                           yymore();
234                         }
235<blockstr>[^\"]          { yymore(); }
236<blockstr>"\\\\"         { yymore(); }
237<blockstr>"\\\""         { yymore(); }
238<blockstr>"\""           {
239                           yy_noeof = noeof_block;
240                           BEGIN(block);
241                           yymore();
242                         }
243<block>[^\{\}\"]*        { yymore(); }
244<block>\/\/[^\n]*        { yymore(); }
245<block>"{"               { blocknest++; yymore(); }
246<block>"}"               {
247                           if (--blocknest <= 0)
248                           {
249                             BEGIN(INITIAL);
250                             yy_noeof = 0;
251                             lvalp->name = dupyytextNL();
252                             return BLOCKTOK;
253                           }
254                           yymore();
255                         }
256"\""                     { BEGIN(string); yy_noeof = noeof_string;}
257~                        { return SYS_BREAK; }
258<string>[^\"]            { yymore(); }
259<string>"\\\\"           { yymore(); }
260<string>"\\\""           { yymore(); }
261<string>"\""             {
262                           char * s;
263                           yy_noeof = 0;
264                           BEGIN(INITIAL);
265                           s = lvalp->name = dupyytext();
266                           while (*yytext)
267                           {
268                             if (*yytext == '\\') yytext++;
269                             *s++ = *yytext++;
270                           }
271                           *s++ = *yytext++;
272                           return STRINGTOK;
273                         }
274
275[ \t\r\n]                /* skip whitespace */
276".."                     { return DOTDOT; }
277"::"                     { return COLONCOLON; }
278"--"                     { return MINUSMINUS; }
279"++"                     { return PLUSPLUS  ; }
280"=="                     { return EQUAL_EQUAL; }
281"&&"                     { lvalp->i='&'; return LOGIC_OP; }
282"||"                     { lvalp->i='|'; return LOGIC_OP; }
283"<="                     { lvalp->i=LE; return COMP_OP; }
284">="                     { lvalp->i=GE; return COMP_OP; }
285"!"                      { return NOT; }
286"!="                     { return NOTEQUAL; }
287"<>"                     { return NOTEQUAL; }
288"**"                     { return '^'; }
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 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}
Note: See TracBrowser for help on using the repository browser.