source: git/Singular/libparse.l @ 3a20c1

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