| 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 |
* Flex override |
| 33 |
*/ |
| 34 |
static void /* yynoreturn */ yy_fatal_error ( const char* msg ); |
| 35 |
|
| 36 |
static void inline |
| 37 |
die_fatal_error( const char msg[] ) { |
| 38 |
cbl_internal_error("scan.o: %s", msg); |
| 39 |
yy_fatal_error(msg); |
| 40 |
} |
| 41 |
|
| 42 |
#define YY_FATAL_ERROR(msg) die_fatal_error((msg)) |
| 43 |
|
| 44 |
/* |
| 45 |
* External functions |
| 46 |
*/ |
| 47 |
|
| 48 |
void parser_enter_file(const char *filename); |
| 49 |
void parser_leave_file(); |
| 50 |
|
| 51 |
bool is_fixed_format(); |
| 52 |
bool include_debug(); |
| 53 |
int lexer_input( char buf[], int max_size, FILE *input ); |
| 54 |
|
| 55 |
const char * keyword_str( int token ); |
| 56 |
|
| 57 |
int repository_function_tok( const char name[] ); |
| 58 |
|
| 59 |
void cobol_set_indicator_column( int column ); |
| 60 |
|
| 61 |
void next_sentence_label(cbl_label_t*); |
| 62 |
|
| 63 |
int repeat_count( const char picture[] ); |
| 64 |
|
| 65 |
size_t program_level(); |
| 66 |
|
| 67 |
int ydfparse(void); |
| 68 |
|
| 69 |
FILE * copy_mode_start(); |
| 70 |
|
| 71 |
/* |
| 72 |
* Public functions and data |
| 73 |
*/ |
| 74 |
|
| 75 |
cbl_label_t *next_sentence; |
| 76 |
|
| 77 |
static bool echo_on = false; |
| 78 |
|
| 79 |
void |
| 80 |
lexer_echo( bool tf ) { |
| 81 |
echo_on = tf; |
| 82 |
} |
| 83 |
|
| 84 |
bool |
| 85 |
lexer_echo() { |
| 86 |
return echo_on; |
| 87 |
} |
| 88 |
|
| 89 |
// IBM says a picture can be up to 50 bytes, not 1000 words. |
| 90 |
// ISO says a picture can be up to 63 bytes. We allow for a NUL terminator. |
| 91 |
static char orig_picture[PICTURE_MAX]; |
| 92 |
static char orig_number[80]; |
| 93 |
|
| 94 |
const char * |
| 95 |
original_picture() { |
| 96 |
const char *out = xstrdup(orig_picture); |
| 97 |
assert(orig_picture[0] != '\0'); |
| 98 |
return out; |
| 99 |
} |
| 100 |
|
| 101 |
char * |
| 102 |
original_number( char input[] = NULL ) { |
| 103 |
if( input ) { |
| 104 |
if(sizeof(orig_number) < strlen(input) ) return NULL; |
| 105 |
strcpy(orig_number, input); |
| 106 |
return input; |
| 107 |
} |
| 108 |
char *out = xstrdup(orig_number); |
| 109 |
assert(orig_number[0] != '\0'); |
| 110 |
return out; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Local functions |
| 115 |
*/ |
| 116 |
static const char * start_condition_str( int sc ); |
| 117 |
static const char * start_condition_is(); |
| 118 |
|
| 119 |
static bool nonspace( char ch ) { return !ISSPACE(ch); } |
| 120 |
|
| 121 |
static int |
| 122 |
numstr_of( const char string[], radix_t radix = decimal_e ) { |
| 123 |
yylval.numstr.radix = radix; |
| 124 |
ydflval.string = yylval.numstr.string = xstrdup(string); |
| 125 |
char *comma = strchr(yylval.numstr.string, ','); |
| 126 |
if( comma && comma[1] == '\0' ) *comma = '\0'; |
| 127 |
if( ! original_number(yylval.numstr.string) ) { |
| 128 |
error_msg(yylloc, "input inconceivably long"); |
| 129 |
return NO_CONDITION; |
| 130 |
} |
| 131 |
|
| 132 |
const char *input = yylval.numstr.string; |
| 133 |
auto eoinput = input + strlen(input); |
| 134 |
auto p = std::find_if( input, eoinput, |
| 135 |
[]( char ch ) { return ch == 'e' || ch == 'E';} ); |
| 136 |
|
| 137 |
if( p < eoinput ) { |
| 138 |
if( eoinput == std::find(input, eoinput, symbol_decimal_point()) ) { |
| 139 |
// no decimal point: 1E0 is a valid user-defined name |
| 140 |
ydflval.string = yylval.string = yylval.numstr.string; |
| 141 |
return NAME; |
| 142 |
} |
| 143 |
assert(input < p); |
| 144 |
// "The literal to the left of the 'E' represents the significand. It may |
| 145 |
// be signed and shall include a decimal point. The significand shall be |
| 146 |
// from 1 to 36 digits in length." |
| 147 |
if( p == std::find(input, p, symbol_decimal_point()) ) { |
| 148 |
return NO_CONDITION; |
| 149 |
} |
| 150 |
auto nx = std::count_if(input, p, fisdigit); |
| 151 |
if( 36 < nx ) { |
| 152 |
error_msg(yylloc, "significand of %s has more than 36 digits (%ld)", input, (long)nx); |
| 153 |
return NO_CONDITION; |
| 154 |
} |
| 155 |
|
| 156 |
// "The literal to the right of the 'E' represents the exponent. It may be |
| 157 |
// signed and shall have a maximum of four digits and no decimal point. " |
| 158 |
// "The maximum permitted value and minimum permitted value of the |
| 159 |
// exponent is implementor-defined." (We allow 9999.) |
| 160 |
nx = std::count_if(p, eoinput, fisdigit); |
| 161 |
if( 4 < nx ) { |
| 162 |
error_msg(yylloc, "exponent %qs more than 4 digits", ++p); |
| 163 |
return NO_CONDITION; |
| 164 |
} |
| 165 |
if( eoinput != std::find(p, eoinput, symbol_decimal_point()) ) { |
| 166 |
error_msg(yylloc, "exponent %qs includes decimal point", ++p); |
| 167 |
return NO_CONDITION; |
| 168 |
} |
| 169 |
|
| 170 |
// "If all the digits in the significand are zero, then all the digits of |
| 171 |
// the exponent shall also be zero and neither significand nor exponent |
| 172 |
// shall have a negative sign." |
| 173 |
bool zero_signficand = std::all_of( input, p, |
| 174 |
[]( char ch ) { |
| 175 |
return !ISDIGIT(ch) || ch == '0'; } ); |
| 176 |
if( zero_signficand ) { |
| 177 |
if( p != std::find(input, p, '-') ) { |
| 178 |
error_msg(yylloc, "zero significand of %s " |
| 179 |
"cannot be negative", input); |
| 180 |
return NO_CONDITION; |
| 181 |
} |
| 182 |
if( eoinput != std::find(p, eoinput, '-') ) { |
| 183 |
error_msg(yylloc, "exponent of zero significand of %s " |
| 184 |
"cannot be negative", input); |
| 185 |
return NO_CONDITION; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if( 1 < std::count(input, eoinput, symbol_decimal_point()) ) { |
| 190 |
error_msg(yylloc, "invalid numeric literal %qs", ++p); |
| 191 |
return NO_CONDITION; |
| 192 |
} |
| 193 |
|
| 194 |
return NUMSTR; |
| 195 |
} |
| 196 |
|
| 197 |
static char * |
| 198 |
null_trim( char name[] ) { |
| 199 |
auto p = std::find_if( name, name + strlen(name), fisspace ); |
| 200 |
if( p < name + strlen(name) ) *p = '\0'; |
| 201 |
return name; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* CDF management |
| 206 |
*/ |
| 207 |
static int final_token; |
| 208 |
|
| 209 |
static inline const char * |
| 210 |
boolalpha( bool tf ) { return tf? "True" : "False"; } |
| 211 |
|
| 212 |
struct cdf_status_t { |
| 213 |
int lineno; |
| 214 |
const char *filename; |
| 215 |
int token; |
| 216 |
bool parsing; |
| 217 |
cdf_status_t() |
| 218 |
: lineno(yylineno), filename(cobol_filename()) |
| 219 |
, token(0), parsing(true) |
| 220 |
{} |
| 221 |
cdf_status_t( int token, bool parsing ) |
| 222 |
: lineno(yylineno), filename(cobol_filename()) |
| 223 |
, token(token), parsing(parsing) |
| 224 |
{} |
| 225 |
bool toggle() { return parsing = ! parsing; } |
| 226 |
|
| 227 |
const char * str() const { |
| 228 |
static char line[132]; |
| 229 |
snprintf(line, sizeof(line), "%s:%d: %s, parsing %s", |
| 230 |
filename, lineno, keyword_str(token), boolalpha(parsing)); |
| 231 |
return line; |
| 232 |
} |
| 233 |
static const char * as_string( const cdf_status_t& status ) { |
| 234 |
return status.str(); |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
/* |
| 239 |
* Scanning status is true if tokens are being parsed and false if not (because |
| 240 |
* CDF is skipping some code). Because CDF status is nested, status is true |
| 241 |
* only if the whole stack is true. That is, if B is stacked on A, and A is |
| 242 |
* false, then all of B is skipped, regardless of >>IF and >>ELSE for B. |
| 243 |
*/ |
| 244 |
static bool run_cdf( int token ); |
| 245 |
|
| 246 |
static class parsing_status_t : public std::stack<cdf_status_t> { |
| 247 |
typedef int (parser_t)(void); |
| 248 |
struct parsing_state_t { |
| 249 |
bool at_eof, expect_field_level; |
| 250 |
int pending_token; |
| 251 |
parser_t *parser; |
| 252 |
parsing_state_t() |
| 253 |
: at_eof(false) |
| 254 |
, expect_field_level(true) |
| 255 |
, pending_token(0) |
| 256 |
, parser(yyparse) |
| 257 |
{} |
| 258 |
} state, shadow; |
| 259 |
|
| 260 |
public: |
| 261 |
bool on() const { // true only if all true |
| 262 |
bool parsing = std::all_of( c.begin(), c.end(), |
| 263 |
[]( const auto& status ) { return status.parsing; } ); |
| 264 |
return parsing; |
| 265 |
} |
| 266 |
|
| 267 |
bool feed_a_parser() const { |
| 268 |
return on() || state.parser == ydfparse; |
| 269 |
} |
| 270 |
|
| 271 |
void need_level( bool tf ) { state.expect_field_level = tf; } |
| 272 |
bool need_level() const { return state.expect_field_level; } |
| 273 |
|
| 274 |
void parser_save( parser_t * new_parser ) { |
| 275 |
shadow = state; |
| 276 |
state.parser = new_parser; |
| 277 |
} |
| 278 |
void parser_restore() { |
| 279 |
state.parser = shadow.parser; |
| 280 |
} |
| 281 |
|
| 282 |
void inject_token( int token ) { state.pending_token = token; } |
| 283 |
int pending_token() { |
| 284 |
int token = state.pending_token; |
| 285 |
state.pending_token = 0; |
| 286 |
return token; |
| 287 |
} |
| 288 |
|
| 289 |
void at_eof( bool tf ) { state.at_eof = shadow.at_eof = tf; assert(tf); } |
| 290 |
bool at_eof() const { return state.at_eof; } |
| 291 |
|
| 292 |
bool in_cdf() const { return state.parser == ydfparse; } |
| 293 |
bool normal() const { return on() && state.parser == yyparse; } |
| 294 |
|
| 295 |
void splat() const { |
| 296 |
int i=0; |
| 297 |
for( const auto& status : c ) { |
| 298 |
yywarn( "%d %s", ++i, status.str() ); |
| 299 |
} |
| 300 |
} |
| 301 |
} parsing; |
| 302 |
|
| 303 |
// Used only by parser, so scanner_normal() obviously true. |
| 304 |
void field_done() { orig_picture[0] = '\0'; parsing.need_level(true); } |
| 305 |
|
| 306 |
static int scanner_token() { |
| 307 |
if( parsing.empty() ) { |
| 308 |
error_msg(yylloc, "%<>>ELSE%> or %<>>END-IF%> without %<>>IF%>"); |
| 309 |
return NO_CONDITION; |
| 310 |
} |
| 311 |
return parsing.top().token; |
| 312 |
} |
| 313 |
|
| 314 |
bool scanner_parsing() { return parsing.on(); } |
| 315 |
bool scanner_normal() { return parsing.normal(); } |
| 316 |
|
| 317 |
void scanner_parsing( int token, bool tf ) { |
| 318 |
parsing.push( cdf_status_t(token, tf) ); |
| 319 |
if( yydebug ) { |
| 320 |
yywarn("%s: parsing now %s, depth %zu", |
| 321 |
keyword_str(token), boolalpha(parsing.on()), parsing.size()); |
| 322 |
parsing.splat(); |
| 323 |
} |
| 324 |
} |
| 325 |
void scanner_parsing_toggle() { |
| 326 |
if( parsing.empty() ) { |
| 327 |
error_msg(yylloc, "%<>>ELSE%> without %<>>IF%>"); |
| 328 |
return; |
| 329 |
} |
| 330 |
parsing.top().toggle(); |
| 331 |
if( yydebug ) { |
| 332 |
yywarn("%s: parsing now %s", |
| 333 |
keyword_str(CDF_ELSE), boolalpha(parsing.on())); |
| 334 |
} |
| 335 |
} |
| 336 |
void scanner_parsing_pop() { |
| 337 |
if( parsing.empty() ) { |
| 338 |
error_msg(yylloc, "%<>>END-IF%> without %<>>IF%>"); |
| 339 |
return; |
| 340 |
} |
| 341 |
parsing.pop(); |
| 342 |
if( yydebug ) { |
| 343 |
yywarn("%s: parsing now %s, depth %zu", |
| 344 |
keyword_str(CDF_END_IF), boolalpha(parsing.on()), |
| 345 |
parsing.size()); |
| 346 |
parsing.splat(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
static bool level_needed() { |
| 352 |
return scanner_normal() && parsing.need_level(); |
| 353 |
} |
| 354 |
|
| 355 |
static void level_found() { |
| 356 |
if( scanner_normal() ) parsing.need_level(false); |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* Trim the scanned location by the amount about to re-scanned. |
| 361 |
* Must be a macro because it expands yyless. |
| 362 |
*/ |
| 363 |
#define myless(N) \ |
| 364 |
do { \ |
| 365 |
auto n(N); \ |
| 366 |
trim_location(n); \ |
| 367 |
yyless(n); \ |
| 368 |
} while(0) |
| 369 |
|
| 370 |
class enter_leave_t { |
| 371 |
typedef void( parser_enter_file_f)(const char *filename); |
| 372 |
typedef void (parser_leave_file_f)(); |
| 373 |
parser_enter_file_f *entering; |
| 374 |
parser_leave_file_f *leaving; |
| 375 |
const char *filename; |
| 376 |
|
| 377 |
public: |
| 378 |
enter_leave_t() : entering(NULL), leaving(NULL), filename(NULL) {} |
| 379 |
enter_leave_t( parser_enter_file_f *entering, const char *filename ) |
| 380 |
: entering(entering), leaving(NULL), filename(filename) |
| 381 |
{} |
| 382 |
explicit enter_leave_t(parser_leave_file_f *leaving) |
| 383 |
: entering(NULL), leaving(leaving), filename(NULL) {} |
| 384 |
|
| 385 |
void notify() { |
| 386 |
if( entering ) { |
| 387 |
cobol_filename(filename, 0); |
| 388 |
if( yy_flex_debug ) dbgmsg("starting line %4d of %s", |
| 389 |
yylineno, filename); |
| 390 |
entering(filename); |
| 391 |
gcc_assert(leaving == NULL); |
| 392 |
} |
| 393 |
if( leaving ) { |
| 394 |
cobol_filename_restore(); |
| 395 |
if( yy_flex_debug ) dbgmsg("resuming line %4d of %s", |
| 396 |
yylineno, cobol_filename()); |
| 397 |
leaving(); |
| 398 |
gcc_assert(entering == NULL); |
| 399 |
} |
| 400 |
} |
| 401 |
}; |
| 402 |
|
| 403 |
static class input_file_status_t { |
| 404 |
std::queue <enter_leave_t> inputs; |
| 405 |
public: |
| 406 |
void enter(const char *filename) { |
| 407 |
inputs.push( enter_leave_t(parser_enter_file, filename) ); |
| 408 |
} |
| 409 |
void leave() { |
| 410 |
inputs.push( enter_leave_t(parser_leave_file) ); |
| 411 |
} |
| 412 |
void notify() { |
| 413 |
while( ! inputs.empty() ) { |
| 414 |
auto enter_leave = inputs.front(); |
| 415 |
enter_leave.notify(); |
| 416 |
inputs.pop(); |
| 417 |
} |
| 418 |
} |
| 419 |
} input_file_status; |
| 420 |
|
| 421 |
void input_file_status_notify() { input_file_status.notify(); } |
| 422 |
|
| 423 |
/* |
| 424 |
* parse.y and cdf.y each define a 4-integer struct to hold a token's location. |
| 425 |
* parse.y uses YYLTYPE yylloc; |
| 426 |
* cdf.y uses YDFLLTYPE ydflloc; |
| 427 |
* |
| 428 |
* The structs have identical definitions with different types and of course |
| 429 |
* names. We define "conversion" between them for convenience. |
| 430 |
* |
| 431 |
* Each parser expects its location value to be updated whenever it calls |
| 432 |
* yylex(). Therefore, here in the lexer we set both locations as each token |
| 433 |
* is scanned, so that both parsers see the same location. |
| 434 |
*/ |
| 435 |
static YDFLTYPE |
| 436 |
ydfltype_of( const YYLTYPE& loc ) { |
| 437 |
YDFLTYPE output { |
| 438 |
loc.first_line, loc.first_column, |
| 439 |
loc.last_line, loc.last_column }; |
| 440 |
return output; |
| 441 |
} |
| 442 |
|
| 443 |
/* |
| 444 |
* After the input filename and yylineno are set, update the location of the |
| 445 |
* scanned token. |
| 446 |
*/ |
| 447 |
static void |
| 448 |
update_location( const YYLTYPE *ploc = nullptr ) { |
| 449 |
YYLTYPE loc = { |
| 450 |
yylloc.last_line, yylloc.last_column, |
| 451 |
yylineno, yylloc.last_column + yyleng |
| 452 |
}; |
| 453 |
if( ploc ) loc = *ploc; |
| 454 |
|
| 455 |
const char *p = static_cast<char*>(memrchr(yytext, '\n', yyleng)); |
| 456 |
if( p ) { |
| 457 |
loc.last_column = (yytext + yyleng) - p; |
| 458 |
} |
| 459 |
|
| 460 |
yylloc = loc; |
| 461 |
ydflloc = ydfltype_of(yylloc); |
| 462 |
|
| 463 |
dbgmsg(" SC: %s location (%d,%d) to (%d,%d)", |
| 464 |
start_condition_is(), |
| 465 |
yylloc.first_line, yylloc.first_column, |
| 466 |
yylloc.last_line, yylloc.last_column); |
| 467 |
} |
| 468 |
|
| 469 |
static void |
| 470 |
reset_location() { |
| 471 |
static const YYLTYPE loc { yylineno, 1, yylineno, 1 }; |
| 472 |
update_location(&loc); |
| 473 |
} |
| 474 |
|
| 475 |
#define YY_USER_ACTION update_location(); |
| 476 |
|
| 477 |
static void |
| 478 |
trim_location( int nkeep) { |
| 479 |
gcc_assert( 0 <= nkeep && nkeep <= yyleng ); |
| 480 |
struct { char *p, *pend; |
| 481 |
size_t size() const { return pend - p; } |
| 482 |
} rescan = { yytext + nkeep, yytext + yyleng }; |
| 483 |
|
| 484 |
auto nline = std::count(rescan.p, rescan.pend, '\n'); |
| 485 |
dbgmsg("%s:%d: yyless(%d), rescan '%.*s' (" HOST_SIZE_T_PRINT_UNSIGNED |
| 486 |
" lines, " HOST_SIZE_T_PRINT_UNSIGNED " bytes)", |
| 487 |
__func__, __LINE__, |
| 488 |
nkeep, |
| 489 |
int(rescan.size()), rescan.p, |
| 490 |
(fmt_size_t)nline, (fmt_size_t)rescan.size()); |
| 491 |
if( nline ) { |
| 492 |
gcc_assert( yylloc.first_line + nline <= yylloc.last_line ); |
| 493 |
yylloc.last_line -= int(nline); |
| 494 |
gcc_assert( yylloc.first_line <= yylloc.last_line ); |
| 495 |
char *p = static_cast<char*>(memrchr(rescan.p, '\n', rescan.size())); |
| 496 |
yylloc.last_column = rescan.pend - ++p; |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
gcc_assert( int(rescan.size()) < yylloc.last_column ); |
| 501 |
yylloc.last_column -= rescan.size(); |
| 502 |
if( yylloc.last_column < yylloc.first_column ) { |
| 503 |
yylloc.first_column = 1; |
| 504 |
} |
| 505 |
|
| 506 |
location_dump(__func__, __LINE__, "yylloc", yylloc); |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
update_location_col( const char str[], int correction = 0) { |
| 511 |
auto col = yylloc.last_column - strlen(str) + correction; |
| 512 |
if( col > 0 ) { |
| 513 |
yylloc.first_column = col; |
| 514 |
} |
| 515 |
location_dump(__func__, __LINE__, "yylloc", yylloc); |
| 516 |
} |
| 517 |
|
| 518 |
#define not_implemented(...) cbl_unimplemented_at(yylloc, __VA_ARGS__) |
| 519 |
|
| 520 |
#define YY_USER_INIT do { \ |
| 521 |
static YYLTYPE ones = {1,1, 1,1}; \ |
| 522 |
yylloc = ones; \ |
| 523 |
ydflloc = ydfltype_of(yylloc); \ |
| 524 |
} while(0) |
| 525 |
|
| 526 |
/* |
| 527 |
* YY_DECL is the generated lexer. The parser calls yylex(). yylex() invokes |
| 528 |
* next_token(), which calls this lexer function. The Flex-generated code |
| 529 |
* updates neither yylval nor yylloc. That job is left to the actions. |
| 530 |
* |
| 531 |
* The parser relies on yylex to set yylval and yylloc each time it is |
| 532 |
* called. It maintains a separate copy for each term, and uses |
| 533 |
* YYLLOC_DEFAULT() to update the location of nonterminals. |
| 534 |
*/ |
| 535 |
#define YY_DECL int lexer(void) |
| 536 |
|
| 537 |
# define YY_INPUT(buf, result, max_size) \ |
| 538 |
{ \ |
| 539 |
if( 0 == (result = lexer_input(buf, max_size, yyin)) ) \ |
| 540 |
result = YY_NULL; \ |
| 541 |
} |
| 542 |
|
| 543 |
#define bcomputable(T, C) \ |
| 544 |
yylval.computational.type=T, \ |
| 545 |
yylval.computational.capacity=C, \ |
| 546 |
yylval.computational.signable=true, BINARY_INTEGER |
| 547 |
#define scomputable(T, C) \ |
| 548 |
yylval.computational.type=T, \ |
| 549 |
yylval.computational.capacity=C, \ |
| 550 |
yylval.computational.signable=true, COMPUTATIONAL |
| 551 |
#define ucomputable(T, C) \ |
| 552 |
yylval.computational.type=T, \ |
| 553 |
yylval.computational.capacity=C, \ |
| 554 |
yylval.computational.signable=false, COMPUTATIONAL |
| 555 |
|
| 556 |
static char *tmpstring = NULL; |
| 557 |
|
| 558 |
#define PROGRAM current_program_index() |
| 559 |
|
| 560 |
// map of alias => canonical |
| 561 |
static std::map <std::string, std::string> keyword_aliases; |
| 562 |
|
| 563 |
const std::string& |
| 564 |
keyword_alias_add( const std::string& keyword, const std::string& alias ) { |
| 565 |
auto p = keyword_aliases.find(alias); |
| 566 |
if( p != keyword_aliases.end() ) return p->second; // error: do not overwrite |
| 567 |
return keyword_aliases[alias] = keyword; |
| 568 |
} |
| 569 |
|
| 570 |
/* |
| 571 |
* Because numeric USAGE types don't have distinct tokens and may have aliases, |
| 572 |
* we keep a table of their canonical names, which we use if we encounter an |
| 573 |
* alias. |
| 574 |
*/ |
| 575 |
struct bint_t { |
| 576 |
int token; |
| 577 |
cbl_field_type_t type; |
| 578 |
uint32_t capacity; |
| 579 |
bool signable; |
| 580 |
}; |
| 581 |
static const std::map <std::string, bint_t > binary_integers { |
| 582 |
{ "COMP-X", { COMPUTATIONAL, FldNumericBin5, 0xFF, false } }, |
| 583 |
{ "COMP-6", { COMPUTATIONAL, FldPacked, 0, false } }, |
| 584 |
{ "COMP-5", { COMPUTATIONAL, FldNumericBin5, 0, false } }, |
| 585 |
{ "COMP-4", { COMPUTATIONAL, FldNumericBinary, 0, true } }, |
| 586 |
{ "COMP-2", { COMPUTATIONAL, FldFloat, 8, false } }, |
| 587 |
{ "COMP-1", { COMPUTATIONAL, FldFloat, 4, false } }, |
| 588 |
{ "COMP", { COMPUTATIONAL, FldNumericBinary, 0, false } }, |
| 589 |
{ "COMPUTATIONAL-X", { COMPUTATIONAL, FldNumericBin5, 0xFF, false } }, |
| 590 |
{ "COMPUTATIONAL-6", { COMPUTATIONAL, FldPacked, 0, false } }, |
| 591 |
{ "COMPUTATIONAL-5", { COMPUTATIONAL, FldNumericBin5, 0, false } }, |
| 592 |
{ "COMPUTATIONAL-4", { COMPUTATIONAL, FldNumericBinary, 0, true } }, |
| 593 |
{ "COMPUTATIONAL-2", { COMPUTATIONAL, FldFloat, 8, false } }, |
| 594 |
{ "COMPUTATIONAL-1", { COMPUTATIONAL, FldFloat, 4, false } }, |
| 595 |
{ "COMPUTATIONAL", { COMPUTATIONAL, FldNumericBinary, 0, false } }, |
| 596 |
{ "BINARY", { BINARY_INTEGER, FldNumericBinary, 0, true } }, |
| 597 |
{ "BINARY-CHAR", { BINARY_INTEGER, FldNumericBin5, 1, true } }, |
| 598 |
{ "BINARY-SHORT", { BINARY_INTEGER, FldNumericBin5, 2, true } }, |
| 599 |
{ "BINARY-LONG", { BINARY_INTEGER, FldNumericBin5, 4, true } }, |
| 600 |
{ "BINARY-DOUBLE", { BINARY_INTEGER, FldNumericBin5, 8, true } }, |
| 601 |
{ "BINARY-LONG-LONG", { BINARY_INTEGER, FldNumericBin5, 8, true } }, |
| 602 |
{ "FLOAT-BINARY-32", { COMPUTATIONAL, FldFloat, 4, false } }, |
| 603 |
{ "FLOAT-BINARY-64", { COMPUTATIONAL, FldFloat, 8, false } }, |
| 604 |
{ "FLOAT-BINARY-128", { COMPUTATIONAL, FldFloat, 16, false } }, |
| 605 |
{ "FLOAT-EXTENDED", { COMPUTATIONAL, FldFloat, 16, false } }, |
| 606 |
{ "FLOAT-LONG", { COMPUTATIONAL, FldFloat, 8, false } }, |
| 607 |
{ "FLOAT-SHORT", { COMPUTATIONAL, FldFloat, 4, false } }, |
| 608 |
}; |
| 609 |
|
| 610 |
static int |
| 611 |
binary_integer_usage( const char name[]) { |
| 612 |
cbl_name_t uname = {}; |
| 613 |
std::transform(name, name + strlen(name), uname, ftoupper); |
| 614 |
|
| 615 |
dbgmsg("%s:%d: checking %s in %zu keyword_aliases", |
| 616 |
__func__, __LINE__, uname, |
| 617 |
keyword_aliases.size() ); |
| 618 |
|
| 619 |
std::string key = uname; |
| 620 |
auto alias = keyword_aliases.find(key); |
| 621 |
if( alias != keyword_aliases.end() ) key = alias->second; |
| 622 |
|
| 623 |
auto p = binary_integers.find(key); |
| 624 |
if( p == binary_integers.end() ) return 0; |
| 625 |
|
| 626 |
yylval.computational.type = p->second.type; |
| 627 |
yylval.computational.capacity = p->second.capacity; |
| 628 |
yylval.computational.signable = p->second.signable; |
| 629 |
dbgmsg("%s:%d: %s has type %d", __func__, __LINE__, |
| 630 |
uname, p->second.type ); |
| 631 |
return p->second.token; |
| 632 |
} |
| 633 |
|
| 634 |
static void |
| 635 |
verify_ws( const YYLTYPE& loc, const char input[], char ch ) { |
| 636 |
if( ! fisspace(ch) ) { |
| 637 |
if( ! (dialect_mf() || dialect_gnu()) ) { |
| 638 |
dialect_error(loc, "separator space required in %qs", input); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
#define verify_ws(C) verify_ws(yylloc, yytext, C) |
| 643 |
|
| 644 |
int |
| 645 |
binary_integer_usage_of( const char name[] ) { |
| 646 |
cbl_name_t uname = {}; |
| 647 |
std::transform(name, name + strlen(name), uname, ftoupper); |
| 648 |
|
| 649 |
auto p = binary_integers.find(uname); |
| 650 |
if( p != binary_integers.end() ) { |
| 651 |
int token = p->second.token; |
| 652 |
switch( token ) { |
| 653 |
case COMPUTATIONAL: |
| 654 |
case BINARY_INTEGER: |
| 655 |
return token; |
| 656 |
default: |
| 657 |
gcc_unreachable(); |
| 658 |
assert(false); |
| 659 |
} |
| 660 |
} |
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
static uint32_t |
| 665 |
level_of( const char input[] ) { |
| 666 |
unsigned int output = 0; |
| 667 |
|
| 668 |
if( input[0] == '0' ) input++; |
| 669 |
|
| 670 |
if( 1 != sscanf(input, "%u", &output) ) { |
| 671 |
yywarn( "%s:%d: invalid level '%s'", __func__, __LINE__, input ); |
| 672 |
} |
| 673 |
|
| 674 |
return output; |
| 675 |
} |
| 676 |
|
| 677 |
static inline int |
| 678 |
ndigit(int len) { |
| 679 |
char *input = TOUPPER(yytext[0]) == 'V'? yytext + 1 : yytext; |
| 680 |
int n = repeat_count(input); |
| 681 |
return n == -1? len : n; |
| 682 |
} |
| 683 |
|
| 684 |
static int |
| 685 |
picset( int token ) { |
| 686 |
static const char * const eop = orig_picture + sizeof(orig_picture); |
| 687 |
char *p = orig_picture + strlen(orig_picture); |
| 688 |
|
| 689 |
if( eop < p + yyleng ) { |
| 690 |
error_msg(yylloc, "PICTURE exceeds maximum size of %zu bytes", |
| 691 |
sizeof(orig_picture) - 1); |
| 692 |
} |
| 693 |
snprintf( p, eop - p, "%s", yytext ); |
| 694 |
return token; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
## Script and data to produce picture_t::followers. |
| 699 |
## Based on ISO Table 10. |
| 700 |
#! /usr/bin/awk -f |
| 701 |
|
| 702 |
BEGIN { |
| 703 |
str = "B0/ , . + +- +- CR/DB cs cs Z* Z* + + cs cs 9 AX S V P P 1 N E" |
| 704 |
split(str, cols) |
| 705 |
} |
| 706 |
|
| 707 |
$1 ~ /CR|DB|cs/ { next } |
| 708 |
|
| 709 |
0 && !nlines++ { |
| 710 |
for( i=0; i < length(cols); i++ ) { |
| 711 |
print i, cols[i], "'" $i "'" |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
$field == "x" { |
| 716 |
if( ! nout++ ) { |
| 717 |
printf "%2d: %5s: \"", field, cols[field - 1] |
| 718 |
} |
| 719 |
|
| 720 |
gsub(/^ +| +$/, "", $1) |
| 721 |
printf "%s", $1 |
| 722 |
} |
| 723 |
|
| 724 |
END { |
| 725 |
if( ! nout++ ) { |
| 726 |
printf "%2d: %5s: \"", field, cols[field - 1] |
| 727 |
} |
| 728 |
print "\"" |
| 729 |
} |
| 730 |
|
| 731 |
B x x x - x - - x - x x x x x x x x - x - x - x |
| 732 |
0 x x x - x - - x - x x x x x x x x - x - x - x |
| 733 |
/ x x x - x - - x - x x x x x x x x - x - x - x |
| 734 |
, x x x - x - - x - x x x x x x x - - x - x |
| 735 |
. x x - - x - - x - x - x - x - x |
| 736 |
+ - - - - - - - - - - - - - - - - - - - - - - - x |
| 737 |
+ |
| 738 |
– |
| 739 |
+ x x x - - - - x x x x - - x x x - - x x x |
| 740 |
CR x x x - - - - x x x x - - x x x - - x x x |
| 741 |
DB x x x - - - - x x x x - - x x x - - x x x |
| 742 |
cs - - - - x |
| 743 |
cs x x x - x - - - - x x - - - - x - - x x x |
| 744 |
|
| 745 |
Z x x - - x - - x - x |
| 746 |
* x x - - x - - x - x |
| 747 |
Z x x x - x - - x - x x - - - - - - - x - x |
| 748 |
* x x x - x - - x - x x - - - - - - - x - x |
| 749 |
+ x x - - - - - x - - - x |
| 750 |
– x x - - - - - x - - - x |
| 751 |
+ x x x - - - - x - - - x x - - - - - x |
| 752 |
– x x x - - - - x - - - x x - - - - - x |
| 753 |
cs x x - - x - - - - - - - - x |
| 754 |
cs x x x - x - - - - - - - - x x - - - x |
| 755 |
|
| 756 |
9 x x x x x - - x - x - x - x - x x x x - x - - x |
| 757 |
A x - - - - - - - - - - - - - - x x |
| 758 |
X x - - - - - - - - - - - - - - x x |
| 759 |
S |
| 760 |
V x x - - x - - x - x - x - x - x - x - x |
| 761 |
P x x - - x - - x - x - x - x - x - x - x |
| 762 |
P - - - - x - - x - - - - - - - - - x x - x |
| 763 |
1 - - - - - - - - - - - - - - - - - - - - - x |
| 764 |
N x - - - - - - - - - - - - - - - - - - - - - x |
| 765 |
E x x x - x - - - - - - - - - - x |
| 766 |
**/ |
| 767 |
|
| 768 |
class picture_t { |
| 769 |
static const char dot = '.', comma = ','; |
| 770 |
|
| 771 |
typedef std::vector<std::string> followings_t; |
| 772 |
static const std::map <char, followings_t> followers; |
| 773 |
|
| 774 |
const char * const begin; |
| 775 |
const char *p, *pend; |
| 776 |
size_t pos; |
| 777 |
struct exclusions_t { // Nonzero if set, > 1 is false. |
| 778 |
// crdb means CR/DB or +/-. |
| 779 |
// pluses means 2 or more consecutive '+'. |
| 780 |
// minuses means 2 or more consecutive '-'. |
| 781 |
// "21) The symbol 'Z' and the symbol '*' are mutually exclusive " |
| 782 |
// stars means '*' or Z. |
| 783 |
unsigned short int crdb, currency, dot, pluses, minuses, stars, zzz; |
| 784 |
exclusions_t() |
| 785 |
: crdb(0), currency(0), dot(0), pluses(0), minuses(0), stars(0) |
| 786 |
{} |
| 787 |
} exclusions; |
| 788 |
YYLTYPE loc; |
| 789 |
|
| 790 |
bool is_crdb() const { // input must be uppercase for CR/DB |
| 791 |
if( p[0] == 'C' || p[0] == 'D' ) { |
| 792 |
char input[3] = { p[0], p[1] }; |
| 793 |
return ( 0 == strcmp(input, "CR") || 0 == strcmp(input, "DB") ); |
| 794 |
} |
| 795 |
return false; |
| 796 |
} |
| 797 |
|
| 798 |
const char * match_paren( const char *paren ) const { |
| 799 |
gcc_assert(paren[0] == '('); // start with opening paren |
| 800 |
paren = std::find_if( paren, pend, |
| 801 |
[]( char ch ) { |
| 802 |
return ch == '(' || ch == ')'; |
| 803 |
} ); |
| 804 |
if( *paren == '(' ) return nullptr; // no nesting |
| 805 |
if( paren == pend ) return nullptr; |
| 806 |
return ++paren; |
| 807 |
} |
| 808 |
|
| 809 |
const char * next_not( char ch ) const { |
| 810 |
return std::find_if( p, pend, |
| 811 |
[ch = TOUPPER(ch)]( char next ) { |
| 812 |
return ch != next; |
| 813 |
} ); |
| 814 |
} |
| 815 |
|
| 816 |
const char * valid_next( const char *p, const std::string& valid ) const { |
| 817 |
if( p == pend || p + 1 == pend ) return pend; |
| 818 |
if( p[1] == '(' ) { |
| 819 |
return match_paren(++p); |
| 820 |
} |
| 821 |
auto pv = std::find(valid.begin(), valid.end(), TOUPPER(p[1])); |
| 822 |
return pv != valid.end()? ++p : nullptr; |
| 823 |
} |
| 824 |
const char * valid_next( const char *p, |
| 825 |
bool first = true, char ch = '\0' ) const { |
| 826 |
if( p == pend || p + 1 == pend ) return pend; |
| 827 |
if( p[0] == '(' ) { |
| 828 |
if( (p = match_paren(p)) == nullptr ) return nullptr; |
| 829 |
} |
| 830 |
if( p[0] == '(' ) return nullptr; // consecutive parentheses |
| 831 |
|
| 832 |
int index = first? 0 : 1; |
| 833 |
if( !ch ) ch = *p; // use current character unless overridden |
| 834 |
auto valid = followers.find(TOUPPER(ch)); |
| 835 |
if( valid == followers.end() ) { |
| 836 |
YYLTYPE loc(yylloc); |
| 837 |
loc.first_column += int(p - begin); |
| 838 |
error_msg( loc, "PICTURE: strange character %qc, giving up", ch ); |
| 839 |
return nullptr; |
| 840 |
} |
| 841 |
return valid_next(p, valid->second[index]); |
| 842 |
} |
| 843 |
|
| 844 |
const char * start() { // start modifies exclusions, but not p |
| 845 |
auto pnext = p; |
| 846 |
|
| 847 |
switch(TOUPPER(p[0])) { |
| 848 |
case comma: case dot: |
| 849 |
// use decimal_is_comma() |
| 850 |
// 4: .: "B0/,+Z*+-9E" |
| 851 |
exclusions.dot++; |
| 852 |
pnext = valid_next(p, "B0/,+Z*+-9E"); |
| 853 |
break; |
| 854 |
case '+': case '-': |
| 855 |
// 6: +-: "B0/,.Z*Z*9VPPE" |
| 856 |
exclusions.crdb++; |
| 857 |
pnext = next_not(p[0]); |
| 858 |
if( p + 1 < pnext ) { |
| 859 |
exclusions.pluses++; |
| 860 |
} |
| 861 |
pnext = valid_next(--pnext, "B0/,.Z*Z*9VPPE"); |
| 862 |
break; |
| 863 |
case 'Z': case '*': |
| 864 |
exclusions.stars++; |
| 865 |
pnext = next_not(p[0]); |
| 866 |
break; |
| 867 |
case 'S': |
| 868 |
// 19: S: "9VP" |
| 869 |
pnext = valid_next(p, "9VP"); |
| 870 |
break; |
| 871 |
} |
| 872 |
|
| 873 |
/* |
| 874 |
* "For fixed editing sign control, the currency symbol, when used, shall |
| 875 |
* be either the leftmost symbol in character-string-1, optionally preceded |
| 876 |
* by one of the symbols '+' or '-' " |
| 877 |
*/ |
| 878 |
if( pnext ) { |
| 879 |
if( p == pnext || p[0] == '+' || p[0] == '-' ) { |
| 880 |
if( symbol_currency(*pnext) ) { |
| 881 |
exclusions.currency++; |
| 882 |
pnext = next_not(*pnext); |
| 883 |
pnext = valid_next(--pnext, true, '$'); |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
return pnext; |
| 889 |
} |
| 890 |
|
| 891 |
const char * next() { // modify state; do not modify position |
| 892 |
auto pnext = p; |
| 893 |
auto loc(picture_t::loc); |
| 894 |
loc.first_column += int(p - begin); |
| 895 |
|
| 896 |
if( is_crdb() ) { |
| 897 |
if( exclusions.crdb++ ) { |
| 898 |
error_msg( loc, "PICTURE: CR/DB and %c/%c may appear only once", '+', '-' ); |
| 899 |
return nullptr; |
| 900 |
} |
| 901 |
if( p + 2 != pend ) { |
| 902 |
error_msg( loc, "PICTURE: CR/DB must appear at the end" ); |
| 903 |
return nullptr; |
| 904 |
} |
| 905 |
return pend; |
| 906 |
} |
| 907 |
|
| 908 |
if( symbol_currency(p[0]) ) { |
| 909 |
if( false && exclusions.currency++ ) { // not enforced |
| 910 |
error_msg( loc, "PICTURE: CURRENCY SYMBOL sequence may appear at most once" ); |
| 911 |
return nullptr; |
| 912 |
} |
| 913 |
return valid_next(p, ! exclusions.dot, '$'); |
| 914 |
} |
| 915 |
|
| 916 |
switch(TOUPPER(p[0])) { |
| 917 |
case '(': |
| 918 |
return match_paren(p); |
| 919 |
break; |
| 920 |
case 'B': case '0': case '/': |
| 921 |
pnext = valid_next(p); |
| 922 |
break; |
| 923 |
case comma: |
| 924 |
if( decimal_is_comma() ) { |
| 925 |
if( exclusions.dot++ ) { |
| 926 |
error_msg( loc, "PICTURE: %qc: may appear at most once", p[0] ); |
| 927 |
return nullptr; |
| 928 |
} |
| 929 |
pnext = valid_next(p, true, dot); |
| 930 |
} else { |
| 931 |
pnext = valid_next(p); |
| 932 |
} |
| 933 |
break; |
| 934 |
case dot: |
| 935 |
if( p + 1 == pend ) { |
| 936 |
pnext = pend; |
| 937 |
} else { |
| 938 |
if( decimal_is_comma() ) { |
| 939 |
pnext = valid_next(p, true, comma ); |
| 940 |
} else { |
| 941 |
if( exclusions.dot++ ) { |
| 942 |
error_msg( loc, "PICTURE: %qc: may appear at most once", p[0] ); |
| 943 |
return nullptr; |
| 944 |
} |
| 945 |
pnext = valid_next(p); |
| 946 |
} |
| 947 |
} |
| 948 |
break; |
| 949 |
|
| 950 |
case '+': case '-': |
| 951 |
// 7 is trailing sign; 13 & 14 are numeric. Leading sign handled by start(). |
| 952 |
if( p + 1 == pend ) { |
| 953 |
if( exclusions.crdb++ ) { |
| 954 |
error_msg( loc, "PICTURE: %c/%c may appear at most once as a sign", '+', '-' ); |
| 955 |
return nullptr; |
| 956 |
} |
| 957 |
pnext = pend; |
| 958 |
} else { |
| 959 |
pnext = next_not(p[0]); |
| 960 |
if( p + 1 < pnext ) { |
| 961 |
if( false && exclusions.pluses++ ) { // not enforced |
| 962 |
error_msg( loc, "PICTURE: %qc: sequence may appear at most once", p[0] ); |
| 963 |
return nullptr; |
| 964 |
} |
| 965 |
} |
| 966 |
pnext = valid_next(pnext, ! exclusions.dot); |
| 967 |
} |
| 968 |
break; |
| 969 |
|
| 970 |
case 'Z': case '*': |
| 971 |
if( false && exclusions.stars++ ) { // not enforced |
| 972 |
error_msg( loc, "PICTURE: %qc: sequence may appear at most once", p[0] ); |
| 973 |
return nullptr; |
| 974 |
} |
| 975 |
if( (pnext = next_not(p[0])) == nullptr ) return pnext; |
| 976 |
pnext = valid_next(pnext, ! exclusions.dot); |
| 977 |
break; |
| 978 |
case 'P': |
| 979 |
pnext = valid_next(pnext, ! exclusions.dot); |
| 980 |
break; |
| 981 |
case '9': |
| 982 |
case 'A': case 'X': |
| 983 |
case 'V': |
| 984 |
case '1': |
| 985 |
case 'N': |
| 986 |
pnext = valid_next(p); |
| 987 |
break; |
| 988 |
case 'E': |
| 989 |
pnext = valid_next(p, "+9"); |
| 990 |
if( pnext && *pnext == '+' ) { |
| 991 |
pnext = valid_next(p, "9"); |
| 992 |
} |
| 993 |
break; |
| 994 |
default: |
| 995 |
error_msg( loc, "PICTURE: %qc: invalid character", p[0] ); |
| 996 |
return nullptr; |
| 997 |
} |
| 998 |
return pnext; |
| 999 |
} |
| 1000 |
|
| 1001 |
public: |
| 1002 |
picture_t( const char *p, int len ) |
| 1003 |
: begin(p) |
| 1004 |
, p(p), pend(p + len) |
| 1005 |
, loc(yylloc) |
| 1006 |
{ |
| 1007 |
assert(TOUPPER(*p) == 'P'); // as in PICTURE (or PICTURE IS) |
| 1008 |
// move p to start of picture string |
| 1009 |
while( (p = std::find_if(p, pend, fisspace)) != pend ) { |
| 1010 |
this->p = p = std::find_if(p, pend, |
| 1011 |
[]( char ch ) { return ! fisspace(ch); } ); |
| 1012 |
} |
| 1013 |
assert(this->p != pend); |
| 1014 |
pos = this->p - begin; |
| 1015 |
} |
| 1016 |
|
| 1017 |
bool is_valid() { |
| 1018 |
if( !p ) return false; |
| 1019 |
if( (p = start()) == nullptr ) { |
| 1020 |
return false; |
| 1021 |
} |
| 1022 |
|
| 1023 |
while( p && p < pend) { |
| 1024 |
p = next(); |
| 1025 |
} |
| 1026 |
return p == pend; |
| 1027 |
} |
| 1028 |
|
| 1029 |
int starts_at() const { return pos; } |
| 1030 |
}; |
| 1031 |
|
| 1032 |
/* |
| 1033 |
* The Followers map gives 1 or 2 lists of valid characters following a |
| 1034 |
* character, the one in the key. If there are two lists, the correct one is |
| 1035 |
* determined by the caller based on the state of the picture string, i.e., |
| 1036 |
* what has been seen before. |
| 1037 |
*/ |
| 1038 |
const std::map <char, picture_t::followings_t> picture_t::followers { |
| 1039 |
/* B0/ */ { 'B', {"B0/,.Z*+-9AXVPNE" } }, |
| 1040 |
/* B0/ */ { '0', {"B0/,.Z*+-9AXVPNE" } }, |
| 1041 |
/* B0/ */ { '/', {"B0/,.Z*+-9AXVPNE" } }, |
| 1042 |
/* , */ { ',', {"B0/,.Z*+-9VPE"} }, |
| 1043 |
/* . */ { '.', {"B0/,Z*+-9E"} }, |
| 1044 |
/* + { '+', "9" }, */ |
| 1045 |
/* +- */ { '+', {"B0/,.Z*9VPE", "" } }, |
| 1046 |
/* +- */ { '-', {"B0/,.Z*9VPE", "" } }, |
| 1047 |
/* CR/DB { 'C', "" }, */ |
| 1048 |
/* cs { 'c', "B0/,.Z*+-9VP" }, */ |
| 1049 |
/* cs { 'c', "+" }, */ |
| 1050 |
/* Z* */ { 'Z', {"B0/,.+Z*9VP", "B0/,+Z*"} }, |
| 1051 |
/* Z* */ { '*', {"B0/,.+Z*9VP", "B0/,+Z*"} }, |
| 1052 |
/* + */ { '+', {"B0/,.+-9VP", "B0/,+-"} }, |
| 1053 |
/* cs */ { '$', {"B0/,.+9VP", "B0/,+"} }, |
| 1054 |
/* 9 */ { '9', {"B0/,.+9AXVPE"} }, |
| 1055 |
/* AX */ { 'A', {"B0/9AX"} }, |
| 1056 |
/* AX */ { 'X', {"B0/9AX"} }, |
| 1057 |
/* S */ { 'S', {"9VP"} }, |
| 1058 |
/* V */ { 'V', {"B0/,+Z*+-9P"} }, |
| 1059 |
/* P */ { 'P', {"+VP", "B0/,+Z*9P"} }, |
| 1060 |
/* 1 */ { '1', {"1"} }, |
| 1061 |
/* N */ { 'N', {"B0/N"} }, |
| 1062 |
/* E */ { 'E', {"+9"} }, |
| 1063 |
}; |
| 1064 |
|
| 1065 |
/* |
| 1066 |
* Although picture_t::is_valid return a bool, it's not used. The validation |
| 1067 |
* routines emit messages where the error is detected. The entire string is |
| 1068 |
* subsequently parsed by the parser, which might otherwise accept an invalid |
| 1069 |
* string, but will usually emit a message of its own. |
| 1070 |
*/ |
| 1071 |
static int |
| 1072 |
validate_picture() { |
| 1073 |
picture_t picture(yytext, yyleng); |
| 1074 |
picture.is_valid(); |
| 1075 |
return picture.starts_at(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
static inline bool |
| 1079 |
is_integer_token( int *pvalue = NULL ) { |
| 1080 |
int v, n = 0; |
| 1081 |
if( pvalue == NULL ) pvalue = &v; |
| 1082 |
return 1 == sscanf(yytext, "%d%n", pvalue, &n) && n == yyleng; |
| 1083 |
} |
| 1084 |
|
| 1085 |
static bool need_nume = false; |
| 1086 |
bool need_nume_set( bool tf ) { |
| 1087 |
dbgmsg( "need_nume now %s", tf? "true" : "false" ); |
| 1088 |
return need_nume = tf; |
| 1089 |
} |
| 1090 |
|
| 1091 |
static int datetime_format_of( const char input[] ); |
| 1092 |
|
| 1093 |
static int symbol_function_token( const char name[] ) { |
| 1094 |
const auto e = symbol_function( 0, name ); |
| 1095 |
return e ? symbol_index(e) : 0; |
| 1096 |
} |
| 1097 |
|
| 1098 |
bool in_procedure_division(void ); |
| 1099 |
|
| 1100 |
static symbol_elem_t * |
| 1101 |
symbol_exists( const char name[] ) { |
| 1102 |
typedef std::map <std::string, size_t> name_cache_t; |
| 1103 |
static std::map <size_t, name_cache_t> cachemap; |
| 1104 |
|
| 1105 |
cbl_name_t lname; |
| 1106 |
std::transform( name, name + strlen(name) + 1, lname, tolower ); |
| 1107 |
auto& cache = cachemap[PROGRAM]; |
| 1108 |
|
| 1109 |
if( in_procedure_division() && cache.empty() ) { |
| 1110 |
for( auto e = symbols_begin(PROGRAM) + 1; |
| 1111 |
PROGRAM == e->program && e < symbols_end(); e++ ) { |
| 1112 |
if( e->type == SymFile ) { |
| 1113 |
cbl_file_t *f(cbl_file_of(e)); |
| 1114 |
cbl_name_t lname; |
| 1115 |
std::transform( f->name, f->name + strlen(f->name) + 1, lname, tolower ); |
| 1116 |
cache[lname] = symbol_index(e); |
| 1117 |
continue; |
| 1118 |
} |
| 1119 |
if( e->type == SymField ) { |
| 1120 |
auto f(cbl_field_of(e)); |
| 1121 |
cbl_name_t lname; |
| 1122 |
std::transform( f->name, f->name + strlen(f->name) + 1, lname, tolower ); |
| 1123 |
cache[lname] = symbol_index(e); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
cache.erase(""); |
| 1127 |
} |
| 1128 |
auto p = cache.find(lname); |
| 1129 |
|
| 1130 |
if( p == cache.end() ) { |
| 1131 |
symbol_elem_t * e = symbol_field( PROGRAM, 0, name ); |
| 1132 |
return e; |
| 1133 |
} |
| 1134 |
|
| 1135 |
return symbol_at(p->second); |
| 1136 |
} |
| 1137 |
|
| 1138 |
static int |
| 1139 |
typed_name( const char name[] ) { |
| 1140 |
if( 0 == PROGRAM ) return NAME; |
| 1141 |
if( need_nume ) { need_nume_set(false); return NUME; } |
| 1142 |
|
| 1143 |
int token = repository_function_tok(name); |
| 1144 |
switch(token) { |
| 1145 |
case 0: |
| 1146 |
break; |
| 1147 |
case FUNCTION_UDF_0: |
| 1148 |
yylval.number = symbol_function_token(name); |
| 1149 |
__attribute__((fallthrough)); |
| 1150 |
default: |
| 1151 |
return token; |
| 1152 |
} |
| 1153 |
|
| 1154 |
struct symbol_elem_t *e = symbol_special( PROGRAM, name ); |
| 1155 |
if( e ) return cbl_special_name_of(e)->token; |
| 1156 |
|
| 1157 |
if( (token = redefined_token(name)) ) { return token; } |
| 1158 |
|
| 1159 |
e = symbol_exists( name ); |
| 1160 |
|
| 1161 |
auto type = e && e->type == SymField? cbl_field_of(e)->type : FldInvalid; |
| 1162 |
|
| 1163 |
switch(type) { |
| 1164 |
case FldLiteralA: |
| 1165 |
{ |
| 1166 |
auto f = cbl_field_of(e); |
| 1167 |
if( is_constant(f) ) { |
| 1168 |
if( f->data.initial ) { |
| 1169 |
int token = cbl_figconst_tok(f->data.initial); |
| 1170 |
if( token ) return token; |
| 1171 |
} |
| 1172 |
int token = datetime_format_of(f->data.initial); |
| 1173 |
if( token ) { |
| 1174 |
yylval.string = xstrdup(f->data.initial); |
| 1175 |
return token; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
__attribute__((fallthrough)); |
| 1180 |
case FldLiteralN: |
| 1181 |
{ |
| 1182 |
const auto f = cbl_field_of(e); |
| 1183 |
if( type == FldLiteralN ) { |
| 1184 |
yylval.numstr.radix = |
| 1185 |
f->has_attr(hex_encoded_e)? hexadecimal_e : decimal_e; |
| 1186 |
yylval.numstr.string = xstrdup(f->data.initial); |
| 1187 |
return NUMSTR; |
| 1188 |
} |
| 1189 |
if( !f->has_attr(record_key_e) ) { // not a key-name literal |
| 1190 |
yylval.literal.set(f); |
| 1191 |
ydflval.string = yylval.literal.data; |
| 1192 |
return LITERAL; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
__attribute__((fallthrough)); |
| 1196 |
case FldInvalid: |
| 1197 |
case FldGroup: |
| 1198 |
case FldForward: |
| 1199 |
case FldIndex: |
| 1200 |
case FldAlphanumeric: |
| 1201 |
case FldPacked: |
| 1202 |
case FldNumericDisplay: |
| 1203 |
case FldNumericEdited: |
| 1204 |
case FldAlphaEdited: |
| 1205 |
case FldNumericBinary: |
| 1206 |
case FldFloat: |
| 1207 |
case FldNumericBin5: |
| 1208 |
case FldPointer: |
| 1209 |
return NAME; |
| 1210 |
case FldSwitch: |
| 1211 |
return SWITCH; |
| 1212 |
case FldClass: |
| 1213 |
return cbl_field_of(e)->level == 88? NAME88 : CLASS_NAME; |
| 1214 |
break; |
| 1215 |
default: |
| 1216 |
yywarn("%s:%d: invalid symbol type %s for symbol %qs", |
| 1217 |
__func__, __LINE__, cbl_field_type_str(type), name); |
| 1218 |
return NAME; |
| 1219 |
} |
| 1220 |
return cbl_field_of(e)->level == 88? NAME88 : NAME; |
| 1221 |
} |
| 1222 |
|
| 1223 |
int |
| 1224 |
retype_name_token() { |
| 1225 |
return typed_name(ydflval.string); |
| 1226 |
} |
| 1227 |
|
| 1228 |
static char * |
| 1229 |
tmpstring_append( int len ) { |
| 1230 |
const char *extant = tmpstring == NULL ? "" : tmpstring; |
| 1231 |
char *s = xasprintf("%s%.*s", extant, len, yytext); |
| 1232 |
free(tmpstring); |
| 1233 |
return tmpstring = s; |
| 1234 |
} |
| 1235 |
|
| 1236 |
#define pop_return yy_pop_state(); return |
| 1237 |
|
| 1238 |
static bool is_not = false; |
| 1239 |
|
| 1240 |
static uint64_t |
| 1241 |
integer_of( const char input[], bool is_hex = false) { |
| 1242 |
uint64_t output = 0; |
| 1243 |
const char *fmt = is_hex? "%ul" : "%hl"; |
| 1244 |
|
| 1245 |
if( input[0] == '0' ) input++; |
| 1246 |
|
| 1247 |
if( 1 != sscanf(input, fmt, &output) ) { |
| 1248 |
yywarn( "%s:%d: invalid integer '%s'", __func__, __LINE__, input ); |
| 1249 |
} |
| 1250 |
|
| 1251 |
return output; |
| 1252 |
} |