source: git/Singular/scanner.l @ 194f5e5

spielwiese
Last change on this file since 194f5e5 was c656484, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: fixed scanner bug: ignoring { and } in <block> (scanner.l) git-svn-id: file:///usr/local/Singular/svn/trunk@1344 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.0 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: scanner.l,v 1.12 1998-04-07 08:30:32 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 feReadLine(char* b, int l);
19#define ALLOC(a) Alloc((a))
20int yylineno  = 0;
21static int blocknest = 0;
22extern char * yytext;
23//extern unsigned char * yytext;
24extern int yyleng;
25extern BOOLEAN noringvars;
26extern int inerror;
27
28static char * dupyytext()
29{
30  //int i = strlen((char *)yytext);
31  //if (i>0) yytext[i-1] = '\0';
32  if (yyleng>0) yytext[yyleng-1] = '\0';
33  return mstrdup((char *)yytext);
34}
35
36static char * dupyytextNL()
37{
38  int i = yyleng;//strlen((char *)yytext);
39  char * rc = (char*)AllocL( 3 + i );
40  if (i>0)
41  {
42    yytext[i-1] = '\0';
43    strcpy( rc, (char *)yytext );
44  }
45  else
46    i++;
47  rc[i-1] = '\n';
48  rc[i] = '\n';
49  rc[i+1] = '\0';
50  return rc;
51}
52
53  #undef YY_DECL
54  #define YY_DECL int yylex(YYSTYPE* lvalp)
55
56  #undef yywrap
57  extern "C" {
58  int yywrap() { return exitVoice(); }
59  }
60
61  #undef YY_INPUT
62  #define YY_INPUT(buf,result,max_size) \
63          result = feReadLine( (char *) (buf), (max_size) )
64
65  #undef YY_USER_ACTION
66  #define YY_USER_ACTION \
67          if ((inerror==1)&&(*yytext>=' '))\
68          { Print("   skipping text from `%s`",yytext);inerror=2; }
69
70%}
71
72digit          [0-9]
73letter         [@a-zA-Z\']
74integer        {digit}+
75monom          {letter}+{digit}*
76rgvars         ({digit}+[/])*{digit}+{monom}+
77realnum        {digit}+"."{digit}+("e"[+-]{digit}+)?
78name           ({letter}({letter}*{digit}*_*)*|_)
79parname        #
80
81/* %start START */
82%option always-interactive
83
84%x string
85%x block
86%x blockstr
87%x brace
88%x bracestr
89%x bracket
90%x asstring
91
92%%
93\/\/[^\n]*               { }
94^#![^\n]*                { }
95pause[ \t\n]*[\.;]       { fePause(); }
96while                    { blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
97                           return WHILE_CMD;}
98for                      { blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
99                           return FOR_CMD;}
100
101("help"|"?")[ \t\n]*     { yy_noeof = noeof_asstring;
102                           BEGIN(asstring);
103                           return HELP_CMD;
104                         }
105
106proc[ \t]+{name}[ \t]*\( {
107                           char c; char *cp;
108                           lvalp->name = mstrdup(iiProcName((char *)yytext,c,cp));
109                           yy_noeof = noeof_procname;
110                           blocknest = 1;
111                           BEGIN(brace);
112                           return PROC_DEF;
113                         }
114<asstring>[^;\n]+        {
115                           lvalp->name = mstrdup((char *)yytext);
116                           yy_noeof = 0; BEGIN(INITIAL);
117                           return STRINGTOK;
118                         }
119<asstring>;              {
120                           yy_noeof = 0; BEGIN(INITIAL);
121                           return *yytext;
122                         }
123
124<brace>"\""              {
125                           yy_noeof = noeof_string;
126                           BEGIN(bracestr);
127                           yymore();
128                         }
129<brace>"("               { if (blocknest++) yymore(); }
130<brace>[^;\(\)]          { if (blocknest) yymore(); }
131<brace>";"               {
132                           if (blocknest)
133                           {
134                             lvalp->name = dupyytext();
135                             return STRINGTOK;
136                           }
137                         }
138<brace>")"               {
139                           if (--blocknest <= 0)
140                           {
141                             yy_noeof = 0;
142                             BEGIN(INITIAL);
143                             lvalp->name = dupyytext();
144                             return STRINGTOK;
145                           }
146                           yymore();
147                         }
148<bracestr>"\""           {
149                           yy_noeof = noeof_brace;
150                           BEGIN(brace);
151                           yymore();
152                         }
153<bracestr>[^\"]          { yymore(); }
154<bracket>"("             { return '('; }
155<bracket>","             { return ','; }
156<bracket>[ \t\n]*        { ; }
157<bracket>[^\(\), \t\n]*  {
158                           lvalp->name = mstrdup((char *)yytext);
159                           return STRINGTOK;
160                         }
161<bracket>\"[^\"]*\"      {
162                           lvalp->name = mstrdup((char *)yytext);
163                           return STRINGTOK;
164                         }
165<bracket>")"             {
166                           yy_noeof = 0; BEGIN(INITIAL);
167                           return ')';
168                         }
169
170"{"                      {
171                           yy_blocklineno = yylineno;
172                           blocknest = 1;
173                           yy_noeof = noeof_block;
174                           BEGIN(block);
175                         }
176<block>"\""              {
177                           yy_noeof = noeof_string;
178                           BEGIN(blockstr);
179                           yymore();
180                         }
181<blockstr>[^\"]          { yymore(); }
182<blockstr>"\\\\"         { yymore(); }
183<blockstr>"\\\""         { yymore(); }
184<blockstr>"\""           {
185                           yy_noeof = noeof_block;
186                           BEGIN(block);
187                           yymore();
188                         }
189<block>[^\{\}\"]*        { yymore(); }
190<block>\/\/[^\n]*        { yymore(); }
191<block>"{"               { blocknest++; yymore(); }
192<block>"}"               {
193                           if (--blocknest <= 0)
194                           {
195                             BEGIN(INITIAL);
196                             yy_noeof = 0;
197                             lvalp->name = dupyytextNL();
198                             return BLOCKTOK;
199                           }
200                           yymore();
201                         }
202"\""                     { BEGIN(string); yy_noeof = noeof_string;}
203~                        { return SYS_BREAK; }
204<string>[^\"]            { yymore(); }
205<string>"\\\\"           { yymore(); }
206<string>"\\\""           { yymore(); }
207<string>"\""             {
208                           char * s;
209                           yy_noeof = 0;
210                           BEGIN(INITIAL);
211                           s = lvalp->name = dupyytext();
212                           while (*yytext)
213                           {
214                             if (*yytext == '\\') yytext++;
215                             *s++ = *yytext++;
216                           }
217                           *s++ = *yytext++;
218                           return STRINGTOK;
219                         }
220
221[ \t\r\n]                /* skip whitespace */
222".."                     { return DOTDOT; }
223"::"                     { return COLONCOLON; }
224"--"                     { return MINUSMINUS; }
225"++"                     { return PLUSPLUS  ; }
226"=="                     { return EQUAL_EQUAL; }
227"&&"                     { return '&'; }
228"||"                     { return '|'; }
229"<="                     { return LE; }
230">="                     { return GE; }
231"!"                      { return NOT; }
232"!="                     { return NOTEQUAL; }
233"<>"                     { return NOTEQUAL; }
234"**"                     { return '^'; }
235\\                       { return '\\'; }
236newline                  {
237                           lvalp->name = mstrdup("\n");
238                           return STRINGTOK;
239                         }
240{integer}                {
241                           lvalp->name = (char *)yytext;
242                           if ((currRing!=NULL)
243                           && (strlen(lvalp->name)>MAX_INT_LEN))
244                           {
245                             lvalp->name = mstrdup((char *)yytext);
246                             return RINGVAR;
247                           }
248                           else
249                             return INT_CONST;
250                         }
251{integer}\/{integer}     {
252                           lvalp->name = mstrdup((char *)yytext);
253                           return RINGVAR;
254                         }
255\$                       {
256                           #ifdef HAVE_TCL
257                           if (tclmode)
258                             PrintTCL('Q',0,NULL);
259                           else
260                           #endif
261                           { if (BVERBOSE(0)) printf("\n$Bye.\n"); }
262                           #ifndef macintosh
263                             #ifdef HAVE_FEREAD
264                               #ifdef HAVE_ATEXIT
265                                 fe_reset_input_mode();
266                               #else
267                                 fe_reset_input_mode(0,NULL);
268                               #endif
269                             #else
270                               #ifdef HAVE_READLINE
271                                 fe_reset_input_mode();
272                               #endif
273                             #endif
274                           #endif
275                           #ifdef sun
276                           #ifndef __svr4__
277                           _cleanup();
278                           _exit(0);
279                           #endif
280                           #endif
281                           exit(0);
282                         }
283(quit|exit)[ \t\n]*[\.;]?  {
284                           #ifdef HAVE_TCL
285                             if (tclmode)
286                               PrintTCL('Q',0,NULL);
287                             else
288                           #endif
289                           #ifdef MM_STAT
290                           mmStat(-500);
291                           #endif
292                           { if (BVERBOSE(0)) printf("\nAuf Wiedersehen.\n"); }
293                           #ifndef macintosh
294                             #ifdef HAVE_FEREAD
295                               #ifdef HAVE_ATEXIT
296                                 fe_reset_input_mode();
297                               #else
298                                 fe_reset_input_mode(0,NULL);
299                               #endif
300                             #else
301                               #ifdef HAVE_READLINE
302                                 fe_reset_input_mode();
303                               #endif
304                             #endif
305                           #endif
306                           #ifdef MDEBUG
307                             #ifdef MLIST
308                             mmTestList();
309                             #endif
310                           #endif
311                           #ifdef sun
312                           #ifndef __svr4__
313                           _cleanup();
314                           _exit(0);
315                           #endif
316                           #endif
317                           exit(0);
318                         }
319
320{rgvars}|{realnum}       {
321                           lvalp->name = mstrdup((char *)yytext);
322                           return RINGVAR;
323                         }
324
325({parname}|{name})       {
326                           /* {name} */
327                           int rc=0;
328                           if (yytext[strlen((char *)yytext)-1] == '\n')
329                           {
330                             yytext[strlen((char *)yytext)-1] = '\0';
331                           }
332                           if (yyleng > 1)
333                           {
334                             rc = IsCmd((char *)yytext,lvalp->i);
335                             if (rc) return rc;
336                           }
337                           lvalp->name = mstrdup((char *)yytext);
338                           return UNKNOWN_IDENT;
339                         }
340
341.                       {
342                           /*if (*yytext == '\n') REJECT;*/
343                           lvalp->i = *yytext;   /* token has own value */
344                           return *yytext;
345                         }
346%%
347
348void * myynewbuffer()
349{
350  void * oldb = yy_current_buffer;
351  yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE));
352  return oldb;
353}
354
355void myyoldbuffer(void * oldb)
356{
357  yy_delete_buffer(yy_current_buffer);
358  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
359  //yy_flush_buffer((YY_BUFFER_STATE)oldb);
360}
Note: See TracBrowser for help on using the repository browser.