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