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