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