source: git/Singular/libparse.l @ 35aab3

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