Debian Codegraph @

gcc/cobol/cdf.y

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 %{
31
32 #include "cobol-system.h"
33 #include "coretypes.h"
34 #include "tree.h"
35 #undef yy_flex_debug
36 #include "../../libgcobol/ec.h"
37 #include "../../libgcobol/common-defs.h"
38 #include "util.h"
39 #include "cbldiag.h"
40 #include "symbols.h"
41 #include "copybook.h"
42 #include "../../libgcobol/exceptl.h"
43 #include "exceptg.h"
44
45 #define COUNT_OF(X) (sizeof(X) / sizeof(X[0]))
46
47 copybook_t copybook;
48
49 static inline bool
50 is_word( int c ) {
51 return c == '_' || ISALNUM(c);
52 }
53
54 static std::pair<long long, bool>
55 integer_literal( const char input[] ) {
56 long long v;
57 int n;
58 bool fOK = 1 == sscanf(input, "%lld%n", &v, &n) &&
59 n == (int)strlen(input);
60 return std::make_pair(v, fOK);
61 }
62
63 /* "The renamed symbols include 'yyparse', 'yylex', 'yyerror',
64 'yynerrs', 'yylval', 'yylloc', 'yychar' and 'yydebug'. [...] The
65 renamed macros include 'YYSTYPE', 'YYLTYPE', and 'YYDEBUG'" */
66
67 extern int yylineno, yyleng;
68 extern char *yytext;
69
70 static int ydflex(void);
71
72 #define PROGRAM current_program_index()
73
74 const YYLTYPE& cobol_location();
75 static YYLTYPE location_set( const YYLTYPE& loc );
76 void input_file_status_notify();
77
78 #define YYLLOC_DEFAULT(Current, Rhs, N) \
79 do { \
80 if (N) \
81 { \
82 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
83 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
84 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
85 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
86 location_dump("cdf.c", N, \
87 "rhs N ", YYRHSLOC (Rhs, N)); \
88 } \
89 else \
90 { \
91 (Current).first_line = \
92 (Current).last_line = YYRHSLOC (Rhs, 0).last_line; \
93 (Current).first_column = \
94 (Current).last_column = YYRHSLOC (Rhs, 0).last_column; \
95 } \
96 location_dump("cdf.c", __LINE__, "current", (Current)); \
97 input_file_status_notify(); \
98 location_set(Current); \
99 } while (0)
100
101 %}
102
103 %code requires {
104 #include "cdfval.h"
105
106 using std::map;
107
108 #pragma GCC diagnostic push
109 #pragma GCC diagnostic ignored "-Wunused-function"
110 static bool
111 cdfval_add( const char name[],
112 const cdfval_t& value, bool override = false )
113 {
114 cdf_values_t& dictionary( cdf_dictionary() );
115
116 if( scanner_parsing() ) {
117 if( ! override ) {
118 if( dictionary.find(name) != dictionary.end() ) return false;
119 }
120 dictionary[name] = value;
121 }
122 return true;
123 }
124 static void
125 cdfval_off( const char name[] ) {
126 cdf_values_t& dictionary( cdf_dictionary() );
127
128 if( scanner_parsing() ) {
129 auto p = dictionary.find(name);
130 if( p == dictionary.end() ) {
131 dictionary[name] = cdfval_t();
132 }
133 dictionary[name].off = true;
134 }
135 }
136 #pragma GCC diagnostic pop
137
138 bool operator==( const cdfval_base_t& lhs, int rhs );
139 bool operator||( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
140 bool operator&&( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
141
142 cdfval_t operator<( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
143 cdfval_t operator<=( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
144 cdfval_t operator==( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
145 cdfval_t operator!=( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
146 cdfval_t operator>=( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
147 cdfval_t operator>( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
148 cdfval_t operator+( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
149 cdfval_t operator-( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
150 cdfval_t operator*( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
151 cdfval_t operator/( const cdfval_base_t& lhs, const cdfval_base_t& rhs );
152 cdfval_t negate( cdfval_base_t lhs );
153
154 }
155
156 %{
157 static char *display_msg;
158 const char * keyword_str( int token );
159
160 exception_turn_t exception_turn;
161
162 bool
163 apply_cdf_turn( const exception_turn_t& turn ) {
164 cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
165
166 for( auto elem : turn.exception_files() ) {
167 std::set<size_t> files(elem.second.begin(), elem.second.end());
168 enabled_exceptions.turn_on_off(turn.enabled,
169 turn.location,
170 elem.first, files);
171 }
172 if( getenv("GCOBOL_SHOW") ) enabled_exceptions.dump();
173 return true;
174 }
175 %}
176
177 %union {
178 bool boolean;
179 int number;
180 const char *string;
181 cdf_arg_t cdfarg;
182 cdfval_base_t cdfval;
183 cbl_file_t *file;
184 std::set<size_t> *files;
185 }
186
187 %printer { fprintf(yyo, "'%s'", $$? "true" : "false" ); } <boolean>
188 %printer { fprintf(yyo, "'%s'", $$ ); } <string>
189 %printer { fprintf(yyo, "%s '%s'",
190 keyword_str($$.token),
191 $$.string? $$.string : "<nil>" ); } <cdfarg>
192 /* cppcheck-suppress invalidPrintfArgType_sint */
193 %printer { fprintf(yyo, HOST_SIZE_T_PRINT_DEC " '%s'",
194 (fmt_size_t)$$.number, $$.string? $$.string : "" ); } <cdfval>
195
196 %type <string> NAME NUMSTR LITERAL PSEUDOTEXT
197 %type <string> LSUB RSUB SUBSCRIPT
198 %type <cdfarg> namelit name_any name_one
199 %type <string> name subscript subscripts inof
200 %token <boolean> BOOL
201 %token <number> FEATURE 365 NUMBER 303 EXCEPTION_NAME 280 "EXCEPTION NAME"
202
203 %type <cdfval> cdf_expr
204 %type <cdfval> cdf_relexpr cdf_reloper cdf_and cdf_bool_expr
205 %type <cdfval> cdf_factor
206 %type <boolean> cdf_cond_expr override except_check
207
208 %type <file> filename
209 %type <files> filenames
210
211 %type <number> cdf_stackable
212
213 %token BY 486
214 %token COPY 362
215 %token CDF_DISPLAY 384 ">>DISPLAY"
216 %token IN 605
217 %token NAME 286
218 %token NUMSTR 305 "numeric literal"
219 %token OF 686
220 %token PSEUDOTEXT 721
221 %token REPLACING 743
222 %token LITERAL 298
223 %token SUPPRESS 376
224
225 %token LSUB 367 "("
226 %token SUBSCRIPT 375 RSUB 372 ")"
227
228 %token CDF_DEFINE 383 ">>DEFINE"
229 %token CDF_IF 385 ">>IF"
230 %token CDF_ELSE 386 ">>ELSE"
231 %token CDF_END_IF 387 ">>END-IF"
232 %token CDF_EVALUATE 388 ">>EVALUATE"
233 %token CDF_WHEN 389 ">>WHEN"
234 %token CDF_END_EVALUATE 390 ">>END-EVALUATE"
235
236 %token ALL 450
237 %token CALL_CONVENTION 391 ">>CALL-CONVENTION"
238 %token COBOL_WORDS 380 ">>COBOL-WORDS"
239 %token CDF_PUSH 394 ">>PUSH"
240 %token CDF_POP 395 ">>POP"
241 %token SOURCE_FORMAT 396 ">>SOURCE FORMAT"
242
243 %token AS 468 CONSTANT 361 DEFINED 363
244 %type <boolean> DEFINED
245 %token OTHER 698 PARAMETER_kw 368 "PARAMETER"
246 %token OFF 687 OVERRIDE 369
247 %token THRU 939
248 %token TRUE_kw 813 "True"
249
250 %token CALL_COBOL 392 "CALL"
251 %token CALL_VERBATIM 393 "CALL (as C)"
252
253 %token TURN 815 CHECKING 496 LOCATION 649 ON 689 WITH 841
254
255 %left OR 940
256 %left AND 941
257 %right NOT 942
258 %left '<' '>' '=' NE 943 LE 944 GE 945
259 %left '-' '+'
260 %left '*' '/'
261 %right NEG 947
262
263 %define api.prefix {ydf}
264 %define api.token.prefix{YDF_}
265
266 %locations
267 %define parse.error verbose
268 %%
269 top: partials { YYACCEPT; }
270 | copy '.'
271 {
272 const char *library = copybook.library();
273 if( !library ) library = "SYSLIB";
274 const char *source = copybook.source();
275 dbgmsg("COPY %s from %s", source, library);
276 YYACCEPT;
277 }
278 | copy error {
279 error_msg(@error, "COPY directive must end in a %<.%>");
280 YYABORT;
281 }
282 | completes { YYACCEPT; }
283 ;
284
285 completes: complete
286 | completes complete
287 | completes partial
288 ;
289 complete: cdf_define
290 | cdf_display
291 | cdf_turn
292 | cdf_call_convention
293 | cdf_push
294 | cdf_pop
295 ;
296
297 /*
298 * To do: read ISO 2022 to see how >>DISPLAY is dictionary!
299 * To do: DISPLAY UPON
300 * To do: decide what to do about newlines, and when; DISPLAY has
301 * {}... in the specification.
302 */
303 cdf_display: CDF_DISPLAY strings {
304 if( scanner_parsing() ) {
305 fprintf(stderr, "%s\n", display_msg);
306 free(display_msg);
307 display_msg = NULL;
308 }
309 }
310 ;
311 strings: LITERAL {
312 display_msg = xstrdup($1);
313 }
314 | strings LITERAL {
315 char *p = display_msg;
316 display_msg = xasprintf("%s %s", p, $2);
317 free(p);
318 }
319 ;
320
321 partials: partial
322 {
323 if( ! scanner_parsing() ) YYACCEPT;
324 }
325 | partials partial
326 {
327 if( ! scanner_parsing() ) YYACCEPT;
328 }
329 ;
330 partial: cdf_if /* text */
331 | CDF_ELSE { scanner_parsing_toggle(); }
332 | CDF_END_IF { scanner_parsing_pop(); }
333 | cdf_evaluate /* text */
334 | cdf_eval_when /* text */
335 | CDF_END_EVALUATE { scanner_parsing_pop(); }
336 ;
337
338 cdf_define: CDF_DEFINE cdf_constant NAME as cdf_expr[value] override
339 {
340 if( keyword_tok($NAME) ) {
341 error_msg(@NAME, "%s is a COBOL keyword", $NAME);
342 YYERROR;
343 }
344 if( !cdfval_add( $NAME, cdfval_t($value), $override) ) {
345 error_msg(@NAME, "name already in dictionary: %s", $NAME);
346 cdf_values_t& dictionary( cdf_dictionary() );
347 const cdfval_t& entry = dictionary[$NAME];
348 if( entry.filename ) {
349 error_msg(@NAME, "%s previously defined in %s:%d",
350 $NAME, entry.filename, entry.lineno);
351 } else {
352 error_msg(@NAME, "%s was defined on the command line", $NAME);
353 }
354 YYERROR;
355 }
356 }
357 | CDF_DEFINE cdf_constant NAME '=' cdf_expr[value] override
358 { /* accept, but as error */
359 if( scanner_parsing() ) {
360 error_msg(@NAME, "CDF error: %s = value invalid", $NAME);
361 }
362 }
363 | CDF_DEFINE cdf_constant NAME as OFF
364 {
365 cdfval_off( $NAME);
366 }
367 | CDF_DEFINE cdf_constant NAME as PARAMETER_kw override
368 /*
369 * "If the PARAMETER phrase is specified, the value referenced
370 * by compilation-variable-name-1 is obtained from the
371 * operating environment by an implementor-defined method...."
372 * It's a noop for us, because parameters defined with -D are
373 * available regardless.
374 */
375 {
376 if( 0 == cdf_dictionary().count($NAME) ) {
377 yywarn("CDF: '%s' is defined AS PARAMETER "
378 "but was not defined", $NAME);
379 }
380 }
381 | CDF_DEFINE FEATURE as ON {
382 auto feature = cbl_gcobol_feature_t($2);
383 if( ! cobol_gcobol_feature_set(feature, true) ) {
384 error_msg(@FEATURE,
385 "%<>>DEFINE %%EBCDIC-MODE%> is invalid within program body");
386 }
387 }
388 | CDF_DEFINE FEATURE as OFF {
389 auto feature = cbl_gcobol_feature_t($2);
390 if( ! cobol_gcobol_feature_set(feature, false) ) {
391 error_msg(@FEATURE,
392 "%<>>DEFINE %%EBCDIC-MODE%> is invalid within program body");
393 }
394 }
395 ;
396 cdf_constant: %empty
397 | CONSTANT
398 ;
399 override: %empty { $$ = false; }
400 | OVERRIDE { $$ = true; }
401 ;
402
403 cdf_turn: TURN except_names except_check
404 {
405 apply_cdf_turn(exception_turn);
406 exception_turn.clear();
407 }
408 ;
409
410 cdf_call_convention:
411 CALL_COBOL {
412 current_call_convention(cbl_call_cobol_e);
413 }
414 | CALL_VERBATIM {
415 current_call_convention(cbl_call_verbatim_e);
416 }
417 ;
418
419 cdf_push: CDF_PUSH cdf_stackable {
420 switch( $cdf_stackable ) {
421 case YDF_ALL: cdf_push(); break;
422 case YDF_CALL_CONVENTION: cdf_push_call_convention(); break;
423 case YDF_CDF_DEFINE: cdf_push_dictionary(); break;
424 case YDF_COBOL_WORDS: cdf_push_current_tokens(); break;
425 case YDF_SOURCE_FORMAT: cdf_push_source_format(); break;
426 default: gcc_unreachable();
427 }
428 }
429 ;
430 cdf_pop: CDF_POP cdf_stackable {
431 switch( $cdf_stackable ) {
432 case YDF_ALL: cdf_pop(); break;
433 case YDF_CALL_CONVENTION: cdf_pop_call_convention(); break;
434 case YDF_CDF_DEFINE: cdf_pop_dictionary(); break;
435 case YDF_COBOL_WORDS: cdf_pop_current_tokens(); break;
436 case YDF_SOURCE_FORMAT: cdf_pop_source_format(); break;
437 default: gcc_unreachable();
438 }
439 }
440 ;
441
442 cdf_stackable: ALL { $$ = YDF_ALL; }
443 | CALL_CONVENTION { $$ = YDF_CALL_CONVENTION; }
444 | COBOL_WORDS { $$ = YDF_COBOL_WORDS; }
445 | CDF_DEFINE { $$ = YDF_CDF_DEFINE; }
446 | SOURCE_FORMAT { $$ = YDF_SOURCE_FORMAT; }
447 ;
448
449 except_names: except_name
450 | except_names except_name
451 ;
452 except_name: EXCEPTION_NAME[ec] {
453 assert($ec != ec_none_e);
454 exception_turn.add_exception(ec_type_t($ec));
455 }
456 | EXCEPTION_NAME[ec] filenames {
457 assert($ec != ec_none_e);
458 std::list<size_t> files($filenames->begin(), $filenames->end());
459 exception_turn.add_exception(ec_type_t($ec), files);
460 }
461 ;
462
463 except_check: CHECKING on { $$ = exception_turn.enable(true); }
464 | CHECKING OFF { $$ = exception_turn.enable(false); }
465 | CHECKING on with LOCATION
466 {
467 $$ = exception_turn.enable(true, true);
468 }
469 ;
470
471 filenames: filename {
472 $$ = new std::set<size_t>;
473 $$->insert(symbol_index(symbol_elem_of($1)));
474 }
475 | filenames filename {
476 $$ = $1;
477 auto inserted = $$->insert(symbol_index(symbol_elem_of($2)));
478 if( ! inserted.second ) {
479 error_msg(@2, "%s: No file-name shall be specified more than "
480 "once for one exception condition", $filename->name);
481 }
482 }
483 ;
484 filename: NAME
485 {
486 struct symbol_elem_t *e = symbol_file(PROGRAM, $1);
487 if( !(e && e->type == SymFile) ) {
488 error_msg(@NAME, "invalid file name '%s'", $NAME);
489 YYERROR;
490 }
491 $$ = cbl_file_of(e);
492 }
493 ;
494
495 cdf_if: CDF_IF cdf_cond_expr {
496 scanner_parsing(YDF_CDF_IF, $2);
497 }
498 | CDF_IF error {
499 } CDF_END_IF { // not pushed, don't pop
500 if( ! scanner_parsing() ) YYACCEPT;
501 }
502 ;
503
504 cdf_evaluate: CDF_EVALUATE cdf_expr
505 | CDF_EVALUATE TRUE_kw
506 ;
507
508 cdf_eval_when: CDF_WHEN cdf_eval_obj
509 ;
510
511 cdf_eval_obj: cdf_cond_expr
512 | cdf_expr THRU cdf_expr
513 | OTHER
514 ;
515
516 cdf_cond_expr: BOOL
517 | NAME DEFINED
518 {
519 cdf_values_t& dictionary( cdf_dictionary() );
520 auto p = dictionary.find($1);
521 bool found = p != dictionary.end();
522 if( !$DEFINED ) found = ! found;
523 $$ = found;
524 if( found ) {
525 dbgmsg("CDF: %s found in dictionary (result %s)",
526 $1, $$? "true" : "false");
527 } else {
528 dbgmsg("CDF: %s not found in dictionary (result %s)",
529 $1, $$? "true" : "false");
530 }
531 }
532 | cdf_bool_expr { $$ = $1(@1) == 0? false : true; }
533 | FEATURE DEFINED {
534 const auto& feature($1);
535 $$ = (feature == int(feature & cbl_gcobol_features));
536 dbgmsg("CDF: feature 0x%02x is %s", $1, $$? "ON" : "OFF");
537 }
538 ;
539
540 /*
541 * "Abbreviated combined relation conditions
542 * shall not be specified."
543 */
544 cdf_bool_expr: cdf_bool_expr OR cdf_and { $$ = cdfval_t($1(@1) || $3(@3)); }
545 | cdf_and
546 ;
547
548 cdf_and: cdf_and AND cdf_reloper { $$ = cdfval_t($1(@1) && $3(@3)); }
549 | cdf_reloper
550 ;
551
552 cdf_reloper: cdf_relexpr
553 | NOT cdf_relexpr { $$ = cdfval_t($2.number? 1 : 0); }
554 ;
555
556 cdf_relexpr: cdf_relexpr '<' cdf_expr { $$ = $1(@1) < $3(@3); }
557 | cdf_relexpr LE cdf_expr { $$ = $1(@1) <= $3(@3); }
558 | cdf_relexpr '=' cdf_expr {
559 $$ = cdfval_t(false);
560 if( ( $1.string && $3.string) ||
561 (!$1.string && !$3.string) )
562 {
563 $$ = $1 == $3;
564 } else {
565 const char *msg = $1.string?
566 "incommensurate comparison is FALSE: '%s' = %ld" :
567 "incommensurate comparison is FALSE: %ld = '%s'" ;
568 error_msg(@1, "%s", msg);
569 }
570 }
571 | cdf_relexpr NE cdf_expr
572 {
573 $$ = cdfval_t(false);
574 if( ( $1.string && $3.string) ||
575 (!$1.string && !$3.string) )
576 {
577 $$ = $1 != $3;
578 } else {
579 const char *msg = $1.string?
580 "incommensurate comparison is FALSE: '%s' = %ld" :
581 "incommensurate comparison is FALSE: %ld = '%s'" ;
582 error_msg(@1, "%s", msg);
583 }
584 }
585 | cdf_relexpr GE cdf_expr { $$ = $1(@1) >= $3(@3); }
586 | cdf_relexpr '>' cdf_expr { $$ = $1(@1) > $3(@3); }
587 | cdf_expr
588 ;
589
590 cdf_expr: cdf_expr '+' cdf_expr { $$ = $1(@1) + $3(@3); }
591 | cdf_expr '-' cdf_expr { $$ = $1(@1) - $3(@3); }
592 | cdf_expr '*' cdf_expr { $$ = $1(@1) * $3(@3); }
593 | cdf_expr '/' cdf_expr { $$ = $1(@1) / $3(@3); }
594 | '+' cdf_expr %prec NEG { $$ = $2(@2); }
595 | '-' cdf_expr %prec NEG { $$ = negate($2(@2)); }
596 | '(' cdf_bool_expr ')' { $$ = $2(@2); }
597 | cdf_factor
598 ;
599
600 cdf_factor: NAME {
601 cdf_values_t& dictionary( cdf_dictionary() );
602 auto that = dictionary.find($1);
603 if( that != dictionary.end() ) {
604 $$ = that->second;
605 } else {
606 if( ! scanner_parsing() ) {
607 yywarn("CDF skipping: no such variable '%s' (ignored)", $1);
608 } else {
609 error_msg(@NAME, "CDF error: no such variable '%s'", $1);
610 }
611 $$ = cdfval_t();
612 }
613 }
614 | NUMBER { $$ = cdfval_t($1); }
615 | LITERAL { $$ = cdfval_t($1); }
616 | NUMSTR {
617 auto value = integer_literal($NUMSTR);
618 if( !value.second ) {
619 error_msg(@1, "CDF error: parsed %qs as %lld",
620 $NUMSTR, value.first);
621 YYERROR;
622 }
623 $$ = cdfval_t(value.first);
624 }
625 ;
626
627 copy: copy_impl
628 ;
629 copy_impl: copybook_name suppress REPLACING replace_bys
630 | copybook_name suppress
631 ;
632 copybook_name: COPY name_one[src]
633 {
634 if( -1 == copybook.open(@src, $src.string) ) {
635 error_msg(@src, "could not open copybook file "
636 "for '%s'", $src.string);
637 YYABORT;
638 }
639 }
640 | COPY name_one[src] IN name_one[lib]
641 {
642 copybook.library(@lib, $lib.string);
643 if( -1 == copybook.open(@src, $src.string) ) {
644 error_msg(@src, "could not open copybook file "
645 "for %<%s%> in %<%s%>", $src.string, $lib.string);
646 YYABORT;
647 }
648 }
649 ;
650
651 replace_bys: replace_by
652 | replace_bys replace_by
653 ;
654
655 replace_by: name_any[a] BY name_any[b]
656 {
657 bool add_whitespace = false;
658 replace_type_t type = {};
659 switch($a.token) {
660 case YDF_NUMSTR:
661 case YDF_LITERAL:
662 type = string_e;
663 break;
664 case YDF_NAME:
665 type = token_e;
666 break;
667 case YDF_PSEUDOTEXT:
668 type = pseudo_e;
669 add_whitespace = $b.token != YDF_PSEUDOTEXT;
670 break;
671 default:
672 cbl_err("%s:%d: logic error on token %s",
673 __FILE__, __LINE__, keyword_str($a.token));
674 break;
675 }
676 char *replacement = const_cast<char*>($b.string);
677 if( add_whitespace ) {
678 char *s = xasprintf(" %s ", replacement);
679 free(replacement);
680 replacement = s;
681 }
682 copybook.replacement( type, $a.string, replacement );
683 }
684 ;
685
686 suppress: %empty
687 | SUPPRESS
688 {
689 copybook.suppress();
690 }
691 ;
692
693 name_any: namelit
694 | PSEUDOTEXT {
695 $$ = cdf_arg_t{YDF_PSEUDOTEXT, $1};
696 }
697 ;
698
699 name_one: NAME
700 {
701 cdf_values_t& dictionary( cdf_dictionary() );
702 cdf_arg_t arg = { YDF_NAME, $1 };
703 auto p = dictionary.find($1);
704
705 if( p != dictionary.end() ) {
706 arg.string = p->second.string;
707 }
708 $$ = arg;
709 }
710 | NUMSTR { $$ = cdf_arg_t{YDF_NUMSTR, $1}; }
711 | LITERAL { $$ = cdf_arg_t{YDF_LITERAL, $1}; }
712 ;
713
714 namelit: name
715 {
716 cdf_values_t& dictionary( cdf_dictionary() );
717 cdf_arg_t arg = { YDF_NAME, $1 };
718 auto p = dictionary.find($1);
719
720 if( p != dictionary.end() ) {
721 arg.string = p->second.string;
722 }
723 $$ = arg;
724 }
725 | name subscripts
726 {
727 char *s = xasprintf( "%s%s", $1, $2 );
728 free(const_cast<char*>($1));
729 free(const_cast<char*>($2));
730
731 cdf_arg_t arg = { YDF_NAME, s };
732 $$ = arg;
733 }
734 | NUMSTR { $$ = cdf_arg_t{YDF_NUMSTR, $1}; }
735 | LITERAL { $$ = cdf_arg_t{YDF_LITERAL, $1}; }
736 ;
737
738 name: NAME
739 | name inof NAME
740 {
741 char *s = xasprintf( "%s %s %s", $1, $2, $3 );
742 assert($$ == $1);
743 free(const_cast<char*>($1));
744 free(const_cast<char*>($3));
745 $$ = s;
746 }
747 ;
748 inof: IN { static const char in[] = "IN"; $$ = in; }
749 | OF { static const char of[] = "OF"; $$ = of; }
750 ;
751
752 subscripts: subscript
753 | subscripts subscript
754 {
755 char *s = xasprintf("%s%s", $1, $2 );
756 if( $$ != $1 ) free(const_cast<char*>($$));
757 free(const_cast<char*>($1));
758 free(const_cast<char*>($2));
759 $$ = s;
760 }
761 ;
762 subscript: SUBSCRIPT
763 | LSUB subscript RSUB
764 {
765 char *s = xasprintf( "%s%s%s", $1, $2, $3 );
766 free(const_cast<char*>($1));
767 free(const_cast<char*>($2));
768 free(const_cast<char*>($3));
769 $$ = s;
770 }
771 ;
772
773 as: %empty
774 | AS
775 ;
776
777 on: %empty
778 | ON
779 ;
780
781 with: %empty
782 | WITH
783 ;
784
785 %%
786
787 static YYLTYPE cdf_location;
788
789 static YYLTYPE
790 location_set( const YYLTYPE& loc ) {
791 return cdf_location = loc;
792 }
793
794 bool // used by cobol1.cc
795 defined_cmd( const char arg[] )
796 {
797 cdf_values_t& dictionary( cdf_dictionary() );
798 cdfval_t value(1);
799
800 char *name = xstrdup(arg);
801 char *p = strchr(name, '=');
802 if(p) {
803 *p++ = '\0';
804 int pos, number;
805 if( 1 == sscanf(p, "%d%n", &number, &pos) && size_t(pos) == strlen(p) ) {
806 value = cdfval_t(number);
807 } else {
808 value = cdfval_t(p); // it's a string
809 }
810 }
811
812 dictionary[name] = value;
813
814 auto cdf_name = dictionary.find(name);
815 assert(cdf_name != dictionary.end());
816 assert(cdf_name->second.is_numeric() || cdf_name->second.string != NULL);
817
818 if( yydebug ) {
819 if( cdf_name->second.is_numeric() ) {
820 dbgmsg("%s: added -D %s = " HOST_SIZE_T_PRINT_DEC,
821 __func__, name, (fmt_size_t)cdf_name->second.as_number());
822 } else {
823 dbgmsg("%s: added -D %s = \"%s\"", __func__, name, cdf_name->second.string);
824 }
825 }
826 return true;
827 }
828
829 bool operator==( const cdfval_base_t& lhs, int rhs ) {
830 gcc_assert( !lhs.string );
831 return lhs.number == rhs;
832 }
833
834 bool operator||( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
835 gcc_assert( !lhs.string && !rhs.string );
836 return lhs.number || rhs.number;
837 }
838
839 bool operator&&( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
840 gcc_assert( !lhs.string && !rhs.string );
841 return lhs.number && rhs.number;
842 }
843
844 cdfval_t operator<( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
845 gcc_assert( !lhs.string && !rhs.string );
846 return cdfval_t(lhs.number < rhs.number);
847 }
848
849 cdfval_t operator<=( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
850 gcc_assert( !lhs.string && !rhs.string );
851 return cdfval_t(lhs.number <= rhs.number);
852 }
853
854 cdfval_t operator==( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
855 if( lhs.string && rhs.string ) {
856 return cdfval_t(0 == strcasecmp(lhs.string, rhs.string));
857 }
858 if( !lhs.string && !rhs.string ) {
859 return cdfval_t(lhs.number == rhs.number);
860 }
861 cbl_internal_error("incommensurate operands");
862 return false;
863 }
864
865 cdfval_t operator!=( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
866 if( lhs.string && rhs.string ) {
867 return cdfval_t(0 != strcasecmp(lhs.string, rhs.string));
868 }
869 if( !lhs.string && !rhs.string ) {
870 return cdfval_t(lhs.number != rhs.number);
871 }
872 cbl_internal_error("incommensurate operands");
873 return false;
874 }
875
876 cdfval_t operator>=( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
877 gcc_assert( !lhs.string && !rhs.string );
878 return cdfval_t(lhs.number >= rhs.number);
879 }
880
881 cdfval_t operator>( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
882 gcc_assert( !lhs.string && !rhs.string );
883 return cdfval_t(lhs.number > rhs.number);
884 }
885
886 cdfval_t operator+( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
887 gcc_assert( !lhs.string && !rhs.string );
888 return cdfval_t(lhs.number + rhs.number);
889 }
890
891 cdfval_t operator-( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
892 gcc_assert( !lhs.string && !rhs.string );
893 return cdfval_t(lhs.number - rhs.number);
894 }
895
896 cdfval_t operator*( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
897 gcc_assert( !lhs.string && !rhs.string );
898 return cdfval_t(lhs.number * rhs.number);
899 }
900
901 cdfval_t operator/( const cdfval_base_t& lhs, const cdfval_base_t& rhs ) {
902 gcc_assert( !lhs.string && !rhs.string );
903 return cdfval_t(lhs.number / rhs.number);
904 }
905
906 cdfval_t negate( cdfval_base_t lhs ) {
907 gcc_assert( !lhs.string );
908 lhs.number = -lhs.number;
909 return lhs;
910 }
911
912 #undef yylex
913 int yylex(void);
914
915 static int ydflex(void) {
916 return yylex();
917 }
918
919 bool
920 cdf_value( const char name[], const cdfval_t& value ) {
921 cdf_values_t& dictionary( cdf_dictionary() );
922 auto p = dictionary.find(name);
923
924 if( p != dictionary.end() ) return false;
925
926 dictionary[name] = value;
927 return true;
928 }
929
930 const cdfval_t *
931 cdf_value( const char name[] ) {
932 cdf_values_t& dictionary( cdf_dictionary() );
933 auto p = dictionary.find(name);
934
935 if( p == dictionary.end() ) return NULL;
936
937 return &p->second;
938 }
939
940 static bool
941 verify_integer( const YDFLTYPE& loc, const cdfval_base_t& val ) {
942 if( val.string ) {
943 error_msg(loc, "'%s' is not an integer", val.string);
944 return false;
945 }
946 return true;
947 }
948
949 const cdfval_base_t&
950 cdfval_base_t::operator()( const YDFLTYPE& loc ) {
951 static cdfval_t zero(0);
952 // cppcheck-suppress returnTempReference
953 return verify_integer(loc, *this) ? *this : zero;
954 }