source: git/Singular/libparse.l @ 4e8195

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