Annotation of parser3/src/main/compile.y, revision 1.61

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

E-mail: