source: git/Singular/scanner.ll

spielwiese
Last change on this file was 857633, checked in by Hans Schoenemann <hannes@…>, 2 years ago
opt: xalloc stuff
  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[0e1846]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
[a4b31c]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"
[0e1846]17
[057e93c]18int feReadLine(char* b, int l);
[c232af]19#define ALLOC(a) omAlloc((a))
[b1dfaf]20#ifndef NEW_FLEX
21#endif /* NEW_LEX */
[64d729]22int blocknest = 0;
[0e1846]23extern char * yytext;
24//extern unsigned char * yytext;
25extern int yyleng;
[530bdb]26extern int inerror;
[0e1846]27
[857633]28#ifdef HAVE_OMALLOC
[5560978]29#undef malloc
30#define malloc omAlloc
31#undef realloc
32#define realloc omRealloc
[ec7aac]33#undef free
[5560978]34#define free omFree
[857633]35#endif
36
[0e1846]37static char * dupyytext()
38{
[c232af]39  char* s;
[0e1846]40  if (yyleng>0) yytext[yyleng-1] = '\0';
[c232af]41  s = omStrDup((char *)yytext);
42  omMarkAsStaticAddr(s);
43  return s;
[0e1846]44}
45
46static char * dupyytextNL()
47{
48  int i = yyleng;//strlen((char *)yytext);
[c232af]49  char * rc = (char*)omAlloc( 3 + i );
50  omMarkAsStaticAddr(rc);
[0e1846]51  if (i>0)
52  {
[e9ad8a6]53    strncpy( rc, (char *)yytext, i-1 );
[0e1846]54  }
55  else
[e9ad8a6]56  {
[0e1846]57    i++;
[e9ad8a6]58  }
[0e1846]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
[057e93c]69  extern "C" {
[0e1846]70  int yywrap() { return exitVoice(); }
[057e93c]71  }
[0e1846]72
73  #undef YY_INPUT
74  #define YY_INPUT(buf,result,max_size) \
[057e93c]75          result = feReadLine( (char *) (buf), (max_size) )
[0e1846]76
77  #undef YY_USER_ACTION
78  #define YY_USER_ACTION \
[057e93c]79          if ((inerror==1)&&(*yytext>=' '))\
80          { Print("   skipping text from `%s`",yytext);inerror=2; }
[0e1846]81
82%}
83
84digit          [0-9]
85letter         [@a-zA-Z\']
86integer        {digit}+
87monom          {letter}+{digit}*
88rgvars         ({digit}+[/])*{digit}+{monom}+
[b7b08c]89realnum        {digit}*"."{digit}+("e"[+-]{digit}+)?
[0e1846]90name           ({letter}({letter}*{digit}*_*)*|_)
91parname        #
92
93/* %start START */
[991705]94%option always-interactive
[0e1846]95
96%x string
97%x block
[6ae4f5]98%x blockstr
[0e1846]99%x brace
100%x bracestr
101%x bracket
102%x asstring
103
104%%
[aad4ca4]105\/\/[^\n]*
106^[ \r\t\n]*#![^\n]*
[e9ad8a6]107"/*"                     {
108                           yy_noeof=noeof_comment;
109                           loop
110                           {
[50d0fa]111                             REGISTER int c;
[e9ad8a6]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                         }
[ff2c077]125while                    { prompt_char='.';
126                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
[0e1846]127                           return WHILE_CMD;}
[ff2c077]128for                      { prompt_char='.';
129                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
[0e1846]130                           return FOR_CMD;}
131
[cc94b0a]132("help"|"?")[ \t\n]*     { yy_noeof = noeof_asstring;
[0e1846]133                           BEGIN(asstring);
134                           return HELP_CMD;
135                         }
136
[c04b94]137example[ \t\n]*          { yy_noeof = noeof_asstring;
138                           BEGIN(asstring);
139                           return EXAMPLE_CMD;
140                         }
141
[0e1846]142proc[ \t]+{name}[ \t]*\( {
143                           char c; char *cp;
[c232af]144                           lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp));
[cc94b0a]145                           yy_noeof = noeof_procname;
[0e1846]146                           blocknest = 1;
147                           BEGIN(brace);
148                           return PROC_DEF;
149                         }
150<asstring>[^;\n]+        {
[c232af]151                           lvalp->name = omStrDup((char *)yytext);
[cc94b0a]152                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]153                           return STRINGTOK;
154                         }
155<asstring>;              {
[cc94b0a]156                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]157                           return *yytext;
158                         }
159
160<brace>"\""              {
[cc94b0a]161                           yy_noeof = noeof_string;
[0e1846]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                           {
[cc94b0a]177                             yy_noeof = 0;
[0e1846]178                             BEGIN(INITIAL);
179                             lvalp->name = dupyytext();
180                             return STRINGTOK;
181                           }
182                           yymore();
183                         }
184<bracestr>"\""           {
[cc94b0a]185                           yy_noeof = noeof_brace;
[0e1846]186                           BEGIN(brace);
187                           yymore();
188                         }
189<bracestr>[^\"]          { yymore(); }
190<bracket>"("             { return '('; }
191<bracket>","             { return ','; }
192<bracket>[ \t\n]*        { ; }
193<bracket>[^\(\), \t\n]*  {
[c232af]194                           lvalp->name = omStrDup((char *)yytext);
[0e1846]195                           return STRINGTOK;
196                         }
197<bracket>\"[^\"]*\"      {
[c232af]198                           lvalp->name = omStrDup((char *)yytext);
[0e1846]199                           return STRINGTOK;
200                         }
201<bracket>")"             {
[cc94b0a]202                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]203                           return ')';
204                         }
205
206"{"                      {
[cc94b0a]207                           yy_blocklineno = yylineno;
[0e1846]208                           blocknest = 1;
[cc94b0a]209                           yy_noeof = noeof_block;
[0e1846]210                           BEGIN(block);
211                         }
[6ae4f5]212<block>"\""              {
[cc94b0a]213                           yy_noeof = noeof_string;
[6ae4f5]214                           BEGIN(blockstr);
215                           yymore();
216                         }
217<blockstr>[^\"]          { yymore(); }
218<blockstr>"\\\\"         { yymore(); }
219<blockstr>"\\\""         { yymore(); }
220<blockstr>"\""           {
[cc94b0a]221                           yy_noeof = noeof_block;
[6ae4f5]222                           BEGIN(block);
223                           yymore();
224                         }
225<block>[^\{\}\"]*        { yymore(); }
[c656484]226<block>\/\/[^\n]*        { yymore(); }
[0e1846]227<block>"{"               { blocknest++; yymore(); }
228<block>"}"               {
229                           if (--blocknest <= 0)
230                           {
231                             BEGIN(INITIAL);
[cc94b0a]232                             yy_noeof = 0;
[0e1846]233                             lvalp->name = dupyytextNL();
234                             return BLOCKTOK;
235                           }
236                           yymore();
237                         }
[cc94b0a]238"\""                     { BEGIN(string); yy_noeof = noeof_string;}
[0e1846]239~                        { return SYS_BREAK; }
240<string>[^\"]            { yymore(); }
241<string>"\\\\"           { yymore(); }
242<string>"\\\""           { yymore(); }
243<string>"\""             {
244                           char * s;
[cc94b0a]245                           yy_noeof = 0;
[0e1846]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; }
[057e93c]259"::"                     { return COLONCOLON; }
[0e1846]260"--"                     { return MINUSMINUS; }
261"++"                     { return PLUSPLUS  ; }
262"=="                     { return EQUAL_EQUAL; }
[7b3094]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; }
[0e1846]267"!"                      { return NOT; }
268"!="                     { return NOTEQUAL; }
269"<>"                     { return NOTEQUAL; }
270"**"                     { return '^'; }
[6be074]271"->"                     { return ARROW; }
[0e1846]272\\                       { return '\\'; }
273newline                  {
[c232af]274                           lvalp->name = omStrDup("\n");
[0e1846]275                           return STRINGTOK;
276                         }
277{integer}                {
278                           lvalp->name = (char *)yytext;
[db5523]279                           return INT_CONST;
[0e1846]280                         }
281{integer}\/{integer}     {
[6bb498]282                           lvalp->name = (char *)yytext;
[85bf0b]283                           return MONOM;
[0e1846]284                         }
[b7b08c]285\$                        {
[e9ad8a6]286                           m2_end(-1);
[0e1846]287                         }
[de25e5]288(quit|exit)[ \t\n]*;     {
[0e1846]289                           #ifdef MM_STAT
290                           mmStat(-500);
291                           #endif
[c232af]292                           #ifdef OM_TRACK
[7fe9e13]293                           #ifndef SING_NDEBUG
[de25e5]294                             omPrintUsedTrackAddrs(stdout, 10);
[0e1846]295                           #endif
[d33552]296                           #endif
[e9ad8a6]297                           m2_end(0);
[0e1846]298                         }
299
300{rgvars}|{realnum}       {
[6bb498]301                           lvalp->name = (char *)yytext;
[85bf0b]302                           return MONOM;
[0e1846]303                         }
[7b3094]304[0-9]+\."e"[+-][0-9]+    {
[b7b08c]305                           lvalp->name = (char *)yytext;
[85bf0b]306                           return MONOM;
[b7b08c]307                         }
308[0-9]+\./[^\.]           {
309                           lvalp->name = (char *)yytext;
[85bf0b]310                           return MONOM;
[b7b08c]311                         }
[7b3094]312
[0e1846]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                           }
[c232af]325                           lvalp->name = omStrDup((char *)yytext);
[0e1846]326                           return UNKNOWN_IDENT;
327                         }
328
[057e93c]329.                       {
[0e1846]330                           /*if (*yytext == '\n') REJECT;*/
[50d0fa]331                           REGISTER char ch= *yytext;
[7b3094]332                           lvalp->i = ch;
[3b1a83c]333                           switch(ch)
334                           {
335                             /* case '&': */
336                             case '|':
[7b3094]337                               return LOGIC_OP;
[3b1a83c]338                             /* case '/': */
339                             case '%':
340                             case '*':
[7b3094]341                               return MULDIV_OP;
[3b1a83c]342                             /* case '<': */
343                             case '>':
344                               return COMP_OP;
345                             default:
346                               break;
347                            }
[7b3094]348                            return ch;
[0e1846]349                         }
350%%
351
352void * myynewbuffer()
353{
[b1dfaf]354  void * oldb = YY_CURRENT_BUFFER;
[057e93c]355  yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE));
[0e1846]356  return oldb;
357}
358
359void myyoldbuffer(void * oldb)
360{
[b1dfaf]361  yy_delete_buffer(YY_CURRENT_BUFFER);
[0e1846]362  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
[057e93c]363  //yy_flush_buffer((YY_BUFFER_STATE)oldb);
[0e1846]364}
[8e884e]365
[310cf4]366void 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
[8e884e]373void my_yy_flush() { YY_FLUSH_BUFFER;BEGIN(0); }
Note: See TracBrowser for help on using the repository browser.