source: git/Singular/libparse.l @ a75e78

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