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

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

E-mail: