source: git/Singular/libparse.l @ b98976

fieker-DuValspielwiese
Last change on this file since b98976 was 512a2b, checked in by Olaf Bachmann <obachman@…>, 24 years ago
p_polys.h git-svn-id: file:///usr/local/Singular/svn/trunk@4606 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.8 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: libparse.l,v 1.42 2000-09-18 09:19:11 obachman 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 1
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 "omalloc.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);
34#ifdef HAVE_NAMESPACES
35void copy_string(lp_modes mode, idhdl pl);
36extern void piCleanUp(procinfov pi);
37#else /* HAVE_NAMESPACES */
38void copy_string(lp_modes mode);
39#endif /* HAVE_NAMESPACES */
40void make_version(char *p, int what);
41
42int brace1 = 0;  /* { } */
43int brace2 = 0;  /* ( ) */
44int brace3 = 0;  /* [ ] */
45int quote  = 0;  /* " */
46int offset = 0;
47BOOLEAN p_static = FALSE;
48int old_state = 0;
49lib_cmds last_cmd = LP_NONE;
50
51char libnamebuf[128];
52char *text_buffer=NULL;
53long string_start;
54
55char *yylp_buffer_start;
56int yylplineno = 1;
57int lpverbose = 0, check = 0;
58int texinfo_out = 0;
59int found_info=0,
60    found_version=0,
61    found_oldhelp = 0,
62    found_proc_in_proc = 0;
63
64char *yylp_errlist[]= {
65   "",
66   "missing close bracket ')' for proc definition in line %d.",  /*  1 */
67   "missing close bracket ')' for procbody in line %d.",         /*  2 */
68   "missing close bracket ']' for procbody in line %d.",         /*  3 */
69   "too many ')' closed brackets in line %d.",                   /*  4 */
70   "too many ']' closed brackets in line %d.",                   /*  5 */
71   "missing close bracket ')' for example in line %d.",          /*  6 */
72   "missing close bracket ']' for example in line %d.",          /*  7 */
73   "cannot assign character '%c' in line %d to any group.",      /*  8 */
74   "there must be a quote missing somewhere before line %d.",    /*  9 */
75   "missing close bracket '}' at end of library in line %d.",    /* 10 */
76   "missing close bracket ')' at end of library in line %d.",    /* 11 */
77   "missing close bracket ']' at end of library in line %d.",    /* 12 */
78   NULL
79};
80int yylp_errno = 0;
81
82#ifdef __MWERKS__
83#ifdef __cplusplus
84extern "C" {
85#endif
86long   ftell(FILE *fp);
87#ifdef macintosh
88int    fileno(FILE *stream);
89FILE   *fdopen(int filedes, char *type);
90int    isatty(int filedes);
91#else
92int    _fileno(FILE *stream);
93FILE   *_fdopen(int filedes, char *type);
94int    _isatty(int filedes);
95#define fileno  _fileno
96#define fdopen  _fdopen
97#define isatty  _isatty
98#endif /* macintosh */
99#ifdef __cplusplus
100}
101#endif
102#endif
103
104#ifdef STANDALONE_PARSER
105procinfov pi;
106void printpi(procinfov pi);
107void pi_clear(procinfov pi);
108extern "C" {
109  int yylpwrap();
110}
111void main_init(int argc, char *argv[]);
112void main_result(char *libname);
113#else /* STANDALONE_PARSER */
114idhdl h0;
115#ifdef HAVE_NAMESPACES
116idhdl h_top;
117extern namehdl namespaceroot;
118#endif /* HAVE_NAMESPACES */
119#define pi IDPROC(h0)
120extern "C"
121{
122  int yylpwrap();
123}
124extern libstackv library_stack;
125#endif /* STANDALONE_PARSER */
126
127static unsigned long help_chksum;
128
129#define SET_DEF_END(mode, pi, p) \
130  if ( mode == LOAD_LIB) pi->data.s.def_end = p;
131#define SET_HELP_START(mode, pi, p) \
132  if ( mode == LOAD_LIB) {pi->data.s.help_start = p; help_chksum = 0;}
133#define SET_HELP_END(mode, pi, p) \
134  if ( mode == LOAD_LIB) {pi->data.s.help_end = p;  \
135              pi->data.s.help_chksum = help_chksum;}
136
137#define SET_BODY_START(mode, pi, l, p) \
138     if ( mode == LOAD_LIB)            \
139     {                                 \
140       pi->data.s.body_lineno = l;     \
141       pi->data.s.body_start = p;      \
142     }
143#define SET_BODY_END(mode, pi, p) \
144     if ( mode == LOAD_LIB)       \
145     {                            \
146       pi->data.s.body_end = p-1; \
147       pi->data.s.proc_end = p-1; \
148     }
149
150#define SET_EXAMPLE_START(mode, pi, l, p) \
151   if ( mode == LOAD_LIB)                 \
152   {                                      \
153     pi->data.s.example_lineno = l;       \
154     pi->data.s.example_start = p;        \
155   }
156#define SET_PROC_END(mode, pi, p)   \
157     if ( mode == LOAD_LIB)         \
158     {                              \
159       pi->data.s.proc_end = p-1;   \
160       if(pi->data.s.body_end==0)   \
161         pi->data.s.body_end = p-1; \
162     }
163
164#define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
165#define IncrCheckSum(c)                          \
166do                                            \
167{                                             \
168  ROTATE_RIGHT(help_chksum);                  \
169  help_chksum += c;                           \
170  help_chksum &= 0xffff;                      \
171}                                             \
172while(0)
173
174#undef YY_DECL
175#ifdef HAVE_NAMESPACES
176#define YY_DECL int yylex(char *newlib, char *libfile, \
177                           lib_style_types *lib_style, \
178                           idhdl pl, BOOLEAN autoexport, lp_modes mode)
179#else /* HAVE_NAMESPACES */
180#define YY_DECL int yylex(char *newlib, char *libfile, \
181                           lib_style_types *lib_style, \
182                           lp_modes mode)
183#endif /* HAVE_NAMESPACES */
184
185#undef YY_INPUT
186#define YY_INPUT(buf,result,max_size) \
187          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
188                  && ferror( yyin ) ) \
189                YY_FATAL_ERROR( "read in flex scanner failed" );
190
191#define YY_USER_INIT { \
192       BEGIN(header); \
193       yylplineno = 1; \
194       yylp_errno = 0; \
195       *lib_style = OLD_LIBSTYLE; \
196       strcpy(libnamebuf,"(**unknown version**)"); \
197     }
198
199#if 0
200<pbody>proc[ \t]+{name}  {
201                           printf("MISSING: PROC-cmd found. ERROR!\n"); }
202<pbody>example[ \t]*\n   {
203                           yylplineno++;
204                           printf("MISSING: EXAMPLE-cmd found. ERROR!\n"); }
205info=+"\"" {
206#endif
207
208%}
209
210digit          [0-9]
211letter         [@a-zA-Z\']
212name           ({letter}({letter}*{digit}*_*)*|_)
213varname        ({letter}({letter}*{digit}*_*\(\))*|_|#)
214letters        ({letter}|{digit}|[_./#%^*:,])
215string         ({letters}*)
216comment        [\/][\/]
217dolar          [$]
218nls            [\n\r]
219symbols        [~!@#$%^&*()_+-=\\\|\[\];:,<.>/\?\' \t\~\`]
220asymbols       ({symbols}|[{}])
221bsymbols       ({symbols}|{escbrack}|[}])
222aletters       ({letter}|{digit}|{asymbols}|{dolar}|{escquote}|{nls})
223bletters       ({letter}|{digit}|{bsymbols}|{dolar}|{quote}|{nls})
224cletters       ({letter}|{digit}|{asymbols}|{dolar}|{quote})
225strings        ({aletters}*)
226escstrings     ({bletters}*)
227fstring        ({cletters}*)
228param          ({name}+{tos}+{varname})
229parameters     ({param}(,{param})*)
230paramlist      ("("{parameters}")")
231quote          [\"]
232escquote       (\\\")
233escbrack       (\\\{)
234tnl            ([ \t\n]*)
235eos            ({quote}+{tnl}+"{")
236tos            ([ \t]*)
237eq             ([ \t]*+=[ \t]*)
238eqnl           ([ \t\n]*+=[ \t\n]*)
239
240/* %start START */
241
242%x header
243%x help
244%x libcmd
245%x pdef
246%x phead
247%x poldhelp
248%x phelp
249%x pbody
250%x pstr
251%x pexample
252%x pestr
253%x string
254%x comment
255%x info
256%x version
257
258%%
259(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
260\/\/*      { old_state = YYSTATE; BEGIN(comment); }
261
262(info+{eqnl}+{quote}+{strings}+{quote}) {
263         yyless(4); old_state = YYSTATE; BEGIN(info);
264       }
265
266(version+{eqnl}+{quote}+{strings}+{quote}) {
267             found_version++;
268             if ( mode != GET_INFO )
269             {
270               make_version(yytext,1);
271               #ifdef STANDALONE_PARSER
272               if (texinfo_out)
273               {
274                 char *c = libnamebuf;
275                 printf("$version = \"");
276                 while (*c != '\0')
277                 {
278                    if (*c  == '$' || *c  == '@') putchar('\\');
279                    if (*c != '\r') putchar(*c);
280                    if (*c  == '\\')
281                    {
282                      c++;
283                      if (*c != '"') putchar('\\');
284                    }
285                    else
286                      c++;
287                 }
288                 printf("\";\n");
289               }
290               else
291                 printf("Version:%s;\n", libnamebuf);
292               #else
293               #ifdef HAVE_NAMESPACES
294               h0 = enterid( omStrDup("version"), myynest, STRING_CMD,
295                                  &IDPACKAGE(pl)->idroot, FALSE );
296               if (h0!=NULL)
297               {
298                  IDSTRING(h0) = omStrDup(libnamebuf);
299               }
300               #else /* HAVE_NAMESPACES */
301               if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
302               text_buffer = omStrDup(libnamebuf);
303               omMarkAsStaticAddr(text_buffer);
304               #endif /* HAVE_NAMESPACES */
305               #endif
306             }
307           }
308
309static     { p_static=TRUE; }
310
311(proc[ \t]+{name})|([ \t]proc[ \t]+{name}) {
312             char proc[256];
313             BEGIN(pdef);
314             found_proc_in_proc = 0;
315             proc[0]='\0';
316             sscanf( yytext, "%*[^p]proc %s", proc);
317             if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
318             #if YYLPDEBUG > 1
319             printf("Newlib:%s\n", newlib);
320             #endif
321             #ifdef STANDALONE_PARSER
322             if ( pi != NULL )
323             {
324               printpi(pi);
325               pi_clear(pi);
326             }
327             pi = (procinfo *)malloc(sizeof(procinfo));
328             iiInitSingularProcinfo(pi, newlib, proc, yylplineno,
329                                    current_pos(0), p_static);
330             #else STANDALONE_PARSER
331             if( mode == LOAD_LIB)
332             {
333                #ifdef HAVE_NAMESPACES
334                h0 = enterid( omStrDup(proc), 0 /*myynest*/, PROC_CMD,
335                                  &IDPACKAGE(pl)->idroot, TRUE);
336                if(!p_static && autoexport)
337                {
338                   namespaceroot->push( NSPACK(namespaceroot->root) ,"");
339                   h_top = enterid( omStrDup(proc), myynest, PROC_CMD,
340                                  &NSROOT(namespaceroot->root), FALSE );
341                   namespaceroot->pop();
342                }
343               #else /* HAVE_NAMESPACES */
344               h0 = enterid( omStrDup(proc), 0 /*myynest*/, PROC_CMD,
345                                   &idroot, TRUE );
346               #endif /* HAVE_NAMESPACES */
347               if (h0!=NULL)
348               {
349                 iiInitSingularProcinfo(IDPROC(h0), newlib, proc,
350                                yylplineno, current_pos(0),p_static);
351                 #ifdef HAVE_NAMESPACES
352                 if (!p_static && h_top != NULL && autoexport)
353                 {
354                   if(IDPROC(h_top)!=NULL) piCleanUp((procinfo *)IDPROC(h_top));
355                   IDPROC(h_top)=IDPROC(h0);
356                   IDPROC(h_top)->ref++;
357                 }
358                 #endif /* HAVE_NAMESPACES */
359                 if (BVERBOSE(V_LOAD_PROC))
360                   Warn( "     proc '%s' registered", proc );
361               }
362               #endif STANDALONE_PARSER
363               SET_DEF_END(mode, pi, current_pos(yyleng+1));
364               #if YYLPDEBUG
365               if(lpverbose)
366               {
367                  printf("// PROCEDURE '%s' status: %s, ", proc,
368                      p_static ? "local" : "global");
369                  printf("starting at line %d,%d: definition end: %d (%d).\n",
370                      yylplineno, current_pos(0), (int)pi->data.s.def_end, brace1);
371               }
372               #endif
373               p_static=FALSE;
374             #ifndef STANDALONE_PARSER
375             }
376             #endif STANDALONE_PARSER
377           }
378example    {
379             BEGIN(pexample);
380             SET_EXAMPLE_START(mode, pi, yylplineno, current_pos(0));
381             #if YYLPDEBUG
382             if(lpverbose)
383             {
384                printf("//     EXAMPLE at line %d,%d (%d)\n", yylplineno,
385                    current_pos(0), brace1);
386             }
387             #endif
388           }
389
390LIB[ \t]+"\"" { quote++;
391             BEGIN(libcmd);
392           }
393
394<header>({comment}+{tos}+{dolar}+Id:+{string}+[^\n]*)|({comment}+{tos}+{dolar}+Header:+{string}+[^\n]*) {
395             make_version(yytext, 0);
396             #if YYLPDEBUG > 1
397             printf("+(id)HEAD:%s\n", yytext);
398             #endif
399           }
400<header>(^{comment}+[^\n]*) {
401             #if YYLPDEBUG
402             printf("+(cmt)HEAD:%s\n", yytext);
403             #endif
404           }
405<header>(^#![^\n]*) {
406             #if YYLPDEBUG > 1
407             printf("-HEAD:%s\n", yytext);
408             #endif
409           }
410<header>^proc\  { yyless(0);
411             BEGIN(INITIAL);
412             yymore();
413           }
414<header>(info+{eqnl}+{quote})|(version+{eqnl}+{quote}) {
415             yyless(0);
416             *lib_style = NEW_LIBSTYLE;
417             BEGIN(INITIAL);
418             yymore();
419           }
420
421<header>^LIB[ \t]+"\""   { quote++;
422             BEGIN(libcmd);
423           }
424<header>\n { yylplineno++; }
425<header>.  {
426             #if YYLPDEBUG > 1
427             printf(" HEAD:%s\n", yytext);
428             #endif
429             yyless(0);
430             BEGIN(help);
431           }
432<help>(^{comment}+[^\n]*)  {
433             #if YYLPDEBUG > 1
434             printf(" HELP:%s\n", yytext);
435             #endif
436             BEGIN(INITIAL); }
437<help>(^#![^\n]*) {
438             #if YYLPDEBUG > 1
439             printf(" HELP:%s\n", yytext);
440             #endif
441             BEGIN(INITIAL);
442           }
443<help>(info+{eqnl}+{quote})|(version+{eqnl}+{quote}) {
444             yyless(0);
445             *lib_style = NEW_LIBSTYLE;
446             BEGIN(INITIAL);
447             yymore();
448           }
449<help>^proc\  {
450             yyless(0);
451             //printf("2) proc found.\n");
452             BEGIN(INITIAL);
453             yymore();
454           }
455<help>^LIB[ \t]+"\""     { quote++;
456             BEGIN(libcmd);
457           }
458
459<help>\n { yylplineno++; }
460<help>({tos}|{comment}+{fstring}) {
461             #if YYLPDEBUG
462             if(lpverbose>2) printf("--->%s<---\n", yytext);
463             #endif
464           }
465<help>.    {
466             found_oldhelp=1;
467             #if YYLPDEBUG > 1
468             printf("-HELP:%s\n", yytext);
469             #endif
470           }
471
472
473<libcmd>{string}"\""     { quote--;
474             yytext[yyleng-1] = '\0';
475             #ifndef STANDALONE_PARSER
476             if ( mode == LOAD_LIB )
477             {
478               library_stack->push(newlib, yytext);
479             }
480             #endif /* STANDALONE_PARSER */
481             #if YYLPDEBUG
482             if(lpverbose>1) printf("LIB:'%s'\n", yytext);
483             #endif
484             BEGIN(INITIAL);
485           }
486
487<pdef>[ \t] { }
488<pdef>\(   {
489             brace2++;
490             #if YYLPDEBUG > 1
491             printf("%s", yytext);
492             #endif
493           }
494<pdef>\)   {
495             brace2--;
496             #if YYLPDEBUG > 1
497             printf(">%s<\n", yytext);
498             printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
499             #endif
500             if(brace2<=0)
501             {
502               #if YYLPDEBUG > 1
503               printf("BEGIN(phead){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
504               #endif
505               SET_DEF_END(mode, pi, current_pos(yyleng));
506               BEGIN(phead);
507             }
508           }
509<pdef>"{"  {
510             if(brace2>0)
511             {
512               #if YYLPDEBUG > 1
513               printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
514               #endif
515               yylp_errno = YYLP_DEF_BR2;
516               return(1);
517             }
518             else
519             {
520               brace1++; BEGIN(pbody);
521               if(lpverbose)
522                  printf("//     BODY at line %d,%d (%d)\n", yylplineno,
523                      current_pos(0), brace1);
524               SET_BODY_START(mode, pi, yylplineno, current_pos(0));
525             }
526           }
527<pdef>\n { yylplineno++;
528              if(brace2<=0)
529              {
530#if YYLPDEBUG > 1
531                printf("BEGIN(phead-2){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
532#endif
533                BEGIN(phead);
534              }
535            }
536<pdef>({comment}[^\n]*)  { }
537<pdef>\/\/*      { old_state = YYSTATE; BEGIN(comment); }
538<pdef>.    {
539             if(brace2<=0)
540             {
541               BEGIN(phead);
542               yyless(0);
543             }
544           }
545
546<phead>({tnl}+{quote}+{strings}+{escquote}+{tnl}+"{") {
547              #if YYLPDEBUG
548              if(lpverbose>2)printf("0-Len=%d;\n", yyleng);
549              #endif
550              if(check)
551              {
552                printf("Procedure %s (line %d) has OLD-STYLE-HELP!\n",
553                       pi->procname, pi->data.s.proc_lineno);
554              }
555              SET_HELP_START(mode, pi, current_pos(0));
556              BEGIN(poldhelp);
557              yyless(0);
558           }
559<phead>({tnl}+{quote}+{strings}+{eos}) {
560              #if YYLPDEBUG
561              if(lpverbose>2)printf("1-Len=%d;\n", yyleng);
562              #endif
563              BEGIN(phelp);
564              yyless(0);
565           }
566<phead>{escstrings}+"{" {
567              if(check && yyleng>2)
568              {
569                printf("Procedure %s (line %d) has OLD-STYLE-HELP!\n",
570                       pi->procname, pi->data.s.proc_lineno);
571              }
572              #if YYLPDEBUG
573              if(lpverbose>2 && yyleng>2)
574                 printf("2-Len=%d, %s;\n", yyleng, pi->procname);
575              #endif
576              SET_HELP_START(mode, pi, current_pos(0));
577              BEGIN(poldhelp);
578              yyless(0);
579           }
580<phead>.   { printf("[%s]", yytext); }
581
582<poldhelp>{escbrack} { }
583<poldhelp>"{" {
584                SET_HELP_END(mode, pi, current_pos(0));
585                brace1++; BEGIN(pbody);
586                if(lpverbose)
587                {
588                   printf("//     HELP from %d to %d\n",
589                       (int)pi->data.s.help_start, (int)pi->data.s.help_end);
590                   printf("//     BODY at line %d,%d (%d)\n", yylplineno,
591                       current_pos(0), brace1);
592                }
593#if YYLPDEBUG > 1
594                printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
595#endif
596                SET_BODY_START(mode, pi, yylplineno, current_pos(0));
597#if YYLPDEBUG > 1
598                printf("BODY at %d/%d", yylplineno, current_pos(0));
599#endif
600              }
601<poldhelp>\n  { yylplineno++; }
602<poldhelp>.   { }
603
604<phelp>{quote} {
605             old_state = YYSTATE;
606             BEGIN(string);
607             SET_HELP_START(mode, pi, current_pos(1));
608           }
609<phelp>{tos} {}
610<phelp>"{" {
611             brace1++; BEGIN(pbody);
612             if(lpverbose)
613             {
614                printf("//     HELP from %d to %d\n",
615                    (int)pi->data.s.help_start, (int)pi->data.s.help_end);
616                printf("//     BODY at line %d,%d (%d)\n", yylplineno,
617                    current_pos(0), brace1);
618             }
619             #if YYLPDEBUG > 1
620             printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
621             #endif
622             SET_BODY_START(mode, pi, yylplineno, current_pos(0));
623             #if YYLPDEBUG > 1
624             printf("BODY at %d/%d", yylplineno, current_pos(0));
625             #endif
626           }
627<phelp>\n  { yylplineno++;}
628
629<pbody>({comment}[^\n]*) { }
630<pbody>{quote} { quote++; old_state = YYSTATE;
631                 BEGIN(string); /* printf("%s", yytext); */
632               }
633
634<pbody>proc+{tos}+{name}+{tnl}+{paramlist}+{tnl}+"{" {
635             if(check) printf("*** found 2 proc whithin procedure '%s'.\n",
636                          pi->procname);
637             yyless(yyleng-1);
638           }
639<pbody>proc+{tos}+{name}+{tnl}+"{" {
640             if(check) printf("*** found 1 proc whithin procedure '%s'.\n",
641                          pi->procname);
642             yyless(yyleng-1);
643           }
644<pbody>"{"     {
645                 brace1++;
646                 #if YYLPDEBUG > 1
647                 printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
648                 #endif
649                }
650<pbody>"}"               {
651                           #if YYLPDEBUG > 1
652                           printf("line: %d, (%d)%s\n",
653                             yylplineno, brace1, yytext);
654                           #endif
655                           brace1--;
656                           if(brace2>0)
657                           {
658                             yylp_errno = YYLP_BODY_BR2;
659                             return(1);
660                           }
661                           if(brace3>0)
662                           {
663                             yylp_errno = YYLP_BODY_BR3;
664                             return(1);
665                           }
666                           if(brace1<=0)
667                           {
668                             SET_BODY_END(mode, pi, current_pos(yyleng));
669                             SET_PROC_END(mode, pi, current_pos(yyleng));
670                             #if YYLPDEBUG > 1
671                               printf("-%d\n", current_pos(0));
672                             #endif
673                             BEGIN(INITIAL);
674                           }
675                         }
676<pbody>"("               {
677                           brace2++; /* printf("%s", yytext); */
678                         }
679<pbody>")"               {
680                           brace2--; /* printf("%s", yytext); */
681                           if(brace2<0) {
682                             yylp_errno = YYLP_BODY_TMBR2;
683                             return(1);
684                           }
685                         }
686<pbody>"["               {
687                           brace3++; /* printf("%s", yytext); */
688                         }
689<pbody>"]"               {
690                           brace3--; /* printf("%s", yytext); */
691                           if(brace3<0) {
692                             yylp_errno = YYLP_BODY_TMBR3;
693                             return(1);
694                           }
695                         }
696<pbody>\n                { yylplineno++; }
697<pbody>.                 { }
698
699<info>{quote} {
700             quote++; BEGIN(string);
701             found_info++;
702             string_start = current_pos(yyleng);
703             *lib_style = NEW_LIBSTYLE;
704             last_cmd = LP_INFO;
705       }
706<info>\n { yylplineno++; }
707<info>.  { }
708
709<string>"\""             { quote--;
710                           #ifdef HAVE_NAMESPACES
711                             copy_string(mode, pl);
712                           #else /* HAVE_NAMESPACES */
713                             copy_string(mode);
714                           #endif /* HAVE_NAMESPACES */
715                           last_cmd = LP_NONE;
716                           if(old_state==phelp)
717                           {
718                              SET_HELP_END(mode, pi, current_pos(0));
719                           }
720                           BEGIN(old_state); /* printf("%s", yytext); */
721                         }
722<string>(\\\\)|(\\\")    { if (old_state == phelp) IncrCheckSum(*yytext);}
723<string>\n               { yylplineno++; if (old_state == phelp) IncrCheckSum('\n');}
724<string>.                { if (old_state == phelp) IncrCheckSum(*yytext);}
725
726<pexample>(\/\/[^\n]*)  { }
727<pexample>"\""           { quote++; old_state = YYSTATE;
728                           BEGIN(string); /* printf("%s", yytext); */
729                         }
730<pexample>"{"            {
731                           brace1++; /* printf("(%d)%s", brace1, yytext); */
732                         }
733<pexample>"}"            {
734                           brace1--; /* printf("(%d)%s", brace1, yytext); */
735                           if(brace1<=0) {
736                             if(brace2>0) { yylp_errno=YYLP_EX_BR2; return(1); }
737                             if(brace3>0) { yylp_errno=YYLP_EX_BR3; return(1); }
738                             BEGIN(INITIAL);
739                             SET_PROC_END(mode, pi, current_pos(yyleng));
740                           }
741                         }
742<pexample>"("            {
743                           brace2++; /* printf("%s", yytext); */
744                         }
745<pexample>")"            {
746                           brace2--; /* printf("%s", yytext); */
747                         }
748<pexample>"["            {
749                           brace3++; /* printf("%s", yytext); */
750                         }
751<pexample>"]"            {
752                           brace3--; /* printf("%s", yytext); */
753                         }
754<pexample>\n             { yylplineno++; }
755<pexample>.              { }
756
757<pestr>"\""              { quote--;
758                           BEGIN(pexample); /* printf("%s", yytext); */
759                         }
760<pestr>\\\\              { }
761<pestr>\\\"              { }
762<pestr>\n                { yylplineno++; }
763<pestr>.                 { }
764
765<comment>\*\/            { BEGIN(old_state); }
766<comment>\n              { yylplineno++; }
767<comment>.               { }
768
769\n                       { yylplineno++; }
770\r                       { }
771;                        { p_static = FALSE;
772                            #if YYLPDEBUG > 1
773                            printf("%s", yytext);
774                            #endif
775                         }
776.                        { p_static = FALSE;
777                           yylp_errno = YYLP_BAD_CHAR;
778                           #ifdef STANDALONE_PARSER
779                           printf("[%d]", *yytext);
780                           #else
781                           if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
782                           text_buffer = omStrDup(yytext);
783                           omMarkAsStaticAddr(text_buffer);
784                           #endif
785                           #if YYLPDEBUG > 1
786                             printf("[%s]", yytext);
787                           #endif
788                           return(1);
789                         }
790
791%%
792
793int current_pos(int i)
794{
795  return(i+offset+(int)(yytext-yylp_buffer_start));
796}
797
798int libread(FILE* f, char* buf, int max_size)
799{ int rc;
800
801  offset = ftell(f);
802  rc  = myfread( buf, 1, max_size, f );
803  #if YYLPDEBUG >2
804    printf("fread: %d of %d\n", rc, max_size);
805  #endif
806  yylp_buffer_start = buf;
807  return rc;
808}
809
810extern "C" {
811  int yylpwrap()
812  {
813    //printf("======================= YYWRAP ====================\n");
814    if(brace1>0) { yylp_errno=YYLP_MISS_BR1; }
815    if(brace2>0) { yylp_errno=YYLP_MISS_BR2; }
816    if(brace3>0) { yylp_errno=YYLP_MISS_BR3; }
817    if(quote>0) { yylp_errno=YYLP_MISSQUOT; }
818    /* printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);/**/
819    if(feof(yyin)) return 1; else return 0;
820  }
821}
822
823void reinit_yylp()
824{
825   brace1 = 0;
826   brace2 = 0;
827   brace3 = 0;
828   quote  = 0;
829   yy_init=1;
830   yy_delete_buffer(yy_current_buffer);
831}
832
833void make_version(char *p,int what)
834{
835  char ver[10];
836  char date[16];
837  ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
838  date[0]='?'; date[1]='\0';
839  if(what) sscanf(p,"%*[^=]= %*s %*s %10s %16s",ver,date);
840  else sscanf(p,"// %*s %*s %10s %16s",ver,date);
841  strcpy(libnamebuf,"(");
842  strcat(libnamebuf,ver);
843  strcat(libnamebuf,",");
844  strcat(libnamebuf,date);
845  strcat(libnamebuf,")");
846  if(what && strcmp(libnamebuf, "(?.?,?)")==0)
847  {
848    sscanf(p,"%*[^\"]\"%[^\"]\"",libnamebuf);
849  }
850  //printf("ID=(%d)%s; \n", what, p);
851}
852
853#ifdef HAVE_NAMESPACES
854void copy_string(lp_modes mode, idhdl pl)
855#else /* HAVE_NAMESPACES */
856void copy_string(lp_modes mode)
857#endif /* HAVE_NAMESPACES */
858{
859#ifdef STANDALONE_PARSER
860  if (texinfo_out && last_cmd == LP_INFO)
861  {
862    long current_location = ftell(yylpin), i = string_start, quote = 0;
863    char c;
864    printf("$info = <<EOT;\n");
865    fseek (yylpin, i, SEEK_SET);
866    while (i< current_location)
867    {
868      c = fgetc(yylpin);
869      if (c == '\\')
870      {
871        quote = (! quote);
872      }
873      else if (c == '"')
874      {
875        if (! quote) break;
876      }
877      else
878        quote = 0;
879      if (c == '@' || c == '$') putchar('\\');
880      if (c != '\r') putchar(c);
881      i++;
882    }
883    fseek (yylpin, current_location, SEEK_SET);
884    printf("\nEOT\n");
885  }
886#else
887  if((last_cmd == LP_INFO)&&(mode == GET_INFO))
888  {
889    int i, offset=0;
890    long current_location = ftell(yylpin);
891    int len = (int)(current_pos(0) - string_start);
892    fseek(yylpin, string_start, SEEK_SET);
893    if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
894    text_buffer = (char *)omAlloc(len+2);
895    omMarkAsStaticAddr(text_buffer);
896    myfread(text_buffer, len, 1, yylpin);
897    fseek(yylpin, current_location, SEEK_SET);
898    text_buffer[len]='\0';
899    offset=0;
900    for(i=0;i<=len; i++)
901    {
902      if(text_buffer[i]=='\\' &&
903         (text_buffer[i+1]=='\"' || text_buffer[i+1]=='{' ||
904          text_buffer[i+1]=='}' || text_buffer[i+1]=='\\'))
905      {
906        i++;
907        offset++;
908      }
909      if(offset>0) text_buffer[i-offset] = text_buffer[i];
910    }
911    #ifdef HAVE_NAMESPACES
912    if( mode != GET_INFO ) {
913      h0 = enterid( omStrDup("info"), myynest, STRING_CMD,
914                          &IDPACKAGE(pl)->idroot, FALSE );
915      if (h0!=NULL)
916      {
917         IDSTRING(h0) = omStrDup(text_buffer);
918      }
919    }
920    #endif /* HAVE_NAMESPACES */
921  }
922#endif /* STANDALONE_PARSER */
923}
924
925void print_init()
926{
927   printf("Init=%d\n", yy_init);
928}
929
930void print_version(lp_modes mode, char *p)
931{
932#ifdef STANDALONE_PARSER
933  //printf("loading %s%s", p, libnamebuf);
934#else
935  if ( mode == LOAD_LIB )
936  {
937    if (BVERBOSE(V_LOAD_LIB) && p!=NULL ) Print(" %s...", p);
938       //Warn( "loading %s%s", p, libnamebuf);
939  }
940#endif
941}
942
943#ifdef STANDALONE_PARSER
944main( int argc, char *argv[] )
945{
946  lib_style_types lib_style;
947  main_init(argc, argv);
948  if(yyin == NULL) {
949    fprintf(stderr, "No library found to parse.\n");
950    exit(1);
951  }
952  if (! texinfo_out)
953  {
954    if(lpverbose)printf("Verbose level=%d\n", lpverbose);
955    if(check)printf("Reporting most possible annomalies.\n");
956    if(lpverbose||check)printf("\n");
957
958    printf( "  %-15s  %20s      %s,%s    %s,%s     %s,%s\n", "Library",
959            "function", "line", "start-eod", "line", "body-eob",
960            "line", "example-eoe");
961  }
962  yylplex(argv[0], argv[0], &lib_style);
963  if(yylp_errno) {
964    printf("ERROR occured: [%d] ", yylp_errno);
965    printf(yylp_errlist[yylp_errno], yylplineno);
966    printf("\n");
967  }
968  else if(pi!=NULL) printpi(pi);
969  if (texinfo_out)
970    printf("1;");
971  exit(0);
972}
973
974#endif /* STANDALONE_PARSER */
975#endif /* HAVE_LIBPARSE */
Note: See TracBrowser for help on using the repository browser.