--- parser3/src/main/compile.y 2021/11/09 20:55:21 1.293 +++ 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-2020 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.293 2021/11/09 20:55:21 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,7 +66,7 @@ static const VString vempty; #define POOL (*PC.pool) #ifndef DOXYGEN -#define CLASS_ADD if(PC.class_add()){ \ +#define CLASS_ADD if(!PC.class_add()){ \ PC.error=pa_strcat(PC.cclass->type(), " - class is already defined"); \ YYERROR; \ } @@ -79,7 +83,8 @@ static const VString vempty; %} -%pure-parser +%define api.pure +%define parse.error verbose %lex-param {Parse_control* pc} %parse-param {Parse_control* pc} @@ -115,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" @@ -126,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 "!|" @@ -148,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; @@ -171,13 +178,16 @@ control_method: '@' STRING '\n' VStateless_class* cclass=new VClass(name.cstr(), PC.request.get_used_filespec(PC.file_no)); PC.cclass_new=cclass; PC.append=false; + RECORD(CLASS, &name); } else { - YYERROR1("@" CLASS_NAME " must contain only one line with class name (contains more then one)"); + 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(LA2S(*strings_code, i)->trim(String::TRIM_END), PC.request.get_used_filespec(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) @@ -192,6 +202,7 @@ control_method: '@' STRING '\n' if(PC.cclass==base_class) YYERROR1("@" CLASS_NAME " equals @" BASE_NAME); PC.cclass->get_class()->set_base(base_class); + RECORD(BASE, &base_name); } else { YYERROR3("'", base_name.cstr(), "': undefined class in @" BASE_NAME); } @@ -222,6 +233,7 @@ control_method: '@' STRING '\n' } else { YYERROR3("'", option.cstr(), "' invalid option. valid options are 'partial', 'locals', 'static' and 'dynamic'"); } + RECORD(OPTION, &option); } } else { YYERROR3("'", command.cstr(), "' invalid special name. valid names are '" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); @@ -277,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; @@ -440,14 +453,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 @@ -642,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; @@ -662,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); @@ -735,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); } @@ -830,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) { @@ -1139,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; @@ -1356,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; @@ -1369,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; @@ -1378,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; @@ -1401,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; @@ -1457,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; @@ -1475,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;