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