Annotation of parser3/src/main/compile.y, revision 1.88
1.24 paf 1: /*
1.87 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
4: Author: Alexander Petrosyan <paf@design.ru>
5:
1.88 ! paf 6: $Id: compile.y,v 1.87 2001/03/10 16:34:38 paf Exp $
! 7: */
! 8:
! 9: /*
! 10: TODO.parser4:
! 11: cache compiled code from request to request. to do that...
! 12: 1: make method definitions, @CLASS, @BASE, @USE instructions,
! 13: which would be executed afterwards, and actions
! 14: now performed at compile time would be delayed to run time.
! 15: 2: make cache expiration on time and on disk-change of class source
! 16: 3: in apache use subpools for compiled class storage
! 17: 4: in iis make up specialized Pool object for that
1.24 paf 18: */
19:
1.1 paf 20: %{
1.9 paf 21: #define YYSTYPE Array/*<op>*/ *
22: #define YYPARSE_PARAM pc
23: #define YYLEX_PARAM pc
24: #define YYDEBUG 1
1.1 paf 25: #define YYERROR_VERBOSE
1.9 paf 26: #define yyerror(msg) real_yyerror((parse_control *)pc, msg)
27: #define YYPRINT(file, type, value) yyprint(file, type, value)
1.1 paf 28:
29: #include <stdio.h>
30: #include <string.h>
31: #include <stdlib.h>
32:
33: #include "compile_tools.h"
1.8 paf 34: #include "pa_value.h"
1.12 paf 35: #include "pa_request.h"
1.39 paf 36: #include "pa_vobject.h"
1.52 paf 37: #include "pa_vdouble.h"
1.85 paf 38: #include "core.h"
1.39 paf 39:
1.85 paf 40: #define SELF_ELEMENT_NAME "self"
41: #define USE_CONTROL_METHOD_NAME "USE"
1.1 paf 42:
1.9 paf 43: int real_yyerror(parse_control *pc, char *s);
44: static void yyprint(FILE *file, int type, YYSTYPE value);
1.1 paf 45: int yylex(YYSTYPE *lvalp, void *pc);
46:
47:
1.8 paf 48: // local convinient inplace typecast & var
1.9 paf 49: #define PC ((parse_control *)pc)
1.25 paf 50: #define POOL *PC->pool
51: #undef NEW
52: #define NEW new(POOL)
1.1 paf 53: %}
54:
55: %pure_parser
56:
1.13 paf 57: %token EON
1.4 paf 58: %token STRING
1.1 paf 59: %token BOGUS
1.55 paf 60:
1.58 paf 61: %token BAD_STRING_COMPARISON_OPERATOR
62:
1.59 paf 63: %token LAND "&&"
1.58 paf 64: %token LOR "||"
1.59 paf 65: %token LXOR "##"
1.58 paf 66:
67: %token NLE "<="
68: %token NGE ">="
69: %token NEQ "=="
70: %token NNE "!="
71:
72: %token SLT "lt"
73: %token SGT "gt"
74: %token SLE "le"
75: %token SGE "ge"
76: %token SEQ "eq"
77: %token SNE "ne"
78:
1.67 paf 79: %token DEF "def"
80: %token IN "in"
81: %token FEXISTS "-f"
82:
1.57 paf 83: /* logical */
1.61 paf 84: %left "lt" "gt" "le" "ge"
85: %left "eq" "ne"
1.59 paf 86: %left '<' '>' "<=" ">=" "##"
1.57 paf 87: %left "==" "!="
88: %left "||"
89: %left "&&"
1.67 paf 90: %left "def" "in" "-f"
1.68 paf 91: %left '!'
1.57 paf 92:
93: /* bitwise */
1.59 paf 94: %left '#'
1.57 paf 95: %left '&' '|'
1.68 paf 96: %left '~'
1.57 paf 97:
1.56 paf 98: /* numerical */
1.55 paf 99: %left '-' '+'
1.57 paf 100: %left '*' '/' '%'
1.56 paf 101: %left NEG /* negation: unary - */
1.1 paf 102:
103: %%
1.88 ! paf 104: all:
1.10 paf 105: one_big_piece {
1.75 paf 106: Method& method=*NEW Method(POOL,
1.85 paf 107: *main_method_name,
1.81 paf 108: 0, 0, /*min, max numbered_params_count*/
1.75 paf 109: 0/*param_names*/, 0/*local_names*/,
110: $1/*parser_code*/, 0/*native_code*/);
1.85 paf 111: PC->vclass->add_method(*main_method_name, method);
1.10 paf 112: }
113: | methods;
114:
115: methods: method | methods method;
116: one_big_piece: maybe_codes;
117:
1.34 paf 118: method: control_method | code_method;
119:
120: control_method: '@' STRING '\n'
121: control_strings {
1.82 paf 122: const String& command=*SLA2S($2);
1.34 paf 123: YYSTYPE strings_code=$4;
1.37 paf 124: if(strings_code->size()<1*2) {
125: strcpy(PC->error, "@");
1.74 paf 126: strcat(PC->error, command.cstr());
1.37 paf 127: strcat(PC->error, " is empty");
128: YYERROR;
129: }
1.74 paf 130: if(command==CLASS_NAME) {
1.76 paf 131: if(PC->vclass!=&PC->request->root_class) { // already changed from default?
1.73 paf 132: strcpy(PC->error, "class already have a name '");
133: strncat(PC->error, PC->vclass->name().cstr(), 100);
134: strcat(PC->error, "'");
135: YYERROR;
136: }
137: if(strings_code->size()==1*2) {
138: // new class' name
1.82 paf 139: const String *name=SLA2S(strings_code);
1.73 paf 140: // creating the class
141: PC->vclass=NEW VClass(POOL);
142: PC->vclass->set_name(*name);
143: // defaulting base. may change with @BASE
1.76 paf 144: PC->vclass->set_base(PC->request->root_class);
1.73 paf 145: // append to request's classes
146: PC->request->classes_array()+=PC->vclass;
147: PC->request->classes().put(*name, PC->vclass);
148: } else {
1.34 paf 149: strcpy(PC->error, "@"CLASS_NAME" must contain sole name");
150: YYERROR;
151: }
152: } else {
1.85 paf 153: if(command==USE_CONTROL_METHOD_NAME) {
1.34 paf 154: for(int i=0; i<strings_code->size(); i+=2) {
1.82 paf 155: String file(*SLA2S(strings_code, i));
156: file.APPEND_CONST(".p");
1.86 paf 157: PC->request->use(file.cstr());
1.34 paf 158: }
1.74 paf 159: } else if(command==BASE_NAME) {
1.76 paf 160: if(PC->vclass->base()!=&PC->request->root_class) { // already changed from default?
1.86 paf 161: strcpy(PC->error, "class already have a base '");
162: strncat(PC->error, PC->vclass->base()->name().cstr(), 100);
163: strcat(PC->error, "'");
1.73 paf 164: YYERROR;
165: }
1.45 paf 166: if(strings_code->size()==1*2) {
1.73 paf 167: // TODO: преодолеть self и циклические base
1.82 paf 168: const String& base_name=*SLA2S(strings_code);
1.45 paf 169: VClass *base=static_cast<VClass *>(
170: PC->request->classes().get(base_name));
171: if(!base) {
172: strcpy(PC->error, base_name.cstr());
173: strcat(PC->error, ": undefined class in @"BASE_NAME);
1.34 paf 174: YYERROR;
175: }
1.45 paf 176: PC->vclass->set_base(*base);
177: } else {
178: strcpy(PC->error, "@"BASE_NAME" must contain sole name");
179: YYERROR;
1.34 paf 180: }
181: } else {
1.74 paf 182: strcpy(PC->error, command.cstr());
1.34 paf 183: strcat(PC->error, ": invalid special name. valid names are "
1.85 paf 184: CLASS_NAME", "USE_CONTROL_METHOD_NAME" and "BASE_NAME);
1.34 paf 185: YYERROR;
186: }
187: }
188: };
1.37 paf 189: control_strings: control_string | control_strings control_string { $$=$1; P($$, $2) };
190: control_string: maybe_string '\n';
191: maybe_string: empty | STRING;
1.34 paf 192:
193: code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n'
1.10 paf 194: maybe_codes {
1.38 paf 195: const String *name=SLA2S($2);
1.10 paf 196:
197: YYSTYPE params_names_code=$3;
1.75 paf 198: Array *params_names=0;
199: if(int size=params_names_code->size()) {
200: params_names=NEW Array(POOL);
201: for(int i=0; i<size; i+=2)
202: *params_names+=SLA2S(params_names_code, i);
203: }
1.10 paf 204:
205: YYSTYPE locals_names_code=$4;
1.75 paf 206: Array *locals_names=0;
207: if(int size=locals_names_code->size()) {
208: locals_names=NEW Array(POOL);
209: for(int i=0; i<size; i+=2)
210: *locals_names+=SLA2S(locals_names_code, i);
211: }
1.10 paf 212:
1.76 paf 213: Method& method=*NEW Method(POOL,
214: *name,
1.81 paf 215: 0, 0/*min,max numbered_params_count*/,
1.76 paf 216: params_names, locals_names,
217: $7, 0);
1.34 paf 218: PC->vclass->add_method(*name, method);
1.8 paf 219: };
1.10 paf 220:
221: maybe_bracketed_strings: empty | bracketed_maybe_strings;
222: bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2};
223: maybe_strings: empty | strings;
224: strings: STRING | strings ';' STRING { $$=$1; P($$, $3) };
225:
226: maybe_comment: empty | STRING;
1.1 paf 227:
228: /* codes */
229:
1.10 paf 230: maybe_codes: empty | codes;
231:
1.1 paf 232: codes: code | codes code {
233: $$=$1;
1.9 paf 234: P($$, $2);
1.1 paf 235: };
1.81 paf 236: code: write_string | action;
1.1 paf 237: action: get | put | with | call;
238:
239: /* get */
240:
1.64 paf 241: get: get_value {
242: $$=$1; /* stack: resulting value */
1.66 paf 243: O($$, OP_WRITE); /* value=pop; wcontext.write(value) */
1.1 paf 244: };
1.64 paf 245: get_value: '$' get_name_value { $$=$2 }
246: get_name_value: name_without_curly_rdive EON | name_in_curly_rdive;
1.1 paf 247: name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 };
1.44 paf 248: name_without_curly_rdive:
249: name_without_curly_rdive_read
250: | name_without_curly_rdive_root
251: | name_without_curly_rdive_class;
1.19 paf 252: name_without_curly_rdive_read: name_without_curly_rdive_code {
1.25 paf 253: $$=N(POOL);
1.22 paf 254: Array *diving_code=$1;
1.82 paf 255: const String *first_name=SLA2S(diving_code);
1.85 paf 256: if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54 paf 257: O($$, OP_WITH_SELF); /* stack: starting context */
1.22 paf 258: P($$, diving_code,
259: /* skip over... */
260: diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
261: } else {
1.54 paf 262: O($$, OP_WITH_READ); /* stack: starting context */
1.22 paf 263: P($$, diving_code);
264: }
265: /* diving code; stack: current context */
1.1 paf 266: };
1.19 paf 267: name_without_curly_rdive_root: ':' name_without_curly_rdive_code {
1.25 paf 268: $$=N(POOL);
1.54 paf 269: O($$, OP_WITH_ROOT); /* stack: starting context */
1.19 paf 270: P($$, $2); /* diving code; stack: current context */
271: };
1.44 paf 272: name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P($$, $2) };
1.19 paf 273: name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) };
1.1 paf 274:
275: /* put */
276:
1.81 paf 277: put: '$' name_expr_wdive construct {
1.20 paf 278: $$=$2; /* stack: context,name */
1.52 paf 279: P($$, $3); /* stack: context,name,constructor_value */
1.20 paf 280: };
1.44 paf 281: name_expr_wdive:
282: name_expr_wdive_write
283: | name_expr_wdive_root
284: | name_expr_wdive_class;
1.28 paf 285: name_expr_wdive_write: name_expr_dive_code {
1.44 paf 286: $$=N(POOL);
1.23 paf 287: Array *diving_code=$1;
1.82 paf 288: const String *first_name=SLA2S(diving_code);
1.85 paf 289: if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54 paf 290: O($$, OP_WITH_SELF); /* stack: starting context */
1.23 paf 291: P($$, diving_code,
292: /* skip over... */
293: diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
294: } else {
1.54 paf 295: O($$, OP_WITH_WRITE); /* stack: starting context */
1.23 paf 296: P($$, diving_code);
297: }
298: /* diving code; stack: current context */
1.20 paf 299: };
1.28 paf 300: name_expr_wdive_root: ':' name_expr_dive_code {
1.25 paf 301: $$=N(POOL);
1.54 paf 302: O($$, OP_WITH_ROOT); /* stack: starting context */
1.9 paf 303: P($$, $2); /* diving code; stack: context,name */
1.1 paf 304: };
1.44 paf 305: name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P($$, $2) };
1.20 paf 306:
1.81 paf 307: construct: construct_by_code | construct_by_expr;
308: construct_by_code: '[' any_constructor_code_value ']' {
309: $$=$2; /* stack: context, name, value */
310: O($$, OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */
311: }
312: ;
313: construct_by_expr: '(' any_expr ')' {
314: $$=$2; /* stack: context, name, value */
315: O($$, OP_CONSTRUCT_EXPR); /* value=pop; name=pop; context=pop; construct(context,name,value) */
316: }
1.52 paf 317: ;
1.55 paf 318: any_constructor_code_value:
319: empty_string_value /* optimized $var[] case */
1.52 paf 320: | STRING /* optimized $var[STRING] case */
1.55 paf 321: | constructor_code_value /* $var[something complex] */
1.1 paf 322: ;
1.55 paf 323: constructor_code_value: constructor_code {
1.25 paf 324: $$=N(POOL);
1.54 paf 325: O($$, OP_CREATE_EWPOOL); /* stack: empty write context */
1.69 paf 326: P($$, $1); /* some code that writes to that context */
1.54 paf 327: O($$, OP_REDUCE_EWPOOL); /* context=pop; stack: context.value() */
1.1 paf 328: };
1.55 paf 329: constructor_code: codes__excluding_sole_str_literal;
1.27 paf 330: codes__excluding_sole_str_literal: action | code codes { $$=$1; P($$, $2) };
331:
1.1 paf 332: /* call */
333:
1.66 paf 334: call: call_value {
335: $$=$1; /* stack: value */
336: O($$, OP_WRITE); /* value=pop; wcontext.write(value) */
337: };
338: call_value: '^' call_name store_params EON { /* ^field.$method{vasya} */
1.42 paf 339: $$=$2; /* with_xxx,diving code; stack: context,method_junction */
1.54 paf 340: O($$, OP_GET_METHOD_FRAME); /* stack: context,method_frame */
1.9 paf 341: P($$, $3); /* filling method_frame.store_params */
1.66 paf 342: O($$, OP_CALL); /* method_frame=pop; ncontext=pop; call(ncontext,method_frame) stack: value */
1.1 paf 343: };
344:
1.43 paf 345: call_name: name_without_curly_rdive;
1.38 paf 346:
1.9 paf 347: store_params: store_param | store_params store_param { $$=$1; P($$, $2) };
1.69 paf 348: store_param:
349: store_square_param
350: | store_round_param
351: | store_curly_param
1.31 paf 352: ;
1.69 paf 353: store_square_param: '[' store_code_param_parts ']' {$$=$2};
354: store_round_param: '(' store_expr_param_parts ')' {$$=$2};
1.10 paf 355: store_curly_param: '{' maybe_codes '}' {
1.25 paf 356: $$=N(POOL);
1.29 paf 357: PCA($$, $2);
1.1 paf 358: };
1.69 paf 359: store_code_param_parts:
360: store_code_param_part
361: | store_code_param_parts ';' store_code_param_part { $$=$1; P($$, $3) }
362: ;
363: store_expr_param_parts:
364: store_expr_param_part
365: | store_expr_param_parts ';' store_expr_param_part { $$=$1; P($$, $3) }
366: ;
367: store_code_param_part:
1.78 paf 368: empty /* optimized [] case */
369: | STRING { /* optimized [STRING] case */
1.32 paf 370: $$=$1;
1.54 paf 371: O($$, OP_STORE_PARAM);
1.32 paf 372: }
1.78 paf 373: | constructor_code_value { /* [something complex] */
1.32 paf 374: $$=$1;
1.77 paf 375: O($$, OP_STORE_PARAM);
1.32 paf 376: }
377: ;
1.69 paf 378: store_expr_param_part: write_expr_value {
379: $$=N(POOL);
380: PCA($$, $1);
381: };
382: write_expr_value: any_expr {
383: $$=$1;
384: O($$, OP_WRITE);
385: };
1.1 paf 386:
387: /* name */
388:
1.20 paf 389: name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P($$, $2) };
1.1 paf 390:
1.9 paf 391: name_path: name_step | name_path name_step { $$=$1; P($$, $2) };
1.1 paf 392: name_step: name_advance1 '.';
393: name_advance1: name_expr_value {
394: /* stack: context */
395: $$=$1; /* stack: context,name */
1.54 paf 396: O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1 paf 397: };
398: name_advance2: name_expr_value {
399: /* stack: context */
400: $$=$1; /* stack: context,name */
1.54 paf 401: O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1 paf 402: }
1.4 paf 403: | STRING BOGUS
1.1 paf 404: ;
405: name_expr_value:
1.4 paf 406: STRING /* subname_is_const */
1.1 paf 407: | name_expr_subvar_value /* $subname_is_var_value */
408: | name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value[$...] */
409: ;
410: name_expr_subvar_value: '$' subvar_ref_name_rdive {
411: $$=$2;
1.54 paf 412: O($$, OP_GET_ELEMENT);
1.1 paf 413: };
1.4 paf 414: name_expr_with_subvar_value: STRING subvar_get_writes {
1.25 paf 415: $$=N(POOL);
1.54 paf 416: O($$, OP_CREATE_EWPOOL);
1.9 paf 417: P($$, $1);
1.54 paf 418: O($$, OP_WRITE);
1.9 paf 419: P($$, $2);
1.54 paf 420: O($$, OP_REDUCE_EWPOOL);
1.1 paf 421: };
1.18 paf 422: subvar_ref_name_rdive: subvar_ref_name_rdive_read | subvar_ref_name_rdive_root;
423: subvar_ref_name_rdive_read: STRING {
1.25 paf 424: $$=N(POOL);
1.54 paf 425: O($$, OP_WITH_READ);
1.9 paf 426: P($$, $1);
1.1 paf 427: };
1.18 paf 428: subvar_ref_name_rdive_root: ':' STRING {
1.25 paf 429: $$=N(POOL);
1.54 paf 430: O($$, OP_WITH_ROOT);
1.18 paf 431: P($$, $2);
432: };
1.9 paf 433: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1 paf 434: subvar__get_write: '$' subvar_ref_name_rdive {
435: $$=$2;
1.54 paf 436: O($$, OP_GET_ELEMENT__WRITE);
1.42 paf 437: };
438:
1.44 paf 439: class_prefix: STRING ':' {
1.72 paf 440: $$=$1; // stack: class name string
441: O($$, OP_GET_CLASS);
1.1 paf 442: };
443:
444:
445: /* with */
446:
447: with: '$' name_without_curly_rdive '{' codes '}' {
448: $$=$2;
1.54 paf 449: O($$, OP_CREATE_RWPOOL);
1.9 paf 450: P($$, $4);
1.54 paf 451: O($$, OP_REDUCE_RWPOOL);
452: O($$, OP_WRITE);
1.1 paf 453: };
1.53 paf 454:
1.56 paf 455: /* expr */
1.53 paf 456:
1.56 paf 457: any_expr:
1.55 paf 458: empty_double_value /* optimized $var() case */
1.81 paf 459: | expr_value /* $var(something) */
1.53 paf 460: ;
1.81 paf 461: expr_value: expr {
462: if(($$=$1)->size()==2) // only one string literal in there?
1.62 paf 463: change_string_literal_to_double_literal($$); // make that string literal Double
464: };
1.56 paf 465: expr:
1.62 paf 466: STRING
1.64 paf 467: | get_value
1.66 paf 468: | call_value
1.64 paf 469: | '"' string_inside_quotes_value '"' { $$ = $2; }
1.60 paf 470: | '(' expr ')' { $$ = $2; }
471: /* stack: operand // stack: @operand */
472: | '-' expr %prec NEG { $$=$2; O($$, OP_NEG) }
1.68 paf 473: | '~' expr { $$=$2; O($$, OP_INV) }
474: | '!' expr { $$=$2; O($$, OP_NOT) }
475: | "def" expr { $$=$2; O($$, OP_DEF) }
476: | "in" expr { $$=$2; O($$, OP_IN) }
477: | "-f" expr { $$=$2; O($$, OP_FEXISTS) }
1.60 paf 478: /* stack: a,b // stack: a@b */
479: | expr '-' expr { $$=$1; P($$, $3); O($$, OP_SUB) }
480: | expr '+' expr { $$=$1; P($$, $3); O($$, OP_ADD) }
481: | expr '*' expr { $$=$1; P($$, $3); O($$, OP_MUL) }
482: | expr '/' expr { $$=$1; P($$, $3); O($$, OP_DIV) }
483: | expr '%' expr { $$=$1; P($$, $3); O($$, OP_MOD) }
484: | expr '&' expr { $$=$1; P($$, $3); O($$, OP_BIN_AND) }
485: | expr '|' expr { $$=$1; P($$, $3); O($$, OP_BIN_OR) }
486: | expr '#' expr { $$=$1; P($$, $3); O($$, OP_BIN_XOR) }
487: | expr "&&" expr { $$=$1; P($$, $3); O($$, OP_LOG_AND) }
488: | expr "||" expr { $$=$1; P($$, $3); O($$, OP_LOG_OR) }
489: | expr "##" expr { $$=$1; P($$, $3); O($$, OP_LOG_XOR) }
490: | expr '<' expr { $$=$1; P($$, $3); O($$, OP_NUM_LT) }
491: | expr '>' expr { $$=$1; P($$, $3); O($$, OP_NUM_GT) }
492: | expr "<=" expr { $$=$1; P($$, $3); O($$, OP_NUM_LE) }
493: | expr ">=" expr { $$=$1; P($$, $3); O($$, OP_NUM_GE) }
494: | expr "==" expr { $$=$1; P($$, $3); O($$, OP_NUM_EQ) }
495: | expr "!=" expr { $$=$1; P($$, $3); O($$, OP_NUM_NE) }
1.61 paf 496: | expr "lt" expr { $$=$1; P($$, $3); O($$, OP_STR_LT) }
497: | expr "gt" expr { $$=$1; P($$, $3); O($$, OP_STR_GT) }
498: | expr "le" expr { $$=$1; P($$, $3); O($$, OP_STR_LE) }
499: | expr "ge" expr { $$=$1; P($$, $3); O($$, OP_STR_GE) }
500: | expr "eq" expr { $$=$1; P($$, $3); O($$, OP_STR_EQ) }
501: | expr "ne" expr { $$=$1; P($$, $3); O($$, OP_STR_NE) }
1.56 paf 502: ;
1.55 paf 503:
1.65 paf 504: string_inside_quotes_value: maybe_codes {
1.64 paf 505: $$=N(POOL);
506: O($$, OP_CREATE_SWPOOL); /* stack: empty write context */
1.69 paf 507: P($$, $1); /* some code that writes to that context */
1.64 paf 508: O($$, OP_REDUCE_SWPOOL); /* context=pop; stack: context.get_string() */
1.53 paf 509: };
1.1 paf 510:
1.27 paf 511: /* basics */
1.1 paf 512:
1.81 paf 513: write_string: STRING {
1.84 paf 514: // optimized from OP_STRING+OP_WRITE to OP_STRING__WRITE
515: change_string_literal_to_write_string_literal($$=$1)
1.54 paf 516: };
517:
1.79 paf 518: empty_double_value: /* empty */ { $$=VL(NEW VDouble(POOL, 0)) };
1.81 paf 519: empty_string_value: /* empty */ { $$=VL(NEW VString(POOL)) };
1.25 paf 520: empty: /* empty */ { $$=N(POOL) };
1.1 paf 521:
522: %%
523:
524: /*
525: 000$111(2222)00
526: 000$111{3333}00
1.9 paf 527: $,^: push,=0
1.1 paf 528: 1:( { break=pop
529: 2:( ) pop
530: 3:{ } pop
531:
532: 000^111(2222)4444{33333}4000
1.9 paf 533: $,^: push,=0
1.1 paf 534: 1:( { break=pop
535: 2:( )=4
536: 3:{ }=4
537: 4:[^({]=pop
538: */
539:
540: int yylex(YYSTYPE *lvalp, void *pc) {
541: #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
1.48 paf 542: #define RC {result=c; goto break2; }
1.1 paf 543:
544: register int c;
545: int result;
546:
547: if(PC->pending_state) {
548: result=PC->pending_state;
549: PC->pending_state=0;
550: return result;
551: }
552:
1.9 paf 553: char *begin=PC->source;
554: char *end;
555: int begin_line=PC->line;
1.67 paf 556: int skip_analized=0;
1.50 paf 557: while(true) {
1.9 paf 558: c=*(end=(PC->source++));
1.1 paf 559:
1.4 paf 560: if(c=='\n') {
1.1 paf 561: PC->line++;
1.8 paf 562: PC->col=0;
1.10 paf 563: } else
1.4 paf 564: PC->col++;
1.73 paf 565:
566: // todo: # in 0+1 column comment
1.1 paf 567:
1.48 paf 568: // escaping: ^^ ^$ ^; ^) ^} ^( ^{ ^"
569: if(c=='^')
570: switch(*PC->source) {
571: case '^': case '$': case ';':
572: case '[': case ']':
573: case '{': case '}':
574: case '"':
1.40 paf 575: if(end!=begin) {
576: // append piece till ^
577: PC->string->APPEND(begin, end-begin, PC->file, begin_line);
578: }
1.63 paf 579: // reset piece 'begin' position & line
1.40 paf 580: begin=PC->source; // ^
1.9 paf 581: begin_line=PC->line;
1.40 paf 582: // skip over ^ and _
1.50 paf 583: PC->source++; PC->col++;
1.40 paf 584: // skip analysis = forced literal
1.1 paf 585: continue;
586: }
587: switch(PC->ls) {
1.10 paf 588:
589: // USER'S = NOT OURS
1.1 paf 590: case LS_USER:
1.48 paf 591: switch(c) {
592: case '$':
1.10 paf 593: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48 paf 594: RC;
595: case '^':
596: push_LS(PC, LS_METHOD_NAME);
597: RC;
598: case '@':
599: if(PC->col==0+1) {
600: push_LS(PC, LS_DEF_NAME);
601: RC;
602: }
603: break;
1.1 paf 604: }
1.48 paf 605: break;
606:
607: // STRING IN EXPRESSION
608: case LS_EXPRESSION_STRING:
609: switch(c) {
610: case '"':
611: pop_LS(PC); //"abc".
612: RC;
613: case '$':
614: push_LS(PC, LS_VAR_NAME_SIMPLE);
615: RC;
616: case '^':
1.10 paf 617: push_LS(PC, LS_METHOD_NAME);
1.48 paf 618: RC;
1.10 paf 619: }
620: break;
621:
622: // METHOD DEFINITION
623: case LS_DEF_NAME:
1.48 paf 624: switch(c) {
625: case '[':
1.10 paf 626: PC->ls=LS_DEF_PARAMS;
1.48 paf 627: RC;
628: case '\n':
629: PC->ls=LS_DEF_SPECIAL_BODY;
630: RC;
1.10 paf 631: }
632: break;
1.48 paf 633:
1.10 paf 634: case LS_DEF_PARAMS:
1.48 paf 635: switch(c) {
636: case ';':
637: RC;
638: case ']':
1.10 paf 639: PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48 paf 640: RC;
1.49 paf 641: case '\n': // wrong. bailing out
1.10 paf 642: pop_LS(PC);
1.48 paf 643: RC;
1.10 paf 644: }
645: break;
1.48 paf 646:
1.10 paf 647: case LS_DEF_LOCALS:
1.48 paf 648: switch(c) {
649: case '[':
650: case ';':
651: RC;
652: case ']':
1.10 paf 653: PC->ls=LS_DEF_COMMENT;
1.48 paf 654: RC;
655: case '\n': // wrong. bailing out
1.10 paf 656: pop_LS(PC);
1.48 paf 657: RC;
1.10 paf 658: }
659: break;
1.48 paf 660:
1.10 paf 661: case LS_DEF_COMMENT:
662: if(c=='\n') {
663: pop_LS(PC);
1.48 paf 664: RC;
1.37 paf 665: }
666: break;
667:
1.48 paf 668: case LS_DEF_SPECIAL_BODY:
1.37 paf 669: if(c=='\n') {
1.48 paf 670: switch(*PC->source) {
671: case '@': case 0: // end of special_code
1.37 paf 672: pop_LS(PC);
1.48 paf 673: break;
674: }
675: RC;
676: }
677: break;
678:
679: // (EXPRESSION)
1.69 paf 680: case LS_VAR_ROUND:
681: case LS_METHOD_ROUND:
1.48 paf 682: switch(c) {
683: case ')':
684: if(--lexical_brackets_nestage==0)
1.69 paf 685: if(PC->ls==LS_METHOD_ROUND) // method round param ended
686: PC->ls=LS_METHOD_AFTER; // look for method end
687: else // PC->ls==LS_VAR_ROUND // variable constructor ended
688: pop_LS(PC); // return to normal life
1.48 paf 689: RC;
690: case '$':
1.69 paf 691: push_LS(PC, LS_EXPRESSION_VAR_NAME);
1.48 paf 692: RC;
693: case '^':
694: push_LS(PC, LS_METHOD_NAME);
695: RC;
696: case '(':
697: lexical_brackets_nestage++;
698: RC;
1.67 paf 699: case '-':
700: if(*PC->source=='f') { // -f
701: skip_analized=1;
702: result=FEXISTS;
703: } else
704: result=c;
705: goto break2;
706: case '+': case '*': case '/': case '%':
1.58 paf 707: case '~':
1.48 paf 708: case ';':
709: RC;
1.59 paf 710: case '&': case '|': case '#':
1.58 paf 711: if(*PC->source==c) { // && ||
1.59 paf 712: result=c=='#'?LXOR:c=='&'?LAND:LOR;
1.67 paf 713: skip_analized=1;
1.58 paf 714: } else
715: result=c;
716: goto break2;
717: case '<': case '>': case '=': case '!':
718: if(*PC->source=='=') { // <= >= == !=
1.67 paf 719: skip_analized=1;
1.58 paf 720: switch(c) {
721: case '<': result=NLE; break;
722: case '>': result=NGE; break;
723: case '=': result=NEQ; break;
724: case '!': result=NNE; break;
725: }
726: } else
727: result=c;
728: goto break2;
1.48 paf 729: case '"':
730: push_LS(PC, LS_EXPRESSION_STRING);
731: RC;
1.50 paf 732: case 'l': case 'g': case 'e': case 'n':
1.51 paf 733: if(end==begin) // right after whitespace
1.58 paf 734: switch(*PC->source) {
1.51 paf 735: // case '?': // ok [and bad cases, yacc would bark at them]
736: case 't': // lt gt [et nt]
1.62 paf 737: result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
1.67 paf 738: skip_analized=1;
1.58 paf 739: goto break2;
1.51 paf 740: case 'e': // le ge ne [ee]
1.58 paf 741: result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
1.67 paf 742: skip_analized=1;
1.58 paf 743: goto break2;
1.51 paf 744: case 'q': // eq [lq gq nq]
1.58 paf 745: result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
1.67 paf 746: skip_analized=1;
747: goto break2;
748: }
749: break;
750: case 'i':
751: if(end==begin) // right after whitespace
752: if(PC->source[0]=='n') { // in
753: skip_analized=1;
754: result=IN;
755: goto break2;
756: }
757: break;
758: case 'd':
759: if(end==begin) // right after whitespace
760: if(PC->source[0]=='e' && PC->source[1]=='f') { // def
761: skip_analized=2;
762: result=DEF;
1.58 paf 763: goto break2;
1.50 paf 764: }
1.48 paf 765: break;
766: case ' ': case '\t': case '\n':
1.63 paf 767: if(end!=begin) { // there were a string after previous operator?
768: result=0; // return that string
769: goto break2;
1.48 paf 770: }
1.63 paf 771: // that's a leading|traling space or after-operator-space
772: // ignoring it
773: // reset piece 'begin' position & line
1.58 paf 774: begin=PC->source; // after whitespace char
1.48 paf 775: begin_line=PC->line;
776: continue;
1.1 paf 777: }
778: break;
779:
1.10 paf 780: // VARIABLE GET/PUT/WITH
1.1 paf 781: case LS_VAR_NAME_SIMPLE:
1.69 paf 782: case LS_EXPRESSION_VAR_NAME:
783: if(PC->ls==LS_EXPRESSION_VAR_NAME) {
1.56 paf 784: // name in expr ends also before binary operators
1.48 paf 785: switch(c) {
786: case '+': case '-': case '*': case '/': case '%':
787: case '&': case '|':
1.83 paf 788: case '=': case '!':
1.48 paf 789: pop_LS(PC);
790: PC->source--; if(--PC->col<0) { PC->line--; PC->col=-1; }
791: result=EON;
792: goto break2;
793: }
794: }
795: switch(c) {
796: case 0:
797: case ' ': case '\t': case '\n':
798: case ';':
1.64 paf 799: case ']': case '}': case ')': case '"':
1.83 paf 800: case '<': case '>': // these stand for HTML brackets and expression binary ops
1.1 paf 801: pop_LS(PC);
1.32 paf 802: PC->source--; if(--PC->col<0) { PC->line--; PC->col=-1; }
1.13 paf 803: result=EON;
1.1 paf 804: goto break2;
1.48 paf 805: case '[':
806: PC->ls=LS_VAR_SQUARE;
1.1 paf 807: lexical_brackets_nestage=1;
1.48 paf 808: RC;
809: case '{':
810: if(begin==end) { // ${name}, no need of EON, switching LS
811: PC->ls=LS_VAR_NAME_CURLY;
812: } else {
813: PC->ls=LS_VAR_CURLY;
814: lexical_brackets_nestage=1;
815: }
1.69 paf 816:
1.48 paf 817: RC;
818: case '(':
1.69 paf 819: PC->ls=LS_VAR_ROUND;
1.1 paf 820: lexical_brackets_nestage=1;
1.48 paf 821: RC;
822: case '.': // name part delim
823: case '$': // name part subvar
824: case ':': // ':name' or 'class:name'
825: RC;
1.1 paf 826: }
827: break;
1.48 paf 828:
1.1 paf 829: case LS_VAR_NAME_CURLY:
1.48 paf 830: switch(c) {
831: case '}': // ${name} finished, restoring LS
1.1 paf 832: pop_LS(PC);
1.48 paf 833: RC;
834: case '.': // name part delim
835: case '$': // name part subvar
836: case ':': // ':name' or 'class:name'
837: RC;
1.1 paf 838: }
839: break;
1.48 paf 840:
841: case LS_VAR_SQUARE:
842: switch(c) {
843: case '$':
1.10 paf 844: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48 paf 845: RC;
846: case '^':
1.10 paf 847: push_LS(PC, LS_METHOD_NAME);
1.48 paf 848: RC;
849: case ']':
1.1 paf 850: if(--lexical_brackets_nestage==0) {
851: pop_LS(PC);
1.48 paf 852: RC;
1.1 paf 853: }
1.48 paf 854: break;
855: case ';': // operator_or_fmt;value delim
856: RC;
857: case '[':
858: lexical_brackets_nestage++;
859: break;
1.1 paf 860: }
861: break;
1.48 paf 862:
1.1 paf 863: case LS_VAR_CURLY:
1.48 paf 864: switch(c) {
865: case '$':
1.10 paf 866: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48 paf 867: RC;
868: case '^':
1.10 paf 869: push_LS(PC, LS_METHOD_NAME);
1.48 paf 870: RC;
871: case '}':
1.1 paf 872: if(--lexical_brackets_nestage==0) {
873: pop_LS(PC);
1.48 paf 874: RC;
1.1 paf 875: }
1.48 paf 876: break;
877: case '{':
1.1 paf 878: lexical_brackets_nestage++;
1.48 paf 879: break;
880: }
1.1 paf 881: break;
882:
1.10 paf 883: // METHOD CALL
1.1 paf 884: case LS_METHOD_NAME:
1.48 paf 885: switch(c) {
886: case '[':
887: PC->ls=LS_METHOD_SQUARE;
1.1 paf 888: lexical_brackets_nestage=1;
1.48 paf 889: RC;
890: case '{':
1.1 paf 891: PC->ls=LS_METHOD_CURLY;
892: lexical_brackets_nestage=1;
1.48 paf 893: RC;
1.69 paf 894: case '(':
895: PC->ls=LS_METHOD_ROUND;
896: lexical_brackets_nestage=1;
897: RC;
1.48 paf 898: case '.': // name part delim
899: case '$': // name part subvar
900: case ':': // ':name' or 'class:name'
901: RC;
1.1 paf 902: }
903: break;
1.48 paf 904:
905: case LS_METHOD_SQUARE:
906: switch(c) {
907: case '$':
1.10 paf 908: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48 paf 909: RC;
910: case '^':
1.10 paf 911: push_LS(PC, LS_METHOD_NAME);
1.48 paf 912: RC;
913: case ';': // param delim
914: RC;
915: case ']':
1.1 paf 916: if(--lexical_brackets_nestage==0) {
917: PC->ls=LS_METHOD_AFTER;
1.48 paf 918: RC;
1.1 paf 919: }
1.48 paf 920: break;
921: case '[':
1.1 paf 922: lexical_brackets_nestage++;
1.48 paf 923: break;
924: }
1.1 paf 925: break;
1.48 paf 926:
1.1 paf 927: case LS_METHOD_CURLY:
1.48 paf 928: switch(c) {
929: case '$':
1.10 paf 930: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48 paf 931: RC;
932: case '^':
1.10 paf 933: push_LS(PC, LS_METHOD_NAME);
1.48 paf 934: RC;
935: case '}':
1.1 paf 936: if(--lexical_brackets_nestage==0) {
937: PC->ls=LS_METHOD_AFTER;
1.48 paf 938: RC;
1.1 paf 939: }
1.48 paf 940: break;
941: case '{':
1.1 paf 942: lexical_brackets_nestage++;
1.48 paf 943: break;
944: }
1.1 paf 945: break;
1.48 paf 946:
1.1 paf 947: case LS_METHOD_AFTER:
1.69 paf 948: if(c=='[') {/* ][ }[ )[ */
1.48 paf 949: PC->ls=LS_METHOD_SQUARE;
1.1 paf 950: lexical_brackets_nestage=1;
1.48 paf 951: RC;
1.1 paf 952: }
1.69 paf 953: if(c=='{') {/* ]{ }{ ){ */
1.1 paf 954: PC->ls=LS_METHOD_CURLY;
1.69 paf 955: lexical_brackets_nestage=1;
956: RC;
957: }
958: if(c=='(') {/* ]( }( )( */
959: PC->ls=LS_METHOD_ROUND;
1.1 paf 960: lexical_brackets_nestage=1;
1.48 paf 961: RC;
1.1 paf 962: }
963: pop_LS(PC);
1.32 paf 964: PC->source--; if(--PC->col<0) { PC->line--; PC->col=-1; }
1.13 paf 965: result=EON;
1.1 paf 966: goto break2;
967: }
1.9 paf 968: if(c==0) {
1.1 paf 969: result=-1;
970: break;
971: }
972: }
973:
974: break2:
1.59 paf 975: if(end!=begin) { // there is last piece?
976: if((c=='@' || c==0) && end[-1]=='\n') { // we are before LS_DEF_NAME or EOF?
977: // strip last \n
1.10 paf 978: end--;
1.59 paf 979: }
980: if(end!=begin) { // last piece still alive?
981: // append it
1.30 paf 982: PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
983: }
1.59 paf 984: }
985: if(PC->string->size()) { // something accumulated?
1.17 paf 986: // create STRING value: array of OP_VALUE+vstring
1.55 paf 987: *lvalp=VL(NEW VString(*PC->string));
1.10 paf 988: // new pieces storage
1.25 paf 989: PC->string=NEW String(POOL);
1.58 paf 990: // make current result be pending for next call, return STRING for now
991: PC->pending_state=result; result=STRING;
992: }
1.67 paf 993: if(skip_analized) {
994: PC->source+=skip_analized; PC->col+=skip_analized;
1.1 paf 995: }
1.58 paf 996: return result;
1.1 paf 997: }
998:
1.9 paf 999: int real_yyerror(parse_control *pc, char *s) /* Called by yyparse on error */
1.1 paf 1000: {
1.16 paf 1001: //fprintf(stderr, "[%s]\n", s);
1.6 paf 1002:
1.46 paf 1003: strncpy(pc->error, s, MAX_STRING); // TODO: перепроверить с треклятым последним байтом
1.1 paf 1004: return 1;
1005: }
1006:
1007: static void
1.9 paf 1008: yyprint(
1.1 paf 1009: FILE *file,
1010: int type,
1011: YYSTYPE value)
1012: {
1.9 paf 1013: if(type==STRING)
1.38 paf 1014: fprintf(file, " \"%s\"", SLA2S(value)->cstr());
1.1 paf 1015: }
1016:
E-mail: