source: git/Singular/libparse.l @ 36741d

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