source: git/Singular/scanner.l @ ec7aac

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