source: git/Singular/scanner.l @ 0a001f

fieker-DuValspielwiese
Last change on this file since 0a001f 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
RevLine 
[0e1846]1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
[341696]5/* $Id$ */
[0e1846]6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <ctype.h>
10
[b1dfaf]11#include <kernel/mod2.h>
12#include <omalloc/omalloc.h>
[599326]13#include <Singular/tok.h>
14#include <Singular/stype.h>
[b1dfaf]15#include <Singular/ipshell.h>
[599326]16#include <kernel/febase.h>
[0e1846]17
[057e93c]18int feReadLine(char* b, int l);
[c232af]19#define ALLOC(a) omAlloc((a))
[b1dfaf]20#ifndef NEW_FLEX
[0e1846]21int yylineno  = 0;
[b1dfaf]22#endif /* NEW_LEX */
[64d729]23int blocknest = 0;
[0e1846]24extern char * yytext;
25//extern unsigned char * yytext;
26extern int yyleng;
[530bdb]27extern int inerror;
[0e1846]28
[ec7aac]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
[a3bc95e]51#undef realloc
[ec7aac]52#define realloc my_realloc
53#undef free
54#define free my_free
[0e1846]55static char * dupyytext()
56{
[c232af]57  char* s;
[0e1846]58  if (yyleng>0) yytext[yyleng-1] = '\0';
[c232af]59  s = omStrDup((char *)yytext);
60  omMarkAsStaticAddr(s);
61  return s;
[0e1846]62}
63
64static char * dupyytextNL()
65{
66  int i = yyleng;//strlen((char *)yytext);
[c232af]67  char * rc = (char*)omAlloc( 3 + i );
68  omMarkAsStaticAddr(rc);
[0e1846]69  if (i>0)
70  {
[e9ad8a6]71    strncpy( rc, (char *)yytext, i-1 );
[0e1846]72  }
73  else
[e9ad8a6]74  {
[0e1846]75    i++;
[e9ad8a6]76  }
[0e1846]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
[057e93c]87  extern "C" {
[0e1846]88  int yywrap() { return exitVoice(); }
[057e93c]89  }
[0e1846]90
91  #undef YY_INPUT
92  #define YY_INPUT(buf,result,max_size) \
[057e93c]93          result = feReadLine( (char *) (buf), (max_size) )
[0e1846]94
95  #undef YY_USER_ACTION
96  #define YY_USER_ACTION \
[057e93c]97          if ((inerror==1)&&(*yytext>=' '))\
98          { Print("   skipping text from `%s`",yytext);inerror=2; }
[0e1846]99
100%}
101
102digit          [0-9]
103letter         [@a-zA-Z\']
104integer        {digit}+
105monom          {letter}+{digit}*
106rgvars         ({digit}+[/])*{digit}+{monom}+
[b7b08c]107realnum        {digit}*"."{digit}+("e"[+-]{digit}+)?
[0e1846]108name           ({letter}({letter}*{digit}*_*)*|_)
109parname        #
110
111/* %start START */
[991705]112%option always-interactive
[0e1846]113
114%x string
115%x block
[6ae4f5]116%x blockstr
[0e1846]117%x brace
118%x bracestr
119%x bracket
120%x asstring
121
122%%
123\/\/[^\n]*               { }
124^#![^\n]*                { }
[e9ad8a6]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                         }
[ff2c077]143while                    { prompt_char='.';
144                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
[0e1846]145                           return WHILE_CMD;}
[ff2c077]146for                      { prompt_char='.';
147                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
[0e1846]148                           return FOR_CMD;}
149
[cc94b0a]150("help"|"?")[ \t\n]*     { yy_noeof = noeof_asstring;
[0e1846]151                           BEGIN(asstring);
152                           return HELP_CMD;
153                         }
154
[c04b94]155example[ \t\n]*          { yy_noeof = noeof_asstring;
156                           BEGIN(asstring);
157                           return EXAMPLE_CMD;
158                         }
159
[0e1846]160proc[ \t]+{name}[ \t]*\( {
161                           char c; char *cp;
[c232af]162                           lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp));
[cc94b0a]163                           yy_noeof = noeof_procname;
[0e1846]164                           blocknest = 1;
165                           BEGIN(brace);
166                           return PROC_DEF;
167                         }
168<asstring>[^;\n]+        {
[c232af]169                           lvalp->name = omStrDup((char *)yytext);
[cc94b0a]170                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]171                           return STRINGTOK;
172                         }
173<asstring>;              {
[cc94b0a]174                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]175                           return *yytext;
176                         }
177
178<brace>"\""              {
[cc94b0a]179                           yy_noeof = noeof_string;
[0e1846]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                           {
[cc94b0a]195                             yy_noeof = 0;
[0e1846]196                             BEGIN(INITIAL);
197                             lvalp->name = dupyytext();
198                             return STRINGTOK;
199                           }
200                           yymore();
201                         }
202<bracestr>"\""           {
[cc94b0a]203                           yy_noeof = noeof_brace;
[0e1846]204                           BEGIN(brace);
205                           yymore();
206                         }
207<bracestr>[^\"]          { yymore(); }
208<bracket>"("             { return '('; }
209<bracket>","             { return ','; }
210<bracket>[ \t\n]*        { ; }
211<bracket>[^\(\), \t\n]*  {
[c232af]212                           lvalp->name = omStrDup((char *)yytext);
[0e1846]213                           return STRINGTOK;
214                         }
215<bracket>\"[^\"]*\"      {
[c232af]216                           lvalp->name = omStrDup((char *)yytext);
[0e1846]217                           return STRINGTOK;
218                         }
219<bracket>")"             {
[cc94b0a]220                           yy_noeof = 0; BEGIN(INITIAL);
[0e1846]221                           return ')';
222                         }
223
224"{"                      {
[cc94b0a]225                           yy_blocklineno = yylineno;
[0e1846]226                           blocknest = 1;
[cc94b0a]227                           yy_noeof = noeof_block;
[0e1846]228                           BEGIN(block);
229                         }
[6ae4f5]230<block>"\""              {
[cc94b0a]231                           yy_noeof = noeof_string;
[6ae4f5]232                           BEGIN(blockstr);
233                           yymore();
234                         }
235<blockstr>[^\"]          { yymore(); }
236<blockstr>"\\\\"         { yymore(); }
237<blockstr>"\\\""         { yymore(); }
238<blockstr>"\""           {
[cc94b0a]239                           yy_noeof = noeof_block;
[6ae4f5]240                           BEGIN(block);
241                           yymore();
242                         }
243<block>[^\{\}\"]*        { yymore(); }
[c656484]244<block>\/\/[^\n]*        { yymore(); }
[0e1846]245<block>"{"               { blocknest++; yymore(); }
246<block>"}"               {
247                           if (--blocknest <= 0)
248                           {
249                             BEGIN(INITIAL);
[cc94b0a]250                             yy_noeof = 0;
[0e1846]251                             lvalp->name = dupyytextNL();
252                             return BLOCKTOK;
253                           }
254                           yymore();
255                         }
[cc94b0a]256"\""                     { BEGIN(string); yy_noeof = noeof_string;}
[0e1846]257~                        { return SYS_BREAK; }
258<string>[^\"]            { yymore(); }
259<string>"\\\\"           { yymore(); }
260<string>"\\\""           { yymore(); }
261<string>"\""             {
262                           char * s;
[cc94b0a]263                           yy_noeof = 0;
[0e1846]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; }
[057e93c]277"::"                     { return COLONCOLON; }
[0e1846]278"--"                     { return MINUSMINUS; }
279"++"                     { return PLUSPLUS  ; }
280"=="                     { return EQUAL_EQUAL; }
[7b3094]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; }
[0e1846]285"!"                      { return NOT; }
286"!="                     { return NOTEQUAL; }
287"<>"                     { return NOTEQUAL; }
288"**"                     { return '^'; }
289\\                       { return '\\'; }
290newline                  {
[c232af]291                           lvalp->name = omStrDup("\n");
[0e1846]292                           return STRINGTOK;
293                         }
294{integer}                {
295                           lvalp->name = (char *)yytext;
[db5523]296                           return INT_CONST;
[0e1846]297                         }
298{integer}\/{integer}     {
[6bb498]299                           lvalp->name = (char *)yytext;
[0e1846]300                           return RINGVAR;
301                         }
[b7b08c]302\$                        {
[e9ad8a6]303                           m2_end(-1);
[0e1846]304                         }
[de25e5]305(quit|exit)[ \t\n]*;     {
[0e1846]306                           #ifdef MM_STAT
307                           mmStat(-500);
308                           #endif
[c232af]309                           #ifdef OM_TRACK
[d33552]310                           #ifndef NDEBUG
[de25e5]311                             omPrintUsedTrackAddrs(stdout, 10);
[0e1846]312                           #endif
[d33552]313                           #endif
[e9ad8a6]314                           m2_end(0);
[0e1846]315                         }
316
317{rgvars}|{realnum}       {
[6bb498]318                           lvalp->name = (char *)yytext;
[0e1846]319                           return RINGVAR;
320                         }
[7b3094]321[0-9]+\."e"[+-][0-9]+    {
[b7b08c]322                           lvalp->name = (char *)yytext;
323                           return RINGVAR;
324                         }
325[0-9]+\./[^\.]           {
326                           lvalp->name = (char *)yytext;
327                           return RINGVAR;
328                         }
[7b3094]329
[0e1846]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                           }
[c232af]342                           lvalp->name = omStrDup((char *)yytext);
[0e1846]343                           return UNKNOWN_IDENT;
344                         }
345
[057e93c]346.                       {
[0e1846]347                           /*if (*yytext == '\n') REJECT;*/
[3b1a83c]348                           register char ch= *yytext;
[7b3094]349                           lvalp->i = ch;
[3b1a83c]350                           switch(ch)
351                           {
352                             /* case '&': */
353                             case '|':
[7b3094]354                               return LOGIC_OP;
[3b1a83c]355                             /* case '/': */
356                             case '%':
357                             case '*':
[7b3094]358                               return MULDIV_OP;
[3b1a83c]359                             /* case '<': */
360                             case '>':
361                               return COMP_OP;
362                             default:
363                               break;
364                            }
[7b3094]365                            return ch;
[0e1846]366                         }
367%%
368
369void * myynewbuffer()
370{
[b1dfaf]371  void * oldb = YY_CURRENT_BUFFER;
[057e93c]372  yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE));
[0e1846]373  return oldb;
374}
375
376void myyoldbuffer(void * oldb)
377{
[b1dfaf]378  yy_delete_buffer(YY_CURRENT_BUFFER);
[0e1846]379  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
[057e93c]380  //yy_flush_buffer((YY_BUFFER_STATE)oldb);
[0e1846]381}
Note: See TracBrowser for help on using the repository browser.