--- parser3/src/main/compile.y 2016/09/20 12:02:22 1.284 +++ parser3/src/main/compile.y 2026/11/27 23:56:54 1.308 @@ -2,13 +2,13 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.284 2016/09/20 12:02:22 moko Exp $"; +volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.308 2026/11/27 23:56:54 moko Exp $"; /** @todo parser4: @@ -23,7 +23,6 @@ volatile const char * IDENT_COMPILE_Y = #define YYSTYPE ArrayOperation* #define YYDEBUG 1 -#define YYERROR_VERBOSE 1 #define yyerror(pc, msg) real_yyerror(pc, msg) #define YYPRINT(file, type, value) yyprint(file, type, value) #define YYMALLOC pa_malloc @@ -39,6 +38,11 @@ volatile const char * IDENT_COMPILE_Y = #include "pa_globals.h" #include "pa_vmethod_frame.h" +// Windows headers define IN +#ifdef IN +#undef IN +#endif + // defines #define CLASS_NAME "CLASS" @@ -62,21 +66,25 @@ static const VString vempty; #define POOL (*PC.pool) #ifndef DOXYGEN -#define CLASS_ADD if(PC.class_add()){ \ - strncpy(PC.error, PC.cclass->type(), MAX_STRING/2); \ - strcat(PC.error, " - class is already defined"); \ - YYERROR; \ +#define CLASS_ADD if(!PC.class_add()){ \ + PC.error=pa_strcat(PC.cclass->type(), " - class is already defined"); \ + YYERROR; \ +} + +#define YYERROR3(header, value, footer) { \ + PC.error=pa_strcat(header, value, footer); \ + YYERROR; \ } -#define PC_ERROR(header, value, footer){ \ - strcpy(PC.error, header); \ - strncat(PC.error, value, MAX_STRING/2); \ - strcat(PC.error, footer); \ +#define YYERROR1(value) { \ + PC.error=value; \ + YYERROR; \ } %} -%pure-parser +%define api.pure +%define parse.error verbose %lex-param {Parse_control* pc} %parse-param {Parse_control* pc} @@ -112,6 +120,7 @@ static const VString vempty; %token IN "in" %token FEXISTS "-f" %token DEXISTS "-d" +%token LEXISTS "-l" %token IS "is" %token LITERAL_TRUE "true" @@ -123,7 +132,7 @@ static const VString vempty; %left "&&" %left '<' '>' "<=" ">=" "lt" "gt" "le" "ge" %left "==" "!=" "eq" "ne" -%left "is" "def" "in" "-f" "-d" +%left "is" "def" "in" "-f" "-d" "-l" /* bitwise */ %left "!|" @@ -145,6 +154,7 @@ all: one_big_piece { Method* method=new Method(Method::CT_ANY, 0, 0 /*min, max numbered_params_count*/, 0 /*param_names*/, 0 /*local_names*/, $1 /*parser_code*/, 0 /*native_code*/, PC.cclass->is_vars_local()); PC.cclass->set_method(PC.alias_method(main_method_name), method); + RECORD(MAIN, &PC.alias_method(main_method_name), method); } | methods; @@ -157,56 +167,47 @@ control_method: '@' STRING '\n' maybe_control_strings { const String& command=LA2S(*$2)->trim(String::TRIM_END); YYSTYPE strings_code=$4; - if(strings_code->count()<1*OPERATIONS_PER_OPVALUE) { - PC_ERROR("@", command.cstr(), " is empty"); - YYERROR; - } + if(strings_code->count()<1*OPERATIONS_PER_OPVALUE) + YYERROR3("@", command.cstr(), " is empty"); if(command==CLASS_NAME) { if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { CLASS_ADD; // new class' name const String& name=LA2S(*strings_code)->trim(String::TRIM_END); // creating the class - VStateless_class* cclass=new VClass; + VStateless_class* cclass=new VClass(name.cstr(), PC.request.get_used_filespec(PC.file_no)); PC.cclass_new=cclass; - PC.cclass_new->set_type(name.cstr()); PC.append=false; + RECORD(CLASS, &name); } else { - strcpy(PC.error, "@" CLASS_NAME " must contain only one line with class name (contains more then one)"); - YYERROR; + YYERROR1("@" CLASS_NAME " must contain only one line with class name (contains more than one)"); } } else if(command==USE_CONTROL_METHOD_NAME) { CLASS_ADD; for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE){ - PC.request.use_file(PC.request.main_class, LA2S(*strings_code, i)->trim(String::TRIM_END), - PC.request.get_used_filename(PC.file_no), strings_code->get(i+1).origin); + const String& file_name=LA2S(*strings_code, i)->trim(String::TRIM_END); + PC.request.use_file(file_name, PC.request.get_used_filespec(PC.file_no), strings_code->get(i+1).origin); + RECORD_USE(&file_name, strings_code->get(i+1).origin); } } else if(command==BASE_NAME) { - if(PC.append){ - PC_ERROR("can't set base while appending methods to class '", PC.cclass->type(), "'"); - YYERROR; - } + if(PC.append) + YYERROR3("can't set base while appending methods to class '", PC.cclass->type(), "'"); CLASS_ADD; - if(PC.cclass->base_class()) { // already changed from default? - PC_ERROR("class already have a base '", PC.cclass->base_class()->type(), "'"); - YYERROR; - } + if(PC.cclass->base_class()) // already changed from default? + YYERROR3("class already have a base '", PC.cclass->base_class()->type(), "'"); if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { const String& base_name=LA2S(*strings_code)->trim(String::TRIM_END); if(VStateless_class *base_class=PC.request.get_class(base_name)) { // @CLASS == @BASE sanity check - if(PC.cclass==base_class) { - strcpy(PC.error, "@" CLASS_NAME " equals @" BASE_NAME); - YYERROR; - } + if(PC.cclass==base_class) + YYERROR1("@" CLASS_NAME " equals @" BASE_NAME); PC.cclass->get_class()->set_base(base_class); + RECORD(BASE, &base_name); } else { - PC_ERROR("'", base_name.cstr(), "': undefined class in @" BASE_NAME); - YYERROR; + YYERROR3("'", base_name.cstr(), "': undefined class in @" BASE_NAME); } } else { - strcpy(PC.error, "@" BASE_NAME " must contain sole name"); - YYERROR; + YYERROR1("@" BASE_NAME " must contain sole name"); } } else if(command==OPTIONS_CONTROL_METHOD_NAME) { for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE) { @@ -216,30 +217,26 @@ control_method: '@' STRING '\n' } else if(option==Symbols::PARTIAL_SYMBOL){ if(PC.cclass_new){ if(VStateless_class* existed=PC.get_existed_class(PC.cclass_new)){ - if(!PC.reuse_existed_class(existed)){ - PC_ERROR("can't append methods to '", PC.cclass_new->type(), "' - the class wasn't marked as partial"); - YYERROR; - } + if(!PC.reuse_existed_class(existed)) + YYERROR3("can't append methods to '", PC.cclass_new->type(), "' - the class wasn't marked as partial"); } else { // marks the new class as partial. we will be able to add methods here later. PC.cclass_new->set_partial(); } } else { - strcpy(PC.error, "'partial' option should be used straight after @" CLASS_NAME); - YYERROR; + YYERROR1("'partial' option should be used straight after @" CLASS_NAME); } } else if(option==Symbols::STATIC_SYMBOL){ PC.set_methods_call_type(Method::CT_STATIC); } else if(option==Symbols::DYNAMIC_SYMBOL){ PC.set_methods_call_type(Method::CT_DYNAMIC); } else { - PC_ERROR("'", option.cstr(), "' invalid option. valid options are 'partial', 'locals', 'static' and 'dynamic'"); - YYERROR; + YYERROR3("'", option.cstr(), "' invalid option. valid options are 'partial', 'locals', 'static' and 'dynamic'"); } + RECORD(OPTION, &option); } } else { - PC_ERROR("'", command.cstr(), "' invalid special name. valid names are '" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); - YYERROR; + YYERROR3("'", command.cstr(), "' invalid special name. valid names are '" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); } }; maybe_control_strings: empty | control_strings; @@ -292,6 +289,7 @@ code_method: '@' STRING bracketed_maybe_ // register in class const String& name=*LA2S(*$2); PC.cclass->set_method(PC.alias_method(name), method); + RECORD(METHOD, &PC.alias_method(name), method); }; maybe_bracketed_strings: empty | bracketed_maybe_strings; @@ -455,17 +453,27 @@ construct: construct_square: '[' { // allow $result_or_other_variable[ letters here any time ] *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; -} any_constructor_code_value { + PC.array=false; // no need to save current value as if() is right after PC.array=true; +} any_constructor_code_values { PC.explicit_result=*reinterpret_cast(&$2); } ']' { // stack: context, name - $$=$3; // stack: context, name, value - O(*$$, OP::OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */ + if(!PC.array){ + $$=$3; // stack: context, name, value + O(*$$, OP::OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */ + } else { + $$ = N(); + OA(*$$, OP::OP_CONSTRUCT_ARRAY, $3); + PC.array=false; + } } ; +any_constructor_code_values: + any_constructor_code_value { $$ = $1; } + | any_constructor_code_values ';' any_constructor_code_value { $$ = $1; P(*$$, *$3); PC.array=true; }; +; construct_round: '(' expr_value ')' { $$=N(); - O(*$$, OP::OP_PREPARE_TO_EXPRESSION); // stack: context, name P(*$$, *$2); // stack: context, name, value O(*$$, OP::OP_CONSTRUCT_EXPR); /* value=pop->as_expr_result; name=pop; context=pop; construct(context,name,value) */ @@ -608,7 +616,6 @@ store_expr_param_part: expr_value { $$=expr_code; } else { YYSTYPE code=N(); - O(*code, OP::OP_PREPARE_TO_EXPRESSION); P(*code, *expr_code); O(*code, OP::OP_WRITE_EXPR_RESULT); $$=N(); @@ -659,6 +666,7 @@ name_expr_value: | name_expr_subvar_value /* $subname_is_var_value */ | name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value */ | name_square_code_value /* [codes] */ +| name_round_expr_value /* (expr) */ ; name_expr_subvar_value: '$' subvar_ref_name_rdive { $$=$2; @@ -679,16 +687,17 @@ name_square_code_value: '[' { } codes { PC.explicit_result=*reinterpret_cast(&$2); } ']' { - $$=N(); -#ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL - if(!maybe_append_simple_diving_code(*$$, *$3)) -#endif - { + $$=N(); + if(!maybe_append_simple_diving_code(*$$, *$3)) { OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */ /* some code that writes to that context */ /* context=pop; stack: context.value() */ } }; +name_round_expr_value: '(' expr_value ')' { + $$ = N(); + P(*$$, *$2); +}; subvar_ref_name_rdive: STRING { $$=N(); O(*$$, OP::OP_WITH_READ); @@ -711,8 +720,7 @@ class_static_prefix: STRING ':' { if(VStateless_class* base=PC.cclass->base_class()) { change_string_literal_value(*$$, *new String(base->type())); } else { - strcpy(PC.error, "no base class declared"); - YYERROR; + YYERROR1("no base class declared"); } code = OP::OP_VALUE__GET_BASE_CLASS; } else { @@ -726,10 +734,8 @@ class_static_prefix: STRING ':' { }; class_constructor_prefix: class_static_prefix ':' { $$=$1; - if(!PC.in_call_value) { - strcpy(PC.error, ":: not allowed here"); - YYERROR; - } + if(!PC.in_call_value) + YYERROR1(":: not allowed here"); O(*$$, OP::OP_PREPARE_TO_CONSTRUCT_OBJECT); }; @@ -755,6 +761,7 @@ expr: | "in" expr { $$=$2; O(*$$, OP::OP_IN); } | "-f" expr { $$=$2; O(*$$, OP::OP_FEXISTS); } | "-d" expr { $$=$2; O(*$$, OP::OP_DEXISTS); } +| "-l" expr { $$=$2; O(*$$, OP::OP_LEXISTS); } /* stack: a,b // stack: a@b */ | expr '-' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_SUB); } | expr '+' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_ADD); } @@ -850,12 +857,12 @@ inline void ungetc(Parse_control& pc, ui } static int yylex(YYSTYPE *lvalp, void *apc) { - register Parse_control& pc=*static_cast(apc); + Parse_control& pc=*static_cast(apc); #define lexical_brackets_nestage pc.brackets_nestages[pc.ls_sp] #define RC {result=c; goto break2; } - register int c; + int c; int result; if(pc.pending_state) { @@ -1159,6 +1166,10 @@ default: skip_analized=1; result=DEXISTS; goto break2; + case 'l': // -l + skip_analized=1; + result=LEXISTS; + goto break2; default: // minus result=c; goto break2; @@ -1376,11 +1387,10 @@ default: goto break2; case '[': // $name.<[>code] - if(pc.pos.col>1/*not first column*/ && ( - end[-1]=='$'/*was start of get*/ || - end[-1]==':'/*was class name delim */ || - end[-1]=='.'/*was name delim */ - )) { + if( end[-1]=='$' /* was start of get */ || + end[-1]==':' /* was class name delim */ || + end[-1]=='.' /* was name delim */ + ) { push_LS(pc, LS_NAME_SQUARE_PART); lexical_brackets_nestage=1; RC; @@ -1389,7 +1399,7 @@ default: lexical_brackets_nestage=1; RC; case '{': - if(begin==end) { // ${name}, no need of EON, switching LS + if(end[-1]=='$') { // ${name}, no need of EON, switching LS, not begin==end as $[a]{$b} will fit pc.ls=LS_VAR_NAME_CURLY; } else { pc.ls=LS_VAR_CURLY; @@ -1398,6 +1408,15 @@ default: RC; case '(': + // $name.<(>expr) + if( // end[-1]=='$' /* $() excluded */ || + end[-1]==':' /* was class name delim */ || + end[-1]=='.' /* was name delim */ + ) { + push_LS(pc, LS_VAR_ROUND); + lexical_brackets_nestage=1; + RC; + } pc.ls=LS_VAR_ROUND; lexical_brackets_nestage=1; RC; @@ -1421,6 +1440,11 @@ default: push_LS(pc, LS_NAME_SQUARE_PART); lexical_brackets_nestage=1; RC; + case '(': + // ${name.<(>expr)} + push_LS(pc, LS_VAR_ROUND); + lexical_brackets_nestage=1; + RC; case '}': // ${name} finished, restoring LS pop_LS(pc); RC; @@ -1477,12 +1501,11 @@ default: case LS_METHOD_NAME: switch(c) { case '[': - // ^name.<[>code].xxx - if(pc.pos.col>1/*not first column*/ && ( - end[-1]=='^'/*was start of call*/ || // never, ^[ is literal... - end[-1]==':'/*was class name delim */ || - end[-1]=='.'/*was name delim */ - )) { + // ^name.<[>code] + if( // end[-1]=='^' /* never, ^[ is literal */ || + end[-1]==':' /* was class name delim */ || + end[-1]=='.' /* was name delim */ + ) { push_LS(pc, LS_NAME_SQUARE_PART); lexical_brackets_nestage=1; RC; @@ -1495,6 +1518,15 @@ default: lexical_brackets_nestage=1; RC; case '(': + // ^name.<(>expr) + if( // end[-1]=='^' /* never, ^( is literal */ || + end[-1]==':' /* was class name delim */ || + end[-1]=='.' /* was name delim */ + ) { + push_LS(pc, LS_VAR_ROUND); + lexical_brackets_nestage=1; + RC; + } pc.ls=LS_METHOD_ROUND; lexical_brackets_nestage=1; RC; @@ -1621,7 +1653,7 @@ break2: } static int real_yyerror(Parse_control *pc, const char *s) { // Called by yyparse on error - strncpy(PC.error, s, MAX_STRING); + PC.error=pa_strdup(s); return 1; }