--- parser3/src/main/compile.y 2001/03/14 08:50:03 1.98 +++ parser3/src/main/compile.y 2001/03/24 16:29:09 1.112 @@ -1,20 +1,22 @@ -/* - Parser +/** @file + Parser: compiler(lexical parser and grammar). + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.98 2001/03/14 08:50:03 paf Exp $ + $Id: compile.y,v 1.112 2001/03/24 16:29:09 paf Exp $ */ -/* - TODO.parser4: - cache compiled code from request to request. to do that... - 1: make method definitions, @CLASS, @BASE, @USE instructions, +/** + @todo parser4: + - cache compiled code from request to request. to do that... + -#: make method definitions, @CLASS, @BASE, @USE instructions, which would be executed afterwards, and actions now performed at compile time would be delayed to run time. - 2: make cache expiration on time and on disk-change of class source - 3: in apache use subpools for compiled class storage - 4: in iis make up specialized Pool object for that + -#: make cache expiration on time and on disk-change of class source + -#: in apache use subpools for compiled class storage + -#: in iis make up specialized Pool object for that */ %{ @@ -22,12 +24,12 @@ #define YYPARSE_PARAM pc #define YYLEX_PARAM pc #define YYDEBUG 1 -#define YYERROR_VERBOSE +#define YYERROR_VERBOSE 1 #define yyerror(msg) real_yyerror((parse_control *)pc, msg) #define YYPRINT(file, type, value) yyprint(file, type, value) +#include "pa_config_includes.h" #include -#include #include #include "compile_tools.h" @@ -50,6 +52,7 @@ int yylex(YYSTYPE *lvalp, void *pc); #define POOL *PC->pool #undef NEW #define NEW new(POOL) +#ifndef DOXYGEN %} %pure_parser @@ -130,7 +133,7 @@ control_method: '@' STRING '\n' YYERROR; } if(command==CLASS_NAME) { - if(PC->cclass!=&PC->request->root_class) { // already changed from default? + if(PC->cclass!=&PC->request->ROOT) { // already changed from default? strcpy(PC->error, "class already have a name '"); strncat(PC->error, PC->cclass->name().cstr(), 100); strcat(PC->error, "'"); @@ -143,7 +146,7 @@ control_method: '@' STRING '\n' PC->cclass=NEW VClass(POOL); PC->cclass->set_name(*name); // defaulting base. may change with @BASE - PC->cclass->set_base(PC->request->root_class); + PC->cclass->set_base(PC->request->ROOT); // append to request's classes PC->request->classes().put(*name, PC->cclass); } else { @@ -152,20 +155,17 @@ control_method: '@' STRING '\n' } } else { if(command==USE_CONTROL_METHOD_NAME) { - for(int i=0; isize(); i+=2) { - String file(*SLA2S(strings_code, i)); - file.APPEND_CONST(".p"); - PC->request->use_file(file.cstr()); - } + for(int i=0; isize(); i+=2) + PC->request->use_file( + PC->request->absolute(*SLA2S(strings_code, i))); } else if(command==BASE_NAME) { - if(PC->cclass->base()!=&PC->request->root_class) { // already changed from default? + if(PC->cclass->base()!=&PC->request->ROOT) { // already changed from default? strcpy(PC->error, "class already have a base '"); strncat(PC->error, PC->cclass->base()->name().cstr(), 100); strcat(PC->error, "'"); YYERROR; } if(strings_code->size()==1*2) { - // TODO: преодолеть self и циклические base const String& base_name=*SLA2S(strings_code); VClass *base=static_cast( PC->request->classes().get(base_name)); @@ -174,6 +174,11 @@ control_method: '@' STRING '\n' strcat(PC->error, ": undefined class in @"BASE_NAME); YYERROR; } + // @CLASS == @BASE sanity check + if(PC->cclass==base) { + strcpy(PC->error, "@"CLASS_NAME" equals @"BASE_NAME); + YYERROR; + } PC->cclass->set_base(*base); } else { strcpy(PC->error, "@"BASE_NAME" must contain sole name"); @@ -239,7 +244,7 @@ action: get | put | with | call; get: get_value { $$=$1; /* stack: resulting value */ - O($$, OP_WRITE); /* value=pop; wcontext.write(value) */ + O($$, OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */ }; get_value: '$' get_name_value { $$=$2 } get_name_value: name_without_curly_rdive EON | name_in_curly_rdive; @@ -332,7 +337,7 @@ codes__excluding_sole_str_literal: actio call: call_value { $$=$1; /* stack: value */ - O($$, OP_WRITE); /* value=pop; wcontext.write(value) */ + O($$, OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */ }; call_value: '^' call_name store_params EON { /* ^field.$method{vasya} */ $$=$2; /* with_xxx,diving code; stack: context,method_junction */ @@ -377,7 +382,7 @@ store_code_param_part: ; store_expr_param_part: write_expr_value { $$=N(POOL); - PCA($$, $1); + PEA($$, $1); }; store_curly_param_part: maybe_codes { $$=N(POOL); @@ -385,7 +390,7 @@ store_curly_param_part: maybe_codes { }; write_expr_value: expr_value { $$=$1; - O($$, OP_WRITE); + O($$, OP_WRITE_EXPR_RESULT); }; /* name */ @@ -419,7 +424,7 @@ name_expr_with_subvar_value: STRING subv $$=N(POOL); O($$, OP_CREATE_EWPOOL); P($$, $1); - O($$, OP_WRITE); + O($$, OP_WRITE_VALUE); P($$, $2); O($$, OP_REDUCE_EWPOOL); }; @@ -453,7 +458,7 @@ with: '$' name_without_curly_rdive '{' c O($$, OP_CREATE_RWPOOL); P($$, $4); O($$, OP_REDUCE_RWPOOL); - O($$, OP_WRITE); + O($$, OP_WRITE_VALUE); }; /* expr */ @@ -512,7 +517,7 @@ string_inside_quotes_value: maybe_codes /* basics */ write_string: STRING { - // optimized from OP_STRING+OP_WRITE to OP_STRING__WRITE + // optimized from OP_STRING+OP_WRITE_VALUE to OP_STRING__WRITE change_string_literal_to_write_string_literal($$=$1) }; @@ -520,6 +525,7 @@ empty_string_value: /* empty */ { $$=VL( empty: /* empty */ { $$=N(POOL) }; %% +#endif /* 000$111(2222)00 @@ -537,7 +543,7 @@ empty: /* empty */ { $$=N(POOL) }; 4:[^({]=pop */ -int yylex(YYSTYPE *lvalp, void *pc) { +static int yylex(YYSTYPE *lvalp, void *pc) { #define lexical_brackets_nestage PC->brackets_nestages[PC->sp] #define RC {result=c; goto break2; } @@ -584,6 +590,17 @@ int yylex(YYSTYPE *lvalp, void *pc) { // skip analysis = forced literal continue; } + if(c=='#' && PC->col==1) { + if(end!=begin) { + // append piece till # + PC->string->APPEND(begin, end-begin, PC->file, begin_line); + } + // reset piece 'begin' position & line + begin=PC->source; + begin_line=PC->line; + // fall into COMMENT lexical state [wait for \n] + push_LS(PC, LS_COMMENT); + } switch(PC->ls) { // USER'S = NOT OURS @@ -604,6 +621,18 @@ int yylex(YYSTYPE *lvalp, void *pc) { } break; + // #COMMENT + case LS_COMMENT: + if(c=='\n') { + // skip comment + begin=PC->source; + begin_line=PC->line; + + pop_LS(PC); + continue; + } + break; + // STRING IN EXPRESSION case LS_EXPRESSION_STRING: switch(c) { @@ -808,6 +837,8 @@ int yylex(YYSTYPE *lvalp, void *pc) { case '+': case '*': case '/': case '%': case '&': case '|': case '=': case '!': + // common delimiters + case '\'': case ',': pop_LS(PC); PC->source--; if(--PC->col<0) { PC->line--; PC->col=-1; } result=EON; @@ -1008,21 +1039,12 @@ break2: return result; } -int real_yyerror(parse_control *pc, char *s) /* Called by yyparse on error */ - { - //fprintf(stderr, "[%s]\n", s); - - strncpy(pc->error, s, MAX_STRING); // TODO: перепроверить с треклятым последним байтом +static int real_yyerror(parse_control *pc, char *s) { // Called by yyparse on error + strncpy(pc->error, s, MAX_STRING); return 1; - } - -static void - yyprint( - FILE *file, - int type, - YYSTYPE value) - { - if(type==STRING) - fprintf(file, " \"%s\"", SLA2S(value)->cstr()); - } +} +static void yyprint(FILE *file, int type, YYSTYPE value) { + if(type==STRING) + fprintf(file, " \"%s\"", SLA2S(value)->cstr()); +}