source: git/Singular/scanner.l @ 8d1d137

fieker-DuValspielwiese
Last change on this file since 8d1d137 was 8d1d137, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: added rString, sleftv->String for RING_CMD, QRING_CMD, string(ring), string(qring) in iparith.cc TEST_OPT_PROT for hilbert driven std git-svn-id: file:///usr/local/Singular/svn/trunk@116 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.3 1997-03-27 12:42:50 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\$[\.;]?                 {
253                           #ifdef HAVE_TCL
254                           if (tclmode)
255                             PrintTCL('Q',0,NULL);
256                           else
257                           #endif
258                           { if (BVERBOSE(0)) printf("\n$Bye.\n"); }
259                           #ifdef sun
260                           #ifndef __svr4__
261                           #ifdef HAVE_FEREAD
262                           fe_reset_input_mode(0,NULL);
263                           #endif
264                           _cleanup();
265                           _exit(0);
266                           #endif
267                           #endif
268                           exit(0);
269                         }
270(quit|exitall)[ \t\n]*[\.;]?       {
271                           #ifdef HAVE_TCL
272                             if (tclmode)
273                               PrintTCL('Q',0,NULL);
274                             else
275                           #endif
276                           #ifdef MM_STAT
277                           mmStat(-500);
278                           #endif
279                           { if (BVERBOSE(0)) printf("\nAuf Wiedersehen.\n"); }
280                           #ifdef MDEBUG
281                             #ifndef macintosh
282                               #ifdef HAVE_FEREAD
283                                 #ifdef HAVE_ATEXIT
284                                   fe_reset_input_mode();
285                                 #else
286                                   fe_reset_input_mode(0,NULL);
287                                 #endif
288                               #endif
289                             #endif
290                             #ifdef MLIST
291                             mmTestList();
292                             #endif
293                           #endif
294                           #ifdef sun
295                           #ifndef __svr4__
296                           _cleanup();
297                           _exit(0);
298                           #endif
299                           #endif
300                           exit(0);
301                         }
302
303{rgvars}|{realnum}       {
304                           lvalp->name = mstrdup((char *)yytext);
305                           return RINGVAR;
306                         }
307
308({parname}|{name})       {
309                           /* {name} */
310                           int rc=0;
311                           if (yytext[strlen((char *)yytext)-1] == '\n')
312                           {
313                             yytext[strlen((char *)yytext)-1] = '\0';
314                           }
315                           if (yyleng > 1)
316                           {
317                             rc = IsCmd((char *)yytext,lvalp->i);
318                             if (rc) return rc;
319                           }
320                           lvalp->name = mstrdup((char *)yytext);
321                           return UNKNOWN_IDENT;
322                         }
323
324.                        {
325                           /*if (*yytext == '\n') REJECT;*/
326                           lvalp->i = *yytext;   /* token has own value */
327                           return *yytext;
328                         }
329%%
330
331
332int mmread(FILE* f, char* b, int l)
333{
334  int rc;
335  b[0] = '\0';
336  if (inputswitch <=0)
337  {
338    char* s;
339    int offset=0;
340    showInput();
341NewRead:
342    if (f==stdin)
343      s = fe_fgets_stdin(b+offset,l-offset);
344    else
345      s = fgets(b+offset,l-offset,f);
346    if (s==NULL)
347    {
348      if (noeof)
349      {
350        switch (noeof)
351        {
352          case noeof_brace:
353          case noeof_block:
354            WerrorS("premature end of file while reading {...}");
355            break;
356          case noeof_asstring:
357            WerrorS("premature end of file while reading till `.`");
358            break;
359          case noeof_string:
360            WerrorS("premature end of file while reading string");
361            break;
362          case noeof_bracket:
363            WerrorS("premature end of file while reading (...)");
364            break;
365          case noeof_procname:
366            WerrorS("premature end of file while reading proc");
367            break;
368        }
369        exit(1);
370      }
371      return 0;
372    }
373    /*else*/
374    {
375      rc=strlen(s)-2;
376      if (s[rc]=='\\')
377      {
378        s[rc]='\0';
379        offset+=rc;
380        if (offset<l) goto NewRead;
381      }
382    }
383    if (feProt&PROT_I)
384    {
385      fputs(s,feProtFile);
386    }
387    rc=strlen(b);
388  }
389  else
390  {
391    rc=readbuf(b,l);
392  }
393  yylineno++;
394  //if ((rc != strlen(b)) printf("++funny error found! (scanner:rc=%d, len=%d)\n",
395  //  rc,(int)strlen(b));
396  if (rc>1)
397  {
398    strncpy(my_yylinebuf,b,rc-1);
399    if (my_yylinebuf[rc-2] == '\n') my_yylinebuf[rc-2] = '\0';
400    my_yylinebuf[rc-1] = '\0';
401    if (((si_echo>voice) && (strncmp(b,";RETURN();",10)!=0))
402    || (traceit&TRACE_SHOW_LINE)
403    || (traceit&TRACE_SHOW_LINE1))
404    {
405      printf("%s",b);
406      mflush();
407      if (traceit&TRACE_SHOW_LINE)
408      {
409        while(fgetc(stdin)!='\n');
410      }
411    }
412    else if (traceit&TRACE_SHOW_LINENO)
413    {
414      printf("{%d}",yylineno);
415      mflush();
416    }
417  }
418  else
419    my_yylinebuf[0] = '\0';
420  return rc;
421}
422
423void * myynewbuffer()
424{
425  void * oldb = yy_current_buffer;
426  yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
427  return oldb;
428}
429
430void myyoldbuffer(void * oldb)
431{
432  yy_delete_buffer(yy_current_buffer);
433  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
434}
Note: See TracBrowser for help on using the repository browser.