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