|
|
1.1 paf 1: %{
1.9 paf 2: #define YYSTYPE Array/*<op>*/ *
3: #define YYPARSE_PARAM pc
4: #define YYLEX_PARAM pc
5: #define YYDEBUG 1
1.1 paf 6: #define YYERROR_VERBOSE
1.9 paf 7: #define yyerror(msg) real_yyerror((parse_control *)pc, msg)
8: #define YYPRINT(file, type, value) yyprint(file, type, value)
1.1 paf 9:
10: #include <stdio.h>
11: #include <string.h>
12: #include <stdlib.h>
13:
14: #include "compile_tools.h"
1.8 paf 15: #include "pa_value.h"
1.12 paf 16: #include "pa_request.h"
1.1 paf 17:
1.9 paf 18: int real_yyerror(parse_control *pc, char *s);
19: static void yyprint(FILE *file, int type, YYSTYPE value);
1.1 paf 20: int yylex(YYSTYPE *lvalp, void *pc);
21:
22:
1.8 paf 23: // local convinient inplace typecast & var
1.9 paf 24: #define PC ((parse_control *)pc)
25: #define pool *PC->pool
1.1 paf 26: %}
27:
28: %pure_parser
29:
1.13 paf 30: %token EON
1.4 paf 31: %token STRING
1.1 paf 32: %token BOGUS
33:
34: %%
35:
1.10 paf 36: all:
37: one_big_piece {
1.8 paf 38: String& name_main=*new(pool) String(pool);
1.11 paf 39: name_main.APPEND_CONST(MAIN_METHOD_NAME);
1.8 paf 40: Array& param_names=*new(pool) Array(pool);
41: Array& local_names=*new(pool) Array(pool);
42: Method *method=new(pool) Method(pool, name_main, param_names, local_names, *$1);
43: *PC->methods+=method;
1.10 paf 44: }
45: | methods;
46:
47: methods: method | methods method;
48: one_big_piece: maybe_codes;
49:
50: method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n'
51: maybe_codes {
52: const String *name=LA2S($2);
53:
54: YYSTYPE params_names_code=$3;
55: Array& params_names=*new(pool) Array(pool);
56: for(int i=0; i<params_names_code->size(); i+=2)
57: params_names+=LA2S(params_names_code, i);
58:
59: YYSTYPE locals_names_code=$4;
60: Array& locals_names=*new(pool) Array(pool);
61: for(int i=0; i<locals_names_code->size(); i+=2)
62: locals_names+=LA2S(locals_names_code, i);
63:
64: Method *method=new(pool) Method(pool, *name, params_names, locals_names, *$7);
65: *PC->methods+=method;
1.8 paf 66: };
1.10 paf 67:
68: maybe_bracketed_strings: empty | bracketed_maybe_strings;
69: bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2};
70: maybe_strings: empty | strings;
71: strings: STRING | strings ';' STRING { $$=$1; P($$, $3) };
72:
73: maybe_comment: empty | STRING;
1.1 paf 74:
75: /* codes */
76:
1.10 paf 77: maybe_codes: empty | codes;
78:
1.1 paf 79: codes: code | codes code {
80: $$=$1;
1.9 paf 81: P($$, $2);
1.1 paf 82: };
83: code: write_str_literal | action;
84: action: get | put | with | call;
85:
86: /* get */
87:
88: get: '$' any_name {
89: $$=$2; /* stack: resulting value */
1.17 paf 90: OP($$, OP_WRITE); /* value=pop; write(value) */
1.1 paf 91: };
92:
1.13 paf 93: any_name: name_without_curly_rdive EON | name_in_curly_rdive;
1.1 paf 94:
95: name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 };
96: name_without_curly_rdive: name_rdive {
97: /*
98: TODO: подсмотреть в $1, и если там в первом элементе первая буква ":"
99: то выкинуть её и делать не OP_WITH_READ, а WITH_ROOT
100: TODO: подсмотреть в $1, и если там первым элементом self,
101: то выкинуть его и делать не OP_WITH_READ, а WITH_SELF
102: */
1.8 paf 103: $$=N(pool); OP($$, OP_WITH_READ); /* stack: starting context */
1.9 paf 104: P($$, $1); /* diving code; stack: current context */
1.1 paf 105: };
1.10 paf 106: name_rdive: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) };
1.1 paf 107:
108: /* put */
109:
110: put: '$' name_expr_dive '(' constructor_value ')' {
111: /*
112: TODO: подсмотреть в $3, и если там в первом элементе первая буква ":"
113: то выкинуть её и делать не OP_WITH_OP_WRITE, а WITH_ROOT
114: TODO: подсмотреть в $3, и если там первым элементом self,
115: то выкинуть его и делать не OP_WITH_OP_WRITE, а WITH_SELF
116: если ничего не осталось - $self(xxx)
117: обругать
118: */
1.8 paf 119: $$=N(pool);
1.3 paf 120: OP($$, OP_WITH_WRITE); /* stack: starting context */
1.9 paf 121: P($$, $2); /* diving code; stack: context,name */
122: P($$, $4); /* stack: context,name,constructor_value */
123: OP($$, OP_CONSTRUCT); /* value=pop; name=pop; context=pop; construct(context,name,value) */
1.1 paf 124: };
125: constructor_value:
126: constructor_one_param_value
127: | constructor_two_params_value /* $var(=;2*2) $var(%d;2*2) $var(+;1) */
128: ;
129: constructor_one_param_value:
1.15 paf 130: empty /* optimized $var() case */
1.17 paf 131: | STRING /* optimized $var(STRING) case */
1.1 paf 132: | complex_constructor_param_value /* $var(something complex) */
133: ;
134: complex_constructor_param_value: complex_constructor_param_body {
1.8 paf 135: $$=N(pool);
1.3 paf 136: OP($$, OP_CREATE_EWPOOL); /* stack: empty write context */
1.9 paf 137: P($$, $1); /* some codes to that context */
138: OP($$, OP_REDUCE_EWPOOL); /* context=pop; stack: context.value() */
1.1 paf 139: };
140: complex_constructor_param_body:
141: codes__excluding_sole_str_literal
142: | codes__str__followed_by__excluding_sole_str_literal
143: ;
1.4 paf 144: constructor_two_params_value: STRING ';' constructor_one_param_value {
1.7 paf 145: char *operator_or_fmt=LA2S($1)->cstr();
1.8 paf 146: $$=N(pool);
1.9 paf 147: P($$, $1); /* stack: ncontext name operator_or_fmt */
1.3 paf 148: P($$, $3); /* stack: ncontext name operator_or_fmt expr */
1.1 paf 149: switch(operator_or_fmt[0]) {
150: case '=': case '%':
1.3 paf 151: OP($$, OP_EXPRESSION_EVAL);
1.1 paf 152: break;
153: case '+': case '-': case '*': case '/':
1.3 paf 154: OP($$, OP_MODIFY_EVAL);
1.1 paf 155: break;
156: default:
1.6 paf 157: strcpy(PC->error, "invalid modification operator");
158: YYERROR;
1.1 paf 159: }
160: /* stack: ncontext name value */
161: };
162:
163:
164: /* call */
165:
1.13 paf 166: call: '^' name_expr_dive store_params EON { /* ^field.$method{vasya} */
1.1 paf 167: /*
168: TODO: подсмотреть в $3, и если там в первом элементе первая буква ":"
169: то выкинуть её и делать не OP_WITH_READ, а WITH_ROOT
170: TODO: подсмотреть в $3, и если там первым элементом self,
171: то выкинуть его и делать не OP_WITH_READ, а WITH_SELF
172: TODO:
173: если первым в $3 идёт result
174: то
175: выкинуть его
176: если там ещё что-то осталось,
177: то
178: не OP_WITH_READ, а WITH_RESULT
179: иначе // ^result(value)
180: обругать безобразие
181: */
1.8 paf 182: $$=N(pool);
1.3 paf 183: OP($$, OP_WITH_READ); /* stack: starting context */
1.9 paf 184: P($$, $2); /* diving code; stack: context,method_name */
185: OP($$, OP_GET_METHOD_FRAME); /* stack: context,method_frame */
186: P($$, $3); /* filling method_frame.store_params */
187: OP($$, OP_CALL); /* method_frame=pop; ncontext=pop; call(ncontext,method_frame) */
1.1 paf 188: };
189:
1.9 paf 190: store_params: store_param | store_params store_param { $$=$1; P($$, $2) };
1.1 paf 191: store_param: store_round_param | store_curly_param;
192: store_round_param: '(' store_param_parts ')' {$$=$2};
1.9 paf 193: store_param_parts: store_param_part | store_param_parts ';' store_param_part { $$=$1; P($$, $3) };
1.1 paf 194: store_param_part: constructor_one_param_value {
195: $$=$1;
1.9 paf 196: OP($$, OP_STORE_PARAM);
1.1 paf 197: }
1.10 paf 198: store_curly_param: '{' maybe_codes '}' {
1.8 paf 199: $$=N(pool);
1.3 paf 200: OP($$, OP_CODE_ARRAY);
1.9 paf 201: AA($$, $2);
202: OP($$, OP_CREATE_JUNCTION);
203: OP($$, OP_STORE_PARAM);
1.1 paf 204: };
205:
206: /* name */
207:
1.9 paf 208: name_expr_dive: name_expr_value | name_path name_expr_value { $$=$1; P($$, $2) };
1.1 paf 209:
1.9 paf 210: name_path: name_step | name_path name_step { $$=$1; P($$, $2) };
1.1 paf 211: name_step: name_advance1 '.';
212: name_advance1: name_expr_value {
213: /* stack: context */
214: $$=$1; /* stack: context,name */
1.9 paf 215: OP($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1 paf 216: };
217: name_advance2: name_expr_value {
218: /* stack: context */
219: $$=$1; /* stack: context,name */
1.9 paf 220: OP($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1 paf 221: }
1.4 paf 222: | STRING BOGUS
1.1 paf 223: ;
224: name_expr_value:
1.4 paf 225: STRING /* subname_is_const */
1.1 paf 226: | name_expr_subvar_value /* $subname_is_var_value */
227: | name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value[$...] */
228: ;
229: name_expr_subvar_value: '$' subvar_ref_name_rdive {
230: $$=$2;
1.9 paf 231: OP($$, OP_GET_ELEMENT);
1.1 paf 232: };
1.4 paf 233: name_expr_with_subvar_value: STRING subvar_get_writes {
1.8 paf 234: $$=N(pool);
1.3 paf 235: OP($$, OP_CREATE_EWPOOL);
1.9 paf 236: P($$, $1);
1.17 paf 237: OP($$, OP_WRITE);
1.9 paf 238: P($$, $2);
239: OP($$, OP_REDUCE_EWPOOL);
1.1 paf 240: };
1.18 ! paf 241: subvar_ref_name_rdive: subvar_ref_name_rdive_read | subvar_ref_name_rdive_root;
! 242: subvar_ref_name_rdive_read: STRING {
! 243: $$=N(pool);
! 244: OP($$, OP_WITH_READ);
1.9 paf 245: P($$, $1);
1.1 paf 246: };
1.18 ! paf 247: subvar_ref_name_rdive_root: ':' STRING {
! 248: $$=N(pool);
! 249: OP($$, OP_WITH_ROOT);
! 250: P($$, $2);
! 251: };
1.9 paf 252: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1 paf 253: subvar__get_write: '$' subvar_ref_name_rdive {
254: $$=$2;
1.17 paf 255: OP($$, OP_GET_ELEMENT__WRITE);
1.1 paf 256: };
257:
258:
259: /* with */
260:
261: with: '$' name_without_curly_rdive '{' codes '}' {
262: $$=$2;
1.9 paf 263: OP($$, OP_CREATE_RWPOOL);
264: P($$, $4);
265: OP($$, OP_REDUCE_RWPOOL);
1.17 paf 266: OP($$, OP_WRITE);
1.1 paf 267: };
268:
269: /* codes_in_brackets */
270:
271: codes__str__followed_by__excluding_sole_str_literal:
272: write_str_literal codes__excluding_sole_str_literal {
273: $$=$1;
1.9 paf 274: P($$, $2);
1.1 paf 275: }
276: ;
277: codes__excluding_sole_str_literal:
278: action
279: | codes__excluding_sole_str_literal write_str_literal {
280: $$=$1;
1.9 paf 281: P($$, $2);
1.1 paf 282: }
283: ;
1.4 paf 284: write_str_literal: STRING {
1.1 paf 285: $$=$1;
1.17 paf 286: OP($$, OP_WRITE);
1.1 paf 287: };
288:
289: /* */
290:
1.8 paf 291: empty: /* empty */ { $$=N(pool) };
1.1 paf 292:
293: %%
294:
295: /*
296: 000$111(2222)00
297: 000$111{3333}00
1.9 paf 298: $,^: push,=0
1.1 paf 299: 1:( { break=pop
300: 2:( ) pop
301: 3:{ } pop
302:
303: 000^111(2222)4444{33333}4000
1.9 paf 304: $,^: push,=0
1.1 paf 305: 1:( { break=pop
306: 2:( )=4
307: 3:{ }=4
308: 4:[^({]=pop
309: */
310:
311: int yylex(YYSTYPE *lvalp, void *pc) {
312: #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
313:
314: register int c;
315: int result;
316:
317: if(PC->pending_state) {
318: result=PC->pending_state;
319: PC->pending_state=0;
320: return result;
321: }
322:
1.9 paf 323: char *begin=PC->source;
324: char *end;
325: int begin_line=PC->line;
1.1 paf 326: while(1) {
1.9 paf 327: c=*(end=(PC->source++));
1.1 paf 328:
1.4 paf 329: if(c=='\n') {
1.1 paf 330: PC->line++;
1.8 paf 331: PC->col=0;
1.10 paf 332: } else
1.4 paf 333: PC->col++;
1.1 paf 334:
335: /* escaping: ^^ ^$ ^; ^) ^} ^( ^{ */
336: if(c=='^') {
337: char pending_c=*PC->source;
338:
1.9 paf 339: if(pending_c=='^' || pending_c=='$' || pending_c==';' ||
340: pending_c=='(' || pending_c==')' ||
341: pending_c=='{' || pending_c=='}') {
1.1 paf 342: /* append piece till ^ */
1.9 paf 343: PC->string->APPEND(begin, end-begin, PC->file, begin_line);
1.1 paf 344: /* reset piece 'start' position & line */
1.9 paf 345: begin=PC->source/*^*/;
346: begin_line=PC->line;
1.1 paf 347: /* skip over ^ and _ */
348: PC->source+=2;
349: /* skip analysis = forced literal */
350: continue;
351: }
352: }
353: switch(PC->ls) {
1.10 paf 354:
355: // USER'S = NOT OURS
1.1 paf 356: case LS_USER:
357: if(c=='$') {
1.10 paf 358: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 359: result=c;
360: goto break2;
361: }
362: if(c=='^') {
1.10 paf 363: push_LS(PC, LS_METHOD_NAME);
364: result=c;
365: goto break2;
366: }
367: if(c=='@' && PC->col==0+1) {
1.1 paf 368: result=c;
1.10 paf 369: push_LS(PC, LS_DEF_NAME);
370: goto break2;
371: }
372:
373: break;
374:
375: // METHOD DEFINITION
376: case LS_DEF_NAME:
377: if(c=='[') {
378: result=c;
379: PC->ls=LS_DEF_PARAMS;
380: goto break2;
381: }
382: if(c=='\n') { // wrong. bailing out
383: result=c;
384: pop_LS(PC);
385: goto break2;
386: }
387: break;
388: case LS_DEF_PARAMS:
389: if(c==';') {
390: result=c;
391: goto break2;
392: }
393: if(c==']') {
394: result=c;
395: PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
396: goto break2;
397: }
398: if(c=='\n') { // wrong. bailing out
399: result=c;
400: pop_LS(PC);
401: goto break2;
402: }
403: break;
404: case LS_DEF_LOCALS:
405: if(c=='[' || c==';') {
406: result=c;
407: goto break2;
408: }
409: if(c==']') {
410: result=c;
411: PC->ls=LS_DEF_COMMENT;
412: goto break2;
413: }
414: if(c=='\n') { // wrong. bailing out
415: result=c;
416: pop_LS(PC);
417: goto break2;
418: }
419: break;
420: case LS_DEF_COMMENT:
421: if(c=='\n') {
422: result=c;
423: pop_LS(PC);
1.1 paf 424: goto break2;
425: }
426: break;
427:
1.10 paf 428: // VARIABLE GET/PUT/WITH
1.1 paf 429: case LS_VAR_NAME_SIMPLE:
430: if(c==0 ||
431: c==' '|| c=='\t' || c=='\n' ||
432: c==')' || c=='}') {
433: pop_LS(PC);
1.4 paf 434: PC->source--; PC->col--;
1.13 paf 435: result=EON;
1.1 paf 436: goto break2;
437: }
1.13 paf 438: if(begin==end && c=='{') { /* ${name}, no need of EON, switching LS */
1.1 paf 439: PC->ls=LS_VAR_NAME_CURLY;
440: result=c;
441: goto break2;
442: }
1.18 ! paf 443: if(c==':') {
! 444: result=c;
! 445: goto break2;
! 446: }
1.1 paf 447: if(c=='(') {
448: PC->ls=LS_VAR_ROUND;
449: lexical_brackets_nestage=1;
450: result=c;
451: goto break2;
452: }
453: if(c=='{') {
454: PC->ls=LS_VAR_CURLY;
455: lexical_brackets_nestage=1;
456: result=c;
457: goto break2;
458: }
459: if(c=='.'/* name part delim */ || c=='$'/* name part subvar */) {
460: result=c;
461: goto break2;
462: }
463: break;
464: case LS_VAR_NAME_CURLY:
1.18 ! paf 465: if(c==':') {
! 466: result=c;
! 467: goto break2;
! 468: }
1.1 paf 469: if(c=='}') { /* ${name} finished, restoring LS */
470: pop_LS(PC);
471: result=c;
472: goto break2;
473: }
474: if(c=='.'/* name part delim */ || c=='$'/*name part subvar*/) {
475: result=c;
476: goto break2;
477: }
478: break;
479: case LS_VAR_ROUND:
480: if(c=='$') {
1.10 paf 481: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 482: result=c;
483: goto break2;
484: }
485: if(c=='^') {
1.10 paf 486: push_LS(PC, LS_METHOD_NAME);
1.1 paf 487: result=c;
488: goto break2;
489: }
490: if(c==')') {
491: if(--lexical_brackets_nestage==0) {
492: pop_LS(PC);
493: result=c;
494: goto break2;
495: }
496: }
497: if(c==';'/* operator_or_fmt;value delim */) {
498: result=c;
499: goto break2;
500: }
501: if(c=='(')
502: lexical_brackets_nestage++;
503: break;
504: case LS_VAR_CURLY:
505: if(c=='$') {
1.10 paf 506: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 507: result=c;
508: goto break2;
509: }
510: if(c=='^') {
1.10 paf 511: push_LS(PC, LS_METHOD_NAME);
1.1 paf 512: result=c;
513: goto break2;
514: }
515: if(c=='}')
516: if(--lexical_brackets_nestage==0) {
517: pop_LS(PC);
518: result=c;
519: goto break2;
520: }
521: if(c=='{')
522: lexical_brackets_nestage++;
523: break;
524:
1.10 paf 525: // METHOD CALL
1.1 paf 526: case LS_METHOD_NAME:
527: if(c=='(') {
528: PC->ls=LS_METHOD_ROUND;
529: lexical_brackets_nestage=1;
530: result=c;
531: goto break2;
532: }
533: if(c=='{') {
534: PC->ls=LS_METHOD_CURLY;
535: lexical_brackets_nestage=1;
536: result=c;
537: goto break2;
538: }
539: if(c=='.'/* name part delim */ || c=='$'/* name part subvar */) {
540: result=c;
541: goto break2;
542: }
543: break;
544: case LS_METHOD_ROUND:
545: if(c=='$') {
1.10 paf 546: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 547: result=c;
548: goto break2;
549: }
550: if(c=='^') {
1.10 paf 551: push_LS(PC, LS_METHOD_NAME);
1.1 paf 552: result=c;
553: goto break2;
554: }
555: if(c==';'/* param delim */) {
556: result=c;
557: goto break2;
558: }
559: if(c==')')
560: if(--lexical_brackets_nestage==0) {
561: PC->ls=LS_METHOD_AFTER;
562: result=c;
563: goto break2;
564: }
565: if(c=='(')
566: lexical_brackets_nestage++;
567: break;
568: case LS_METHOD_CURLY:
569: if(c=='$') {
1.10 paf 570: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 571: result=c;
572: goto break2;
573: }
574: if(c=='^') {
1.10 paf 575: push_LS(PC, LS_METHOD_NAME);
1.1 paf 576: result=c;
577: goto break2;
578: }
579: if(c=='}')
580: if(--lexical_brackets_nestage==0) {
581: PC->ls=LS_METHOD_AFTER;
582: result=c;
583: goto break2;
584: }
585: if(c=='{')
586: lexical_brackets_nestage++;
587: break;
588: case LS_METHOD_AFTER:
589: if(c=='(') {/* )( }( */
590: PC->ls=LS_METHOD_ROUND;
591: lexical_brackets_nestage=1;
592: result=c;
593: goto break2;
594: }
595: if(c=='{') {/* ){ }{ */
596: PC->ls=LS_METHOD_CURLY;
597: lexical_brackets_nestage=1;
598: result=c;
599: goto break2;
600: }
601: pop_LS(PC);
1.4 paf 602: PC->source--; PC->col--;
1.13 paf 603: result=EON;
1.1 paf 604: goto break2;
605: }
1.9 paf 606: if(c==0) {
1.1 paf 607: result=-1;
1.4 paf 608: PC->source--; PC->col--;
1.1 paf 609: break;
610: }
611: }
612:
613: break2:
1.9 paf 614: if(begin==end)
1.1 paf 615: return result;
616: else {
617: PC->pending_state=result;
1.10 paf 618: // strip last \n before LS_DEF_NAME or EOF
619: if((c=='@' || c==0) && end[-1]=='\n')
620: end--;
621: // append last piece
1.9 paf 622: PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
1.17 paf 623: // create STRING value: array of OP_VALUE+vstring
624: *lvalp=L(new(pool) VString(PC->string));
1.10 paf 625: // new pieces storage
1.8 paf 626: PC->string=new(pool) String(pool);
1.10 paf 627: // go!
1.4 paf 628: return STRING;
1.1 paf 629: }
630: }
631:
1.9 paf 632: int real_yyerror(parse_control *pc, char *s) /* Called by yyparse on error */
1.1 paf 633: {
1.16 paf 634: //fprintf(stderr, "[%s]\n", s);
1.6 paf 635:
636: s[MAX_STRING-1]=0; strcpy(pc->error, s);
1.1 paf 637: return 1;
638: }
639:
640: static void
1.9 paf 641: yyprint(
1.1 paf 642: FILE *file,
643: int type,
644: YYSTYPE value)
645: {
1.9 paf 646: if(type==STRING)
647: fprintf(file, " \"%s\"", LA2S(value)->cstr());
1.1 paf 648: }
649: