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

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

E-mail: