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