source: git/Singular/libparse.l @ d636ad

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