source: git/Singular/libparse.l @ 84a6a7

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