source: git/Singular/libparse.l @ d5f35ac

spielwiese
Last change on this file since d5f35ac was d5f35ac, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-04-27 Olaf Bachmann <obachman@mathematik.uni-kl.de> * febase.cc (myfread): introduced myfread and myfopen which assure that newlines in text files are always \n git-svn-id: file:///usr/local/Singular/svn/trunk@1483 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 19.6 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: libparse.l,v 1.12 1998-04-27 12:34:18 obachman Exp $ */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <ctype.h>
10#ifdef STANDALONE_PARSER
11#  include "utils.h"
12#  define HAVE_LIBPARSER
13//#  define YYLPDEBUG 3
14#else
15#  include "mod2.h"
16#  include "subexpr.h"
17#  include "grammar.h"
18#  include "ipshell.h"
19#  include "ipid.h"
20#  include "tok.h"
21#  include "febase.h"
22#  include "mmemory.h"
23#endif
24#include "libparse.h"
25
26#ifdef HAVE_LIBPARSER
27#  define YY_SKIP_YYWRAP
28
29typedef enum { LP_NONE, LP_INFO, LP_VERSION} lib_cmds;
30
31int libread(FILE* f, char* buf, int max_size);
32int current_pos(int i);
33void print_version(lp_modes mode, char *p);
34void copy_string(lp_modes mode);
35void make_version(char *p, int what);
36
37int brace1 = 0;  /* { } */
38int brace2 = 0;  /* ( ) */
39int brace3 = 0;  /* [ ] */
40int quote  = 0;  /* " */
41int offset = 0;
42BOOLEAN p_static = FALSE;
43int old_state = 0;
44lib_cmds last_cmd = LP_NONE;
45
46char libnamebuf[128];
47char *text_buffer;
48long string_start;
49
50char *yylp_buffer_start;
51int yylplineno = 1;
52
53char *yylp_errlist[]= {
54   "",
55   "missing close bracket ')' for proc definition in line %d.",  /*  1 */
56   "missing close bracket ')' for procbody in line %d.",         /*  2 */
57   "missing close bracket ']' for procbody in line %d.",         /*  3 */
58   "too many ')' closed brackets in line %d.",                   /*  4 */
59   "too many ']' closed brackets in line %d.",                   /*  5 */
60   "missing close bracket ')' for example in line %d.",          /*  6 */
61   "missing close bracket ']' for example in line %d.",          /*  7 */
62   "cannot assign charater in line %d to any group.",            /*  8 */
63   "there must be a quote missing somewhere before line %d.",    /*  9 */
64   "missing close bracket '}' at end of library in line %d.",    /* 10 */
65   "missing close bracket ')' at end of library in line %d.",    /* 11 */
66   "missing close bracket ']' at end of library in line %d.",    /* 12 */
67   NULL
68};
69int yylp_errno = 0;
70
71#  define YYLP_ERR_NONE    0
72#  define YYLP_DEF_BR2     1
73#  define YYLP_BODY_BR2    2
74#  define YYLP_BODY_BR3    3
75#  define YYLP_BODY_TMBR2  4
76#  define YYLP_BODY_TMBR3  5
77#  define YYLP_EX_BR2      6
78#  define YYLP_EX_BR3      7
79#  define YYLP_BAD_CHAR    8
80#  define YYLP_MISSQUOT    9
81#  define YYLP_MISS_BR1   10
82#  define YYLP_MISS_BR2   11
83#  define YYLP_MISS_BR3   12
84
85#  ifdef __MWERKS__
86#    ifdef __cplusplus
87extern "C" {
88#    endif
89#    ifdef macintosh
90int    fileno(FILE *stream);
91FILE   *fdopen(int filedes, char *type);
92long   ftell(FILE *fp);
93long   tell(int filedes);
94int    isatty(int filedes);
95#else
96int    _fileno(FILE *stream);
97FILE   *_fdopen(int filedes, char *type);
98long   _ftell(FILE *fp);
99long   _tell(int filedes);
100int    _isatty(int filedes);
101#      define fileno  _fileno
102#      define fdopen  _fdopen
103#      define ftell    _ftell
104#      define tell    _tell
105#      define isatty  _isatty
106#    endif /* macintosh */
107#    ifdef __cplusplus
108}
109#    endif
110#  endif
111
112#  ifdef STANDALONE_PARSER
113procinfov pi;
114printpi(procinfov pi);
115pi_clear(procinfov pi);
116extern "C" {
117  int yylpwrap();
118}
119#  else /* STANDALONE_PARSER */
120idhdl h0;
121#    define pi IDPROC(h0)
122extern "C"
123{
124  int yylpwrap();
125}
126extern libstackv library_stack;
127#  endif /* STANDALONE_PARSER */
128
129#  define SET_DEF_END(mode, pi, p) \
130     if ( mode == LOAD_LIB) pi->data.s.def_end = p;
131#  define SET_HELP_START(mode, pi, p) \
132     if ( mode == LOAD_LIB) pi->data.s.help_start = p; \
133
134#  define SET_BODY_START(mode, pi, l, p) \
135     if ( mode == LOAD_LIB) { \
136       pi->data.s.body_lineno = l; \
137       pi->data.s.body_start = p; \
138     }
139#  define SET_BODY_END(mode, pi, p) \
140     if ( mode == LOAD_LIB) { \
141       pi->data.s.body_end = p-1; \
142       pi->data.s.proc_end = p-1; \
143     }
144
145#  define SET_EXAMPLE_START(mode, pi, l, p) \
146     if ( mode == LOAD_LIB) { \
147       pi->data.s.example_lineno = l; \
148       pi->data.s.example_start = p; \
149     }
150#  define SET_PROC_END(mode, pi, p) \
151     if ( mode == LOAD_LIB) { \
152       pi->data.s.proc_end = p-1; \
153       if(pi->data.s.body_end==0) pi->data.s.body_end = p-1; \
154     }
155
156#  undef YY_DECL
157#  define YY_DECL int yylex(char *newlib, char *libfile, \
158                                 lib_style_types *lib_style, \
159                                 lp_modes mode)
160
161#  undef YY_INPUT
162#  define YY_INPUT(buf,result,max_size) \
163          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
164                  && ferror( yyin ) ) \
165                YY_FATAL_ERROR( "read in flex scanner failed" );
166
167#  define YY_USER_INIT { \
168       BEGIN(header); \
169       yylplineno = 1; \
170       yylp_errno = 0; \
171       *lib_style = OLD_LIBSTYLE; \
172       strcpy(libnamebuf,"(**unknown version**)"); \
173     }
174
175#  if 0
176<pbody>proc[ \t]+{name}  {
177                           printf("MISSING: PROC-cmd found. ERROR!\n"); }
178<pbody>example[ \t]*\n   {
179                           yylplineno++;
180                           printf("MISSING: EXAMPLE-cmd found. ERROR!\n"); }
181#  endif
182%}
183
184digit          [0-9]
185letter         [@a-zA-Z\']
186name           ({letter}({letter}*{digit}*_*)*|_)
187letters        ({letter}|{digit}|[_./#%^*:,])
188string         ({letters}*)
189comment        [\/][\/]
190dolar          [$]
191symbols        [~!@#$%^&*()_+-={}\\\|\[\];:,<.>/\?\' \n\~\`\r]
192aletters       ({letter}|{digit}|{symbols}|{dolar}|{escquote})
193strings        ({aletters}*)
194quote          [\"]
195escquote       (\\\")
196taborspace     [ \t]
197tos            ({taborspace}*)
198
199/* %start START */
200
201%x header
202%x help
203%x libcmd
204%x pdef
205%x phead
206%x pbody
207%x pstr
208%x pexample
209%x pestr
210%x string
211%x comment
212
213%%
214(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
215\/\/*      { old_state = YYSTATE; BEGIN(comment); }
216
217info=+"\"" { old_state = YYSTATE; quote++; BEGIN(string);
218             string_start = current_pos(yyleng);
219             *lib_style = NEW_LIBSTYLE;
220             last_cmd = LP_INFO;
221           }
222
223(version=+{quote}+{strings}+{quote})|(version+{tos}+=+{quote}+{strings}+{quote})|(version+{tos}+=+{tos}+{quote}+{strings}+{quote})|(version=+{tos}+{quote}+{strings}+{quote}) {
224             if ( mode != GET_INFO ) {
225               make_version(yytext,1);
226#ifdef STANDALONE_PARSER
227               printf("Version:%s;\n", libnamebuf);
228#else
229               //text_buffer = mstrdup(libnamebuf);
230               print_version(mode, libnamebuf);
231#endif
232             }
233           }
234
235static     { p_static=TRUE; }
236
237(proc[ \t]+{name})|([ \t]proc[ \t]+{name}) {
238             char proc[256];
239             BEGIN(pdef);
240#if YYLPDEBUG > 0
241             printf("Newlib:%s\n", newlib);
242#endif
243#ifdef STANDALONE_PARSER
244               if ( pi != NULL ) {
245                 printpi(pi);
246                 pi_clear(pi);
247               }
248               pi = (procinfo *)malloc(sizeof(procinfo));
249               iiInitSingularProcinfo(pi, newlib, yytext+5, yylplineno,
250                                        current_pos(0), p_static);
251#else STANDALONE_PARSER
252             if( mode == LOAD_LIB) {
253               proc[0]='\0';
254               sscanf( yytext, "%*[^p]proc %s", proc);
255               if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
256               h0 = enterid( mstrdup(proc), myynest, PROC_CMD,
257                                   &idroot, FALSE );
258               if (h0!=NULL) {
259                 iiInitSingularProcinfo(IDPROC(h0), newlib, proc,
260                                yylplineno, current_pos(0),p_static);
261                 if (BVERBOSE(V_LOAD_PROC))
262                   Warn( "     proc %s loaded", proc );
263               }
264#endif STANDALONE_PARSER
265               SET_DEF_END(mode, pi, current_pos(yyleng+1));
266#if YYLPDEBUG
267               printf("PROC %d at %d/%d: (%d) %s\n", p_static,
268                      yylplineno, current_pos(0), brace1, yytext);
269#endif
270               p_static=FALSE;
271#ifndef STANDALONE_PARSER
272             }
273#endif STANDALONE_PARSER
274           }
275example    {
276             BEGIN(pexample);
277             SET_EXAMPLE_START(mode, pi, yylplineno, current_pos(0));
278#if YYLPDEBUG
279             printf("EXAMPLE at %d/%d (%d)\n", yylplineno,
280                    current_pos(0), brace1);
281#endif
282           }
283
284LIB[ \t]+"\"" { quote++;
285             BEGIN(libcmd);
286           }
287
288<header>({comment}+{tos}+{dolar}+Id:+{string}+[^\n]*)|({comment}+{tos}+{dolar}+Header:+{string}+[^\n]*) {
289             make_version(yytext, 0);
290             //print_version(mode, "");
291#if YYLPDEBUG > 1
292             printf("+(id)HEAD:%s\n", yytext);
293#endif
294           }
295<header>(^{comment}+[^\n]*) {
296#if YYLPDEBUG > 1
297             printf("+(cmt)HEAD:%s\n", yytext);
298#endif
299           }
300<header>(^#![^\n]*) {
301#if YYLPDEBUG > 1
302             printf("-HEAD:%s\n", yytext);
303#endif
304           }
305<header>^proc\  { yyless(0);
306             //print_version(mode, libfile);
307             BEGIN(INITIAL);
308             yymore();
309           }
310<header>(info=\")|(version=\")|(version+{tos}+=\")|(version+{tos}+=+{tos}+\")|(version=+{tos}+\") { yyless(0);
311             *lib_style = NEW_LIBSTYLE;
312             BEGIN(INITIAL);
313             yymore();
314           }
315
316<header>^LIB[ \t]+"\""   { quote++;
317             //print_version(mode, libfile);
318             BEGIN(libcmd);
319           }
320<header>\n { yylplineno++; }
321<header>.  {
322#if YYLPDEBUG > 1
323             printf(" HEAD:%s\n", yytext);
324#endif
325             //print_version(mode, libfile);
326             BEGIN(help);
327           }
328<help>(^{comment}+[^\n]*)  {
329#if YYLPDEBUG > 1
330             printf(" HELP:%s\n", yytext);
331#endif
332             BEGIN(INITIAL); }
333<help>(^#![^\n]*) {
334#if YYLPDEBUG > 1
335             printf(" HELP:%s\n", yytext);
336#endif
337             BEGIN(INITIAL);
338           }
339<help>(info=\")|(version=\")|(version+{tos}+=\")|(version+{tos}+=+{tos}+\")|(version=+{tos}+\") { yyless(0);
340             *lib_style = NEW_LIBSTYLE;
341             BEGIN(INITIAL);
342             yymore();
343           }
344<help>^proc\  {
345             yyless(0);
346             //printf("2) proc found.\n");
347             BEGIN(INITIAL);
348             yymore();
349           }
350<help>^LIB[ \t]+"\""     { quote++;
351             BEGIN(libcmd);
352           }
353
354<help>\n { yylplineno++; }
355<help>.    {
356#if YYLPDEBUG > 1
357             printf("-HELP:%s\n", yytext);
358#endif
359           }
360
361
362<libcmd>{string}"\""     { quote--;
363             yytext[yyleng-1] = '\0';
364#ifndef STANDALONE_PARSER
365             if ( mode == LOAD_LIB ) {
366             library_stack->push(newlib, yytext);
367           }
368#endif /* STANDALONE_PARSER */
369#if YYLPDEBUG
370             printf("LIB:'%s'\n", yytext);
371#endif
372             BEGIN(INITIAL);
373           }
374
375<pdef>[ \t] { }
376<pdef>\(   {
377             brace2++;
378#if YYLPDEBUG
379             printf("%s", yytext);
380#endif
381           }
382<pdef>\)   {
383             brace2--;
384#if YYLPDEBUG
385             printf(">%s<\n", yytext);
386             printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
387#endif
388             if(brace2<=0) {
389#if YYLPDEBUG
390               printf("BEGIN(phead){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
391#endif
392               SET_DEF_END(mode, pi, current_pos(yyleng));
393               SET_HELP_START(mode, pi, current_pos(yyleng));
394               BEGIN(phead);
395             }
396           }
397<pdef>"{"  {
398             if(brace2>0) {
399#if YYLPDEBUG
400               printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
401#endif
402               yylp_errno = YYLP_DEF_BR2;
403               return(1);
404             } else {
405               brace1++; BEGIN(pbody);
406               SET_BODY_START(mode, pi, yylplineno, current_pos(0));
407             }
408           }
409<pdef>\n { yylplineno++;
410              if(brace2<=0) {
411#if YYLPDEBUG
412                printf("BEGIN(phead-2){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
413#endif
414                SET_HELP_START(mode, pi, current_pos(0));
415                BEGIN(phead);
416              }
417            }
418<pdef>.    {
419             if(brace2<=0) {
420               SET_HELP_START(mode, pi, current_pos(0));
421               BEGIN(phead);
422             }
423           }
424
425<phead>"\\{"             { }
426<phead>"{"               {
427                           brace1++; BEGIN(pbody);
428#if YYLPDEBUG
429                           printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
430#endif
431                           SET_BODY_START(mode, pi, yylplineno, current_pos(0));
432#if YYLPDEBUG
433                           printf("BODY at %d/%d", yylplineno, current_pos(0));
434#endif
435                         }
436<phead>\n             { yylplineno++; }
437<phead>.                 { }
438
439<pbody>({comment}[^\n]*) { }
440<pbody>"\""              { quote++; old_state = YYSTATE;
441                           BEGIN(string); /* printf("%s", yytext); */
442                         }
443<pbody>"{"               {
444                           brace1++;
445#if YYLPDEBUG
446                           printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
447#endif
448                         }
449<pbody>"}"               {
450#if YYLPDEBUG
451                           printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
452#endif
453                           brace1--;
454                           if(brace2>0) {
455                             yylp_errno = YYLP_BODY_BR2;
456                             return(1);
457                           }
458                           if(brace3>0) {
459                             yylp_errno = YYLP_BODY_BR3;
460                             return(1);
461                           }
462                           if(brace1<=0) {
463                             SET_BODY_END(mode, pi, current_pos(yyleng));
464                             SET_PROC_END(mode, pi, current_pos(yyleng));
465#if YYLPDEBUG
466                             printf("-%d\n", current_pos(0));
467#endif
468                             BEGIN(INITIAL);
469                           }
470                         }
471<pbody>"("               {
472                           brace2++; /* printf("%s", yytext); */
473                         }
474<pbody>")"               {
475                           brace2--; /* printf("%s", yytext); */
476                           if(brace2<0) {
477                             yylp_errno = YYLP_BODY_TMBR2;
478                             return(1);
479                           }
480                         }
481<pbody>"["               {
482                           brace3++; /* printf("%s", yytext); */
483                         }
484<pbody>"]"               {
485                           brace3--; /* printf("%s", yytext); */
486                           if(brace3<0) {
487                             yylp_errno = YYLP_BODY_TMBR3;
488                             return(1);
489                           }
490                         }
491<pbody>\n                { yylplineno++; }
492<pbody>.                 { }
493
494<string>"\""             { quote--;
495                           copy_string(mode);
496                           last_cmd = LP_NONE;
497                           BEGIN(old_state); /* printf("%s", yytext); */
498                         }
499<string>(\\\\)|(\\\")    { }
500<string>\n               { yylplineno++; }
501<string>.                { }
502
503<pexample>(\/\/[^\n]*)  { }
504<pexample>"\""           { quote++; old_state = YYSTATE;
505                           BEGIN(string); /* printf("%s", yytext); */
506                         }
507<pexample>"{"            {
508                           brace1++; /* printf("(%d)%s", brace1, yytext); */
509                         }
510<pexample>"}"            {
511                           brace1--; /* printf("(%d)%s", brace1, yytext); */
512                           if(brace1<=0) {
513                             if(brace2>0) { yylp_errno=YYLP_EX_BR2; return(1); }
514                             if(brace3>0) { yylp_errno=YYLP_EX_BR3; return(1); }
515                             BEGIN(INITIAL);
516                             SET_PROC_END(mode, pi, current_pos(yyleng));
517                           }
518                         }
519<pexample>"("            {
520                           brace2++; /* printf("%s", yytext); */
521                         }
522<pexample>")"            {
523                           brace2--; /* printf("%s", yytext); */
524                         }
525<pexample>"["            {
526                           brace3++; /* printf("%s", yytext); */
527                         }
528<pexample>"]"            {
529                           brace3--; /* printf("%s", yytext); */
530                         }
531<pexample>\n             { yylplineno++; }
532<pexample>.              { }
533
534<pestr>"\""              { quote--;
535                           BEGIN(pexample); /* printf("%s", yytext); */
536                         }
537<pestr>\\\\              { }
538<pestr>\\\"              { }
539<pestr>\n                { yylplineno++; }
540<pestr>.                 { }
541
542<comment>\*\/            { BEGIN(old_state); }
543<comment>\n              { yylplineno++; }
544<comment>.               { }
545
546\n                       { yylplineno++; }
547\r                       { }
548;                        { p_static = FALSE;
549#if YYLPDEBUG
550                            printf("%s", yytext);
551#endif
552                         }
553.                        { p_static = FALSE;
554                           yylp_errno = YYLP_BAD_CHAR;
555                           printf("[%d]", *yytext);
556#if YYLPDEBUG
557                           printf("[%s]", yytext);
558#endif
559                           return(1);
560                         }
561
562%%
563
564int current_pos(int i)
565{
566  return(i+offset+(int)(yytext-yylp_buffer_start));
567}
568
569int libread(FILE* f, char* buf, int max_size)
570{ int rc;
571
572  offset = ftell(f);
573  rc  = myfread( buf, 1, max_size, f );
574#if YYLPDEBUG >2
575  printf("fread: %d of %d\n", rc, max_size);
576#endif
577#ifdef macintosh
578  char *p = buf, *q;
579  while( (q = strchr(p, '\r')) != NULL) {
580    *q = '\n';
581    p = q;
582  }
583#endif
584  yylp_buffer_start = buf;
585  return rc;
586}
587
588extern "C" {
589  int yylpwrap() {
590    //printf("======================= YYWRAP ====================\n");
591    if(brace1>0) { yylp_errno=YYLP_MISS_BR1; }
592    if(brace2>0) { yylp_errno=YYLP_MISS_BR2; }
593    if(brace3>0) { yylp_errno=YYLP_MISS_BR3; }
594    if(quote>0) { yylp_errno=YYLP_MISSQUOT; }
595    /* printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);/**/
596    if(feof(yyin)) return 1; else return 0;
597  }
598}
599
600void reinit_yylp()
601{
602   brace1 = 0;
603   brace2 = 0;
604   brace3 = 0;
605   quote  = 0;
606   yy_init=1;
607   yy_delete_buffer(yy_current_buffer);
608}
609
610void make_version(char *p,int what)
611{
612  char ver[10];
613  char date[16];
614  ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
615  date[0]='?'; date[1]='\0';
616  if(what) sscanf(p,"%*[^=]= %*s %*s %10s %16s",ver,date);
617  else sscanf(p,"// %*s %*s %10s %16s",ver,date);
618  strcpy(libnamebuf,"(");
619  strcat(libnamebuf,ver);
620  strcat(libnamebuf,",");
621  strcat(libnamebuf,date);
622  strcat(libnamebuf,")");
623//  printf("ID=(%d)%s; \n", what, p);
624}
625
626void copy_string(lp_modes mode)
627{
628#  ifndef STANDALONE_PARSER
629  if((last_cmd == LP_INFO)&&(mode == GET_INFO))
630  {
631    long current_location = ftell(yylpin);
632    int len = (int)(current_pos(0) - string_start);
633    fseek(yylpin, string_start, SEEK_SET);
634    text_buffer = (char *)AllocL(len+1);
635    text_buffer[len]='\0';
636    myfread(text_buffer, len, 1, yylpin);
637    fseek(yylpin, current_location, SEEK_SET);
638  }
639#  endif /* STANDALONE_PARSER */
640}
641
642void print_init()
643{
644   printf("Init=%d\n", yy_init);
645}
646
647void print_version(lp_modes mode, char *p)
648{
649#  ifdef STANDALONE_PARSER
650  //printf("loading %s%s", p, libnamebuf);
651#  else
652  if ( mode == LOAD_LIB ) {
653    if (BVERBOSE(V_LOAD_LIB) && p!=NULL ) Print(" %s...", p);
654       //Warn( "loading %s%s", p, libnamebuf);
655  }
656#  endif
657}
658
659#  ifdef STANDALONE_PARSER
660main( int argc, char *argv[] )
661{
662  lib_style_types lib_style;
663  ++argv, --argc;  /* skip over program name */
664  if ( argc > 0 )
665     yyin = myfopen( argv[0], "rb" );
666  else
667     yyin = stdin;
668
669  printf( "  %-15s  %20s      %s,%s    %s,%s     %s,%s\n", "Library",
670          "function", "line", "start-eod", "line", "body-eob",
671          "line", "example-eoe");
672  yylplex(argv[0], argv[0], &lib_style);
673  if(yylp_errno) {
674    printf("ERROR occured: [%d] ", yylp_errno);
675    printf(yylp_errlist[yylp_errno], yylplineno);
676    printf("\n");
677  }
678  else if(pi!=NULL) printpi(pi);
679}
680
681#  endif /* STANDALONE_PARSER */
682#endif /* HAVE_LIBPARSE */
Note: See TracBrowser for help on using the repository browser.