source: git/old_modgen/tools/scanner.l @ c7d29b

fieker-DuValspielwiese
Last change on this file since c7d29b was c36fda, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
Moved all the outdated modgen related stuff to /old_modgen/ for now
  • Property mode set to 100644
File size: 13.8 KB
Line 
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
10#include "modgen.h"
11#include <kernel/mod2.h>
12#include <febase.h>
13#include <grammar.h>
14#include <ipid.h>
15#include <ipshell.h>
16#include <mmemory.h>
17#include <structs.h>
18#include <subexpr.h>
19#include <tok.h>
20
21#define DEBUG 0
22 
23#  define YYLP_ERR_NONE    0
24#  define YYLP_DEF_BR2     1
25#  define YYLP_BODY_BR2    2
26#  define YYLP_BODY_BR3    3
27#  define YYLP_BODY_TMBR2  4
28#  define YYLP_BODY_TMBR3  5
29#  define YYLP_EX_BR2      6
30#  define YYLP_EX_BR3      7
31#  define YYLP_BAD_CHAR    8
32#  define YYLP_MISSQUOT    9
33#  define YYLP_MISS_BR1   10
34#  define YYLP_MISS_BR2   11
35#  define YYLP_MISS_BR3   12
36
37const char sNoName[]="_";
38int brace1 = 0;  /* { } */
39int brace2 = 0;  /* ( ) */
40int brace3 = 0;  /* [ ] */
41int quote  = 0;  /* " */
42int offset = 0;
43
44 char       my_yylinebuf[80];
45int *old_states = NULL;
46int state_level = -1;
47int state_max = 0;
48int yylineno = 1;
49int tok;
50int  myynest = -1;
51int  traceit = 0;
52moddef module_def;
53 int yylplineno = 1;
54 int yylp_errno = 0;
55
56 long C_start;
57 long C_end;
58 char c_codetype = 0;
59#define C_CODE_NONE 0
60#define C_CODE_PROC 1
61#define C_CODE_MAIN 2
62 
63 char string_type = 0;
64#define STRING_NONE 0
65#define STRING_INFO 1
66#define STRING_VERS 2
67 long string_start = 0;
68 long string_end = 0;
69 
70char *yylp_buffer_start;
71
72int libread(FILE* f, char* buf, int max_size);
73int current_pos(int i);
74int read_string(char **buffer, long *start, long end);
75 
76void push_state(int state, int new_state);
77void pop_state();
78 
79#  undef YY_INPUT
80#  define YY_INPUT(buf,result,max_size) \
81          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
82                  && ferror( yyin ) ) \
83                YY_FATAL_ERROR( "read in flex scanner failed" );
84
85
86extern "C"
87{
88  int yywrap();
89}
90
91%}
92
93digit          [0-9]
94letter         [@a-zA-Z\']
95name           ({letter}({letter}*{digit}*_*)*|_)
96fname          ({letter}({letter}*{digit}*_*.)*|_)
97letters        ({letter}|{digit}|[_./#%^*:,])
98string         ({letters}*)
99comment        [\/][\/]
100dolar          [$]
101symbols        [~!@#$%^&*()_+-={}\\\|\[\];:,<.>/\?\' \n\~\`\r]
102aletters       ({letter}|{digit}|{symbols}|{dolar}|{escquote})
103strings        ({aletters}*)
104quote          [\"]
105escquote       (\\\")
106taborspace     [ \t]
107tos            ({taborspace}*)
108eq             (=|{tos}+=|=+{tos}|{tos}+=+{tos})
109tnl            ([ \t\n]*)
110col            (;|{tos}+;)
111eqnl           ([ \t\n]*+=[ \t\n]*)
112
113/* %start START */
114
115%x pdef
116%x comment
117%x procdef
118%x ctext
119%x string
120%x cstring
121
122%%
123(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
124\/\/*        { push_state(YYSTATE, comment); }
125
126(module+{eqnl}+{quote}+{name}+{quote}) {
127             char *buff = (char *)malloc(yyleng+4);
128             memset(buff, '\0', yyleng+4);
129             sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff);
130             module_def.name = (char *)malloc(strlen(buff)+1);
131             memset(module_def.name, '\0', strlen(buff)+1);
132             memcpy(module_def.name, buff, strlen(buff));
133             strcat(buff, ".cc");
134             Add2files(&module_def, buff);
135             free(buff);
136           }
137
138(version+{eqnl}+{quote}+{strings}+{quote}) {
139             yyless(4);
140             string_type = STRING_VERS;
141             push_state(YYSTATE, cstring);
142           }
143
144(helpfile+{eqnl}+{quote}+{fname}+{quote}) {
145             char *buff = (char *)malloc(yyleng+1);
146             memset(buff, '\0', yyleng+1);
147             sscanf( yytext, "%*[^\"]\"%[^\"]\"", buff);
148             module_def.helpfile = (char *)malloc(strlen(buff)+1);
149             memset(module_def.helpfile, '\0', strlen(buff)+1);
150             memcpy(module_def.helpfile, buff, strlen(buff));
151             printf("==>HELP:'%s'\n", module_def.helpfile);
152             free(buff);
153           }
154
155(cxxsource+{eqnl}+{fname}) {
156             char *buff = (char *)malloc(yyleng+1);
157             memset(buff, '\0', yyleng+1);
158             sscanf( yytext, "%*[^=]=%s", buff);
159             Add2files(&module_def,buff);
160             free(buff);
161           }
162
163(info+{eqnl}+{quote}+{strings}+{quote}) {
164             yyless(4);
165             string_type = STRING_INFO;
166             push_state(YYSTATE, cstring);
167           }
168
169<cstring>{quote} { quote++; push_state(YYSTATE, string);
170                string_start = current_pos(yyleng); }
171<cstring>\n { yylplineno++; }
172<cstring>{col} {
173            switch(string_type) {
174                case STRING_INFO:                     
175                  read_string(&module_def.info, &string_start, string_end);
176                  break;
177                 
178                case STRING_VERS:                     
179                  read_string(&module_def.version, &string_start, string_end);
180                  make_version(module_def.version, &module_def);
181                  break;
182            }
183            string_type = STRING_NONE;
184            pop_state(); }
185<cstring>.  { }
186
187
188
189(proc+{tos}+{name})|({tos}+proc+{tos}+{name}) {
190             char *proc = (char *)malloc(yyleng+1);
191             memset(proc, '\0', yyleng+1);
192             push_state(YYSTATE, pdef);
193             sscanf( yytext, "%*[^p]proc %s", proc);
194             if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
195             Add2proclist(&module_def, proc, "none", "NONE", NONE);
196             free(proc);
197           }
198
199(proc+{tos}+{name}+{tos}+{name})|({tos}+proc+{tos}+{name}+{tos}+{name}) {
200             char *proc = (char *)malloc(yyleng+1);
201             char *ret_val = (char *)malloc(yyleng+1);
202             memset(proc, '\0', yyleng+1);
203             memset(ret_val, '\0', yyleng+1);
204
205             char n2[32];
206             int cmd;
207             memset(n2, '\0', 32);
208             push_state(YYSTATE, pdef);
209             sscanf( yytext, "%*[^p]proc %s %s", ret_val, proc);
210             if(strlen(proc)<1) sscanf( yytext, "proc %s %s", ret_val, proc);
211             printf("'%s'\n", yytext);
212             printf("R: proc '%s' '%s'\n", ret_val, proc);
213             cmd = IsCmd(ret_val, tok);
214             if(cmd!=0)
215                Add2proclist(&module_def, proc, ret_val,
216                                          decl2str(tok,n2), tok);
217             else {
218               printf("proc '%s': Invalid return parameter %s.\n",
219                         proc, ret_val);
220               Add2proclist(&module_def, proc, "none",
221                                          "NONE", NONE);
222             }
223             free(proc);
224             free(ret_val);
225           }
226
227<pdef>[ \t]  { }
228<pdef>"("    { }
229<pdef>{name}+, {
230             char param[256], n2[32];
231             int cmd;
232             memset(n2, '\0', 32);
233             memset(param, '\0', 256);
234             sscanf( yytext, "%[^,],", param);
235             cmd = IsCmd(param, tok);
236             if(cmd!=0)AddParam(&module_def,
237                                param, decl2str(tok,n2), tok);
238             else printf("proc '%s': Invalid parameter %s.\n",
239                         module_def.procs[module_def.proccnt-1].procname,
240                         param);
241           }
242<pdef>{name}+")"+{tnl}+"{" {
243             char param[256], n2[32];
244             int cmd;
245             BEGIN(procdef);
246             memset(param, '\0', 256);
247             memset(n2, '\0', 32);
248             sscanf( yytext, "%[^)])", param);
249             cmd = IsCmd(param, tok);
250             if(cmd!=0)AddParam(&module_def,
251                                param, decl2str(tok,n2), tok);
252             else printf("proc '%s': Invalid parameter %s.\n",
253                         module_def.procs[module_def.proccnt-1].procname,
254                         param);
255           }
256<pdef>")"+{tnl}+"{" {
257             BEGIN(procdef);
258           }
259
260<pdef>"{" {
261             BEGIN(procdef);
262           }
263
264<pdef>(")"+{eqnl}+{name})|({eq}+{name}) {
265             char funcname[256];
266             pop_state();
267             sscanf( yytext, "%*[^=]=%s", funcname);
268             if(strlen(funcname)<=0)
269                sscanf( yytext, "=%s", funcname);
270             free(module_def.procs[module_def.proccnt-1].funcname);
271             module_def.procs[module_def.proccnt-1].funcname =
272                (char *)malloc(strlen(funcname)+1);
273             memset(module_def.procs[module_def.proccnt-1].funcname,
274                    '\0', strlen(funcname)+1);
275             memcpy(module_def.procs[module_def.proccnt-1].funcname,
276                    funcname, strlen(funcname));
277           }
278
279<pdef>({name}+")"+{eqnl}+{name}) {
280             char param[256], n2[32],funcname[256];
281             int cmd;
282             pop_state();
283             memset(param, '\0', 256);
284             memset(n2, '\0', 32);
285             memset(funcname, '\0', 256);
286             sscanf( yytext, "%[^)])%*[^=]=%s", param, funcname);
287             if(strlen(funcname)<=0)
288                sscanf( yytext, "%[^)])=%s", param, funcname);
289             free(module_def.procs[module_def.proccnt-1].funcname);
290             module_def.procs[module_def.proccnt-1].funcname =
291                (char *)malloc(strlen(funcname)+1);
292             memset(module_def.procs[module_def.proccnt-1].funcname,
293                    '\0', strlen(funcname)+1);
294             memcpy(module_def.procs[module_def.proccnt-1].funcname,
295                    funcname, strlen(funcname));
296             cmd = IsCmd(param, tok);
297             if(cmd!=0)AddParam(&module_def,
298                                param, decl2str(tok,n2), tok);
299             else printf("proc '%s': Invalid parameter %s.\n",
300                         module_def.procs[module_def.proccnt-1].procname, param);
301           }
302
303<pdef>.      { }
304
305<procdef>"}"  { pop_state(); }
306<procdef>RETURN+{eqnl}+{name}+{col}   {
307  printf("Return:\n");}
308<procdef>function+{eqnl}+{name}+{col} {
309       char *funcname = (char *)malloc(yyleng+1);
310       memset(funcname, '\0', yyleng+1);
311       sscanf( yytext, "%*[^=]=%[^;];", funcname);
312       if(strlen(funcname)<=0)
313         sscanf( yytext, "=%[^;];", funcname);
314       free(module_def.procs[module_def.proccnt-1].funcname);
315       module_def.procs[module_def.proccnt-1].funcname =
316         (char *)malloc(strlen(funcname)+   1);
317       memset(module_def.procs[module_def.proccnt-1].funcname,
318              '\0', strlen(funcname)+1);
319       memcpy(module_def.procs[module_def.proccnt-1].funcname,
320              funcname, strlen(funcname));
321     }
322
323<procdef>checkring+{col}            {
324  printf("Do checkring\n"); }
325<procdef>C+{eqnl}+"{" {
326                push_state(YYSTATE, ctext);
327                C_start=current_pos(yyleng);
328                yyless(yyleng-1);
329                c_codetype = C_CODE_PROC;
330              }
331<procdef>.                          { printf("%s", yytext); }
332
333<comment>\*\/            { pop_state(); }
334<comment>\n              { yylineno++; }
335<comment>.               { }
336
337<ctext>({comment}[^\n]*) { }
338<ctext>\/\/*   { push_state(YYSTATE, comment); }
339<ctext>{quote} { quote++; push_state(YYSTATE, string); }
340<ctext>"{"     { brace1++; }
341<ctext>"}"     {
342       brace1--;
343       if(brace2>0) {
344         yylp_errno = YYLP_BODY_BR2;
345         return(1);
346       }
347       if(brace3>0) {
348         yylp_errno = YYLP_BODY_BR3;
349         return(1);
350       }
351       if(brace1<=0) {
352         C_end = current_pos(yyleng)-1;
353         printf("Ctext: %d-%d\n", C_start, C_end);
354         switch(c_codetype) {
355             case C_CODE_PROC:
356               read_string(&(module_def.procs[module_def.proccnt-1].c_code),
357                           &C_start, C_end);
358               break;
359
360             case C_CODE_MAIN:
361               break;
362         }
363         c_codetype = C_CODE_NONE;
364         pop_state();
365       }
366     }
367<ctext>"("       { brace2++; }
368<ctext>")"       { brace2--;
369                   if(brace2<0) {
370                     yylp_errno = YYLP_BODY_TMBR2;
371                     return(1);
372                   }
373                 }
374<ctext>"["       { brace3++; }
375<ctext>"]"       { brace3--;
376                   if(brace3<0) {
377                     yylp_errno = YYLP_BODY_TMBR3;
378                     return(1);
379                   }
380                 }
381<ctext>\n  { yylineno++; }
382<ctext>.   { }
383
384<string>{quote} { quote--; string_end = current_pos(yyleng)-1; pop_state(); }
385<string>(\\\\)|(\\\") { }
386<string>\n            { yylplineno++; }
387<string>.             { }
388
389
390C+{eqnl}+"{" { push_state(YYSTATE, ctext);
391               C_start=current_pos(yyleng);
392               yyless(yyleng-1);
393             }
394\n           { yylineno++; }
395\r           { }
396.            { }
397
398%%
399
400extern "C" {
401  int yywrap() {
402    //printf("yywrap()\n");
403    return 1;
404  }
405}
406
407int libread(FILE* f, char* buf, int max_size)
408{ int rc;
409
410  offset = ftell(f);
411  rc  = fread( buf, 1, max_size, f );
412#if YYLPDEBUG >2
413  printf("fread: %d of %d\n", rc, max_size);
414#endif
415  yylp_buffer_start = buf;
416  return rc;
417}
418
419int current_pos(int i)
420{
421  return(i+offset+(int)(yytext-yylp_buffer_start));
422}
423
424int read_string(char **p, long *start, long end)
425{
426  char *buffer;
427 
428  if(*start > 0 ) {
429    long len = end - *start;
430    if(len>=0) {
431      long cur = ftell(yyin);
432      buffer = (char *)malloc(len+1);
433      memset(buffer, '\0', len+1);
434      fseek(yyin, *start, SEEK_SET);
435      fread(buffer, 1, len, yyin);
436      fseek(yyin, cur, SEEK_SET);
437      *p = buffer;
438    }
439    *start = 0;
440  }
441}
442               
443void push_state(int state, int new_state)
444{
445  state_level++;
446#if DEBUG
447  printf("====>PUSH to new state %d/%d l=%d\n", state, new_state, state_level);
448#endif
449  if(state_level>=state_max) {
450    state_max++;
451    if(old_states == NULL)
452      old_states = (int *)malloc(sizeof(int));
453    else {
454      old_states = (int *)realloc(old_states, state_max*sizeof(int));
455    }
456  }
457  old_states[state_level] = state;
458  BEGIN(new_state);
459}
460
461void pop_state()
462{
463#if DEBUG
464  printf("====>Back to old state %d, l=%d\n", old_states[state_level],
465         state_level);
466#endif
467  BEGIN(old_states[state_level]);
468  state_level--;
469  if(state_level<0) state_level = -1;
470}
471
472main( int argc, char *argv[] )
473{
474  ++argv, --argc;  /* skip over program name */
475  if ( argc > 0 )
476     yyin = fopen( argv[0], "rb" );
477  else
478     yyin = stdin;
479  module_def.name=NULL;
480  module_def.version=NULL;
481  module_def.info=NULL;
482  module_def.helpfile=NULL;
483  module_def.procs=NULL;
484  module_def.proccnt = 0;
485  module_def.files = NULL;
486  module_def.filecnt = 0;
487  yylex();
488  PrintProclist(&module_def);
489  generate_mod(&module_def);
490  mod_create_makefile(&module_def);
491
492}
Note: See TracBrowser for help on using the repository browser.