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

1.24      paf         1: /*
1.62    ! paf         2:   $Id: compile.y,v 1.61 2001/03/07 09:29:54 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.62    ! paf       388: |      optimized_expr /* $var(something) */
1.53      paf       389: ;
1.62    ! paf       390: optimized_expr: expr {
        !           391:        if(($$=$1)->size()==2) { // only one string literal in there?
        !           392:                change_string_literal_to_double_literal($$); // make that string literal Double
        !           393:        }
        !           394: };
1.56      paf       395: expr: 
1.62    ! paf       396:        STRING
1.60      paf       397: |      '(' expr ')' { $$ = $2; }
                    398: /* stack: operand // stack: @operand */
                    399: |      '-' expr %prec NEG { $$=$2;  O($$, OP_NEG) }
                    400: |      '~' expr %prec INV { $$=$2;      O($$, OP_INV) }
                    401: |      '!' expr %prec NOT { $$=$2;  O($$, OP_NOT) }
1.57      paf       402: /*todo: def in fexists*/
1.60      paf       403: /* stack: a,b // stack: a@b */
                    404: |      expr '-' expr { $$=$1;  P($$, $3);  O($$, OP_SUB) }
                    405: |      expr '+' expr { $$=$1;  P($$, $3);  O($$, OP_ADD) }
                    406: |      expr '*' expr { $$=$1;  P($$, $3);  O($$, OP_MUL) }
                    407: |      expr '/' expr { $$=$1;  P($$, $3);  O($$, OP_DIV) }
                    408: |      expr '%' expr { $$=$1;  P($$, $3);  O($$, OP_MOD) }
                    409: |      expr '&' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_AND) }
                    410: |      expr '|' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_OR) }
                    411: |      expr '#' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_XOR) }
                    412: |      expr "&&" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_AND) }
                    413: |      expr "||" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_OR) }
                    414: |      expr "##" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_XOR) }
                    415: |      expr '<' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LT) }
                    416: |      expr '>' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GT) }
                    417: |      expr "<=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LE) }
                    418: |      expr ">=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GE) }
                    419: |      expr "==" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_EQ) }
                    420: |      expr "!=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_NE) }
1.61      paf       421: |      expr "lt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LT) }
                    422: |      expr "gt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GT) }
                    423: |      expr "le" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LE) }
                    424: |      expr "ge" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GE) }
                    425: |      expr "eq" expr { $$=$1;  P($$, $3);  O($$, OP_STR_EQ) }
                    426: |      expr "ne" expr { $$=$1;  P($$, $3);  O($$, OP_STR_NE) }
1.56      paf       427: ;
1.55      paf       428: 
1.53      paf       429: /*
1.56      paf       430: complex_expr_value: complex_expr {
1.53      paf       431:        $$=N(POOL); 
1.54      paf       432:        O($$, OP_CREATE_SWPOOL); /* stack: empty write context * /
1.53      paf       433:        P($$, $1); /* some codes to that context * /
1.54      paf       434:        O($$, OP_REDUCE_SWPOOL); /* context=pop; stack: context.get_string() * /
1.53      paf       435: };
                    436: */
1.1       paf       437: 
1.27      paf       438: /* basics */
1.1       paf       439: 
1.4       paf       440: write_str_literal: STRING {
1.59      paf       441:        $$=$1;
                    442:        O($$, OP_WRITE);
1.54      paf       443: };
                    444: 
1.55      paf       445: empty_double_value: /* empty */ { $$=VL(NEW VDouble(POOL)) };
                    446: empty_string_value: /* empty */ { $$=VL(NEW VString(POOL)) };
1.25      paf       447: empty: /* empty */ { $$=N(POOL) };
1.1       paf       448: 
                    449: %%
                    450: 
                    451: /*
                    452:        000$111(2222)00 
                    453:                000$111{3333}00
1.9       paf       454:        $,^: push,=0
1.1       paf       455:        1:( { break=pop
                    456:        2:( )  pop
                    457:        3:{ }  pop
                    458: 
                    459:        000^111(2222)4444{33333}4000
1.9       paf       460:        $,^: push,=0
1.1       paf       461:        1:( { break=pop
                    462:        2:( )=4
                    463:        3:{ }=4
                    464:                4:[^({]=pop
                    465: */
                    466: 
                    467: int yylex(YYSTYPE *lvalp, void *pc) {
                    468:        #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
1.48      paf       469:        #define RC {result=c; goto break2; }
1.1       paf       470: 
                    471:     register int c;
                    472:     int result;
                    473:        
                    474:        if(PC->pending_state) {
                    475:                result=PC->pending_state;
                    476:                PC->pending_state=0;
                    477:                return result;
                    478:        }
                    479:        
1.9       paf       480:        char *begin=PC->source;
                    481:        char *end;
                    482:        int begin_line=PC->line;
1.58      paf       483:        bool skip_analized_char=false;
1.50      paf       484:        while(true) {
1.9       paf       485:                c=*(end=(PC->source++));
1.1       paf       486: 
1.4       paf       487:                if(c=='\n') {
1.1       paf       488:                        PC->line++;
1.8       paf       489:                        PC->col=0;
1.10      paf       490:                } else
1.4       paf       491:                        PC->col++;
1.1       paf       492: 
1.48      paf       493:                // escaping: ^^ ^$ ^; ^) ^} ^( ^{ ^"
                    494:                if(c=='^') 
                    495:                        switch(*PC->source) {
                    496:                        case '^': case '$': case ';':
                    497:                        case '[': case ']':
                    498:                        case '{': case '}':
                    499:                        case '"':
1.40      paf       500:                                if(end!=begin) {
                    501:                                        // append piece till ^
                    502:                                        PC->string->APPEND(begin, end-begin, PC->file, begin_line);
                    503:                                }
                    504:                                // reset piece 'start' position & line
                    505:                                begin=PC->source; // ^
1.9       paf       506:                                begin_line=PC->line;
1.40      paf       507:                                // skip over ^ and _
1.50      paf       508:                                PC->source++;  PC->col++;
1.40      paf       509:                                // skip analysis = forced literal
1.1       paf       510:                                continue;
                    511:                        }
                    512:                switch(PC->ls) {
1.10      paf       513: 
                    514:                // USER'S = NOT OURS
1.1       paf       515:                case LS_USER:
1.48      paf       516:                        switch(c) {
                    517:                        case '$':
1.10      paf       518:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       519:                                RC;
                    520:                        case '^':
                    521:                                push_LS(PC, LS_METHOD_NAME);
                    522:                                RC;
                    523:                        case '@':
                    524:                                if(PC->col==0+1) {
                    525:                                        push_LS(PC, LS_DEF_NAME);
                    526:                                        RC;
                    527:                                }
                    528:                                break;
1.1       paf       529:                        }
1.48      paf       530:                        break;
                    531:                        
                    532:                // STRING IN EXPRESSION
                    533:                case LS_EXPRESSION_STRING:
                    534:                        switch(c) {
                    535:                        case '"':
                    536:                                pop_LS(PC); //"abc".
                    537:                                RC;
                    538:                        case '$':
                    539:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
                    540:                                RC;
                    541:                        case '^':
1.10      paf       542:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       543:                                RC;
1.10      paf       544:                        }
                    545:                        break;
                    546: 
                    547:                // METHOD DEFINITION
                    548:                case LS_DEF_NAME:
1.48      paf       549:                        switch(c) {
                    550:                        case '[':
1.10      paf       551:                                PC->ls=LS_DEF_PARAMS;
1.48      paf       552:                                RC;
                    553:                        case '\n':
                    554:                                PC->ls=LS_DEF_SPECIAL_BODY;
                    555:                                RC;
1.10      paf       556:                        }
                    557:                        break;
1.48      paf       558: 
1.10      paf       559:                case LS_DEF_PARAMS:
1.48      paf       560:                        switch(c) {
                    561:                        case ';':
                    562:                                RC;
                    563:                        case ']':
1.10      paf       564:                                PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf       565:                                RC;
1.49      paf       566:                        case '\n': // wrong. bailing out
1.10      paf       567:                                pop_LS(PC);
1.48      paf       568:                                RC;
1.10      paf       569:                        }
                    570:                        break;
1.48      paf       571: 
1.10      paf       572:                case LS_DEF_LOCALS:
1.48      paf       573:                        switch(c) {
                    574:                        case '[':
                    575:                        case ';':
                    576:                                RC;
                    577:                        case ']':
1.10      paf       578:                                PC->ls=LS_DEF_COMMENT;
1.48      paf       579:                                RC;
                    580:                        case '\n': // wrong. bailing out
1.10      paf       581:                                pop_LS(PC);
1.48      paf       582:                                RC;
1.10      paf       583:                        }
                    584:                        break;
1.48      paf       585: 
1.10      paf       586:                case LS_DEF_COMMENT:
                    587:                        if(c=='\n') {
                    588:                                pop_LS(PC);
1.48      paf       589:                                RC;
1.37      paf       590:                        }
                    591:                        break;
                    592: 
1.48      paf       593:                case LS_DEF_SPECIAL_BODY:
1.37      paf       594:                        if(c=='\n') {
1.48      paf       595:                                switch(*PC->source) {
                    596:                                case '@': case 0: // end of special_code
1.37      paf       597:                                        pop_LS(PC);
1.48      paf       598:                                        break;
                    599:                                }
                    600:                                RC;
                    601:                        }
                    602:                        break;
                    603: 
                    604:                // (EXPRESSION)
                    605:                case LS_EXPRESSION_BODY:
                    606:                        switch(c) {
                    607:                        case ')':
                    608:                                if(--lexical_brackets_nestage==0)
                    609:                                        pop_LS(PC); //(EXPRESSION).
                    610:                                RC;
                    611:                        case '$':
                    612:                                push_LS(PC, LS_VAR_NAME_IN_EXPRESSION);                         
                    613:                                RC;
                    614:                        case '^':
                    615:                                push_LS(PC, LS_METHOD_NAME);
                    616:                                RC;
                    617:                        case '(':
                    618:                                lexical_brackets_nestage++;
                    619:                                RC;
                    620:                        case '+': case '-': case '*': case '/': case '%': 
1.58      paf       621:                        case '~':
1.48      paf       622:                        case ';':
                    623:                                RC;
1.59      paf       624:                        case '&': case '|':  case '#':
1.58      paf       625:                                if(*PC->source==c) { // && ||
1.59      paf       626:                                        result=c=='#'?LXOR:c=='&'?LAND:LOR;
1.58      paf       627:                                        skip_analized_char=true;
                    628:                                } else
                    629:                                        result=c;
                    630:                                goto break2;
                    631:                        case '<': case '>': case '=': case '!': 
                    632:                                if(*PC->source=='=') { // <= >= == !=
                    633:                                        skip_analized_char=true;
                    634:                                        switch(c) {
                    635:                                        case '<': result=NLE; break;
                    636:                                        case '>': result=NGE; break;
                    637:                                        case '=': result=NEQ; break;
                    638:                                        case '!': result=NNE; break;
                    639:                                        }
                    640:                                } else
                    641:                                        result=c;
                    642:                                goto break2;
1.48      paf       643:                        case '"':
                    644:                                push_LS(PC, LS_EXPRESSION_STRING);
                    645:                                RC;
1.50      paf       646:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf       647:                                if(end==begin) // right after whitespace
1.58      paf       648:                                        switch(*PC->source) {
1.51      paf       649: //                                     case '?': // ok [and bad cases, yacc would bark at them]
                    650:                                        case 't': // lt gt [et nt]
1.62    ! paf       651:                                                result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
        !           652:                                                skip_analized_char=true;
1.58      paf       653:                                                goto break2;
1.51      paf       654:                                        case 'e': // le ge ne [ee]
1.58      paf       655:                                                result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
1.62    ! paf       656:                                                skip_analized_char=true;
1.58      paf       657:                                                goto break2;
1.51      paf       658:                                        case 'q': // eq [lq gq nq]
1.58      paf       659:                                                result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
1.62    ! paf       660:                                                skip_analized_char=true;
1.58      paf       661:                                                goto break2;
1.50      paf       662:                                        }
1.48      paf       663:                                break;
                    664:                        case ' ': case '\t': case '\n':
                    665:                                if(end!=begin) {
1.58      paf       666:                                        // append piece till whitespace char
1.48      paf       667:                                        PC->string->APPEND(begin, end-begin, PC->file, begin_line);
                    668:                                }
                    669:                                // reset piece 'start' position & line
1.58      paf       670:                                begin=PC->source; // after whitespace char
1.48      paf       671:                                begin_line=PC->line;
                    672:                                continue;
1.1       paf       673:                        }
                    674:                        break;
                    675: 
1.10      paf       676:                // VARIABLE GET/PUT/WITH
1.1       paf       677:                case LS_VAR_NAME_SIMPLE:
1.48      paf       678:                case LS_VAR_NAME_IN_EXPRESSION:
                    679:                        if(PC->ls==LS_VAR_NAME_IN_EXPRESSION) {
1.56      paf       680:                                // name in expr ends also before binary operators 
1.48      paf       681:                                switch(c) {
                    682:                                case '+': case '-': case '*': case '/': case '%': 
                    683:                                case '&': case '|': 
                    684:                                case '<': case '>': case '=': case '!':
                    685:                                        pop_LS(PC);
                    686:                                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
                    687:                                        result=EON;
                    688:                                        goto break2;
                    689:                                }
                    690:                        }
                    691:                        switch(c) {
                    692:                        case 0:
                    693:                        case ' ': case '\t': case '\n':
                    694:                        case ';':
                    695:                        case ']': case '}':
1.1       paf       696:                                pop_LS(PC);
1.32      paf       697:                                PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       698:                                result=EON;
1.1       paf       699:                                goto break2;
1.48      paf       700:                        case '[':
                    701:                                PC->ls=LS_VAR_SQUARE;
1.1       paf       702:                                lexical_brackets_nestage=1;
1.48      paf       703:                                RC;
                    704:                        case '{':
                    705:                                if(begin==end) { // ${name}, no need of EON, switching LS
                    706:                                        PC->ls=LS_VAR_NAME_CURLY; 
                    707:                                } else {
                    708:                                        PC->ls=LS_VAR_CURLY;
                    709:                                        lexical_brackets_nestage=1;
                    710:                                }
                    711:                                
                    712:                                RC;
                    713:                        case '(':
                    714:                                PC->ls=LS_EXPRESSION_BODY;
1.1       paf       715:                                lexical_brackets_nestage=1;
1.48      paf       716:                                RC;
                    717:                        case '.': // name part delim
                    718:                        case '$': // name part subvar
                    719:                        case ':': // ':name' or 'class:name'
                    720:                                RC;
1.1       paf       721:                        }
                    722:                        break;
1.48      paf       723: 
1.1       paf       724:                case LS_VAR_NAME_CURLY:
1.48      paf       725:                        switch(c) {
                    726:                        case '}': // ${name} finished, restoring LS
1.1       paf       727:                                pop_LS(PC);
1.48      paf       728:                                RC;
                    729:                        case '.': // name part delim
                    730:                        case '$': // name part subvar
                    731:                        case ':': // ':name' or 'class:name'
                    732:                                RC;
1.1       paf       733:                        }
                    734:                        break;
1.48      paf       735: 
                    736:                case LS_VAR_SQUARE:
                    737:                        switch(c) {
                    738:                        case '$':
1.10      paf       739:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       740:                                RC;
                    741:                        case '^':
1.10      paf       742:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       743:                                RC;
                    744:                        case ']':
1.1       paf       745:                                if(--lexical_brackets_nestage==0) {
                    746:                                        pop_LS(PC);
1.48      paf       747:                                        RC;
1.1       paf       748:                                }
1.48      paf       749:                                break;
                    750:                        case ';': // operator_or_fmt;value delim
                    751:                                RC;
                    752:                        case '[':
                    753:                                lexical_brackets_nestage++;
                    754:                                break;
1.1       paf       755:                        }
                    756:                        break;
1.48      paf       757: 
1.1       paf       758:                case LS_VAR_CURLY:
1.48      paf       759:                        switch(c) {
                    760:                        case '$':
1.10      paf       761:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       762:                                RC;
                    763:                        case '^':
1.10      paf       764:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       765:                                RC;
                    766:                        case '}':
1.1       paf       767:                                if(--lexical_brackets_nestage==0) {
                    768:                                        pop_LS(PC);
1.48      paf       769:                                        RC;
1.1       paf       770:                                }
1.48      paf       771:                                break;
                    772:                        case '{':
1.1       paf       773:                                lexical_brackets_nestage++;
1.48      paf       774:                                break;
                    775:                        }
1.1       paf       776:                        break;
                    777: 
1.10      paf       778:                // METHOD CALL
1.1       paf       779:                case LS_METHOD_NAME:
1.48      paf       780:                        switch(c) {
                    781:                        case '[':
                    782:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       783:                                lexical_brackets_nestage=1;
1.48      paf       784:                                RC;
                    785:                        case '{':
1.1       paf       786:                                PC->ls=LS_METHOD_CURLY;
                    787:                                lexical_brackets_nestage=1;
1.48      paf       788:                                RC;
                    789:                        case '.': // name part delim 
                    790:                        case '$': // name part subvar
                    791:                        case ':': // ':name' or 'class:name'
                    792:                                RC;
1.1       paf       793:                        }
                    794:                        break;
1.48      paf       795: 
                    796:                case LS_METHOD_SQUARE:
                    797:                        switch(c) {
                    798:                        case '$':
1.10      paf       799:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       800:                                RC;
                    801:                        case '^':
1.10      paf       802:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       803:                                RC;
                    804:                        case ';': // param delim
                    805:                                RC;
                    806:                        case ']':
1.1       paf       807:                                if(--lexical_brackets_nestage==0) {
                    808:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       809:                                        RC;
1.1       paf       810:                                }
1.48      paf       811:                                break;
                    812:                        case '[':
1.1       paf       813:                                lexical_brackets_nestage++;
1.48      paf       814:                                break;
                    815:                        }
1.1       paf       816:                        break;
1.48      paf       817: 
1.1       paf       818:                case LS_METHOD_CURLY:
1.48      paf       819:                        switch(c) {
                    820:                        case '$':
1.10      paf       821:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       822:                                RC;
                    823:                        case '^':
1.10      paf       824:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       825:                                RC;
                    826:                        case '}':
1.1       paf       827:                                if(--lexical_brackets_nestage==0) {
                    828:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       829:                                        RC;
1.1       paf       830:                                }
1.48      paf       831:                                break;
                    832:                        case '{':
1.1       paf       833:                                lexical_brackets_nestage++;
1.48      paf       834:                                break;
                    835:                        }
1.1       paf       836:                        break;
1.48      paf       837: 
1.1       paf       838:                case LS_METHOD_AFTER:
1.47      paf       839:                        if(c=='[') {/* )( }( */
1.48      paf       840:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       841:                                lexical_brackets_nestage=1;
1.48      paf       842:                                RC;
1.1       paf       843:                        }                                          
                    844:                        if(c=='{') {/* ){ }{ */
                    845:                                PC->ls=LS_METHOD_CURLY;
                    846:                                lexical_brackets_nestage=1;
1.48      paf       847:                                RC;
1.1       paf       848:                        }                                          
                    849:                        pop_LS(PC);
1.32      paf       850:                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       851:                        result=EON;
1.1       paf       852:                        goto break2;
                    853:                }
1.9       paf       854:                if(c==0) {
1.1       paf       855:                        result=-1;
                    856:                        break;
                    857:                }
                    858:        }
                    859: 
                    860: break2:
1.59      paf       861:        if(end!=begin) { // there is last piece?
                    862:                if((c=='@' || c==0) && end[-1]=='\n') { // we are before LS_DEF_NAME or EOF?
                    863:                        // strip last \n
1.10      paf       864:                        end--;
1.59      paf       865:                }
                    866:                if(end!=begin) { // last piece still alive?
                    867:                        // append it
1.30      paf       868:                        PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
                    869:                }
1.59      paf       870:        }
                    871:        if(PC->string->size()) { // something accumulated?
1.17      paf       872:                // create STRING value: array of OP_VALUE+vstring
1.55      paf       873:                *lvalp=VL(NEW VString(*PC->string));
1.10      paf       874:                // new pieces storage
1.25      paf       875:                PC->string=NEW String(POOL);
1.58      paf       876:                // make current result be pending for next call, return STRING for now
                    877:                PC->pending_state=result;  result=STRING;
                    878:        }
                    879:        if(skip_analized_char) {
                    880:                PC->source++;  PC->col++;
1.1       paf       881:        }
1.58      paf       882:        return result;
1.1       paf       883: }
                    884: 
1.9       paf       885: int real_yyerror(parse_control *pc, char *s)  /* Called by yyparse on error */
1.1       paf       886:      {
1.16      paf       887:        //fprintf(stderr, "[%s]\n", s);
1.6       paf       888: 
1.46      paf       889:           strncpy(pc->error, s, MAX_STRING); // TODO: перепроверить с треклятым последним байтом
1.1       paf       890:           return 1;
                    891:      }
                    892: 
                    893: static void
1.9       paf       894:      yyprint(
1.1       paf       895:           FILE *file,
                    896:           int type,
                    897:           YYSTYPE value)
                    898:      {
1.9       paf       899:        if(type==STRING)
1.38      paf       900:          fprintf(file, " \"%s\"", SLA2S(value)->cstr());
1.1       paf       901:      }
                    902: 

E-mail: