source: git/Singular/libparse.l @ 3bef0a

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