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

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