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

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

E-mail: