--- parser3/src/main/compile.y 2015/10/21 21:45:25 1.273 +++ parser3/src/main/compile.y 2016/05/25 11:35:15 1.281 @@ -2,13 +2,13 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) */ -volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.273 2015/10/21 21:45:25 moko Exp $"; +volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.281 2016/05/25 11:35:15 moko Exp $"; /** @todo parser4: @@ -41,11 +41,9 @@ volatile const char * IDENT_COMPILE_Y = // defines +#define CLASS_NAME "CLASS" #define USE_CONTROL_METHOD_NAME "USE" #define OPTIONS_CONTROL_METHOD_NAME "OPTIONS" -#define OPTION_ALL_VARS_LOCAL_NAME "locals" -#define OPTION_PARTIAL_CLASS "partial" -#define REM_OPERATOR_NAME "rem" // forwards @@ -64,10 +62,16 @@ static const VString vempty; #define POOL (*PC.pool) #ifndef DOXYGEN -#define CLASS_ADD if(PC.class_add()){ \ - strncpy(PC.error, PC.cclass->name().cstr(), MAX_STRING/2); \ - strcat(PC.error, " - class is already defined"); \ - YYERROR; \ +#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 PC_ERROR(header, value, footer){ \ + strcpy(PC.error, header); \ + strncat(PC.error, value, MAX_STRING/2); \ + strcat(PC.error, footer); \ } %} @@ -84,7 +88,6 @@ static const VString vempty; %token BAD_HEX_LITERAL %token BAD_METHOD_DECL_START %token BAD_METHOD_PARAMETER_NAME_CHARACTER -%token BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE %token LAND "&&" %token LOR "||" @@ -140,10 +143,7 @@ static const VString vempty; %% 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*/); + 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->set_method(PC.alias_method(main_method_name), method); } | methods; @@ -158,9 +158,7 @@ control_method: '@' STRING '\n' const String& command=LA2S(*$2)->trim(String::TRIM_END); YYSTYPE strings_code=$4; if(strings_code->count()<1*OPERATIONS_PER_OPVALUE) { - strcpy(PC.error, "@"); - strcat(PC.error, command.cstr()); - strcat(PC.error, " is empty"); + PC_ERROR("@", command.cstr(), " is empty"); YYERROR; } if(command==CLASS_NAME) { @@ -171,7 +169,7 @@ control_method: '@' STRING '\n' // creating the class VStateless_class* cclass=new VClass; PC.cclass_new=cclass; - PC.cclass_new->set_name(name); + PC.cclass_new->set_type(name.cstr()); PC.append=false; } else { strcpy(PC.error, "@" CLASS_NAME " must contain only one line with class name (contains more then one)"); @@ -185,16 +183,12 @@ control_method: '@' STRING '\n' } } else if(command==BASE_NAME) { if(PC.append){ - strcpy(PC.error, "can't set base while appending methods to class '"); - strncat(PC.error, PC.cclass->name().cstr(), MAX_STRING/2); - strcat(PC.error, "'"); + PC_ERROR("can't set base while appending methods to class '", PC.cclass->type(), "'"); YYERROR; } CLASS_ADD; if(PC.cclass->base_class()) { // already changed from default? - strcpy(PC.error, "class already have a base '"); - strncat(PC.error, PC.cclass->base_class()->name().cstr(), MAX_STRING/2); - strcat(PC.error, "'"); + PC_ERROR("class already have a base '", PC.cclass->base_class()->type(), "'"); YYERROR; } if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { @@ -208,13 +202,11 @@ control_method: '@' STRING '\n' } PC.cclass->get_class()->set_base(base_class); } else { // they asked to derive from a class without methods ['env' & co] - strcpy(PC.error, base_name.cstr()); - strcat(PC.error, ": you can not derive from this class in @" BASE_NAME); + PC_ERROR("'", base_name.cstr(), "': you can not derive from this class in @" BASE_NAME); YYERROR; } } else { - strcpy(PC.error, base_name.cstr()); - strcat(PC.error, ": undefined class in @" BASE_NAME); + PC_ERROR("'", base_name.cstr(), "': undefined class in @" BASE_NAME); YYERROR; } } else { @@ -224,15 +216,13 @@ control_method: '@' STRING '\n' } else if(command==OPTIONS_CONTROL_METHOD_NAME) { for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE) { const String& option=LA2S(*strings_code, i)->trim(String::TRIM_END); - if(option==OPTION_ALL_VARS_LOCAL_NAME){ + if(option==Symbols::LOCALS_SYMBOL){ PC.set_all_vars_local(); - } else if(option==OPTION_PARTIAL_CLASS){ + } 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)){ - strcpy(PC.error, "can't append methods to '"); - strncat(PC.error, PC.cclass_new->name().cstr(), MAX_STRING/2); - strcat(PC.error, "' - the class wasn't marked as partial"); + PC_ERROR("can't append methods to '", PC.cclass_new->type(), "' - the class wasn't marked as partial"); YYERROR; } } else { @@ -240,28 +230,20 @@ control_method: '@' STRING '\n' PC.cclass_new->set_partial(); } } else { - strcpy(PC.error, "'" OPTION_PARTIAL_CLASS "' option should be used straight after @" CLASS_NAME); + strcpy(PC.error, "'partial' option should be used straight after @" CLASS_NAME); YYERROR; } - } else if(option==method_call_type_static){ + } else if(option==Symbols::STATIC_SYMBOL){ PC.set_methods_call_type(Method::CT_STATIC); - } else if(option==method_call_type_dynamic){ + } else if(option==Symbols::DYNAMIC_SYMBOL){ PC.set_methods_call_type(Method::CT_DYNAMIC); } else { - strcpy(PC.error, "'"); - strncat(PC.error, option.cstr(), MAX_STRING/2); - strcat(PC.error, "' invalid option. valid options are " - "'" OPTION_PARTIAL_CLASS "', '" OPTION_ALL_VARS_LOCAL_NAME "'" - ", '" METHOD_CALL_TYPE_STATIC "' and '" METHOD_CALL_TYPE_DYNAMIC "'" - ); + PC_ERROR("'", option.cstr(), "' invalid option. valid options are 'partial', 'locals', 'static' and 'dynamic'"); YYERROR; } } } else { - strcpy(PC.error, "'"); - strncat(PC.error, command.cstr(), MAX_STRING/2); - strcat(PC.error, "' invalid special name. valid names are " - "'" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); + PC_ERROR("'", command.cstr(), "' invalid special name. valid names are '" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); YYERROR; } }; @@ -289,9 +271,9 @@ code_method: '@' STRING bracketed_maybe_ locals_names=new ArrayString; for(int i=0; icount(); #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT - if( - count!=3 - || !change_first(*code, OP::OP_VALUE__GET_ELEMENT, /*=>*/OP::OP_VALUE__GET_ELEMENT__WRITE) - ) + if(count!=3 || !change_first(*code, OP::OP_VALUE__GET_ELEMENT, /*=>*/OP::OP_VALUE__GET_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT - if( - count!=3 - || !change_first(*code, OP::OP_WITH_SELF__VALUE__GET_ELEMENT, /*=>*/OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE) - ) + if(count!=3 || !change_first(*code, OP::OP_WITH_SELF__VALUE__GET_ELEMENT, /*=>*/OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT - if( - count!=5 - || !change_first(*code, OP::OP_GET_OBJECT_ELEMENT, /*=>*/OP::OP_GET_OBJECT_ELEMENT__WRITE) - ) + if(count!=5 || !change_first(*code, OP::OP_GET_OBJECT_ELEMENT, /*=>*/OP::OP_GET_OBJECT_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT - if( - count!=5 - || !change_first(*code, OP::OP_GET_OBJECT_VAR_ELEMENT, /*=>*/OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE) - ) + if(count!=5 || !change_first(*code, OP::OP_GET_OBJECT_VAR_ELEMENT, /*=>*/OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL - if( - !change(*code, count-1/* last */, OP::OP_GET_ELEMENT__SPECIAL, /*=>*/OP::OP_GET_ELEMENT__SPECIAL__WRITE) - ) + if(!change(*code, count-1/* last */, OP::OP_GET_ELEMENT__SPECIAL, /*=>*/OP::OP_GET_ELEMENT__SPECIAL__WRITE) ) #endif { - change_or_append(*code, count-1 /* last */, - OP::OP_GET_ELEMENT, /*=>*/OP::OP_GET_ELEMENT__WRITE, - /*or */OP::OP_WRITE_VALUE - ); /* value=pop; wcontext.write(value) */ + change_or_append(*code, count-1 /* last */, OP::OP_GET_ELEMENT, /*=>*/OP::OP_GET_ELEMENT__WRITE, /*or */OP::OP_WRITE_VALUE ); /* value=pop; wcontext.write(value) */ } P(*$$, *code); @@ -411,11 +376,7 @@ name_without_curly_rdive_read: name_with #endif #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT - if( - count>=4 - && (*diving_code)[0].code==OP::OP_VALUE - && (*diving_code)[3].code==OP::OP_GET_ELEMENT - ){ + if(count>=4 && (*diving_code)[0].code==OP::OP_VALUE && (*diving_code)[3].code==OP::OP_GET_ELEMENT ){ // optimization O(*$$, (PC.in_call_value && count==4) @@ -472,11 +433,7 @@ name_expr_wdive_root: name_expr_dive_cod // $self. } else #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT - if( - count>=4 - && (*diving_code)[0].code==OP::OP_VALUE - && (*diving_code)[3].code==OP::OP_GET_ELEMENT - ){ + if(count>=4 && (*diving_code)[0].code==OP::OP_VALUE && (*diving_code)[3].code==OP::OP_GET_ELEMENT ){ O(*$$, OP::OP_WITH_ROOT__VALUE__GET_ELEMENT); P(*$$, *diving_code, 1/*offset*/, 2/*limit*/); // copy origin+value if(count>4) @@ -549,26 +506,24 @@ call: call_value { #endif { $$=$1; /* stack: value */ - if(!change_first(*$$, OP::OP_CONSTRUCT_OBJECT, /*=>*/OP::OP_CONSTRUCT_OBJECT__WRITE)) - change_or_append(*$$, count-2 /* second last */, - OP::OP_CALL, /*=>*/ OP::OP_CALL__WRITE, - /*or */OP::OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */ + if(!change_first(*$$, OP::OP_CONSTRUCT_OBJECT, /*=>*/ OP::OP_CONSTRUCT_OBJECT__WRITE)) + change_or_append(*$$, count-2 /* second last */, OP::OP_CALL, /*=>*/ OP::OP_CALL__WRITE, /*or */ OP::OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */ } }; call_value: '^' { - PC.in_call_value=true; - } - call_name { - PC.in_call_value=false; - } - store_params EON { /* ^field.$method{vasya} */ + PC.in_call_value=true; +} +call_name { + PC.in_call_value=false; +} +store_params EON { /* ^field.$method{vasya} */ #ifdef OPTIMIZE_BYTECODE_CUT_REM_OPERATOR #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT const String* operator_name=LA2S(*$3, 0, OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR); #else const String* operator_name=LA2S(*$3, 1); #endif - if(operator_name && *operator_name==REM_OPERATOR_NAME){ + if(operator_name && SYMBOLS_EQ(*operator_name,REM_SYMBOL)){ $$=N(); } else #endif @@ -735,9 +690,9 @@ name_square_code_value: '[' { if(!maybe_append_simple_diving_code(*$$, *$3)) #endif { - OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */ - /* some code that writes to that context */ - /* context=pop; stack: context.value() */ + OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */ + /* some code that writes to that context */ + /* context=pop; stack: context.value() */ } }; subvar_ref_name_rdive: STRING { @@ -759,7 +714,7 @@ class_static_prefix: STRING ':' { $$=$1; // stack: class name string if(*LA2S(*$$) == BASE_NAME) { // pseudo BASE class if(VStateless_class* base=PC.cclass->base_class()) { - change_string_literal_value(*$$, base->name()); + change_string_literal_value(*$$, *new String(base->type())); } else { strcpy(PC.error, "no base class declared"); YYERROR; @@ -1064,13 +1019,12 @@ default: } if(pc.explicit_result && c) switch(c) { + default: + pc.string.append(c); case '\n': case ' ': case '\t': begin=pc.source; begin_pos=pc.pos; - continue; // skip it - default: - result=BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE; - goto break2; + continue; } break; @@ -1597,13 +1551,12 @@ default: } if(pc.explicit_result && c) switch(c) { + default: + pc.string.append(c); case '\n': case ' ': case '\t': begin=pc.source; begin_pos=pc.pos; - continue; // skip it - default: - result=BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE; - goto break2; + continue; } break; @@ -1612,17 +1565,17 @@ default: pc.ls=LS_METHOD_SQUARE; lexical_brackets_nestage=1; RC; - } + } if(c=='{') {/* ]{ }{ ){ */ pc.ls=LS_METHOD_CURLY; lexical_brackets_nestage=1; RC; - } + } if(c=='(') {/* ]( }( )( */ pc.ls=LS_METHOD_ROUND; lexical_brackets_nestage=1; RC; - } + } pop_LS(pc); pc.ungetc(); result=EON; @@ -1648,9 +1601,12 @@ break2: } if(!pc.string.is_empty()) { // something accumulated? // create STRING value: array of OP_VALUE+origin+vstring - *lvalp=VL( - new VString(*new String(pc.string, String::L_CLEAN)), - pc.file_no, pc.string_start.line, pc.string_start.col); +#ifdef SYMBOLS_CACHING + Value *lookup=symbols->get(pc.string); +#else + Value *lookup=0; +#endif + *lvalp=VL(lookup ? lookup : new VString(*new String(pc.string, String::L_CLEAN)), pc.file_no, pc.string_start.line, pc.string_start.col); // new pieces storage pc.string.clear(); pc.string_start.clear(); @@ -1664,8 +1620,8 @@ break2: } static int real_yyerror(Parse_control *pc, const char *s) { // Called by yyparse on error - strncpy(PC.error, s, MAX_STRING); - return 1; + strncpy(PC.error, s, MAX_STRING); + return 1; } static void yyprint(FILE *file, int type, YYSTYPE value) {