--- parser3/src/main/compile.y 2010/04/28 17:56:22 1.254 +++ parser3/src/main/compile.y 2012/05/30 02:25:47 1.263 @@ -2,12 +2,14 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.254 2010/04/28 17:56:22 pretender Exp $ + */ +volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.263 2012/05/30 02:25:47 misha Exp $"; + /** @todo parser4: - cache compiled code from request to request. to do that... @@ -35,7 +37,6 @@ #include "pa_vobject.h" #include "pa_vdouble.h" #include "pa_globals.h" -#include "pa_vvoid.h" #include "pa_vmethod_frame.h" // defines @@ -54,7 +55,7 @@ static int yylex(YYSTYPE* lvalp, void* p static const VBool vfalse(false); static const VBool vtrue(true); -static const VVoid vvoid; +static const VString vempty; // local convinient inplace typecast & var #undef PC @@ -145,7 +146,7 @@ method: control_method | code_method; control_method: '@' STRING '\n' maybe_control_strings { - const String& command=*LA2S(*$2); + const String& command=LA2S(*$2)->trim(String::TRIM_END); YYSTYPE strings_code=$4; if(strings_code->count()<1*OPERATIONS_PER_OPVALUE) { strcpy(PC.error, "@"); @@ -155,8 +156,9 @@ control_method: '@' STRING '\n' } if(command==CLASS_NAME) { if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { + PC.class_add(); // new class' name - const String& name=*LA2S(*strings_code); + const String& name=LA2S(*strings_code)->trim(String::TRIM_END); // creating the class VStateless_class* cclass=new VClass; PC.cclass_new=cclass; @@ -166,8 +168,9 @@ control_method: '@' STRING '\n' YYERROR; } } else if(command==USE_CONTROL_METHOD_NAME) { + PC.class_add(); for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE) - PC.request.use_file(PC.request.main_class, *LA2S(*strings_code, i)); + PC.request.use_file(PC.request.main_class, LA2S(*strings_code, i)->trim(String::TRIM_END), PC.request.get_used_filename(PC.file_no)); } else if(command==BASE_NAME) { if(PC.append){ strcpy(PC.error, "can't set base while appending methods to class '"); @@ -183,7 +186,7 @@ control_method: '@' STRING '\n' YYERROR; } if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { - const String& base_name=*LA2S(*strings_code); + const String& base_name=LA2S(*strings_code)->trim(String::TRIM_END); if(Value* base_class_value=PC.request.get_class(base_name)) { // @CLASS == @BASE sanity check if(VStateless_class *base_class=base_class_value->get_class()) { @@ -208,7 +211,7 @@ 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); + const String& option=LA2S(*strings_code, i)->trim(String::TRIM_END); if(option==OPTION_ALL_VARS_LOCAL_NAME){ PC.set_all_vars_local(); } else if(option==OPTION_PARTIAL_CLASS){ @@ -221,18 +224,24 @@ control_method: '@' STRING '\n' YYERROR; } } else { - // mark new class as partial. we can add methods to it later. + // marks the new class as partial. we will be able to add methods here later. PC.cclass_new->set_partial(); } } else { strcpy(PC.error, "'"OPTION_PARTIAL_CLASS"' option should be used straight after @"CLASS_NAME); YYERROR; } + } else if(option==method_call_type_static){ + PC.set_methods_call_type(Method::CT_STATIC); + } else if(option==method_call_type_dynamic){ + 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"' and '"OPTION_ALL_VARS_LOCAL_NAME"'"); + "'"OPTION_PARTIAL_CLASS"', '"OPTION_ALL_VARS_LOCAL_NAME"'" + ", '"METHOD_CALL_TYPE_STATIC"' and '"METHOD_CALL_TYPE_DYNAMIC"'" + ); YYERROR; } } @@ -245,7 +254,7 @@ control_method: '@' STRING '\n' } }; maybe_control_strings: empty | control_strings; -control_strings: control_string | control_strings control_string { $$=$1; P(*$$, *$2) }; +control_strings: control_string | control_strings control_string { $$=$1; P(*$$, *$2); }; control_string: maybe_string '\n'; maybe_string: empty | STRING; @@ -281,14 +290,12 @@ code_method: '@' STRING bracketed_maybe_ Method* method=new Method( //name, - Method::CT_ANY, + GetMethodCallType(PC, *$2), 0, 0/*min,max numbered_params_count*/, params_names, locals_names, 0/*to be filled later in next {} */, 0, all_vars_local); *reinterpret_cast(&$$)=method; - - // todo: check [][;result;] } maybe_codes { Method* method=reinterpret_cast($7); // fill in the code @@ -300,9 +307,9 @@ code_method: '@' STRING bracketed_maybe_ }; maybe_bracketed_strings: empty | bracketed_maybe_strings; -bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2}; +bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2;}; maybe_strings: empty | strings; -strings: STRING | strings ';' STRING { $$=$1; P(*$$, *$3) }; +strings: STRING | strings ';' STRING { $$=$1; P(*$$, *$3); }; maybe_comment: empty | STRING; @@ -310,7 +317,7 @@ maybe_comment: empty | STRING; maybe_codes: empty | codes; -codes: code | codes code { $$=$1; P(*$$, *$2) }; +codes: code | codes code { $$=$1; P(*$$, *$2); }; code: write_string | action; action: get | put | call; @@ -357,9 +364,9 @@ get: get_value { P(*$$, *code); }; -get_value: '$' get_name_value { $$=$2 }; +get_value: '$' get_name_value { $$=$2; }; get_name_value: name_without_curly_rdive EON | name_in_curly_rdive; -name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 }; +name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2; }; name_without_curly_rdive: name_without_curly_rdive_read | name_without_curly_rdive_class; @@ -416,8 +423,8 @@ name_without_curly_rdive_read: name_with #endif /* diving code; stack: current context */ }; -name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P(*$$, *$2) }; -name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P(*$$, *$2) }; +name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P(*$$, *$2); }; +name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P(*$$, *$2); }; /* put */ @@ -468,7 +475,7 @@ name_expr_wdive_write: '.' name_expr_div O(*$$, OP::OP_WITH_WRITE); /* stack: starting context */ P(*$$, *$2); /* diving code; stack: context,name */ }; -name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P(*$$, *$2) }; +name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P(*$$, *$2); }; construct: construct_square @@ -501,7 +508,7 @@ construct_curly: '{' maybe_codes '}' { }; any_constructor_code_value: - void_value /* optimized $var[] case */ + empty_value /* optimized $var[] case */ | STRING /* optimized $var[STRING] case */ | constructor_code_value /* $var[something complex] */ ; @@ -512,7 +519,7 @@ constructor_code_value: constructor_code /* context=pop; stack: context.value() */ }; constructor_code: codes__excluding_sole_str_literal; -codes__excluding_sole_str_literal: action | code codes { $$=$1; P(*$$, *$2) }; +codes__excluding_sole_str_literal: action | code codes { $$=$1; P(*$$, *$2); }; /* call */ @@ -549,8 +556,9 @@ call_value: '^' { YYSTYPE params_code=$5; if(params_code->count()==3) { // probably [] case. [OP::OP_VALUE+origin+Void] if(Value* value=LA2V(*params_code)) // it is OP_VALUE+origin+value? - if(value->is_void()) // value is VVoid? - params_code=0; // ^zzz[] case. don't append lone empty param. + if(const String * string=value->get_string()) + if(string->is_empty()) // value is empty string? + params_code=0; // ^zzz[] case. don't append lone empty param. } /* stack: context, method_junction */ @@ -577,7 +585,7 @@ call_value: '^' { call_name: name_without_curly_rdive; -store_params: store_param | store_params store_param { $$=$1; P(*$$, *$2) }; +store_params: store_param | store_params store_param { $$=$1; P(*$$, *$2); }; store_param: store_square_param | store_round_param @@ -588,20 +596,20 @@ store_square_param: '[' { *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; } store_code_param_parts { PC.explicit_result=*reinterpret_cast(&$2); -} ']' {$$=$3}; -store_round_param: '(' store_expr_param_parts ')' {$$=$2}; -store_curly_param: '{' store_curly_param_parts '}' {$$=$2}; +} ']' {$$=$3;}; +store_round_param: '(' store_expr_param_parts ')' {$$=$2;}; +store_curly_param: '{' store_curly_param_parts '}' {$$=$2;}; store_code_param_parts: store_code_param_part -| store_code_param_parts ';' store_code_param_part { $$=$1; P(*$$, *$3) } +| store_code_param_parts ';' store_code_param_part { $$=$1; P(*$$, *$3); } ; store_expr_param_parts: store_expr_param_part -| store_expr_param_parts ';' store_expr_param_part { $$=$1; P(*$$, *$3) } +| store_expr_param_parts ';' store_expr_param_part { $$=$1; P(*$$, *$3); } ; store_curly_param_parts: store_curly_param_part -| store_curly_param_parts ';' store_curly_param_part { $$=$1; P(*$$, *$3) } +| store_curly_param_parts ';' store_curly_param_part { $$=$1; P(*$$, *$3); } ; store_code_param_part: code_param_value { $$=$1; @@ -625,16 +633,16 @@ store_curly_param_part: maybe_codes { OA(*$$, OP::OP_CURLY_CODE__STORE_PARAM, $1); }; code_param_value: - void_value /* optimized [;...] case */ + empty_value /* optimized [;...] case */ | STRING /* optimized [STRING] case */ | constructor_code_value /* [something complex] */ ; /* name */ -name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P(*$$, *$2) }; +name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P(*$$, *$2); }; -name_path: name_step | name_path name_step { $$=$1; P(*$$, *$2) }; +name_path: name_step | name_path name_step { $$=$1; P(*$$, *$2); }; name_step: name_advance1 '.'; name_advance1: name_expr_value { // we know that name_advance1 not called from ^xxx context @@ -686,7 +694,7 @@ subvar_ref_name_rdive: STRING { O(*$$, OP::OP_WITH_READ); P(*$$, *$1); }; -subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P(*$$, *$2) }; +subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P(*$$, *$2); }; subvar__get_write: '$' subvar_ref_name_rdive { $$=$2; O(*$$, OP::OP_GET_ELEMENT__WRITE); @@ -707,7 +715,7 @@ class_static_prefix: STRING ':' { } } // optimized OP_VALUE+origin+string+OP_GET_CLASS => OP_VALUE__GET_CLASS+origin+string - maybe_change_first_opcode(*$$, OP::OP_VALUE, OP::OP_VALUE__GET_CLASS) + maybe_change_first_opcode(*$$, OP::OP_VALUE, OP::OP_VALUE__GET_CLASS); }; class_constructor_prefix: class_static_prefix ':' { $$=$1; @@ -728,46 +736,46 @@ expr: | false_value | get_value | call_value -| '"' string_inside_quotes_value '"' { $$ = $2 } -| '\'' string_inside_quotes_value '\'' { $$ = $2 } +| '"' string_inside_quotes_value '"' { $$ = $2; } +| '\'' string_inside_quotes_value '\'' { $$ = $2; } | '(' expr ')' { $$ = $2; } /* stack: operand // stack: @operand */ -| '-' expr %prec NUNARY { $$=$2; O(*$$, OP::OP_NEG) } -| '+' expr %prec NUNARY { $$=$2 } -| '~' expr { $$=$2; O(*$$, OP::OP_INV) } -| '!' expr { $$=$2; O(*$$, OP::OP_NOT) } -| "def" expr { $$=$2; O(*$$, OP::OP_DEF) } -| "in" expr { $$=$2; O(*$$, OP::OP_IN) } -| "-f" expr { $$=$2; O(*$$, OP::OP_FEXISTS) } -| "-d" expr { $$=$2; O(*$$, OP::OP_DEXISTS) } +| '-' expr %prec NUNARY { $$=$2; O(*$$, OP::OP_NEG); } +| '+' expr %prec NUNARY { $$=$2; } +| '~' expr { $$=$2; O(*$$, OP::OP_INV); } +| '!' expr { $$=$2; O(*$$, OP::OP_NOT); } +| "def" expr { $$=$2; O(*$$, OP::OP_DEF); } +| "in" expr { $$=$2; O(*$$, OP::OP_IN); } +| "-f" expr { $$=$2; O(*$$, OP::OP_FEXISTS); } +| "-d" expr { $$=$2; O(*$$, OP::OP_DEXISTS); } /* stack: a,b // stack: a@b */ -| expr '-' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_SUB) } -| expr '+' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_ADD) } -| expr '*' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_MUL) } -| expr '/' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_DIV) } -| expr '%' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_MOD) } -| expr '\\' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_INTDIV) } -| expr "<<" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_SL) } -| expr ">>" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_SR) } -| expr '&' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_AND) } -| expr '|' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_OR) } -| expr "!|" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_XOR) } -| expr "&&" expr { $$=$1; OA(*$$, OP::OP_NESTED_CODE, $3); O(*$$, OP::OP_LOG_AND) } -| expr "||" expr { $$=$1; OA(*$$, OP::OP_NESTED_CODE, $3); O(*$$, OP::OP_LOG_OR) } -| expr "!||" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_LOG_XOR) } -| expr '<' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_LT) } -| expr '>' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_GT) } -| expr "<=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_LE) } -| expr ">=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_GE) } -| expr "==" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_EQ) } -| expr "!=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_NE) } -| expr "lt" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_LT) } -| expr "gt" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_GT) } -| expr "le" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_LE) } -| expr "ge" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_GE) } -| expr "eq" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_EQ) } -| expr "ne" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_NE) } -| expr "is" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_IS) } +| expr '-' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_SUB); } +| expr '+' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_ADD); } +| expr '*' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_MUL); } +| expr '/' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_DIV); } +| expr '%' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_MOD); } +| expr '\\' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_INTDIV); } +| expr "<<" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_SL); } +| expr ">>" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_SR); } +| expr '&' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_AND); } +| expr '|' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_OR); } +| expr "!|" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_BIN_XOR); } +| expr "&&" expr { $$=$1; OA(*$$, OP::OP_NESTED_CODE, $3); O(*$$, OP::OP_LOG_AND); } +| expr "||" expr { $$=$1; OA(*$$, OP::OP_NESTED_CODE, $3); O(*$$, OP::OP_LOG_OR); } +| expr "!||" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_LOG_XOR); } +| expr '<' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_LT); } +| expr '>' expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_GT); } +| expr "<=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_LE); } +| expr ">=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_GE); } +| expr "==" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_EQ); } +| expr "!=" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_NUM_NE); } +| expr "lt" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_LT); } +| expr "gt" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_GT); } +| expr "le" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_LE); } +| expr "ge" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_GE); } +| expr "eq" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_EQ); } +| expr "ne" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_STR_NE); } +| expr "is" expr { $$=$1; P(*$$, *$3); O(*$$, OP::OP_IS); } ; double_or_STRING: STRING { @@ -798,14 +806,14 @@ string_inside_quotes_value: maybe_codes write_string: STRING { // optimized OP_STRING+OP_WRITE_VALUE => OP_STRING__WRITE - change_string_literal_to_write_string_literal(*($$=$1)) + change_string_literal_to_write_string_literal(*($$=$1)); }; -void_value: /* empty */ { $$=VL(/*we know that we will not change it*/const_cast(&vvoid), 0, 0, 0) } -true_value: "true" { $$ = VL(/*we know that we will not change it*/const_cast(&vtrue), 0, 0, 0) } -false_value: "false" { $$ = VL(/*we know that we will not change it*/const_cast(&vfalse), 0, 0, 0) } +empty_value: /* empty */ { $$=VL(/*we know that we will not change it*/const_cast(&vempty), 0, 0, 0); } +true_value: "true" { $$ = VL(/*we know that we will not change it*/const_cast(&vtrue), 0, 0, 0); } +false_value: "false" { $$ = VL(/*we know that we will not change it*/const_cast(&vfalse), 0, 0, 0); } -empty: /* empty */ { $$=N() }; +empty: /* empty */ { $$=N(); }; %% #endif @@ -1576,12 +1584,9 @@ default: break2: if(end!=begin) { // there is last piece? - if((c=='@' || c==0) && end[-1]=='\n') { // we are before LS_DEF_NAME or EOF? - // strip last \n - end--; - if(end!=begin && end[-1]=='\n') // allow one empty line before LS_DEF_NAME + if(c=='@' || c==0) // we are before LS_DEF_NAME or EOF? + while(end!=begin && end[-1]=='\n') // trim all empty lines before LS_DEF_NAME and EOF end--; - } if(end!=begin && pc.ls!=LS_USER_COMMENT) { // last piece still alive and not comment? if(!pc.string_start) pc.string_start=begin_pos;