source: git/Singular/libparse.ll @ 06c0b3

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