--- parser3/src/main/compile.y 2001/03/13 09:05:31 1.94 +++ 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.94 2001/03/13 09:05:31 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" @@ -35,10 +37,10 @@ #include "pa_request.h" #include "pa_vobject.h" #include "pa_vdouble.h" -#include "core.h" +#include "pa_globals.h" #define SELF_ELEMENT_NAME "self" -#define USE_CONTROL_METHOD_NAME "use" +#define USE_CONTROL_METHOD_NAME "USE" int real_yyerror(parse_control *pc, char *s); static void yyprint(FILE *file, int type, YYSTYPE value); @@ -50,6 +52,7 @@ int yylex(YYSTYPE *lvalp, void *pc); #define POOL *PC->pool #undef NEW #define NEW new(POOL) +#ifndef DOXYGEN %} %pure_parser @@ -79,8 +82,10 @@ int yylex(YYSTYPE *lvalp, void *pc); %token DEF "def" %token IN "in" %token FEXISTS "-f" +%token IS "is" /* logical */ +%left "is" %left "lt" "gt" "le" "ge" %left "eq" "ne" %left '<' '>' "<=" ">=" "##" @@ -128,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, "'"); @@ -141,9 +146,8 @@ 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_array()+=PC->cclass; PC->request->classes().put(*name, PC->cclass); } else { strcpy(PC->error, "@"CLASS_NAME" must contain sole name"); @@ -151,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)); @@ -173,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"); @@ -238,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; @@ -331,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 */ @@ -376,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); @@ -384,7 +390,7 @@ store_curly_param_part: maybe_codes { }; write_expr_value: expr_value { $$=$1; - O($$, OP_WRITE); + O($$, OP_WRITE_EXPR_RESULT); }; /* name */ @@ -418,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); }; @@ -452,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 */ @@ -498,6 +504,7 @@ expr: | expr "ge" expr { $$=$1; P($$, $3); O($$, OP_STR_GE) } | expr "eq" expr { $$=$1; P($$, $3); O($$, OP_STR_EQ) } | expr "ne" expr { $$=$1; P($$, $3); O($$, OP_STR_NE) } +| expr "is" expr { $$=$1; P($$, $3); O($$, OP_IS) } ; string_inside_quotes_value: maybe_codes { @@ -510,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) }; @@ -518,6 +525,7 @@ empty_string_value: /* empty */ { $$=VL( empty: /* empty */ { $$=N(POOL) }; %% +#endif /* 000$111(2222)00 @@ -535,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; } @@ -582,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 @@ -602,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) { @@ -747,10 +778,19 @@ int yylex(YYSTYPE *lvalp, void *pc) { break; case 'i': if(end==begin) // right after whitespace - if(PC->source[0]=='n') { // in - skip_analized=1; - result=IN; - goto break2; + switch(PC->source[0]) { + case 'n': + { // in + skip_analized=1; + result=IN; + goto break2; + } + case 's': + { // is + skip_analized=1; + result=IS; + goto break2; + } } break; case 'd': @@ -797,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; @@ -997,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()); +}