source: git/Singular/libparse.l @ 237b3e4

spielwiese
Last change on this file since 237b3e4 was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 30.2 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 <Singular/mod2.h>
11#ifdef STANDALONE_PARSER
12  #include <Singular/utils.h>
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 <kernel/options.h>
23  #include <kernel/febase.h>
24  #include <omalloc.h>
25#endif
26#include <Singular/libparse.h>
27
28#ifdef HAVE_LIBPARSER
29#define YY_SKIP_YYWRAP
30
31typedef enum { LP_NONE, LP_INFO, LP_CATEGORY, LP_URL, LP_VERSION} lib_cmds;
32
33int libread(FILE* f, char* buf, int max_size);
34int current_pos(int i);
35void print_version(lp_modes mode, char *p);
36void copy_string(lp_modes mode);
37extern void piCleanUp(procinfov pi);
38void make_version(char *p, int what);
39
40int brace1 = 0;  /* { } */
41int brace2 = 0;  /* ( ) */
42int brace3 = 0;  /* [ ] */
43int quote  = 0;  /* " */
44int offset = 0;
45BOOLEAN p_static = FALSE;
46int old_state = 0;
47lib_cmds last_cmd = LP_NONE;
48
49char libnamebuf[128];
50char *text_buffer=NULL;
51long string_start;
52
53char *yylp_buffer_start;
54int yylplineno = 1;
55int lpverbose = 0, check = 0;
56int texinfo_out = 0;
57int found_info=0,
58    found_cat=0,
59    found_version=0,
60    found_oldhelp = 0,
61    found_proc_in_proc = 0;
62
63const char *yylp_errlist[]= {
64   "",
65   "missing close bracket ')' for proc definition in line %d.",  /*  1 */
66   "missing close bracket ')' for procbody in line %d.",         /*  2 */
67   "missing close bracket ']' for procbody in line %d.",         /*  3 */
68   "too many ')' closed brackets in line %d.",                   /*  4 */
69   "too many ']' closed brackets in line %d.",                   /*  5 */
70   "missing close bracket ')' for example in line %d.",          /*  6 */
71   "missing close bracket ']' for example in line %d.",          /*  7 */
72   "cannot assign character '%c' in line %d to any group.",      /*  8 */
73   "there must be a quote missing somewhere before line %d.",    /*  9 */
74   "missing close bracket '}' at end of library in line %d.",    /* 10 */
75   "missing close bracket ')' at end of library in line %d.",    /* 11 */
76   "missing close bracket ']' at end of library in line %d.",    /* 12 */
77   NULL
78};
79int yylp_errno = 0;
80
81#ifdef STANDALONE_PARSER
82procinfov pi;
83int category_out = 0;
84void printpi(procinfov pi);
85void pi_clear(procinfov pi);
86extern "C" {
87  int yylpwrap();
88}
89void main_init(int argc, char *argv[]);
90void main_result(char *libname);
91#else /* STANDALONE_PARSER */
92idhdl h0;
93idhdl h_top;
94#define pi IDPROC(h0)
95extern "C"
96{
97  int yylpwrap();
98}
99extern libstackv library_stack;
100#endif /* STANDALONE_PARSER */
101
102static unsigned long help_chksum;
103
104#define SET_DEF_END(mode, pi, p) \
105  if ( mode == LOAD_LIB) pi->data.s.def_end = p;
106#define SET_HELP_START(mode, pi, p) \
107  if ( mode == LOAD_LIB) {pi->data.s.help_start = p; help_chksum = 0;}
108#define SET_HELP_END(mode, pi, p) \
109  if ( mode == LOAD_LIB) {pi->data.s.help_end = p;  \
110              pi->data.s.help_chksum = help_chksum;}
111
112#define SET_BODY_START(mode, pi, l, p) \
113     if ( mode == LOAD_LIB)            \
114     {                                 \
115       pi->data.s.body_lineno = l;     \
116       pi->data.s.body_start = p;      \
117     }
118#define SET_BODY_END(mode, pi, p) \
119     if ( mode == LOAD_LIB)       \
120     {                            \
121       pi->data.s.body_end = p-1; \
122       pi->data.s.proc_end = p-1; \
123     }
124
125#define SET_EXAMPLE_START(mode, pi, l, p) \
126   if ( mode == LOAD_LIB)                 \
127   {                                      \
128     pi->data.s.example_lineno = l;       \
129     pi->data.s.example_start = p;        \
130   }
131#define SET_PROC_END(mode, pi, p)   \
132     if ( mode == LOAD_LIB)         \
133     {                              \
134       pi->data.s.proc_end = p-1;   \
135       if(pi->data.s.body_end==0)   \
136         pi->data.s.body_end = p-1; \
137     }
138
139#define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
140#define IncrCheckSum(c)                          \
141do                                            \
142{                                             \
143  ROTATE_RIGHT(help_chksum);                  \
144  help_chksum += c;                           \
145  help_chksum &= 0xffff;                      \
146}                                             \
147while(0)
148
149#undef YY_DECL
150#define YY_DECL int yylex(char *newlib, char *libfile, \
151                           lib_style_types *lib_style, \
152                           idhdl pl, BOOLEAN autoexport, lp_modes mode)
153#undef YY_INPUT
154#define YY_INPUT(buf,result,max_size) \
155          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
156                  && ferror( yyin ) ) \
157                YY_FATAL_ERROR( "read in flex scanner failed" );
158
159#define YY_USER_INIT { \
160       BEGIN(header); \
161       yylplineno = 1; \
162       yylp_errno = 0; \
163       *lib_style = OLD_LIBSTYLE; \
164       strcpy(libnamebuf,"(**unknown version**)"); \
165     }
166
167#if 0
168<pbody>proc[ \t]+{name}  {
169                           printf("MISSING: PROC-cmd found. ERROR!\n"); }
170<pbody>example[ \t]*\n   {
171                           yylplineno++;
172                           printf("MISSING: EXAMPLE-cmd found. ERROR!\n"); }
173info=+"\"" {
174#endif
175
176%}
177
178digit          [0-9]
179letter         [@a-zA-Z\']
180name           ({letter}({letter}*{digit}*_*)*|_)
181varname        ({letter}({letter}*{digit}*_*\(\))*|_|#)
182letters        ({letter}|{digit}|[_./#%^*:,])
183string         ({letters}*)
184comment        [\/][\/]
185dolar          [$]
186nls            [\n\r]
187symbols        [~!@#$%^&*()_+-=\\\|\[\];:,<.>/\?\' \t\~\`]
188asymbols       ({symbols}|[{}])
189bsymbols       ({symbols}|{escbrack}|[}])
190aletters       ({letter}|{digit}|{asymbols}|{dolar}|{escquote}|{nls})
191bletters       ({letter}|{digit}|{bsymbols}|{dolar}|{quote}|{nls})
192cletters       ({letter}|{digit}|{asymbols}|{dolar}|{quote})
193strings        ({aletters}*)
194escstrings     ({bletters}*)
195fstring        ({cletters}*)
196param          ({name}+{tos}+{varname})
197parameters     ({param}(,{param})*)
198paramlist      ("("{parameters}")")
199quote          [\"]
200escquote       (\\\")
201escbrack       (\\\{)
202tnl            ([ \t\n\r]*)
203eos            ({quote}+{tnl}+"{")
204tos            ([ \t]*)
205eq             ([ \t]*+=[ \t]*)
206eqnl           ([ \t\n\r]*+=[ \t\n\r]*)
207
208/* %start START */
209
210%x header
211%x help
212%x libcmd
213%x libcmd2
214%x pdef
215%x phead
216%x poldhelp
217%x phelp
218%x pbody
219%x pstr
220%x pexample
221%x pestr
222%x string
223%x comment
224%x info
225%x category
226%x url
227%x version
228
229%%
230(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
231\/\/*      { old_state = YYSTATE; BEGIN(comment); }
232
233(info+{eqnl}+{quote}+{strings}+{quote}) {
234         yyless(4); old_state = YYSTATE; BEGIN(info);
235       }
236(category+{eqnl}+{quote}+{strings}+{quote}) {
237         yyless(8); old_state = YYSTATE; BEGIN(category);
238       }
239(url+{eqnl}+{quote}+{strings}+{quote}) {
240             if ( mode != GET_INFO )
241             {
242               #ifdef STANDALONE_PARSER
243               if (texinfo_out)
244               {
245                 char *c = yytext;
246                 printf("$url = \"");
247                 while ((*c != '\0') && (*c != '"')) c++;
248                 c++;
249                 while ((*c != '\0') && (*c != '"'))
250                 {
251                    if (*c != '\r') putchar(*c);
252                    c++;
253                 }
254                 printf("\";\n");
255               }
256               #endif
257             }
258       }
259
260(version+{eqnl}+{quote}+{strings}+{quote}) {
261             found_version++;
262             if ( mode != GET_INFO )
263             {
264               make_version(yytext,1);
265               #ifdef STANDALONE_PARSER
266               if (texinfo_out)
267               {
268                 char *c = libnamebuf;
269                 printf("$version = \"");
270                 while (*c != '\0')
271                 {
272                    if (*c  == '$' || *c  == '@') putchar('\\');
273                    if (*c != '\r') putchar(*c);
274                    if (*c  == '\\')
275                    {
276                      c++;
277                      if (*c != '"') putchar('\\');
278                    }
279                    else
280                      c++;
281                 }
282                 printf("\";\n");
283               }
284               else if (!category_out)
285                 printf("Version:%s;\n", libnamebuf);
286               #else
287               if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
288               text_buffer = omStrDup(libnamebuf);
289               omMarkAsStaticAddr(text_buffer);
290               #endif
291             }
292           }
293
294static     { p_static=TRUE; }
295
296(proc[ \t]+{name})|([ \t]proc[ \t]+{name}) {
297             char proc[256];
298             BEGIN(pdef);
299             found_proc_in_proc = 0;
300             proc[0]='\0';
301             sscanf( yytext, "%*[^p]proc %s", proc);
302             if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
303             #if YYLPDEBUG > 1
304             printf("Newlib:%s\n", newlib);
305             #endif
306             #ifdef STANDALONE_PARSER
307             if ( pi != NULL )
308             {
309               printpi(pi);
310               pi_clear(pi);
311             }
312             #ifdef STANDALONE_PARSER
313             pi = (procinfo *)malloc(sizeof(procinfo));
314             #else
315             pi = (procinfo *)omAlloc(sizeof(procinfo));
316             #endif
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);
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) piCleanUp((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 (line %d) has OLD-STYLE-HELP!\n",
563                       pi->procname, pi->data.s.proc_lineno);
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 (line %d) has OLD-STYLE-HELP!\n",
580                       pi->procname, pi->data.s.proc_lineno);
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[10];
853  char date[16];
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
965main( 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    exit(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 occured: [%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  exit(0);
995}
996
997#endif /* STANDALONE_PARSER */
998#endif /* HAVE_LIBPARSE */
Note: See TracBrowser for help on using the repository browser.