source: git/Singular/libparse.l @ 795d89

spielwiese
Last change on this file since 795d89 was 795d89, checked in by Kai Krüger <krueger@…>, 26 years ago
small fixes in libparse. git-svn-id: file:///usr/local/Singular/svn/trunk@1199 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 17.5 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: libparse.l,v 1.2 1998-03-06 12:12:03 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
25#ifdef HAVE_LIBPARSER
26#define YY_SKIP_YYWRAP
27
28int libread(FILE* f, char* buf, int max_size);
29int current_pos(int i);
30
31int brace1 = 0;  /* { } */
32int brace2 = 0;  /* ( ) */
33int brace3 = 0;  /* [ ] */
34int quote  = 0;  /* " */
35int offset = 0;
36int p_static = 0;
37int old_state = 0;
38
39char libnamebuf[128];
40
41char *yylp_buffer_start;
42int yylplineno = 1;
43
44char *yylp_errlist[]= {
45   "",
46   "missing close bracket ')' for proc definition in line %d.",  /*  1 */
47   "missing close bracket ')' for procbody in line %d.",         /*  2 */
48   "missing close bracket ']' for procbody in line %d.",         /*  3 */
49   "too many ')' closed brackets in line %d.",                   /*  4 */
50   "too many ']' closed brackets in line %d.",                   /*  5 */
51   "missing close bracket ')' for example in line %d.",          /*  6 */
52   "missing close bracket ']' for example in line %d.",          /*  7 */
53   "cannot assign charater in line %d to any group.",            /*  8 */
54   "there must be a quote missing somewhere before line %d.",    /*  9 */
55   "missing close bracket '}' at end of library in line %d.",    /* 10 */
56   "missing close bracket ')' at end of library in line %d.",    /* 11 */
57   "missing close bracket ']' at end of library in line %d.",    /* 11 */
58   NULL
59};
60int yylp_errno = 0;
61
62#define YYLP_ERR_NONE    0
63#define YYLP_DEF_BR2     1
64#define YYLP_BODY_BR2    2
65#define YYLP_BODY_BR3    3
66#define YYLP_BODY_TMBR2  4
67#define YYLP_BODY_TMBR3  5
68#define YYLP_EX_BR2      6
69#define YYLP_EX_BR3      7
70#define YYLP_BAD_CHAR    8
71#define YYLP_MISSQUOT    9
72#define YYLP_MISS_BR1   10
73#define YYLP_MISS_BR2   11
74#define YYLP_MISS_BR3   12
75
76print_version(char *p);
77
78#ifdef STANDALONE_PARSER
79procinfov pi;
80procinfo *iiInitSingularProcinfo(procinfov pi, char *libname,
81                                 char *procname, int line, long pos,
82                                 int pstatic = 0);
83printpi(procinfov pi);
84pi_clear(procinfov pi);
85extern "C" {
86  int yylpwrap();
87}
88#else /* STANDALONE_PARSER */
89procinfo *iiInitSingularProcinfo(procinfov pi, char *libname,
90                                 char *procname, int line, long pos,
91                                 BOOLEAN pstatic = 0);
92idhdl h;
93#  define pi IDPROC(h)
94extern libstackv library_stack;
95#endif /* STANDALONE_PARSER */
96
97#define SET_DEF_END(pi, p) \
98   pi->data.s.def_end = p;
99#define SET_HELP_START(pi, p) \
100   pi->data.s.help_start = p; \
101
102#define SET_BODY_START(pi, l, p) \
103   { pi->data.s.body_lineno = l; \
104     pi->data.s.body_start = p; \
105   }
106#define SET_BODY_END(pi, p) { \
107   pi->data.s.body_end = p-1; \
108   pi->data.s.proc_end = p-1; \
109   }
110
111#define SET_EXAMPLE_START(pi, l, p) \
112   { pi->data.s.example_lineno = l; \
113     pi->data.s.example_start = p; \
114   }
115#define SET_PROC_END(pi, p) \
116   { pi->data.s.proc_end = p-1; \
117     if(pi->data.s.body_end==0) pi->data.s.body_end = p-1; \
118   }
119
120  #undef YY_DECL
121  #define YY_DECL int yylex(char *newlib, char *libfile)
122
123  #undef YY_INPUT
124  #define YY_INPUT(buf,result,max_size) \
125          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
126                  && ferror( yyin ) ) \
127                YY_FATAL_ERROR( "read in flex scanner failed" );
128
129  #define YY_USER_INIT { \
130       BEGIN(header); \
131       yylplineno = 1; \
132       yylp_errno = 0; \
133       strcpy(libnamebuf,"(**unknown version**)"); \
134     }
135
136#if 0
137<pbody>proc[ \t]+{name}  {
138                           printf("MISSING: PROC-cmd found. ERROR!\n"); }
139<pbody>example[ \t]*\n   {
140                           yylplineno++;
141                           printf("MISSING: EXAMPLE-cmd found. ERROR!\n"); }
142#endif
143%}
144
145digit          [0-9]
146letter         [@a-zA-Z\']
147name           ({letter}({letter}*{digit}*_*)*|_)
148letters        ({letter}|{digit}|[_./#%^*:,])
149string         ({letters}*)
150comment        [\/][\/]
151dolar          [$]
152
153/* %start START */
154
155%x header
156%x help
157%x libcmd
158%x pdef
159%x phead
160%x pbody
161%x pstr
162%x pexample
163%x pestr
164%x string
165%x comment
166
167%%
168(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
169\/\*                     { old_state = YYSTATE; BEGIN(comment); }
170
171static                   { p_static=1; }
172
173(proc[ \t]+{name})|([ \t]proc[ \t]+{name}) {
174                           char proc[256];
175                           BEGIN(pdef);
176#if YYLPDEBUG
177                           printf("Newlib:%s\n", newlib);
178#endif
179#ifdef STANDALONE_PARSER
180                           if(pi!=NULL) {
181                             printpi(pi);
182                             pi_clear(pi);
183                           }
184                           pi = (procinfo *)malloc(sizeof(procinfo));
185                           iiInitSingularProcinfo(pi, newlib, yytext+5,
186                                                      yylplineno,
187                                                      current_pos(0),
188                                                      p_static);
189#else STANDALONE_PARSER
190                           proc[0]='\0';
191                           sscanf( yytext, "%*[^p]proc %s", proc);
192                           if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
193                           h = enterid( mstrdup(proc), myynest, PROC_CMD,
194                                               &idroot, FALSE );
195                           if (h!=NULL)
196                           {
197                             iiInitSingularProcinfo(IDPROC(h), newlib, proc,
198                                         yylplineno, current_pos(0),p_static);
199                             if (BVERBOSE(V_LOAD_PROC))
200                                 Warn( "     proc %s loaded", proc );
201                           }
202#endif STANDALONE_PARSER
203                           SET_DEF_END(pi, current_pos(yyleng+1));
204#if YYLPDEBUG
205                           printf("PROC %d at %d/%d: (%d) %s\n", p_static,
206                                  yylplineno, current_pos(0), brace1, yytext);
207#endif
208                           p_static=0;
209                         }
210example                  {
211                           BEGIN(pexample);
212                           SET_EXAMPLE_START(pi, yylplineno, current_pos(0));
213#if YYLPDEBUG
214                           printf("EXAMPLE at %d/%d (%d)\n", yylplineno,
215                                  current_pos(0), brace1);
216#endif
217                         }
218
219LIB[ \t]+"\""            { quote++;
220                           BEGIN(libcmd);
221                         }
222
223<header>({comment}+[ \t]*+{dolar}+Id:+{string}+[^\n]*)|({comment}+[ \t]*+{dolar}+Header:+{string}+[^\n]*) {
224                           char ver[10];
225                           char date[16];
226                           ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
227                           date[0]='?'; date[1]='\0';
228                           sscanf(yytext,"// %*s %*s %10s %16s",ver,date);
229                           strcpy(libnamebuf,"(");
230                           strcat(libnamebuf,ver);
231                           strcat(libnamebuf,",");
232                           strcat(libnamebuf,date);
233                           strcat(libnamebuf,")");
234                           //printf("ID=%s; \n", yytext);
235                         }
236<header>^{comment}+[^\n]* {
237#if YYLPDEBUG > 1
238                            printf("+HEAD:%s\n", yytext);
239#endif
240                          }
241<header>(^#![^\n]*)      {
242#if YYLPDEBUG > 1
243                           printf("-HEAD:%s\n", yytext);
244#endif
245                         }
246<header>^proc\           { yyless(0);
247                           print_version(libfile);
248                           BEGIN(INITIAL);
249                           yymore();
250                         }
251<header>^LIB[ \t]+"\""   { quote++;
252                           print_version(libfile);
253                           BEGIN(libcmd);
254                         }
255<header>\n               { yylplineno++; }
256<header>.                {
257#if YYLPDEBUG > 1
258                           printf(" HEAD:%s\n", yytext);
259#endif
260                           print_version(libfile);
261                           BEGIN(help);
262                         }
263<help>^{comment}+[^\n]*  {
264#if YYLPDEBUG > 1
265                           printf(" HELP:%s\n", yytext);
266#endif
267                           BEGIN(INITIAL); }
268<help>^#![^\n]*          {
269#if YYLPDEBUG > 1
270                           printf(" HELP:%s\n", yytext);
271#endif
272                           BEGIN(INITIAL);
273                         }
274<help>^proc\             { yyless(0);
275                           //printf("2) proc found.\n");
276                           BEGIN(INITIAL);
277                           yymore();
278                         }
279<help>^LIB[ \t]+"\""     { quote++;
280                           BEGIN(libcmd);
281                         }
282
283<help>\n                 { yylplineno++; }
284<help>.                  {
285#if YYLPDEBUG > 1
286                           printf("-HELP:%s\n", yytext);
287#endif
288                         }
289
290
291<libcmd>{string}"\""     { quote--;
292                           yytext[yyleng-1] = '\0';
293#ifndef STANDALONE_PARSER
294                           library_stack->push(newlib, yytext);
295#endif /* STANDALONE_PARSER */
296#if YYLPDEBUG
297                           printf("LIB:'%s'\n", yytext);
298#endif
299                           BEGIN(INITIAL);
300                         }
301
302<pdef>[ \t]              { }
303<pdef>\(                 {
304                           brace2++;
305#if YYLPDEBUG
306                           printf("%s", yytext);
307#endif
308                         }
309<pdef>\)                 {
310                           brace2--;
311#if YYLPDEBUG
312                           printf(">%s<\n", yytext);
313                           printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
314#endif
315                           if(brace2<=0) {
316#if YYLPDEBUG
317                             printf("BEGIN(phead){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
318#endif
319                             SET_DEF_END(pi, current_pos(yyleng));
320                             SET_HELP_START(pi, current_pos(yyleng));
321                             BEGIN(phead);
322                           }
323                         }
324<pdef>"{"                {
325                           if(brace2>0) {
326#if YYLPDEBUG
327                             printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
328#endif
329                             yylp_errno = YYLP_DEF_BR2;
330                             return(1);
331                           } else {
332                             brace1++; BEGIN(pbody);
333                             SET_BODY_START(pi, yylplineno, current_pos(0));
334                           }
335                         }
336<pdef>\n                 { yylplineno++;
337                           if(brace2<=0) {
338#if YYLPDEBUG
339                             printf("BEGIN(phead-2){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
340#endif
341                             SET_HELP_START(pi, current_pos(0));
342                             BEGIN(phead);
343                           }
344                         }
345<pdef>.                  {
346                           if(brace2<=0) {
347                             SET_HELP_START(pi, current_pos(0));
348                             BEGIN(phead);
349                           }
350                         }
351
352<phead>"\\{"             { }
353<phead>"{"               {
354                           brace1++; BEGIN(pbody);
355#if YYLPDEBUG
356                           printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
357#endif
358                           SET_BODY_START(pi, yylplineno, current_pos(0));
359#if YYLPDEBUG
360                           printf("BODY at %d/%d", yylplineno, current_pos(0));
361#endif
362                         }
363<phead>\n                { yylplineno++; }
364<phead>.                 { }
365
366<pbody>{comment}[^\n]*        { }
367<pbody>"\""              { quote++; old_state = YYSTATE;
368                           BEGIN(string); /* printf("%s", yytext); */
369                         }
370<pbody>"{"               {
371                           brace1++;
372#if YYLPDEBUG
373                           printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
374#endif
375                         }
376<pbody>"}"               {
377#if YYLPDEBUG
378                           printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
379#endif
380                           brace1--;
381                           if(brace2>0) {
382                             yylp_errno = YYLP_BODY_BR2;
383                             return(1);
384                           }
385                           if(brace3>0) {
386                             yylp_errno = YYLP_BODY_BR3;
387                             return(1);
388                           }
389                           if(brace1<=0) {
390                             SET_BODY_END(pi, current_pos(yyleng));
391                             SET_PROC_END(pi, current_pos(yyleng));
392#if YYLPDEBUG
393                             printf("-%d\n", current_pos(0));
394#endif
395                             BEGIN(INITIAL);
396                           }
397                         }
398<pbody>"("               {
399                           brace2++; /* printf("%s", yytext); */
400                         }
401<pbody>")"               {
402                           brace2--; /* printf("%s", yytext); */
403                           if(brace2<0) {
404                             yylp_errno = YYLP_BODY_TMBR2;
405                             return(1);
406                           }
407                         }
408<pbody>"["               {
409                           brace3++; /* printf("%s", yytext); */
410                         }
411<pbody>"]"               {
412                           brace3--; /* printf("%s", yytext); */
413                           if(brace3<0) {
414                             yylp_errno = YYLP_BODY_TMBR3;
415                             return(1);
416                           }
417                         }
418<pbody>\n                { yylplineno++; }
419<pbody>.                 { }
420
421<string>"\""             { quote--;
422                           BEGIN(old_state); /* printf("%s", yytext); */
423                         }
424<string>\\\\             { }
425<string>\\\"             { }
426<string>\n               { yylplineno++; }
427<string>.                { }
428
429<pexample>\/\/[^\n]*     { }
430<pexample>"\""           { quote++; old_state = YYSTATE;
431                           BEGIN(string); /* printf("%s", yytext); */
432                         }
433<pexample>"{"            {
434                           brace1++; /* printf("(%d)%s", brace1, yytext); */
435                         }
436<pexample>"}"            {
437                           brace1--; /* printf("(%d)%s", brace1, yytext); */
438                           if(brace1<=0) {
439                             if(brace2>0) { yylp_errno=YYLP_EX_BR2; return(1); }
440                             if(brace3>0) { yylp_errno=YYLP_EX_BR3; return(1); }
441                             BEGIN(INITIAL);
442                             SET_PROC_END(pi, current_pos(yyleng));
443                           }
444                         }
445<pexample>"("            {
446                           brace2++; /* printf("%s", yytext); */
447                         }
448<pexample>")"            {
449                           brace2--; /* printf("%s", yytext); */
450                         }
451<pexample>"["            {
452                           brace3++; /* printf("%s", yytext); */
453                         }
454<pexample>"]"            {
455                           brace3--; /* printf("%s", yytext); */
456                         }
457<pexample>\n             { yylplineno++; }
458<pexample>.              { }
459
460<pestr>"\""              { quote--;
461                           BEGIN(pexample); /* printf("%s", yytext); */
462                         }
463<pestr>\\\\              { }
464<pestr>\\\"              { }
465<pestr>\n                { yylplineno++; }
466<pestr>.                 { }
467
468<comment>\*\/            { BEGIN(old_state); }
469<comment>\n              { yylplineno++; }
470<comment>.               { }
471
472\n                       { yylplineno++; }
473;                        { p_static = 0;
474#if YYLPDEBUG
475                            printf("%s", yytext);
476#endif
477                         }
478.                        { p_static = 0;
479                           yylp_errno = YYLP_BAD_CHAR;
480#if YYLPDEBUG
481                           printf("%s", yytext);
482#endif
483                           return(1);
484                         }
485
486%%
487
488int current_pos(int i)
489{
490  return(i+offset+(int)(yytext-yylp_buffer_start));
491}
492
493int libread(FILE* f, char* buf, int max_size)
494{ int rc;
495
496  offset = ftell(f);
497  rc  = fread( buf, 1, max_size, f );
498  yylp_buffer_start = buf;
499  return rc;
500}
501
502extern "C" {
503  int yylpwrap() {
504    //printf("======================= YYWRAP ====================\n");
505    if(brace1>0) { yylp_errno=YYLP_MISS_BR1; }
506    if(brace2>0) { yylp_errno=YYLP_MISS_BR2; }
507    if(brace3>0) { yylp_errno=YYLP_MISS_BR3; }
508    if(quote>0) { yylp_errno=YYLP_MISSQUOT; }
509    /* printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);/**/
510    if(feof(yyin)) return 1; else return 0;
511  }
512}
513
514void reinit_yylp()
515{
516   brace1 = 0;
517   brace2 = 0;
518   brace3 = 0;
519   quote  = 0;
520   yy_init=1;
521   yy_delete_buffer(yy_current_buffer);
522}
523
524void print_init()
525{
526   printf("Init=%d\n", yy_init);
527}
528
529print_version(char *p)
530{
531#ifdef STANDALONE_PARSER
532   //printf("loading %s%s", p, libnamebuf);
533#else
534   if (BVERBOSE(V_LOAD_LIB))
535      Warn( "loading %s%s", p, libnamebuf);
536#endif
537}
538
539#ifdef STANDALONE_PARSER
540main( int argc, char *argv[] )
541{
542  ++argv, --argc;  /* skip over program name */
543  if ( argc > 0 )
544     yyin = fopen( argv[0], "r" );
545  else
546     yyin = stdin;
547
548  printf( "  %-15s  %20s      %s,%s    %s,%s     %s,%s\n", "Library",
549          "function", "line", "start-eod", "line", "body-eop",
550          "line", "example-eoe");
551  yylplex(argv[0], argv[0]);
552  if(yylp_errno) {
553    printf("ERROR accured: ");
554    printf(yylp_errlist[yylp_errno], yylplineno);
555    printf("\n");
556  }
557  else if(pi!=NULL) printpi(pi);
558}
559
560#endif /* STANDALONE_PARSER */
561#endif /* HAVE_LIBPARSE */
Note: See TracBrowser for help on using the repository browser.