--- parser3/src/main/compile.y 2001/02/21 13:18:31 1.9 +++ parser3/src/main/compile.y 2001/02/22 09:14:46 1.15 @@ -13,6 +13,7 @@ #include "compile_tools.h" #include "pa_value.h" +#include "pa_request.h" int real_yyerror(parse_control *pc, char *s); static void yyprint(FILE *file, int type, YYSTYPE value); @@ -26,24 +27,55 @@ int yylex(YYSTYPE *lvalp, void *pc); %pure_parser -%token END_OF_NAME +%token EON %token STRING %token BOGUS %% -result: input { +all: + one_big_piece { String& name_main=*new(pool) String(pool); - name_main.APPEND_CONST("main"); + name_main.APPEND_CONST(MAIN_METHOD_NAME); Array& param_names=*new(pool) Array(pool); Array& local_names=*new(pool) Array(pool); Method *method=new(pool) Method(pool, name_main, param_names, local_names, *$1); *PC->methods+=method; +} +| methods; + +methods: method | methods method; +one_big_piece: maybe_codes; + +method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' + maybe_codes { + const String *name=LA2S($2); + + YYSTYPE params_names_code=$3; + Array& params_names=*new(pool) Array(pool); + for(int i=0; isize(); i+=2) + params_names+=LA2S(params_names_code, i); + + YYSTYPE locals_names_code=$4; + Array& locals_names=*new(pool) Array(pool); + for(int i=0; isize(); i+=2) + locals_names+=LA2S(locals_names_code, i); + + Method *method=new(pool) Method(pool, *name, params_names, locals_names, *$7); + *PC->methods+=method; }; -input: empty | codes; + +maybe_bracketed_strings: empty | bracketed_maybe_strings; +bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2}; +maybe_strings: empty | strings; +strings: STRING | strings ';' STRING { $$=$1; P($$, $3) }; + +maybe_comment: empty | STRING; /* codes */ +maybe_codes: empty | codes; + codes: code | codes code { $$=$1; P($$, $2); @@ -55,10 +87,10 @@ action: get | put | with | call; get: '$' any_name { $$=$2; /* stack: resulting value */ - OP($$, OP_WRITE); /* value=pop; write(value) */ + OP($$, OP_WRITE_VALUE); /* value=pop; write(value) */ }; -any_name: name_without_curly_rdive END_OF_NAME | name_in_curly_rdive; +any_name: name_without_curly_rdive EON | name_in_curly_rdive; name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 }; name_without_curly_rdive: name_rdive { @@ -71,7 +103,7 @@ name_without_curly_rdive: name_rdive { $$=N(pool); OP($$, OP_WITH_READ); /* stack: starting context */ P($$, $1); /* diving code; stack: current context */ }; -name_rdive: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) } +name_rdive: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) }; /* put */ @@ -95,11 +127,13 @@ constructor_value: | constructor_two_params_value /* $var(=;2*2) $var(%d;2*2) $var(+;1) */ ; constructor_one_param_value: - empty_value /* optimized $var() case */ -| STRING /* optimized $var(STRING) case */ + empty /* optimized $var() case */ +| string_value /* optimized $var(STRING) case */ | complex_constructor_param_value /* $var(something complex) */ ; -empty_value: empty; +string_value: STRING { + $$=LAS2LAVS($1); +}; complex_constructor_param_value: complex_constructor_param_body { $$=N(pool); OP($$, OP_CREATE_EWPOOL); /* stack: empty write context */ @@ -132,7 +166,7 @@ constructor_two_params_value: STRING ';' /* call */ -call: '^' name_expr_dive store_params END_OF_NAME { /* ^field.$method{vasya} */ +call: '^' name_expr_dive store_params EON { /* ^field.$method{vasya} */ /* TODO: подсмотреть в $3, и если там в первом элементе первая буква ":" то выкинуть её и делать не OP_WITH_READ, а WITH_ROOT @@ -164,7 +198,7 @@ store_param_part: constructor_one_param_ $$=$1; OP($$, OP_STORE_PARAM); } -store_curly_param: '{' input '}' { +store_curly_param: '{' maybe_codes '}' { $$=N(pool); OP($$, OP_CODE_ARRAY); AA($$, $2); @@ -203,7 +237,7 @@ name_expr_with_subvar_value: STRING subv $$=N(pool); OP($$, OP_CREATE_EWPOOL); P($$, $1); - OP($$, OP_WRITE); + OP($$, OP_WRITE_STRING); P($$, $2); OP($$, OP_REDUCE_EWPOOL); }; @@ -218,7 +252,7 @@ subvar_ref_name_rdive: STRING { subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) }; subvar__get_write: '$' subvar_ref_name_rdive { $$=$2; - OP($$, OP_GET_ELEMENT__WRITE); + OP($$, OP_GET_ELEMENT__WRITE_VALUE); }; @@ -229,7 +263,7 @@ with: '$' name_without_curly_rdive '{' c OP($$, OP_CREATE_RWPOOL); P($$, $4); OP($$, OP_REDUCE_RWPOOL); - OP($$, OP_WRITE); + OP($$, OP_WRITE_VALUE); }; /* codes_in_brackets */ @@ -249,7 +283,7 @@ codes__excluding_sole_str_literal: ; write_str_literal: STRING { $$=$1; - OP($$, OP_WRITE); + OP($$, OP_WRITE_STRING); }; /* */ @@ -295,7 +329,7 @@ int yylex(YYSTYPE *lvalp, void *pc) { if(c=='\n') { PC->line++; PC->col=0; - } + } else PC->col++; /* escaping: ^^ ^$ ^; ^) ^} ^( ^{ */ @@ -317,30 +351,91 @@ int yylex(YYSTYPE *lvalp, void *pc) { } } switch(PC->ls) { + + // USER'S = NOT OURS case LS_USER: if(c=='$') { - push_LS(PC); PC->ls=LS_VAR_NAME_SIMPLE; + push_LS(PC, LS_VAR_NAME_SIMPLE); result=c; goto break2; } if(c=='^') { - push_LS(PC); PC->ls=LS_METHOD_NAME; + push_LS(PC, LS_METHOD_NAME); + result=c; + goto break2; + } + if(c=='@' && PC->col==0+1) { + result=c; + push_LS(PC, LS_DEF_NAME); + goto break2; + } + + break; + + // METHOD DEFINITION + case LS_DEF_NAME: + if(c=='[') { + result=c; + PC->ls=LS_DEF_PARAMS; + goto break2; + } + if(c=='\n') { // wrong. bailing out + result=c; + pop_LS(PC); + goto break2; + } + break; + case LS_DEF_PARAMS: + if(c==';') { + result=c; + goto break2; + } + if(c==']') { + result=c; + PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT; + goto break2; + } + if(c=='\n') { // wrong. bailing out + result=c; + pop_LS(PC); + goto break2; + } + break; + case LS_DEF_LOCALS: + if(c=='[' || c==';') { result=c; goto break2; } + if(c==']') { + result=c; + PC->ls=LS_DEF_COMMENT; + goto break2; + } + if(c=='\n') { // wrong. bailing out + result=c; + pop_LS(PC); + goto break2; + } + break; + case LS_DEF_COMMENT: + if(c=='\n') { + result=c; + pop_LS(PC); + goto break2; + } break; - /* VAR */ + // VARIABLE GET/PUT/WITH case LS_VAR_NAME_SIMPLE: if(c==0 || c==' '|| c=='\t' || c=='\n' || c==')' || c=='}') { pop_LS(PC); PC->source--; PC->col--; - result=END_OF_NAME; + result=EON; goto break2; } - if(begin==end && c=='{') { /* ${name}, no need of END_OF_NAME, switching LS */ + if(begin==end && c=='{') { /* ${name}, no need of EON, switching LS */ PC->ls=LS_VAR_NAME_CURLY; result=c; goto break2; @@ -375,12 +470,12 @@ int yylex(YYSTYPE *lvalp, void *pc) { break; case LS_VAR_ROUND: if(c=='$') { - push_LS(PC); PC->ls=LS_VAR_NAME_SIMPLE; + push_LS(PC, LS_VAR_NAME_SIMPLE); result=c; goto break2; } if(c=='^') { - push_LS(PC); PC->ls=LS_METHOD_NAME; + push_LS(PC, LS_METHOD_NAME); result=c; goto break2; } @@ -400,12 +495,12 @@ int yylex(YYSTYPE *lvalp, void *pc) { break; case LS_VAR_CURLY: if(c=='$') { - push_LS(PC); PC->ls=LS_VAR_NAME_SIMPLE; + push_LS(PC, LS_VAR_NAME_SIMPLE); result=c; goto break2; } if(c=='^') { - push_LS(PC); PC->ls=LS_METHOD_NAME; + push_LS(PC, LS_METHOD_NAME); result=c; goto break2; } @@ -419,7 +514,7 @@ int yylex(YYSTYPE *lvalp, void *pc) { lexical_brackets_nestage++; break; - /* METHOD */ + // METHOD CALL case LS_METHOD_NAME: if(c=='(') { PC->ls=LS_METHOD_ROUND; @@ -440,12 +535,12 @@ int yylex(YYSTYPE *lvalp, void *pc) { break; case LS_METHOD_ROUND: if(c=='$') { - push_LS(PC); PC->ls=LS_VAR_NAME_SIMPLE; + push_LS(PC, LS_VAR_NAME_SIMPLE); result=c; goto break2; } if(c=='^') { - push_LS(PC); PC->ls=LS_METHOD_NAME; + push_LS(PC, LS_METHOD_NAME); result=c; goto break2; } @@ -464,12 +559,12 @@ int yylex(YYSTYPE *lvalp, void *pc) { break; case LS_METHOD_CURLY: if(c=='$') { - push_LS(PC); PC->ls=LS_VAR_NAME_SIMPLE; + push_LS(PC, LS_VAR_NAME_SIMPLE); result=c; goto break2; } if(c=='^') { - push_LS(PC); PC->ls=LS_METHOD_NAME; + push_LS(PC, LS_METHOD_NAME); result=c; goto break2; } @@ -497,7 +592,7 @@ int yylex(YYSTYPE *lvalp, void *pc) { } pop_LS(PC); PC->source--; PC->col--; - result=END_OF_NAME; + result=EON; goto break2; } if(c==0) { @@ -512,13 +607,16 @@ break2: return result; else { PC->pending_state=result; - /* append last piece */ + // strip last \n before LS_DEF_NAME or EOF + if((c=='@' || c==0) && end[-1]=='\n') + end--; + // append last piece PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/); - /* create STRING value: array of OP_STRING+string */ + // create STRING value: array of OP_STRING+string *lvalp=L(PC->string); - /* new pieces storage */ + // new pieces storage PC->string=new(pool) String(pool); - /* go */ + // go! return STRING; } }