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