--- parser3/src/main/compile.y 2008/08/15 15:26:14 1.229 +++ parser3/src/main/compile.y 2009/04/17 09:11:15 1.231 @@ -5,7 +5,7 @@ Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.229 2008/08/15 15:26:14 misha Exp $ + $Id: compile.y,v 1.231 2009/04/17 09:11:15 misha Exp $ */ /** @@ -43,7 +43,7 @@ #define USE_CONTROL_METHOD_NAME "USE" #define OPTIONS_CONTROL_METHOD_NAME "OPTIONS" #define OPTION_ALL_VARS_LOCAL_NAME "locals" -#define OPTION_APPEND_METHODS "append" +#define OPTION_PARTIAL_CLASS "partial" // forwards @@ -210,22 +210,28 @@ control_method: '@' STRING '\n' const String& option=*LA2S(*strings_code, i); if(option==OPTION_ALL_VARS_LOCAL_NAME){ PC.set_all_vars_local(); - } else if(option==OPTION_APPEND_METHODS) { - if(!PC.class_reuse()){ - if(PC.cclass_new){ - strcpy(PC.error, "can't append methods to '"); - strncat(PC.error, PC.cclass_new->name().cstr(), MAX_STRING/2); - strcat(PC.error, "' - the class doesn't exist"); + } else if(option==OPTION_PARTIAL_CLASS){ + 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"); + YYERROR; + } } else { - strcpy(PC.error, "'"OPTION_APPEND_METHODS"' option should be used straight after @"CLASS_NAME); + // mark new class as partial. we can add methods to it later. + PC.cclass_new->set_partial(); } + } else { + strcpy(PC.error, "'"OPTION_PARTIAL_CLASS"' option should be used straight after @"CLASS_NAME); YYERROR; } } else { strcpy(PC.error, "'"); strncat(PC.error, option.cstr(), MAX_STRING/2); strcat(PC.error, "' invalid option. valid options are " - "'"OPTION_APPEND_METHODS"' and '"OPTION_ALL_VARS_LOCAL_NAME"'"); + "'"OPTION_PARTIAL_CLASS"' and '"OPTION_ALL_VARS_LOCAL_NAME"'"); YYERROR; } } @@ -245,6 +251,7 @@ maybe_string: empty | STRING; code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' { PC.class_add(); PC.explicit_result=false; + PC.write_to_result=false; YYSTYPE params_names_code=$3; ArrayString* params_names=0; @@ -269,9 +276,8 @@ code_method: '@' STRING bracketed_maybe_ *locals_names+=local_name; } } - if(!all_vars_local && PC.cclass && PC.cclass->is_vars_local()){ + if(!all_vars_local && PC.cclass && PC.cclass->is_vars_local()) all_vars_local=true; - } Method* method=new Method( //name, @@ -284,12 +290,16 @@ code_method: '@' STRING bracketed_maybe_ // todo: check [][;result;] } maybe_codes { - Method& method=*reinterpret_cast($7); - // fill in the code - method.parser_code=$8; - // register in class - const String& name=*LA2S(*$2); - PC.cclass->add_method(PC.alias_method(name), method); + Method& method=*reinterpret_cast($7); + // fill in the code + method.parser_code=$8; + + // write to $result was detected in method's code during compilation + method.write_to_result=PC.write_to_result; + + // register in class + const String& name=*LA2S(*$2); + PC.cclass->add_method(PC.alias_method(name), method); }; maybe_bracketed_strings: empty | bracketed_maybe_strings; @@ -369,6 +379,8 @@ name_expr_wdive_root: name_expr_dive_cod /* skip over... */ diving_code->count()>=4?4/*OP::OP_VALUE+origin+string+OP::OP_GET_ELEMENTx*/:3/*OP::OP_+origin+string*/); } else { + if(first_name && *first_name==RESULT_VAR_NAME) + PC.write_to_result=true; O(*$$, OP::OP_WITH_ROOT); /* stack: starting context */ P(*$$, *diving_code); }