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

1.24      paf         1: /*
1.54    ! paf         2:   $Id: compile.y,v 1.53 2001/03/06 14:09:36 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.54    ! paf       155:        O($$, 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.54    ! paf       168:                O($$, OP_WITH_SELF); /* stack: starting context */
1.22      paf       169:                P($$, diving_code, 
                    170:                        /* skip over... */
                    171:                        diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
                    172:        } else {
1.54    ! paf       173:                O($$, OP_WITH_READ); /* stack: starting context */
1.22      paf       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.54    ! paf       180:        O($$, OP_WITH_ROOT); /* stack: starting context */
1.19      paf       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.54    ! paf       191:        O($$, OP_CONSTRUCT); /* value=pop; name=pop; context=pop; construct(context,name,value) */
1.20      paf       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) {
1.54    ! paf       202:                O($$, OP_WITH_SELF); /* stack: starting context */
1.23      paf       203:                P($$, diving_code, 
                    204:                        /* skip over... */
                    205:                        diving_code->size()>2?3/*OP_+string+get_element*/:2/*OP_+string*/);
                    206:        } else {
1.54    ! paf       207:                O($$, OP_WITH_WRITE); /* stack: starting context */
1.23      paf       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.54    ! paf       214:        O($$, 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 }
1.53      paf       221: |      '(' expression_value ')' { $$=$2 }
1.52      paf       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.54    ! paf       230:        O($$, OP_CREATE_EWPOOL); /* stack: empty write context */
1.9       paf       231:        P($$, $1); /* some codes to that context */
1.54    ! paf       232:        O($$, 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.1       paf       237: /* call */
                    238: 
1.38      paf       239: call: '^' call_name store_params EON { /* ^field.$method{vasya} */
1.42      paf       240:        $$=$2; /* with_xxx,diving code; stack: context,method_junction */
1.54    ! paf       241:        O($$, OP_GET_METHOD_FRAME); /* stack: context,method_frame */
1.9       paf       242:        P($$, $3); /* filling method_frame.store_params */
1.54    ! paf       243:        O($$, OP_CALL); /* method_frame=pop; ncontext=pop; call(ncontext,method_frame) */
1.1       paf       244: };
                    245: 
1.43      paf       246: call_name: name_without_curly_rdive;
1.38      paf       247: 
1.9       paf       248: store_params: store_param | store_params store_param { $$=$1; P($$, $2) };
1.1       paf       249: store_param: store_round_param | store_curly_param;
1.47      paf       250: store_round_param: '[' store_param_parts ']' {$$=$2};
1.31      paf       251: store_param_parts:
1.32      paf       252:        store_param_part
                    253: |      store_param_parts ';' store_param_part { $$=$1; P($$, $3) }
1.31      paf       254: ;
1.10      paf       255: store_curly_param: '{' maybe_codes '}' {
1.25      paf       256:        $$=N(POOL); 
1.29      paf       257:        PCA($$, $2);
1.54    ! paf       258:        O($$, OP_STORE_PARAM);
1.1       paf       259: };
1.32      paf       260: store_param_part: 
1.33      paf       261:        empty /* optimized () case */
                    262: |      STRING { /* optimized (STRING) case */
1.32      paf       263:        $$=$1;
1.54    ! paf       264:        O($$, OP_STORE_PARAM);
1.32      paf       265: }
1.52      paf       266: |      complex_constructor_code_value { /* (something complex) */
1.32      paf       267:        $$=$1;
1.54    ! paf       268:        O($$, OP_STORE_PARAM);
1.32      paf       269: }
                    270: ;
1.1       paf       271: 
                    272: /* name */
                    273: 
1.20      paf       274: name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P($$, $2) };
1.1       paf       275: 
1.9       paf       276: name_path: name_step | name_path name_step { $$=$1; P($$, $2) };
1.1       paf       277: name_step: name_advance1 '.';
                    278: name_advance1: name_expr_value {
                    279:        /* stack: context */
                    280:        $$=$1; /* stack: context,name */
1.54    ! paf       281:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       282: };
                    283: name_advance2: name_expr_value {
                    284:        /* stack: context */
                    285:        $$=$1; /* stack: context,name */
1.54    ! paf       286:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       287: }
1.4       paf       288: |      STRING BOGUS
1.1       paf       289: ;
                    290: name_expr_value: 
1.4       paf       291:        STRING /* subname_is_const */
1.1       paf       292: |      name_expr_subvar_value /* $subname_is_var_value */
                    293: |      name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value[$...] */
                    294: ;
                    295: name_expr_subvar_value: '$' subvar_ref_name_rdive {
                    296:        $$=$2;
1.54    ! paf       297:        O($$, OP_GET_ELEMENT);
1.1       paf       298: };
1.4       paf       299: name_expr_with_subvar_value: STRING subvar_get_writes {
1.25      paf       300:        $$=N(POOL); 
1.54    ! paf       301:        O($$, OP_CREATE_EWPOOL);
1.9       paf       302:        P($$, $1);
1.54    ! paf       303:        O($$, OP_WRITE);
1.9       paf       304:        P($$, $2);
1.54    ! paf       305:        O($$, OP_REDUCE_EWPOOL);
1.1       paf       306: };
1.18      paf       307: subvar_ref_name_rdive: subvar_ref_name_rdive_read | subvar_ref_name_rdive_root;
                    308: subvar_ref_name_rdive_read: STRING {
1.25      paf       309:        $$=N(POOL); 
1.54    ! paf       310:        O($$, OP_WITH_READ);
1.9       paf       311:        P($$, $1);
1.1       paf       312: };
1.18      paf       313: subvar_ref_name_rdive_root: ':' STRING {
1.25      paf       314:        $$=N(POOL); 
1.54    ! paf       315:        O($$, OP_WITH_ROOT);
1.18      paf       316:        P($$, $2);
                    317: };
1.9       paf       318: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1       paf       319: subvar__get_write: '$' subvar_ref_name_rdive {
                    320:        $$=$2;
1.54    ! paf       321:        O($$, OP_GET_ELEMENT__WRITE);
1.42      paf       322: };
                    323: 
1.44      paf       324: class_prefix: STRING ':' {
1.42      paf       325:        String& name=*SLA2S($1);
                    326:        VClass *vclass=static_cast<VClass *>(PC->request->classes().get(name));
                    327:        if(!vclass) {
                    328:                strcpy(PC->error, "'");
                    329:                strcat(PC->error, name.cstr());
                    330:                strcat(PC->error, "' class is undefined in call");
                    331:                YYERROR;
                    332:        }
1.48      paf       333:        //TODO: убрать зависимость от статических @USE, сделать имя, а не ссылку
1.42      paf       334:        $$=CL(vclass); // vclass
1.1       paf       335: };
                    336: 
                    337: 
                    338: /* with */
                    339: 
                    340: with: '$' name_without_curly_rdive '{' codes '}' {
                    341:        $$=$2;
1.54    ! paf       342:        O($$, OP_CREATE_RWPOOL);
1.9       paf       343:        P($$, $4);
1.54    ! paf       344:        O($$, OP_REDUCE_RWPOOL);
        !           345:        O($$, OP_WRITE);
1.1       paf       346: };
1.53      paf       347: 
                    348: /* expression */
                    349: 
                    350: expression_value:
                    351:        empty_value /* optimized $var() case */
1.54    ! paf       352: |      number /* optimized $var(STRING) case */
1.53      paf       353: |      complex_expression /* $var(something complex) */
                    354: ;
                    355: complex_expression: expression_operand '*' expression_operand {
                    356:        $$=$1; // stack: first operand
                    357:        P($$, $3); // stack: first,second operands
1.54    ! paf       358:        O($$, OP_MUL); // value=first*second; stack: value
1.53      paf       359: };
1.54    ! paf       360: expression_operand: number;
1.53      paf       361: 
                    362: /*
                    363: complex_expression_value: complex_expression {
                    364:        $$=N(POOL); 
1.54    ! paf       365:        O($$, OP_CREATE_SWPOOL); /* stack: empty write context * /
1.53      paf       366:        P($$, $1); /* some codes to that context * /
1.54    ! paf       367:        O($$, OP_REDUCE_SWPOOL); /* context=pop; stack: context.get_string() * /
1.53      paf       368: };
                    369: */
1.1       paf       370: 
1.27      paf       371: /* basics */
1.1       paf       372: 
1.4       paf       373: write_str_literal: STRING {
1.38      paf       374:        if(SLA2S($1)->size()) {
1.30      paf       375:                $$=$1;
1.54    ! paf       376:                O($$, OP_WRITE);
1.30      paf       377:        } else {
                    378:                // optimized case of special end of macro. see yylex
                    379:                $$=N(POOL);
                    380:        }
1.26      paf       381: };
1.54    ! paf       382: number: STRING {
        !           383:        change_string_literal_to_double_literal($$=$1);
        !           384: };
        !           385: 
1.38      paf       386: empty_value: /* empty */ { $$=SL(NEW VString(POOL)) };
1.25      paf       387: empty: /* empty */ { $$=N(POOL) };
1.1       paf       388: 
                    389: %%
                    390: 
                    391: /*
                    392:        000$111(2222)00 
                    393:                000$111{3333}00
1.9       paf       394:        $,^: push,=0
1.1       paf       395:        1:( { break=pop
                    396:        2:( )  pop
                    397:        3:{ }  pop
                    398: 
                    399:        000^111(2222)4444{33333}4000
1.9       paf       400:        $,^: push,=0
1.1       paf       401:        1:( { break=pop
                    402:        2:( )=4
                    403:        3:{ }=4
                    404:                4:[^({]=pop
                    405: */
                    406: 
                    407: int yylex(YYSTYPE *lvalp, void *pc) {
                    408:        #define lexical_brackets_nestage PC->brackets_nestages[PC->sp]
1.48      paf       409:        #define RC {result=c; goto break2; }
1.1       paf       410: 
                    411:     register int c;
                    412:     int result;
                    413:        
                    414:        if(PC->pending_state) {
                    415:                result=PC->pending_state;
                    416:                PC->pending_state=0;
                    417:                return result;
                    418:        }
                    419:        
1.9       paf       420:        char *begin=PC->source;
                    421:        char *end;
                    422:        int begin_line=PC->line;
1.50      paf       423:        while(true) {
1.9       paf       424:                c=*(end=(PC->source++));
1.1       paf       425: 
1.4       paf       426:                if(c=='\n') {
1.1       paf       427:                        PC->line++;
1.8       paf       428:                        PC->col=0;
1.10      paf       429:                } else
1.4       paf       430:                        PC->col++;
1.1       paf       431: 
1.48      paf       432:                // escaping: ^^ ^$ ^; ^) ^} ^( ^{ ^"
                    433:                if(c=='^') 
                    434:                        switch(*PC->source) {
                    435:                        case '^': case '$': case ';':
                    436:                        case '[': case ']':
                    437:                        case '{': case '}':
                    438:                        case '"':
1.40      paf       439:                                if(end!=begin) {
                    440:                                        // append piece till ^
                    441:                                        PC->string->APPEND(begin, end-begin, PC->file, begin_line);
                    442:                                }
                    443:                                // reset piece 'start' position & line
                    444:                                begin=PC->source; // ^
1.9       paf       445:                                begin_line=PC->line;
1.40      paf       446:                                // skip over ^ and _
1.50      paf       447:                                PC->source++;  PC->col++;
1.40      paf       448:                                // skip analysis = forced literal
1.1       paf       449:                                continue;
                    450:                        }
                    451:                switch(PC->ls) {
1.10      paf       452: 
                    453:                // USER'S = NOT OURS
1.1       paf       454:                case LS_USER:
1.48      paf       455:                        switch(c) {
                    456:                        case '$':
1.10      paf       457:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       458:                                RC;
                    459:                        case '^':
                    460:                                push_LS(PC, LS_METHOD_NAME);
                    461:                                RC;
                    462:                        case '@':
                    463:                                if(PC->col==0+1) {
                    464:                                        push_LS(PC, LS_DEF_NAME);
                    465:                                        RC;
                    466:                                }
                    467:                                break;
1.1       paf       468:                        }
1.48      paf       469:                        break;
                    470:                        
                    471:                // STRING IN EXPRESSION
                    472:                case LS_EXPRESSION_STRING:
                    473:                        switch(c) {
                    474:                        case '"':
                    475:                                pop_LS(PC); //"abc".
                    476:                                RC;
                    477:                        case '$':
                    478:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
                    479:                                RC;
                    480:                        case '^':
1.10      paf       481:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       482:                                RC;
1.10      paf       483:                        }
                    484:                        break;
                    485: 
                    486:                // METHOD DEFINITION
                    487:                case LS_DEF_NAME:
1.48      paf       488:                        switch(c) {
                    489:                        case '[':
1.10      paf       490:                                PC->ls=LS_DEF_PARAMS;
1.48      paf       491:                                RC;
                    492:                        case '\n':
                    493:                                PC->ls=LS_DEF_SPECIAL_BODY;
                    494:                                RC;
1.10      paf       495:                        }
                    496:                        break;
1.48      paf       497: 
1.10      paf       498:                case LS_DEF_PARAMS:
1.48      paf       499:                        switch(c) {
                    500:                        case ';':
                    501:                                RC;
                    502:                        case ']':
1.10      paf       503:                                PC->ls=*PC->source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf       504:                                RC;
1.49      paf       505:                        case '\n': // wrong. bailing out
1.10      paf       506:                                pop_LS(PC);
1.48      paf       507:                                RC;
1.10      paf       508:                        }
                    509:                        break;
1.48      paf       510: 
1.10      paf       511:                case LS_DEF_LOCALS:
1.48      paf       512:                        switch(c) {
                    513:                        case '[':
                    514:                        case ';':
                    515:                                RC;
                    516:                        case ']':
1.10      paf       517:                                PC->ls=LS_DEF_COMMENT;
1.48      paf       518:                                RC;
                    519:                        case '\n': // wrong. bailing out
1.10      paf       520:                                pop_LS(PC);
1.48      paf       521:                                RC;
1.10      paf       522:                        }
                    523:                        break;
1.48      paf       524: 
1.10      paf       525:                case LS_DEF_COMMENT:
                    526:                        if(c=='\n') {
                    527:                                pop_LS(PC);
1.48      paf       528:                                RC;
1.37      paf       529:                        }
                    530:                        break;
                    531: 
1.48      paf       532:                case LS_DEF_SPECIAL_BODY:
1.37      paf       533:                        if(c=='\n') {
1.48      paf       534:                                switch(*PC->source) {
                    535:                                case '@': case 0: // end of special_code
1.37      paf       536:                                        pop_LS(PC);
1.48      paf       537:                                        break;
                    538:                                }
                    539:                                RC;
                    540:                        }
                    541:                        break;
                    542: 
                    543:                // (EXPRESSION)
                    544:                case LS_EXPRESSION_BODY:
                    545:                        switch(c) {
                    546:                        case ')':
                    547:                                if(--lexical_brackets_nestage==0)
                    548:                                        pop_LS(PC); //(EXPRESSION).
                    549:                                RC;
                    550:                        case '$':
                    551:                                push_LS(PC, LS_VAR_NAME_IN_EXPRESSION);                         
                    552:                                RC;
                    553:                        case '^':
                    554:                                push_LS(PC, LS_METHOD_NAME);
                    555:                                RC;
                    556:                        case '(':
                    557:                                lexical_brackets_nestage++;
                    558:                                RC;
                    559:                        case '+': case '-': case '*': case '/': case '%': 
1.49      paf       560:                        case '&': case '|': 
                    561:                        case '<': case '>': case '=': case '!':
1.48      paf       562:                        case ';':
                    563:                                RC;
                    564:                        case '"':
                    565:                                push_LS(PC, LS_EXPRESSION_STRING);
                    566:                                RC;
1.50      paf       567:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf       568:                                if(end==begin) // right after whitespace
                    569:                                        switch(char next_c=*PC->source) {
                    570: //                                     case '?': // ok [and bad cases, yacc would bark at them]
                    571:                                        case 't': // lt gt [et nt]
                    572:                                        case 'e': // le ge ne [ee]
                    573:                                        case 'q': // eq [lq gq nq]
                    574:                                                PC->source++;  PC->col++;
                    575:                                                PC->pending_state=next_c;
                    576:                                                return c;
1.50      paf       577:                                        }
1.48      paf       578:                                break;
                    579:                        case ' ': case '\t': case '\n':
                    580:                                if(end!=begin) {
                    581:                                        // append piece till whitespace
                    582:                                        PC->string->APPEND(begin, end-begin, PC->file, begin_line);
                    583:                                }
                    584:                                // reset piece 'start' position & line
1.50      paf       585:                                begin=PC->source; // after whitespace
1.48      paf       586:                                begin_line=PC->line;
                    587:                                continue;
1.1       paf       588:                        }
                    589:                        break;
                    590: 
1.10      paf       591:                // VARIABLE GET/PUT/WITH
1.1       paf       592:                case LS_VAR_NAME_SIMPLE:
1.48      paf       593:                case LS_VAR_NAME_IN_EXPRESSION:
                    594:                        if(PC->ls==LS_VAR_NAME_IN_EXPRESSION) {
                    595:                                // name in expression ends also before binary operators 
                    596:                                switch(c) {
                    597:                                case '+': case '-': case '*': case '/': case '%': 
                    598:                                case '&': case '|': 
                    599:                                case '<': case '>': case '=': case '!':
                    600:                                        pop_LS(PC);
                    601:                                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
                    602:                                        result=EON;
                    603:                                        goto break2;
                    604:                                }
                    605:                        }
                    606:                        switch(c) {
                    607:                        case 0:
                    608:                        case ' ': case '\t': case '\n':
                    609:                        case ';':
                    610:                        case ']': case '}':
1.1       paf       611:                                pop_LS(PC);
1.32      paf       612:                                PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       613:                                result=EON;
1.1       paf       614:                                goto break2;
1.48      paf       615:                        case '[':
                    616:                                PC->ls=LS_VAR_SQUARE;
1.1       paf       617:                                lexical_brackets_nestage=1;
1.48      paf       618:                                RC;
                    619:                        case '{':
                    620:                                if(begin==end) { // ${name}, no need of EON, switching LS
                    621:                                        PC->ls=LS_VAR_NAME_CURLY; 
                    622:                                } else {
                    623:                                        PC->ls=LS_VAR_CURLY;
                    624:                                        lexical_brackets_nestage=1;
                    625:                                }
                    626:                                
                    627:                                RC;
                    628:                        case '(':
                    629:                                PC->ls=LS_EXPRESSION_BODY;
1.1       paf       630:                                lexical_brackets_nestage=1;
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: 
1.1       paf       639:                case LS_VAR_NAME_CURLY:
1.48      paf       640:                        switch(c) {
                    641:                        case '}': // ${name} finished, restoring LS
1.1       paf       642:                                pop_LS(PC);
1.48      paf       643:                                RC;
                    644:                        case '.': // name part delim
                    645:                        case '$': // name part subvar
                    646:                        case ':': // ':name' or 'class:name'
                    647:                                RC;
1.1       paf       648:                        }
                    649:                        break;
1.48      paf       650: 
                    651:                case LS_VAR_SQUARE:
                    652:                        switch(c) {
                    653:                        case '$':
1.10      paf       654:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       655:                                RC;
                    656:                        case '^':
1.10      paf       657:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       658:                                RC;
                    659:                        case ']':
1.1       paf       660:                                if(--lexical_brackets_nestage==0) {
                    661:                                        pop_LS(PC);
1.48      paf       662:                                        RC;
1.1       paf       663:                                }
1.48      paf       664:                                break;
                    665:                        case ';': // operator_or_fmt;value delim
                    666:                                RC;
                    667:                        case '[':
                    668:                                lexical_brackets_nestage++;
                    669:                                break;
1.1       paf       670:                        }
                    671:                        break;
1.48      paf       672: 
1.1       paf       673:                case LS_VAR_CURLY:
1.48      paf       674:                        switch(c) {
                    675:                        case '$':
1.10      paf       676:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       677:                                RC;
                    678:                        case '^':
1.10      paf       679:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       680:                                RC;
                    681:                        case '}':
1.1       paf       682:                                if(--lexical_brackets_nestage==0) {
                    683:                                        pop_LS(PC);
1.48      paf       684:                                        RC;
1.1       paf       685:                                }
1.48      paf       686:                                break;
                    687:                        case '{':
1.1       paf       688:                                lexical_brackets_nestage++;
1.48      paf       689:                                break;
                    690:                        }
1.1       paf       691:                        break;
                    692: 
1.10      paf       693:                // METHOD CALL
1.1       paf       694:                case LS_METHOD_NAME:
1.48      paf       695:                        switch(c) {
                    696:                        case '[':
                    697:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       698:                                lexical_brackets_nestage=1;
1.48      paf       699:                                RC;
                    700:                        case '{':
1.1       paf       701:                                PC->ls=LS_METHOD_CURLY;
                    702:                                lexical_brackets_nestage=1;
1.48      paf       703:                                RC;
                    704:                        case '.': // name part delim 
                    705:                        case '$': // name part subvar
                    706:                        case ':': // ':name' or 'class:name'
                    707:                                RC;
1.1       paf       708:                        }
                    709:                        break;
1.48      paf       710: 
                    711:                case LS_METHOD_SQUARE:
                    712:                        switch(c) {
                    713:                        case '$':
1.10      paf       714:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       715:                                RC;
                    716:                        case '^':
1.10      paf       717:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       718:                                RC;
                    719:                        case ';': // param delim
                    720:                                RC;
                    721:                        case ']':
1.1       paf       722:                                if(--lexical_brackets_nestage==0) {
                    723:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       724:                                        RC;
1.1       paf       725:                                }
1.48      paf       726:                                break;
                    727:                        case '[':
1.1       paf       728:                                lexical_brackets_nestage++;
1.48      paf       729:                                break;
                    730:                        }
1.1       paf       731:                        break;
1.48      paf       732: 
1.1       paf       733:                case LS_METHOD_CURLY:
1.48      paf       734:                        switch(c) {
                    735:                        case '$':
1.10      paf       736:                                push_LS(PC, LS_VAR_NAME_SIMPLE);
1.48      paf       737:                                RC;
                    738:                        case '^':
1.10      paf       739:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       740:                                RC;
                    741:                        case '}':
1.1       paf       742:                                if(--lexical_brackets_nestage==0) {
                    743:                                        PC->ls=LS_METHOD_AFTER;
1.48      paf       744:                                        RC;
1.1       paf       745:                                }
1.48      paf       746:                                break;
                    747:                        case '{':
1.1       paf       748:                                lexical_brackets_nestage++;
1.48      paf       749:                                break;
                    750:                        }
1.1       paf       751:                        break;
1.48      paf       752: 
1.1       paf       753:                case LS_METHOD_AFTER:
1.47      paf       754:                        if(c=='[') {/* )( }( */
1.48      paf       755:                                PC->ls=LS_METHOD_SQUARE;
1.1       paf       756:                                lexical_brackets_nestage=1;
1.48      paf       757:                                RC;
1.1       paf       758:                        }                                          
                    759:                        if(c=='{') {/* ){ }{ */
                    760:                                PC->ls=LS_METHOD_CURLY;
                    761:                                lexical_brackets_nestage=1;
1.48      paf       762:                                RC;
1.1       paf       763:                        }                                          
                    764:                        pop_LS(PC);
1.32      paf       765:                        PC->source--;  if(--PC->col<0) { PC->line--;  PC->col=-1; }
1.13      paf       766:                        result=EON;
1.1       paf       767:                        goto break2;
                    768:                }
1.9       paf       769:                if(c==0) {
1.1       paf       770:                        result=-1;
                    771:                        break;
                    772:                }
                    773:        }
                    774: 
                    775: break2:
1.9       paf       776:        if(begin==end)
1.1       paf       777:                return result;
                    778:        else {
                    779:                PC->pending_state=result;
1.10      paf       780:                // strip last \n before LS_DEF_NAME or EOF
                    781:                if((c=='@' || c==0) && end[-1]=='\n')
                    782:                        end--;
1.30      paf       783:                if(end!=begin) {
                    784:                        // append last piece
                    785:                        PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/);
                    786:                }
1.17      paf       787:                // create STRING value: array of OP_VALUE+vstring
1.45      paf       788:                *lvalp=SL(NEW VString(*PC->string));
1.10      paf       789:                // new pieces storage
1.25      paf       790:                PC->string=NEW String(POOL);
1.10      paf       791:                // go!
1.4       paf       792:                return STRING;
1.1       paf       793:        }
                    794: }
                    795: 
1.9       paf       796: int real_yyerror(parse_control *pc, char *s)  /* Called by yyparse on error */
1.1       paf       797:      {
1.16      paf       798:        //fprintf(stderr, "[%s]\n", s);
1.6       paf       799: 
1.46      paf       800:           strncpy(pc->error, s, MAX_STRING); // TODO: перепроверить с треклятым последним байтом
1.1       paf       801:           return 1;
                    802:      }
                    803: 
                    804: static void
1.9       paf       805:      yyprint(
1.1       paf       806:           FILE *file,
                    807:           int type,
                    808:           YYSTYPE value)
                    809:      {
1.9       paf       810:        if(type==STRING)
1.38      paf       811:          fprintf(file, " \"%s\"", SLA2S(value)->cstr());
1.1       paf       812:      }
                    813: 

E-mail: