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