--- parser3/src/main/compile.y 2016/11/27 23:08:28 1.288 +++ parser3/src/main/compile.y 2026/04/23 18:50:11 1.302 @@ -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-2024 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.288 2016/11/27 23:08:28 moko Exp $"; +volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.302 2026/04/23 18:50:11 moko Exp $"; /** @todo parser4: @@ -62,16 +62,19 @@ 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 PC_ERROR(header, value, footer){ \ - strcpy(PC.error, header); \ - strncat(PC.error, value, MAX_STRING/2); \ - strcat(PC.error, footer); \ +#define YYERROR3(header, value, footer) { \ + PC.error=pa_strcat(header, value, footer); \ + YYERROR; \ +} + +#define YYERROR1(value) { \ + PC.error=value; \ + YYERROR; \ } %} @@ -157,10 +160,8 @@ 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; @@ -171,8 +172,7 @@ control_method: '@' STRING '\n' PC.cclass_new=cclass; PC.append=false; } 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; @@ -180,31 +180,23 @@ control_method: '@' STRING '\n' PC.request.use_file(LA2S(*strings_code, i)->trim(String::TRIM_END), PC.request.get_used_filespec(PC.file_no), 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); } 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) { @@ -214,30 +206,25 @@ 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'"); } } } 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; @@ -453,14 +440,25 @@ 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(); // stack: context, name @@ -655,6 +653,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; @@ -675,16 +674,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); @@ -707,8 +707,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 { @@ -722,10 +721,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); }; @@ -846,12 +843,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) { @@ -1372,11 +1369,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; @@ -1385,7 +1381,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; @@ -1394,6 +1390,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; @@ -1417,6 +1422,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; @@ -1473,12 +1483,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; @@ -1491,6 +1500,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; @@ -1617,7 +1635,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; }