--- parser3/src/main/compile.y 2003/04/02 16:05:20 1.204.2.8.2.9 +++ parser3/src/main/compile.y 2004/04/06 14:15:22 1.212 @@ -5,7 +5,7 @@ Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.204.2.8.2.9 2003/04/02 16:05:20 paf Exp $ + $Id: compile.y,v 1.212 2004/04/06 14:15:22 paf Exp $ */ /** @@ -27,6 +27,8 @@ #define yyerror(msg) real_yyerror((Parse_control *)pc, msg) #define YYPRINT(file, type, value) yyprint(file, type, value) +// includes + #include "compile_tools.h" #include "pa_value.h" #include "pa_request.h" @@ -36,12 +38,15 @@ #include "pa_vvoid.h" #include "pa_vmethod_frame.h" +// defines + #define USE_CONTROL_METHOD_NAME "USE" -static int real_yyerror(Parse_control *pc, char *s); -static void yyprint(FILE *file, int type, YYSTYPE value); -static int yylex(YYSTYPE *lvalp, void *pc); +// forwards +static int real_yyerror(Parse_control* pc, char* s); +static void yyprint(FILE* file, int type, YYSTYPE value); +static int yylex(YYSTYPE* lvalp, void* pc); // local convinient inplace typecast & var #undef PC @@ -61,7 +66,7 @@ static int yylex(YYSTYPE *lvalp, void *p %token BAD_HEX_LITERAL %token BAD_METHOD_DECL_START %token BAD_METHOD_PARAMETER_NAME_CHARACTER -%token BAD_MATH_OPERATOR_CHARACTER +%token BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE %token LAND "&&" %token LOR "||" @@ -114,11 +119,11 @@ static int yylex(YYSTYPE *lvalp, void *p %% all: one_big_piece { - Method& method=*new Method(/*main_method_name, */Method::CT_ANY, + 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->add_method(main_method_name, method); + PC.cclass->add_method(PC.alias_method(main_method_name), method); } | methods; @@ -131,7 +136,7 @@ control_method: '@' STRING '\n' maybe_control_strings { const String& command=*LA2S(*$2); YYSTYPE strings_code=$4; - if(strings_code->count()<1*2) { + if(strings_code->count()<1*OPERATIONS_PER_OPVALUE) { strcpy(PC.error, "@"); strcat(PC.error, command.cstr()); strcat(PC.error, " is empty"); @@ -144,7 +149,7 @@ control_method: '@' STRING '\n' strcat(PC.error, "'"); YYERROR; } - if(strings_code->count()==1*2) { + if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { // new class' name const String& name=*LA2S(*strings_code); // creating the class @@ -154,11 +159,11 @@ control_method: '@' STRING '\n' // append to request's classes PC.request.classes().put(name, cclass); } else { - strcpy(PC.error, "@"CLASS_NAME" must contain sole name"); + strcpy(PC.error, "@"CLASS_NAME" must contain only one line with class name (contains more then one)"); YYERROR; } } else if(command==USE_CONTROL_METHOD_NAME) { - for(size_t i=0; icount(); i+=2) + for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE) PC.request.use_file(PC.request.main_class, *LA2S(*strings_code, i)); } else if(command==BASE_NAME) { if(PC.cclass->base_class()) { // already changed from default? @@ -167,7 +172,7 @@ control_method: '@' STRING '\n' strcat(PC.error, "'"); YYERROR; } - if(strings_code->count()==1*2) { + if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { const String& base_name=*LA2S(*strings_code); if(Value* base_class_value=PC.request.classes().get(base_name)) { // @CLASS == @BASE sanity check @@ -204,15 +209,15 @@ control_strings: control_string | contro control_string: maybe_string '\n'; maybe_string: empty | STRING; -code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' - maybe_codes { +code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' { + PC.explicit_result=false; const String& name=*LA2S(*$2); YYSTYPE params_names_code=$3; ArrayString* params_names=0; if(int size=params_names_code->count()) { params_names=new ArrayString; - for(int i=0; icount()) { locals_names=new ArrayString; - for(int i=0; iadd_method(name, method); + 0/*to be filled later in next {} */, 0); + PC.cclass->add_method(PC.alias_method(name), *method); + *reinterpret_cast(&$$)=method; + + // todo: check [][;result;] +} maybe_codes { + // fill in the code + reinterpret_cast($7)->parser_code=$8; }; maybe_bracketed_strings: empty | bracketed_maybe_strings; @@ -268,19 +284,19 @@ name_without_curly_rdive_read: name_with ArrayOperation* diving_code=$1; const String* first_name=LA2S(*diving_code); // self.xxx... -> xxx... - // OP_VALUE+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+... + // OP_VALUE+origin+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+... if(first_name && *first_name==SELF_ELEMENT_NAME) { - O(*$$, OP_WITH_SELF); /* stack: starting context */ - P(*$$, *diving_code, + O(*yyval, OP_WITH_SELF); /* stack: starting context */ + P(*yyval, *diving_code, /* skip over... */ - diving_code->count()>=3?3/*OP_VALUE+string+OP_GET_ELEMENTx*/:2/*OP_+string*/); + diving_code->count()>=4?4/*OP_VALUE+origin+string+OP_GET_ELEMENTx*/:3/*OP_+origin+string*/); } else { - O(*$$, OP_WITH_READ); /* stack: starting context */ + O(*yyval, OP_WITH_READ); /* stack: starting context */ // ^if ELEMENT -> ^if ELEMENT_OR_OPERATOR - // OP_VALUE+string+OP_GET_ELEMENT. -> OP_VALUE+string+OP_GET_ELEMENT_OR_OPERATOR. - if(PC.in_call_value && diving_code->count()==3) - diving_code->put(2, OP_GET_ELEMENT_OR_OPERATOR); + // OP_VALUE+origin+string+OP_GET_ELEMENT. -> OP_VALUE+origin+string+OP_GET_ELEMENT_OR_OPERATOR. + if(PC.in_call_value && diving_code->count()==4) + diving_code->put(4-1, OP_GET_ELEMENT_OR_OPERATOR); P(*$$, *diving_code); } /* diving code; stack: current context */ @@ -303,12 +319,12 @@ name_expr_wdive_root: name_expr_dive_cod ArrayOperation* diving_code=$1; const String* first_name=LA2S(*diving_code); // $self.xxx... -> $xxx... - // OP_VALUE+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+... + // OP_VALUE+origin+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+... if(first_name && *first_name==SELF_ELEMENT_NAME) { O(*$$, OP_WITH_SELF); /* stack: starting context */ P(*$$, *diving_code, /* skip over... */ - diving_code->count()>=3?3/*OP_VALUE+string+OP_GET_ELEMENTx*/:2/*OP_+string*/); + diving_code->count()>=4?4/*OP_VALUE+origin+string+OP_GET_ELEMENTx*/:3/*OP_+origin+string*/); } else { O(*$$, OP_WITH_ROOT); /* stack: starting context */ P(*$$, *diving_code); @@ -327,9 +343,14 @@ construct: | construct_round | construct_curly ; -construct_square: '[' any_constructor_code_value ']' { +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.explicit_result=reinterpret_cast($2); +} ']' { // stack: context, name - $$=$2; // stack: context, name, value + $$=$3; // stack: context, name, value O(*$$, OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */ } ; @@ -379,8 +400,8 @@ call_value: '^' { $$=$3; /* with_xxx,diving code; stack: context,method_junction */ YYSTYPE params_code=$5; - if(params_code->count()==3) { // probably [] case. [OP_VALUE + Void + STORE_PARAM] - if(Value* value=LA2V(*params_code)) // it is OP_VALUE + value? + if(params_code->count()==4) { // probably [] case. [OP_VALUE+origin+Void+STORE_PARAM] + if(Value* value=LA2V(*params_code)) // it is OP_VALUE+origin+value? if(!value->is_defined()) // value is VVoid? params_code=0; // ^zzz[] case. don't append lone empty param. } @@ -396,7 +417,12 @@ store_param: | store_round_param | store_curly_param ; -store_square_param: '[' store_code_param_parts ']' {$$=$2}; +store_square_param: '[' { + // allow ^call[ letters here any time ] + *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}; store_code_param_parts: @@ -475,9 +501,14 @@ name_expr_with_subvar_value: STRING subv $$=N(); OA(*$$, OP_STRING_POOL, code); }; -name_square_code_value: '[' codes ']' { +name_square_code_value: '[' { + // allow $result_or_other_variable[ letters here any time ] + *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; +} codes { + PC.explicit_result=reinterpret_cast($2); +} ']' { $$=N(); - OA(*$$, OP_OBJECT_POOL, $2); /* stack: empty write context */ + OA(*$$, OP_OBJECT_POOL, $3); /* stack: empty write context */ /* some code that writes to that context */ /* context=pop; stack: context.value() */ }; @@ -607,6 +638,14 @@ empty: /* empty */ { $$=N() }; 4:[^({]=pop */ +inline void ungetc(Parse_control& pc, uint last_line_end_col) { + pc.source--; + if(pc.pos.col==0) { + --pc.pos.line; pc.pos.col=last_line_end_col; + } else + --pc.pos.col; + +} static int yylex(YYSTYPE *lvalp, void *apc) { register Parse_control& pc=*static_cast(apc); @@ -630,13 +669,13 @@ static int yylex(YYSTYPE *lvalp, void *a c=*(end=(pc.source++)); // fprintf(stderr, "\nchar: %c %02X; nestage: %d, sp=%d", c, c, lexical_brackets_nestage, pc.sp); - if(c=='\n') { - pc.pos.line++; - pc.pos.col=0; - } else - pc.pos.col++; + if(c=='\n') + pc.pos_next_line(); + else + pc.pos_next_c(c); +// fprintf(stderr, "\nchar: %c file(%d:%d)", c, pc.pos.line, pc.pos.col); - if(c=='@' && pc.pos.col==0+1) { + if(pc.pos.col==0+1 && c=='@') { if(pc.ls==LS_DEF_SPECIAL_BODY) { // @SPECIAL // ... @@ -649,7 +688,8 @@ static int yylex(YYSTYPE *lvalp, void *a } else // @ in first column inside some code [when could that be?] result=BAD_METHOD_DECL_START; goto break2; - } else if(c=='^') { + } + if(c=='^') { if(pc.ls==LS_METHOD_AFTER) { // handle after-method situation pop_LS(pc); @@ -680,7 +720,7 @@ default: if(!pc.string_start) pc.string_start=begin_pos; // append piece till ^ - pc.string.append_strdup(begin, end-begin); + pc.string.append_strdup_know_length(begin, end-begin); } // reset piece 'begin' position & line begin=pc.source; // ->punctuation @@ -696,7 +736,7 @@ default: if(!pc.string_start) pc.string_start=begin_pos; // append piece till ^ - pc.string.append_strdup(begin, end-begin); + pc.string.append_strdup_know_length(begin, end-begin); } // #HH ? if(pc.source[0]=='#' && pc.source[1] && pc.source[2]) { @@ -729,7 +769,7 @@ default: if(!pc.string_start) pc.string_start=begin_pos; // append piece till # - pc.string.append_strdup(begin, end-begin); + pc.string.append_strdup_know_length(begin, end-begin); } // fall into COMMENT lexical state [wait for \n] push_LS(pc, LS_USER_COMMENT); @@ -739,7 +779,7 @@ default: // USER'S = NOT OURS case LS_USER: - case LS_NAME_SQUARE_PART: // name.[here].xxx + case LS_NAME_SQUARE_PART: // name.[here].xxx if(pc.trim_bof) switch(c) { case '\n': case ' ': case '\t': @@ -768,6 +808,16 @@ default: lexical_brackets_nestage++; break; } + if(pc.explicit_result && c) + switch(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; + } break; // #COMMENT @@ -874,7 +924,7 @@ default: if(!pc.string_start) pc.string_start=begin_pos; // append piece till # - pc.string.append_strdup(begin, end-begin); + pc.string.append_strdup_know_length(begin, end-begin); } // fall into COMMENT lexical state [wait for \n] push_LS(pc, LS_EXPRESSION_COMMENT); @@ -1057,7 +1107,7 @@ default: // expression integer division case '\\': pop_LS(pc); - pc.source--; if(--pc.pos.col<0) { pc.pos.line--; pc.pos.col=-1; } + pc.ungetc(); result=EON; goto break2; } @@ -1069,7 +1119,7 @@ default: switch(c) { case ':': pop_LS(pc); - pc.source--; if(--pc.pos.col<0) { pc.pos.line--; pc.pos.col=-1; } + pc.ungetc(); result=EON; goto break2; } @@ -1089,7 +1139,7 @@ default: // before call case '^': pop_LS(pc); - pc.source--; if(--pc.pos.col<0) { pc.pos.line--; pc.pos.col=-1; } + pc.ungetc(); result=EON; goto break2; case '[': @@ -1221,6 +1271,7 @@ default: case ':': // ':name' or 'class:name' case '^': // ^abc^xxx wrong. bailing out case ']': case '}': case ')': // ^abc]}) wrong. bailing out + case ' ': // ^if ( wrong. bailing out RC; } break; @@ -1267,6 +1318,16 @@ default: lexical_brackets_nestage++; break; } + if(pc.explicit_result && c) + switch(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; + } break; case LS_METHOD_AFTER: @@ -1286,7 +1347,7 @@ default: RC; } pop_LS(pc); - pc.source--; if(--pc.pos.col<0) { pc.pos.line--; pc.pos.col=-1; } + pc.ungetc(); result=EON; goto break2; } @@ -1308,11 +1369,11 @@ break2: if(!pc.string_start) pc.string_start=begin_pos; // append it - pc.string.append_strdup(begin, end-begin); + pc.string.append_strdup_know_length(begin, end-begin); } } if(!pc.string.is_empty()) { // something accumulated? - // create STRING value: array of OP_VALUE+vstring + // 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);