source: git/Singular/libparse.ll @ dcd92d

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