source: git/Singular/scanner.l @ f6b5f0

fieker-DuValspielwiese
Last change on this file since f6b5f0 was f6b5f0, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: change Log: and Header: to Id: git-svn-id: file:///usr/local/Singular/svn/trunk@73 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.1 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: scanner.l,v 1.2 1997-03-24 14:25:43 Singular Exp $ */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <ctype.h>
10
11#include "mod2.h"
12#include "tok.h"
13#include "stype.h"
14#include "ipshell.h"
15#include "mmemory.h"
16#include "febase.h"
17
18int mmread(FILE* f, char* b, int l);
19#define ALLOC(a) Alloc((a))
20int yylineno  = 0;
21static int noeof = 0;
22enum
23{
24  noeof_brace = 1,
25  noeof_asstring,
26  noeof_block,
27  noeof_string,
28  noeof_bracket,
29  noeof_procname
30};
31static int blocknest = 0;
32extern char * yytext;
33//extern unsigned char * yytext;
34extern int yyleng;
35extern int inputswitch;
36extern int noringvars;
37extern inerror;
38
39static char * dupyytext()
40{
41  //int i = strlen((char *)yytext);
42  //if (i>0) yytext[i-1] = '\0';
43  if (yyleng>0) yytext[yyleng-1] = '\0';
44  return mstrdup((char *)yytext);
45}
46
47static char * dupyytextNL()
48{
49  int i = yyleng;//strlen((char *)yytext);
50  char * rc = (char*)AllocL( 3 + i );
51  if (i>0)
52  {
53    yytext[i-1] = '\0';
54    strcpy( rc, (char *)yytext );
55  }
56  else
57    i++;
58  rc[i-1] = '\n';
59  rc[i] = '\n';
60  rc[i+1] = '\0';
61  return rc;
62}
63
64  #undef YY_DECL
65  #define YY_DECL int yylex(YYSTYPE* lvalp)
66
67  #undef yywrap
68  int yywrap() { return exitVoice(); }
69
70  #undef YY_INPUT
71  #define YY_INPUT(buf,result,max_size) \
72          if ( (result = mmread( (yyin), (char *) buf, max_size )) < 0 ) \
73              YY_FATAL_ERROR( "read() in flex scanner failed" );
74
75  #undef YY_USER_ACTION
76  #define YY_USER_ACTION \
77          if (inerror==1) { Print("   skipping text from `%s`",yytext);inerror=2; }
78
79%}
80
81digit          [0-9]
82letter         [@a-zA-Z\']
83integer        {digit}+
84monom          {letter}+{digit}*
85rgvars         ({digit}+[/])*{digit}+{monom}+
86realnum        {digit}+"."{digit}+("e"[+-]{digit}+)?
87name           ({letter}({letter}*{digit}*_*)*|_)
88parname        #
89
90/* %start START */
91
92%x string
93%x block
94%x brace
95%x bracestr
96%x bracket
97%x asstring
98
99%%
100\/\/[^\n]*               { }
101^#![^\n]*                { }
102pause[ \t\n]*[\.;]       { fePause(); }
103while                    { blocknest = 0; noeof = noeof_brace; BEGIN(brace);
104                           return WHILE_CMD;}
105for                      { blocknest = 0; noeof = noeof_brace; BEGIN(brace);
106                           return FOR_CMD;}
107
108("help"|"?")[ \t\n]*     { noeof = noeof_asstring;
109                           BEGIN(asstring);
110                           return HELP_CMD;
111                         }
112
113example[ \t\n]*          { noeof = noeof_asstring;
114                           BEGIN(asstring);
115                           return EXAMPLE_CMD;
116                         }
117
118proc[ \t]+{name}[ \t]*\( {
119                           char c; char *cp;
120                           lvalp->name = mstrdup(iiProcName((char *)yytext,c,cp));
121                           noeof = noeof_procname;
122                           blocknest = 1;
123                           BEGIN(brace);
124                           return PROC_DEF;
125                         }
126<asstring>[^;\n]+        {
127                           lvalp->name = mstrdup((char *)yytext);
128                           noeof = 0; BEGIN(INITIAL);
129                           return STRINGTOK;
130                         }
131<asstring>;              {
132                           noeof = 0; BEGIN(INITIAL);
133                           return *yytext;
134                         }
135
136<brace>"\""              {
137                           noeof = noeof_brace;
138                           BEGIN(bracestr);
139                           yymore();
140                         }
141<brace>"("               { if (blocknest++) yymore(); }
142<brace>[^;\(\)]          { if (blocknest) yymore(); }
143<brace>";"               {
144                           if (blocknest)
145                           {
146                             lvalp->name = dupyytext();
147                             return STRINGTOK;
148                           }
149                         }
150<brace>")"               {
151                           if (--blocknest <= 0)
152                           {
153                             noeof = 0;
154                             BEGIN(INITIAL);
155                             lvalp->name = dupyytext();
156                             return STRINGTOK;
157                           }
158                           yymore();
159                         }
160<bracestr>"\""           {
161                           noeof = noeof_brace;
162                           BEGIN(brace);
163                           yymore();
164                         }
165<bracestr>[^\"]          { yymore(); }
166<bracket>"("             { return '('; }
167<bracket>","             { return ','; }
168<bracket>[ \t\n]*        { ; }
169<bracket>[^\(\), \t\n]*  {
170                           lvalp->name = mstrdup((char *)yytext);
171                           return STRINGTOK;
172                         }
173<bracket>\"[^\"]*\"      {
174                           lvalp->name = mstrdup((char *)yytext);
175                           return STRINGTOK;
176                         }
177<bracket>")"             {
178                           noeof = 0; BEGIN(INITIAL);
179                           return ')';
180                         }
181
182"{"                      {
183                           blocklineno = yylineno;
184                           blocknest = 1;
185                           noeof = noeof_block;
186                           BEGIN(block);
187                         }
188<block>[^\{\}]*          { yymore(); }
189<block>"{"               { blocknest++; yymore(); }
190<block>"}"               {
191                           if (--blocknest <= 0)
192                           {
193                             BEGIN(INITIAL);
194                             noeof = 0;
195                             lvalp->name = dupyytextNL();
196                             return BLOCKTOK;
197                           }
198                           yymore();
199                         }
200"\""                     { BEGIN(string); noeof = noeof_string;}
201~                        { return SYS_BREAK; }
202<string>[^\"]            { yymore(); }
203<string>"\\\\"           { yymore(); }
204<string>"\\\""           { yymore(); }
205<string>"\""             {
206                           char * s;
207                           noeof = 0;
208                           BEGIN(INITIAL);
209                           s = lvalp->name = dupyytext();
210                           while (*yytext)
211                           {
212                             if (*yytext == '\\') yytext++;
213                             *s++ = *yytext++;
214                           }
215                           *s++ = *yytext++;
216                           return STRINGTOK;
217                         }
218
219[ \t\r\n]                /* skip whitespace */
220".."                     { return DOTDOT; }
221"--"                     { return MINUSMINUS; }
222"++"                     { return PLUSPLUS  ; }
223"=="                     { return EQUAL_EQUAL; }
224"&&"                     { return '&'; }
225"||"                     { return '|'; }
226"<="                     { return LE; }
227">="                     { return GE; }
228"!"                      { return NOT; }
229"!="                     { return NOTEQUAL; }
230"<>"                     { return NOTEQUAL; }
231"**"                     { return '^'; }
232\\                       { return '\\'; }
233newline                  {
234                           lvalp->name = mstrdup("\n");
235                           return STRINGTOK;
236                         }
237{integer}                {
238                           lvalp->name = (char *)yytext;
239                           if ((currRing!=NULL)
240                           && (strlen(lvalp->name)>=MAX_INT_LEN-1))
241                           {
242                             lvalp->name = mstrdup((char *)yytext);
243                             return RINGVAR;
244                           }
245                           else
246                             return INT_CONST;
247                         }
248{integer}\/{integer}     {
249                           lvalp->name = mstrdup((char *)yytext);
250                           return RINGVAR;
251                         }
252                         //exit[ \t\n]*[\.;]?       { /*if (exitFile())*/ return EXIT_CMD; }
253\$[\.;]?                 {
254                           #ifdef HAVE_TCL
255                           if (tclmode)
256                             PrintTCL('Q',0,NULL);
257                           else
258                           #endif
259                           { if (BVERBOSE(0)) printf("\n$Bye.\n"); }
260                           #ifdef sun
261                           #ifndef __svr4__
262                           #ifdef HAVE_FEREAD
263                           fe_reset_input_mode(0,NULL);
264                           #endif
265                           _cleanup();
266                           _exit(0);
267                           #endif
268                           #endif
269                           exit(0);
270                         }
271(quit|exitall)[ \t\n]*[\.;]?       {
272                           #ifdef HAVE_TCL
273                             if (tclmode)
274                               PrintTCL('Q',0,NULL);
275                             else
276                           #endif
277                           #ifdef MM_STAT
278                           mmStat(-500);
279                           #endif
280                           { if (BVERBOSE(0)) printf("\nAuf Wiedersehen.\n"); }
281                           #ifdef MDEBUG
282                             #ifndef macintosh
283                               #ifdef HAVE_FEREAD
284                                 #ifdef HAVE_ATEXIT
285                                   fe_reset_input_mode();
286                                 #else
287                                   fe_reset_input_mode(0,NULL);
288                                 #endif
289                               #endif
290                             #endif
291                             #ifdef MLIST
292                             mmTestList();
293                             #endif
294                           #endif
295                           #ifdef sun
296                           #ifndef __svr4__
297                           _cleanup();
298                           _exit(0);
299                           #endif
300                           #endif
301                           exit(0);
302                         }
303
304{rgvars}|{realnum}       {
305                           lvalp->name = mstrdup((char *)yytext);
306                           return RINGVAR;
307                         }
308
309({parname}|{name})       {
310                           /* {name} */
311                           int rc=0;
312                           if (yytext[strlen((char *)yytext)-1] == '\n')
313                           {
314                             yytext[strlen((char *)yytext)-1] = '\0';
315                           }
316                           if (yyleng > 1)
317                           {
318                             rc = IsCmd((char *)yytext,lvalp->i);
319                             if (rc) return rc;
320                           }
321                           lvalp->name = mstrdup((char *)yytext);
322                           return UNKNOWN_IDENT;
323                         }
324
325.                        {
326                           /*if (*yytext == '\n') REJECT;*/
327                           lvalp->i = *yytext;   /* token has own value */
328                           return *yytext;
329                         }
330%%
331
332
333int mmread(FILE* f, char* b, int l)
334{
335  int rc;
336  b[0] = '\0';
337  if (inputswitch <=0)
338  {
339    char* s;
340    int offset=0;
341    showInput();
342NewRead:
343    if (f==stdin)
344      s = fe_fgets_stdin(b+offset,l-offset);
345    else
346      s = fgets(b+offset,l-offset,f);
347    if (s==NULL)
348    {
349      if (noeof)
350      {
351        switch (noeof)
352        {
353          case noeof_brace:
354          case noeof_block:
355            WerrorS("premature end of file while reading {...}");
356            break;
357          case noeof_asstring:
358            WerrorS("premature end of file while reading till `.`");
359            break;
360          case noeof_string:
361            WerrorS("premature end of file while reading string");
362            break;
363          case noeof_bracket:
364            WerrorS("premature end of file while reading (...)");
365            break;
366          case noeof_procname:
367            WerrorS("premature end of file while reading proc");
368            break;
369        }
370        exit(1);
371      }
372      return 0;
373    }
374    /*else*/
375    {
376      rc=strlen(s)-2;
377      if (s[rc]=='\\')
378      {
379        s[rc]='\0';
380        offset+=rc;
381        if (offset<l) goto NewRead;
382      }
383    }
384    if (feProt&PROT_I)
385    {
386      fputs(s,feProtFile);
387    }
388    rc=strlen(b);
389  }
390  else
391  {
392    rc=readbuf(b,l);
393  }
394  yylineno++;
395  //if ((rc != strlen(b)) printf("++funny error found! (scanner:rc=%d, len=%d)\n",
396  //  rc,(int)strlen(b));
397  if (rc>1)
398  {
399    strncpy(my_yylinebuf,b,rc-1);
400    if (my_yylinebuf[rc-2] == '\n') my_yylinebuf[rc-2] = '\0';
401    my_yylinebuf[rc-1] = '\0';
402    if (((si_echo>voice) && (strncmp(b,";RETURN();",10)!=0))
403    || (traceit&TRACE_SHOW_LINE)
404    || (traceit&TRACE_SHOW_LINE1))
405    {
406      printf("%s",b);
407      mflush();
408      if (traceit&TRACE_SHOW_LINE)
409      {
410        while(fgetc(stdin)!='\n');
411      }
412    }
413    else if (traceit&TRACE_SHOW_LINENO)
414    {
415      printf("{%d}",yylineno);
416      mflush();
417    }
418  }
419  else
420    my_yylinebuf[0] = '\0';
421  return rc;
422}
423
424void * myynewbuffer()
425{
426  void * oldb = yy_current_buffer;
427  yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
428  return oldb;
429}
430
431void myyoldbuffer(void * oldb)
432{
433  yy_delete_buffer(yy_current_buffer);
434  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
435}
Note: See TracBrowser for help on using the repository browser.