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