Debian Codegraph @

gcc/cobol/scan_post.h

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 static const char *
32 start_condition_str( int sc ) {
33 const char *state = "???";
34 switch(sc) {
35 case INITIAL: state = "INITIAL"; break;
36 case addr_of: state = "addr_of"; break;
37 case basis: state = "basis"; break;
38 case bool_state: state = "bool_state"; break;
39 case cdf_state: state = "cdf_state"; break;
40 case classify: state = "classify"; break;
41 case cobol_words: state = "cobol_words"; break;
42 case comment_entries: state = "comment_entries"; break;
43 case copy_state: state = "copy_state"; break;
44 case date_state: state = "date_state"; break;
45 case datetime_fmt: state = "datetime_fmt"; break;
46 case dot_state: state = "dot_state"; break;
47 case exception: state = "exception"; break;
48 case field_level: state = "field_level"; break;
49 case field_state: state = "field_state"; break;
50 case function: state = "function"; break;
51 case hex_state: state = "hex_state"; break;
52 case ident_state: state = "ident_state"; break;
53 case integer_count: state = "integer_count"; break;
54 case name_state: state = "name_state"; break;
55 case numeric_state: state = "numeric_state"; break;
56 case numstr_state: state = "numstr_state"; break;
57 case partial_name: state = "partial_name"; break;
58 case picture: state = "picture"; break;
59 case picture_count: state = "picture_count"; break;
60 case procedure_div: state = "procedure_div"; break;
61 case program_id_state: state = "program_id_state"; break;
62 case quoted1: state = "quoted1"; break;
63 case quoted2: state = "quoted2"; break;
64 case quoteq: state = "quoteq"; break;
65 case raising: state = "raising"; break;
66 case sort_state: state = "sort_state"; break;
67 case subscripts: state = "subscripts"; break;
68 }
69 return state;
70 }
71
72 static const char *
73 start_condition_is() { return start_condition_str( YY_START ); }
74
75 /*
76 * Match datetime constants.
77 *
78 * A 78 or CONSTANT could have a special literal for formatted
79 * date/time functions.
80 */
81
82 static int
83 datetime_format_of( const char input[] ) {
84
85 static const char date_fmt_b[] = "YYYYMMDD|YYYYDDD|YYYYWwwD";
86 static const char date_fmt_e[] = "YYYY-MM-DD|YYYY-DDD|YYYY-Www-D";
87
88 static const char time_fmt_b[] =
89 "hhmmss([.,]s+)?|hhmmss([.,]s+)?Z|hhmmss([.,]s+)?[+]hhmm|";
90 static const char time_fmt_e[] =
91 "hh:mm:ss([.,]s+)?|hh:mm:ss([.,]s+)?Z|hh:mm:ss([.,]s+)?[+]hh:mm";
92
93 static char date_pattern[ 3 * sizeof(date_fmt_e) ];
94 static char time_pattern[ 3 * sizeof(time_fmt_e) ];
95 static char datetime_pattern[ 6 * sizeof(time_fmt_e) ];
96
97 static struct pattern_t {
98 regex_t re;
99 const char *regex;
100 int token;
101 } patterns[] = {
102 { {}, datetime_pattern, DATETIME_FMT },
103 { {}, date_pattern, DATE_FMT },
104 { {}, time_pattern, TIME_FMT },
105 }, * eopatterns = patterns + COUNT_OF(patterns);;
106
107 // compile patterns
108 if( ! date_pattern[0] ) {
109 sprintf(date_pattern, "%s|%s", date_fmt_b, date_fmt_e);
110 sprintf(time_pattern, "%s|%s", time_fmt_b, time_fmt_e);
111
112 sprintf(datetime_pattern, "(%sT%s)|(%sT%s)",
113 date_fmt_b, time_fmt_b,
114 date_fmt_e, time_fmt_e);
115
116 for( auto p = patterns; p < eopatterns; p++ ) {
117 static const int cflags = REG_EXTENDED | REG_ICASE;
118 int erc;
119
120 if( 0 != (erc = regcomp(&p->re, p->regex, cflags)) ) {
121 static char msg[80];
122 regerror(erc, &p->re, msg, sizeof(msg));
123 yywarn("%s:%d: %s: %s", __func__, __LINE__, keyword_str(p->token), msg);
124 }
125 }
126 }
127
128 // applies only in the datetime_fmt start condition
129 if( datetime_fmt == YY_START ) {
130 yy_pop_state();
131 if( input == NULL ) return 0;
132
133 // See if the input is a date, time, or datetime pattern string.
134 static const int nmatch = 3;
135 regmatch_t matches[nmatch];
136
137 auto p = std::find_if( patterns, eopatterns,
138 [input, &matches]( auto& pattern ) {
139 auto erc = regexec( &pattern.re, input,
140 COUNT_OF(matches), matches, 0 );
141 return erc == 0;
142 } );
143
144 return p != eopatterns? p->token : 0;
145 }
146 return 0;
147 }
148
149
150 /*
151 * >>DEFINE, >>IF, and >>EVALUATE
152 */
153
154 static bool
155 is_cdf_token( int token ) {
156 switch(token) {
157 case CDF_DEFINE:
158 case CDF_DISPLAY:
159 case CDF_IF: case CDF_ELSE: case CDF_END_IF:
160 case CDF_EVALUATE: case CDF_WHEN: case CDF_END_EVALUATE:
161 case CDF_PUSH:
162 case CDF_POP:
163 return true;
164 case CALL_COBOL:
165 case CALL_VERBATIM:
166 case COPY:
167 case TURN:
168 return true;
169 }
170 return false;
171 }
172
173 static bool
174 is_cdf_condition_token( int token ) {
175 switch(token) {
176 case CDF_IF: case CDF_ELSE: case CDF_END_IF:
177 case CDF_EVALUATE: case CDF_WHEN: case CDF_END_EVALUATE:
178 return true;
179 }
180 return false;
181 }
182
183 /*
184 * IF and EVALUATE are partially parsed in cdf.y. ELSE and WHEN, etc., are
185 * valid only in context.
186 */
187 static bool
188 valid_conditional_context( int token ) {
189 switch(token) {
190 case CDF_DEFINE:
191 case CDF_IF:
192 case CDF_EVALUATE:
193 return true;
194 case CDF_ELSE:
195 case CDF_END_IF:
196 return scanner_token() == CDF_IF;
197 case CDF_WHEN:
198 case CDF_END_EVALUATE:
199 return scanner_token() == CDF_EVALUATE;
200 }
201 return true; // all other CDF tokens valid regardless of context
202 }
203
204 static bool
205 run_cdf( int token ) {
206 if( ! valid_conditional_context(token) ) {
207 error_msg(yylloc, "CDF syntax error at '%s'", keyword_str(token));
208 return false;
209 }
210
211 parsing.inject_token(token); // because it will be needed by CDF parser
212
213 if( yy_flex_debug ) dbgmsg("CDF parser start with '%s'", keyword_str(token));
214
215 parsing.parser_save(ydfparse);
216
217 int erc = ydfparse(); // Parse the CDF directive.
218
219 parsing.parser_restore();
220
221 if( YY_START == cdf_state ) yy_pop_state();
222
223 if( yy_flex_debug ) {
224 dbgmsg("CDF parser returned %d, scanner SC <%s>", erc, start_condition_is());
225 }
226
227 return 0 == erc;
228 }
229
230 #include <queue>
231 struct pending_token_t {
232 int token;
233 YYSTYPE value;
234 pending_token_t( int token, YYSTYPE value ) : token(token), value(value) {}
235 };
236 #define PENDING(T) pending_token_t( (T), yylval )
237
238 static std::queue<pending_token_t> pending_tokens;
239
240 int next_token() {
241 int token = lexer();
242 return token;
243 }
244
245 extern int ydfchar;
246 bool in_procedure_division(void);
247
248 // act on CDF tokens
249 int
250 prelex() {
251 static bool in_cdf = false;
252 int token = next_token();
253
254 if( in_cdf ) { return token; }
255 if( ! is_cdf_token(token) ) { return token; }
256
257 in_cdf = true;
258
259 assert(is_cdf_token(token));
260
261 while( is_cdf_token(token) ) {
262
263 if( ! run_cdf(token) ) {
264 dbgmsg( ">>CDF parser failed, ydfchar %d", ydfchar );
265 }
266 // Return the CDF's discarded lookahead token, if extant.
267 token = ydfchar > 0? ydfchar : next_token();
268 if( token == NO_CONDITION && parsing.at_eof() ) {
269 return YYEOF;
270 }
271
272 // Reenter cdf parser only if next token could affect parsing state.
273 if( ! parsing.on() && ! is_cdf_condition_token(token) ) break;
274 }
275
276 if( yy_flex_debug ) {
277 dbgmsg("scanner SC <%s>", start_condition_is());
278 }
279
280 if( YY_START == copy_state || YY_START == cdf_state ) {
281 if( token == NAME ) {
282 auto tok = keyword_tok(ydflval.string);
283 if( tok ) token = tok;
284 }
285 yy_pop_state();
286 dbgmsg("scanner SC <%s>, token now %s",
287 start_condition_is(), keyword_str(token));
288 }
289
290 /*
291 * The final, rejected CDF token might be a LEVEL number.
292 */
293 if( YY_START == field_state && level_needed() ) {
294 switch( token ) {
295 case NUMSTR:
296 if( yy_flex_debug ) yywarn("final token is NUMSTR");
297 yylval.number = level_of(yylval.numstr.string);
298 token = LEVEL;
299 break;
300 case YDF_NUMBER:
301 if( yy_flex_debug ) yywarn("final token is %<YDF_NUMBER%>");
302 yylval.number = ydflval.number;
303 token = LEVEL;
304 break;
305 }
306 if( token == LEVEL ) {
307 switch(yylval.number) {
308 case 66:
309 token = LEVEL66;
310 break;
311 case 78:
312 token = LEVEL78;
313 break;
314 case 88:
315 token = LEVEL78;
316 break;
317 }
318 }
319 }
320
321 dbgmsg( ">>CDF parser done, %s returning "
322 "%s (because final_token %s, lookhead %d) on line %d", __func__,
323 keyword_str(token), keyword_str(final_token),
324 ydfchar, yylineno );
325 in_cdf = false;
326 return token;
327 }
328
329 /* There are 2 parsers and one scanner.
330 * yyparse calls yylex.
331 * yylex calls prelex
332 * prelex calls lexer, the scanner produced by flex.
333 * lexer reads input from yyin via lexer_input.
334 *
335 * prelex intercepts CDF statements, each of which it parses with ydfparse.
336 * ydfparse affects CDF variables, which may affect how yylex treats
337 * the input stream.
338 *
339 * Because the lexer is called recursively:
340 *
341 * yyparse -> yylex -> ydfparse -> yylex
342 *
343 * the global state of the scanner has changed when ydfparse returns. Part of
344 * that state is the unused lookahead token that ydfparse discarded, stored in
345 * final_token. prelex then returns final_token as its own, which is duly
346 * returned to yyparse.
347 */
348
349 int
350 yylex(void) {
351 static bool produce_next_sentence_target = false;
352 int token = parsing.pending_token();
353
354 if( parsing.at_eof() ) return YYEOF;
355 if( token ) return token;
356
357 /*
358 * NEXT SENTENCE jumps to an implied CONTINUE at the next dot ('.').
359 * Documentation says variously that the implied CONTINUE is before or after
360 * that dot, but the meaning is one: after the statement that precedes the
361 * dot.
362 *
363 * When the lexer encounters the dot, it returns it to the parser, which may
364 * use it as a look-ahead token to decide the grammar production. By the
365 * time it returns to the lexer looking for its next token, the parser will
366 * have taken whatever actions the dot decided. At that point, the lexer
367 * injects the label that NEXT SENTENCE jumps to.
368 */
369 if( produce_next_sentence_target ) {
370 next_sentence_label(next_sentence);
371 produce_next_sentence_target = false;
372 }
373
374 do {
375 token = prelex();
376 if( yy_flex_debug ) {
377 if( parsing.in_cdf() ) {
378 dbgmsg( "%s:%d: <%s> routing %s to CDF parser", __func__, __LINE__,
379 start_condition_is(), keyword_str(token) );
380 } else if( !parsing.on() ) {
381 dbgmsg( "eating %s because conditional compilation is FALSE",
382 keyword_str(token) );
383 }
384 }
385
386 } while( token && ! parsing.feed_a_parser() );
387
388 if( next_sentence && token == '.' ) {
389 produce_next_sentence_target = true;
390 }
391
392 if( parsing.normal() ) {
393 final_token = token;
394 }
395
396 if( token == YYEOF && parsing.in_cdf() ) {
397 if( yy_flex_debug) dbgmsg("deflecting EOF");
398 parsing.at_eof(true);
399 return NO_CONDITION;
400 }
401
402 return token;
403 }