source: git/Singular/libparse.ll @ e6cdad

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