|
|
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.4 paf 241: subvar_ref_name_rdive: STRING {
1.1 paf 242: /*
243: TODO: подсмотреть в $1, и если там в первом элементе первая буква ":"
244: то выкинуть её и делать не OP_WITH_READ, а WITH_ROOT
245: */
1.8 paf 246: $$=N(pool); OP($$, OP_WITH_READ);
1.9 paf 247: P($$, $1);
1.1 paf 248: };
1.9 paf 249: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1 paf 250: subvar__get_write: '$' subvar_ref_name_rdive {
251: $$=$2;
1.17 ! paf 252: OP($$, OP_GET_ELEMENT__WRITE);
1.1 paf 253: };
254:
255:
256: /* with */
257:
258: with: '$' name_without_curly_rdive '{' codes '}' {
259: $$=$2;
1.9 paf 260: OP($$, OP_CREATE_RWPOOL);
261: P($$, $4);
262: OP($$, OP_REDUCE_RWPOOL);
1.17 ! paf 263: OP($$, OP_WRITE);
1.1 paf 264: };
265:
266: /* codes_in_brackets */
267:
268: codes__str__followed_by__excluding_sole_str_literal:
269: write_str_literal codes__excluding_sole_str_literal {
270: $$=$1;
1.9 paf 271: P($$, $2);
1.1 paf 272: }
273: ;
274: codes__excluding_sole_str_literal:
275: action
276: | codes__excluding_sole_str_literal write_str_literal {
277: $$=$1;
1.9 paf 278: P($$, $2);
1.1 paf 279: }
280: ;
1.4 paf 281: write_str_literal: STRING {
1.1 paf 282: $$=$1;
1.17 ! paf 283: OP($$, OP_WRITE);
1.1 paf 284: };
285:
286: /* */
287:
1.8 paf 288: empty: /* empty */ { $$=N(pool) };
1.1 paf 289:
290: %%
291:
292: /*
293: 000$111(2222)00
294: 000$111{3333}00
1.9 paf 295: $,^: push,=0
1.1 paf 296: 1:( { break=pop
297: 2:( ) pop
298: 3:{ } pop
299:
300: 000^111(2222)4444{33333}4000
1.9 paf 301: $,^: push,=0
1.1 paf 302: 1:( { break=pop
303: 2:( )=4
304: 3:{ }=4
305: 4:[^({]=pop
306: */
307:
308: int yylex(YYSTYPE *lvalp, void *pc) {
309: #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
310:
311: register int c;
312: int result;
313:
314: if(PC->pending_state) {
315: result=PC->pending_state;
316: PC->pending_state=0;
317: return result;
318: }
319:
1.9 paf 320: char *begin=PC->source;
321: char *end;
322: int begin_line=PC->line;
1.1 paf 323: while(1) {
1.9 paf 324: c=*(end=(PC->source++));
1.1 paf 325:
1.4 paf 326: if(c=='\n') {
1.1 paf 327: PC->line++;
1.8 paf 328: PC->col=0;
1.10 paf 329: } else
1.4 paf 330: PC->col++;
1.1 paf 331:
332: /* escaping: ^^ ^$ ^; ^) ^} ^( ^{ */
333: if(c=='^') {
334: char pending_c=*PC->source;
335:
1.9 paf 336: if(pending_c=='^' || pending_c=='$' || pending_c==';' ||
337: pending_c=='(' || pending_c==')' ||
338: pending_c=='{' || pending_c=='}') {
1.1 paf 339: /* append piece till ^ */
1.9 paf 340: PC->string->APPEND(begin, end-begin, PC->file, begin_line);
1.1 paf 341: /* reset piece 'start' position & line */
1.9 paf 342: begin=PC->source/*^*/;
343: begin_line=PC->line;
1.1 paf 344: /* skip over ^ and _ */
345: PC->source+=2;
346: /* skip analysis = forced literal */
347: continue;
348: }
349: }
350: switch(PC->ls) {
1.10 paf 351:
352: // USER'S = NOT OURS
1.1 paf 353: case LS_USER:
354: if(c=='$') {
1.10 paf 355: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 356: result=c;
357: goto break2;
358: }
359: if(c=='^') {
1.10 paf 360: push_LS(PC, LS_METHOD_NAME);
361: result=c;
362: goto break2;
363: }
364: if(c=='@' && PC->col==0+1) {
1.1 paf 365: result=c;
1.10 paf 366: push_LS(PC, LS_DEF_NAME);
367: goto break2;
368: }
369:
370: break;
371:
372: // METHOD DEFINITION
373: case LS_DEF_NAME:
374: if(c=='[') {
375: result=c;
376: PC->ls=LS_DEF_PARAMS;
377: goto break2;
378: }
379: if(c=='\n') { // wrong. bailing out
380: result=c;
381: pop_LS(PC);
382: goto break2;
383: }
384: break;
385: case LS_DEF_PARAMS:
386: if(c==';') {
387: result=c;
388: goto break2;
389: }
390: if(c==']') {
391: result=c;
392: PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
393: goto break2;
394: }
395: if(c=='\n') { // wrong. bailing out
396: result=c;
397: pop_LS(PC);
398: goto break2;
399: }
400: break;
401: case LS_DEF_LOCALS:
402: if(c=='[' || c==';') {
403: result=c;
404: goto break2;
405: }
406: if(c==']') {
407: result=c;
408: PC->ls=LS_DEF_COMMENT;
409: goto break2;
410: }
411: if(c=='\n') { // wrong. bailing out
412: result=c;
413: pop_LS(PC);
414: goto break2;
415: }
416: break;
417: case LS_DEF_COMMENT:
418: if(c=='\n') {
419: result=c;
420: pop_LS(PC);
1.1 paf 421: goto break2;
422: }
423: break;
424:
1.10 paf 425: // VARIABLE GET/PUT/WITH
1.1 paf 426: case LS_VAR_NAME_SIMPLE:
427: if(c==0 ||
428: c==' '|| c=='\t' || c=='\n' ||
429: c==')' || c=='}') {
430: pop_LS(PC);
1.4 paf 431: PC->source--; PC->col--;
1.13 paf 432: result=EON;
1.1 paf 433: goto break2;
434: }
1.13 paf 435: if(begin==end && c=='{') { /* ${name}, no need of EON, switching LS */
1.1 paf 436: PC->ls=LS_VAR_NAME_CURLY;
437: result=c;
438: goto break2;
439: }
440: if(c=='(') {
441: PC->ls=LS_VAR_ROUND;
442: lexical_brackets_nestage=1;
443: result=c;
444: goto break2;
445: }
446: if(c=='{') {
447: PC->ls=LS_VAR_CURLY;
448: lexical_brackets_nestage=1;
449: result=c;
450: goto break2;
451: }
452: if(c=='.'/* name part delim */ || c=='$'/* name part subvar */) {
453: result=c;
454: goto break2;
455: }
456: break;
457: case LS_VAR_NAME_CURLY:
458: if(c=='}') { /* ${name} finished, restoring LS */
459: pop_LS(PC);
460: result=c;
461: goto break2;
462: }
463: if(c=='.'/* name part delim */ || c=='$'/*name part subvar*/) {
464: result=c;
465: goto break2;
466: }
467: break;
468: case LS_VAR_ROUND:
469: if(c=='$') {
1.10 paf 470: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 471: result=c;
472: goto break2;
473: }
474: if(c=='^') {
1.10 paf 475: push_LS(PC, LS_METHOD_NAME);
1.1 paf 476: result=c;
477: goto break2;
478: }
479: if(c==')') {
480: if(--lexical_brackets_nestage==0) {
481: pop_LS(PC);
482: result=c;
483: goto break2;
484: }
485: }
486: if(c==';'/* operator_or_fmt;value delim */) {
487: result=c;
488: goto break2;
489: }
490: if(c=='(')
491: lexical_brackets_nestage++;
492: break;
493: case LS_VAR_CURLY:
494: if(c=='$') {
1.10 paf 495: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 496: result=c;
497: goto break2;
498: }
499: if(c=='^') {
1.10 paf 500: push_LS(PC, LS_METHOD_NAME);
1.1 paf 501: result=c;
502: goto break2;
503: }
504: if(c=='}')
505: if(--lexical_brackets_nestage==0) {
506: pop_LS(PC);
507: result=c;
508: goto break2;
509: }
510: if(c=='{')
511: lexical_brackets_nestage++;
512: break;
513:
1.10 paf 514: // METHOD CALL
1.1 paf 515: case LS_METHOD_NAME:
516: if(c=='(') {
517: PC->ls=LS_METHOD_ROUND;
518: lexical_brackets_nestage=1;
519: result=c;
520: goto break2;
521: }
522: if(c=='{') {
523: PC->ls=LS_METHOD_CURLY;
524: lexical_brackets_nestage=1;
525: result=c;
526: goto break2;
527: }
528: if(c=='.'/* name part delim */ || c=='$'/* name part subvar */) {
529: result=c;
530: goto break2;
531: }
532: break;
533: case LS_METHOD_ROUND:
534: if(c=='$') {
1.10 paf 535: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 536: result=c;
537: goto break2;
538: }
539: if(c=='^') {
1.10 paf 540: push_LS(PC, LS_METHOD_NAME);
1.1 paf 541: result=c;
542: goto break2;
543: }
544: if(c==';'/* param delim */) {
545: result=c;
546: goto break2;
547: }
548: if(c==')')
549: if(--lexical_brackets_nestage==0) {
550: PC->ls=LS_METHOD_AFTER;
551: result=c;
552: goto break2;
553: }
554: if(c=='(')
555: lexical_brackets_nestage++;
556: break;
557: case LS_METHOD_CURLY:
558: if(c=='$') {
1.10 paf 559: push_LS(PC, LS_VAR_NAME_SIMPLE);
1.1 paf 560: result=c;
561: goto break2;
562: }
563: if(c=='^') {
1.10 paf 564: push_LS(PC, LS_METHOD_NAME);
1.1 paf 565: result=c;
566: goto break2;
567: }
568: if(c=='}')
569: if(--lexical_brackets_nestage==0) {
570: PC->ls=LS_METHOD_AFTER;
571: result=c;
572: goto break2;
573: }
574: if(c=='{')
575: lexical_brackets_nestage++;
576: break;
577: case LS_METHOD_AFTER:
578: if(c=='(') {/* )( }( */
579: PC->ls=LS_METHOD_ROUND;
580: lexical_brackets_nestage=1;
581: result=c;
582: goto break2;
583: }
584: if(c=='{') {/* ){ }{ */
585: PC->ls=LS_METHOD_CURLY;
586: lexical_brackets_nestage=1;
587: result=c;
588: goto break2;
589: }
590: pop_LS(PC);
1.4 paf 591: PC->source--; PC->col--;
1.13 paf 592: result=EON;
1.1 paf 593: goto break2;
594: }
1.9 paf 595: if(c==0) {
1.1 paf 596: result=-1;
1.4 paf 597: PC->source--; PC->col--;
1.1 paf 598: break;
599: }
600: }
601:
602: break2:
1.9 paf 603: if(begin==end)
1.1 paf 604: return result;
605: else {
606: PC->pending_state=result;
1.10 paf 607: // strip last \n before LS_DEF_NAME or EOF
608: if((c=='@' || c==0) && end[-1]=='\n')
609: end--;
610: // append last piece
1.9 paf 611: PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
1.17 ! paf 612: // create STRING value: array of OP_VALUE+vstring
! 613: *lvalp=L(new(pool) VString(PC->string));
1.10 paf 614: // new pieces storage
1.8 paf 615: PC->string=new(pool) String(pool);
1.10 paf 616: // go!
1.4 paf 617: return STRING;
1.1 paf 618: }
619: }
620:
1.9 paf 621: int real_yyerror(parse_control *pc, char *s) /* Called by yyparse on error */
1.1 paf 622: {
1.16 paf 623: //fprintf(stderr, "[%s]\n", s);
1.6 paf 624:
625: s[MAX_STRING-1]=0; strcpy(pc->error, s);
1.1 paf 626: return 1;
627: }
628:
629: static void
1.9 paf 630: yyprint(
1.1 paf 631: FILE *file,
632: int type,
633: YYSTYPE value)
634: {
1.9 paf 635: if(type==STRING)
636: fprintf(file, " \"%s\"", LA2S(value)->cstr());
1.1 paf 637: }
638: