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