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