source: git/Singular/libparse.l @ f36635

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