source: git/Singular/libparse.l @ b35b93

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