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

1.168     parser      1: %{
1.105     paf         2: /** @file
1.106     paf         3:        Parser: compiler(lexical parser and grammar).
                      4: 
1.87      paf         5:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.168     parser      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.106     paf         7: 
1.202   ! paf         8:        $Id: compile.y,v 1.201 2002/10/15 14:28:57 paf Exp $
1.88      paf         9: */
                     10: 
1.106     paf        11: /**
                     12:        @todo parser4: 
                     13:        - cache compiled code from request to request. to do that...
                     14:                -#:     make method definitions, @CLASS, @BASE, @USE instructions,
1.88      paf        15:                        which would be executed afterwards, and actions
                     16:                        now performed at compile time would be delayed to run time.
1.106     paf        17:                -#:     make cache expiration on time and on disk-change of class source
                     18:                -#:     in apache use subpools for compiled class storage
                     19:                -#:     in iis make up specialized Pool object for that
1.24      paf        20: */
                     21: 
1.90      paf        22: #define YYSTYPE  Array/*<Operation>*/ *
1.9       paf        23: #define YYPARSE_PARAM  pc
                     24: #define YYLEX_PARAM  pc
                     25: #define YYDEBUG  1
1.106     paf        26: #define YYERROR_VERBOSE        1
1.9       paf        27: #define yyerror(msg)  real_yyerror((parse_control *)pc, msg)
                     28: #define YYPRINT(file, type, value)  yyprint(file, type, value)
1.1       paf        29: 
                     30: #include "compile_tools.h"
1.8       paf        31: #include "pa_value.h"
1.12      paf        32: #include "pa_request.h"
1.39      paf        33: #include "pa_vobject.h"
1.52      paf        34: #include "pa_vdouble.h"
1.98      paf        35: #include "pa_globals.h"
1.141     parser     36: #include "pa_vvoid.h"
1.200     paf        37: #include "pa_vmethod_frame.h"
1.39      paf        38: 
1.97      paf        39: #define USE_CONTROL_METHOD_NAME "USE"
1.1       paf        40: 
1.125     paf        41: static int real_yyerror(parse_control *pc, char *s);
1.9       paf        42: static void yyprint(FILE *file, int type, YYSTYPE value);
1.125     paf        43: static int yylex(YYSTYPE *lvalp, void *pc);
1.1       paf        44: 
                     45: 
1.8       paf        46: // local convinient inplace typecast & var
1.114     paf        47: #define PC  (*(parse_control *)pc)
                     48: #define POOL  (*PC.pool)
1.25      paf        49: #undef NEW
                     50: #define NEW new(POOL)
1.107     paf        51: #ifndef DOXYGEN
1.1       paf        52: %}
                     53: 
                     54: %pure_parser
                     55: 
1.13      paf        56: %token EON
1.4       paf        57: %token STRING
1.1       paf        58: %token BOGUS
1.55      paf        59: 
1.58      paf        60: %token BAD_STRING_COMPARISON_OPERATOR
1.114     paf        61: %token BAD_HEX_LITERAL
1.171     parser     62: %token BAD_METHOD_DECL_START
1.193     paf        63: %token BAD_METHOD_PARAMETER_NAME_CHARACTER
                     64: %token BAD_MATH_OPERATOR_CHARACTER
1.58      paf        65: 
1.59      paf        66: %token LAND "&&"
1.58      paf        67: %token LOR "||"
1.193     paf        68: %token LXOR "!||"
                     69: %token NXOR "!|"
1.58      paf        70: 
                     71: %token NLE "<="
                     72: %token NGE ">="
                     73: %token NEQ "=="
                     74: %token NNE "!="
1.195     paf        75: %token NSL "<<"
                     76: %token NSR ">>"
1.58      paf        77: 
                     78: %token SLT "lt"
                     79: %token SGT "gt"
                     80: %token SLE "le"
                     81: %token SGE "ge"
                     82: %token SEQ "eq"
                     83: %token SNE "ne"
                     84: 
1.67      paf        85: %token DEF "def"
                     86: %token IN "in"
                     87: %token FEXISTS "-f"
1.127     paf        88: %token DEXISTS "-d"
1.95      paf        89: %token IS "is"
1.67      paf        90: 
1.57      paf        91: /* logical */
1.193     paf        92: %left "!||"
1.57      paf        93: %left "||"
                     94: %left "&&"
1.131     paf        95: %left '<' '>' "<=" ">="   "lt" "gt" "le" "ge"
                     96: %left "==" "!="  "eq" "ne"
                     97: %left "is" "def" "in" "-f" "-d"
1.57      paf        98: 
                     99: /* bitwise */
1.193     paf       100: %left "!|"
1.131     paf       101: %left '|'
                    102: %left '&' 
1.202   ! paf       103: %left "<<"  ">>"
1.57      paf       104: 
1.56      paf       105: /* numerical */
1.202   ! paf       106: %left '+' '-'
        !           107: %left '*' '/' '\\' '%'
        !           108: %left NUNARY   /* unary - + */
        !           109: 
        !           110: /* out-of-group */
        !           111: %left '~' /* bitwise */
        !           112: %left '!'  /* logical */
1.1       paf       113: 
                    114: %%
1.195     paf       115: all:
1.10      paf       116:        one_big_piece {
1.75      paf       117:        Method& method=*NEW Method(POOL, 
1.187     paf       118:                PC.request->main_method_name, 
1.122     paf       119:                Method::CT_ANY,
1.81      paf       120:                0, 0, /*min, max numbered_params_count*/
1.75      paf       121:                0/*param_names*/, 0/*local_names*/, 
                    122:                $1/*parser_code*/, 0/*native_code*/);
1.187     paf       123:        PC.cclass->add_method(PC.request->main_method_name, method);
1.10      paf       124: }
                    125: |      methods;
                    126: 
                    127: methods: method | methods method;
                    128: one_big_piece: maybe_codes;
                    129: 
1.34      paf       130: method: control_method | code_method;
                    131: 
                    132: control_method: '@' STRING '\n' 
1.132     paf       133:                                maybe_control_strings {
1.120     paf       134:        const String& command=*LA2S($2);
1.34      paf       135:        YYSTYPE strings_code=$4;
1.37      paf       136:        if(strings_code->size()<1*2) {
1.114     paf       137:                strcpy(PC.error, "@");
                    138:                strcat(PC.error, command.cstr());
                    139:                strcat(PC.error, " is empty");
1.37      paf       140:                YYERROR;
                    141:        }
1.74      paf       142:        if(command==CLASS_NAME) {
1.188     paf       143:                if(PC.cclass->base_class()) { // already changed from default?
1.114     paf       144:                        strcpy(PC.error, "class already have a name '");
                    145:                        strncat(PC.error, PC.cclass->name().cstr(), 100);
                    146:                        strcat(PC.error, "'");
1.73      paf       147:                        YYERROR;
                    148:                }
                    149:                if(strings_code->size()==1*2) {
                    150:                        // new class' name
1.120     paf       151:                        const String *name=LA2S(strings_code);
1.73      paf       152:                        // creating the class
1.114     paf       153:                        PC.cclass=NEW VClass(POOL);
                    154:                        PC.cclass->set_name(*name);
1.73      paf       155:                        // append to request's classes
1.114     paf       156:                        PC.request->classes().put(*name, PC.cclass);
1.73      paf       157:                } else {
1.114     paf       158:                        strcpy(PC.error, "@"CLASS_NAME" must contain sole name");
1.34      paf       159:                        YYERROR;
                    160:                }
1.130     paf       161:        } else if(command==USE_CONTROL_METHOD_NAME) {
                    162:                for(int i=0; i<strings_code->size(); i+=2) 
1.198     paf       163:                        PC.request->use_file(PC.request->main_class, *LA2S(strings_code, i));
1.130     paf       164:        } else if(command==BASE_NAME) {
1.188     paf       165:                if(PC.cclass->base_class()) { // already changed from default?
1.130     paf       166:                        strcpy(PC.error, "class already have a base '");
1.188     paf       167:                        strncat(PC.error, PC.cclass->base_class()->name().cstr(), 100);
1.130     paf       168:                        strcat(PC.error, "'");
                    169:                        YYERROR;
                    170:                }
                    171:                if(strings_code->size()==1*2) {
                    172:                        const String& base_name=*LA2S(strings_code);
1.189     paf       173:                        Value *vbase_class=static_cast<VClass *>(
1.130     paf       174:                                PC.request->classes().get(base_name));
1.189     paf       175:                        VStateless_class *base_class=vbase_class?vbase_class->get_class():0;
                    176:                        if(!base_class) {
1.130     paf       177:                                strcpy(PC.error, base_name.cstr());
                    178:                                strcat(PC.error, ": undefined class in @"BASE_NAME);
1.73      paf       179:                                YYERROR;
                    180:                        }
1.130     paf       181:                        // @CLASS == @BASE sanity check
1.189     paf       182:                        if(PC.cclass==base_class) {
1.130     paf       183:                                strcpy(PC.error, "@"CLASS_NAME" equals @"BASE_NAME);
1.45      paf       184:                                YYERROR;
1.34      paf       185:                        }
1.189     paf       186:                        PC.cclass->set_base(base_class);
1.34      paf       187:                } else {
1.130     paf       188:                        strcpy(PC.error, "@"BASE_NAME" must contain sole name");
1.34      paf       189:                        YYERROR;
                    190:                }
1.130     paf       191:        } else {
                    192:                strcpy(PC.error, "'");
                    193:                strncat(PC.error, command.cstr(), MAX_STRING/2);
                    194:                strcat(PC.error, "' invalid special name. valid names are "
                    195:                        "'"CLASS_NAME"', '"USE_CONTROL_METHOD_NAME"' and '"BASE_NAME"'");
                    196:                YYERROR;
1.34      paf       197:        }
                    198: };
1.132     paf       199: maybe_control_strings: empty | control_strings;
1.37      paf       200: control_strings: control_string | control_strings control_string { $$=$1; P($$, $2) };
                    201: control_string: maybe_string '\n';
                    202: maybe_string: empty | STRING;
1.34      paf       203: 
                    204: code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' 
1.10      paf       205:                        maybe_codes {
1.120     paf       206:        const String *name=LA2S($2);
1.10      paf       207: 
                    208:        YYSTYPE params_names_code=$3;
1.75      paf       209:        Array *params_names=0;
                    210:        if(int size=params_names_code->size()) {
                    211:                params_names=NEW Array(POOL);
                    212:                for(int i=0; i<size; i+=2)
1.120     paf       213:                        *params_names+=LA2S(params_names_code, i);
1.75      paf       214:        }
1.10      paf       215: 
                    216:        YYSTYPE locals_names_code=$4;
1.75      paf       217:        Array *locals_names=0;
                    218:        if(int size=locals_names_code->size()) {
                    219:                locals_names=NEW Array(POOL);
                    220:                for(int i=0; i<size; i+=2)
1.120     paf       221:                        *locals_names+=LA2S(locals_names_code, i);
1.75      paf       222:        }
1.10      paf       223: 
1.76      paf       224:        Method& method=*NEW Method(POOL, 
                    225:                *name, 
1.122     paf       226:                Method::CT_ANY,
1.81      paf       227:                0, 0/*min,max numbered_params_count*/, 
1.76      paf       228:                params_names, locals_names, 
                    229:                $7, 0);
1.114     paf       230:        PC.cclass->add_method(*name, method);
1.8       paf       231: };
1.10      paf       232: 
                    233: maybe_bracketed_strings: empty | bracketed_maybe_strings;
                    234: bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2};
                    235: maybe_strings: empty | strings;
                    236: strings: STRING | strings ';' STRING { $$=$1; P($$, $3) };
                    237: 
                    238: maybe_comment: empty | STRING;
1.1       paf       239: 
                    240: /* codes */
                    241: 
1.10      paf       242: maybe_codes: empty | codes;
                    243: 
1.90      paf       244: codes: code | codes code { $$=$1; P($$, $2) };
1.81      paf       245: code: write_string | action;
1.149     parser    246: action: get | put | call;
1.1       paf       247: 
                    248: /* get */
                    249: 
1.64      paf       250: get: get_value {
1.184     paf       251:        $$=$1; /* stack: resulting value */ 
                    252:        changetail_or_append($$, 
1.186     paf       253:                OP_GET_ELEMENT, false,  /*->*/OP_GET_ELEMENT__WRITE,
                    254:                /*or */OP_WRITE_VALUE
1.184     paf       255:                ); /* value=pop; wcontext.write(value) */
1.1       paf       256: };
1.163     parser    257: get_value: '$' get_name_value { $$=$2 };
1.64      paf       258: get_name_value: name_without_curly_rdive EON | name_in_curly_rdive;
1.1       paf       259: name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2 };
1.44      paf       260: name_without_curly_rdive: 
                    261:        name_without_curly_rdive_read 
                    262: |      name_without_curly_rdive_class;
1.19      paf       263: name_without_curly_rdive_read: name_without_curly_rdive_code {
1.25      paf       264:        $$=N(POOL); 
1.22      paf       265:        Array *diving_code=$1;
1.120     paf       266:        const String *first_name=LA2S(diving_code);
1.179     paf       267:        // self.xxx... -> xxx...
                    268:        // OP_VALUE+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+...
1.85      paf       269:        if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54      paf       270:                O($$, OP_WITH_SELF); /* stack: starting context */
1.22      paf       271:                P($$, diving_code, 
                    272:                        /* skip over... */
1.179     paf       273:                        diving_code->size()>=3?3/*OP_VALUE+string+OP_GET_ELEMENTx*/:2/*OP_+string*/);
1.22      paf       274:        } else {
1.54      paf       275:                O($$, OP_WITH_READ); /* stack: starting context */
1.179     paf       276: 
1.201     paf       277:                // ^if ELEMENT -> ^if ELEMENT_OR_OPERATOR
                    278:                // OP_VALUE+string+OP_GET_ELEMENT. -> OP_VALUE+string+OP_GET_ELEMENT_OR_OPERATOR.
                    279:                if(PC.in_call_value && diving_code->size()==3)
                    280:                        diving_code->put_int(2, OP_GET_ELEMENT_OR_OPERATOR);
1.22      paf       281:                P($$, diving_code);
                    282:        }
                    283:        /* diving code; stack: current context */
1.1       paf       284: };
1.155     parser    285: name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P($$, $2) };
1.19      paf       286: name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P($$, $2) };
1.1       paf       287: 
                    288: /* put */
                    289: 
1.81      paf       290: put: '$' name_expr_wdive construct {
1.20      paf       291:        $$=$2; /* stack: context,name */
1.52      paf       292:        P($$, $3); /* stack: context,name,constructor_value */
1.20      paf       293: };
1.44      paf       294: name_expr_wdive: 
1.157     parser    295:        name_expr_wdive_root
                    296: |      name_expr_wdive_write
1.44      paf       297: |      name_expr_wdive_class;
1.157     parser    298: name_expr_wdive_root: name_expr_dive_code {
1.44      paf       299:        $$=N(POOL);
1.23      paf       300:        Array *diving_code=$1;
1.120     paf       301:        const String *first_name=LA2S(diving_code);
1.179     paf       302:        // $self.xxx... -> $xxx...
                    303:        // OP_VALUE+string+OP_GET_ELEMENT+... -> OP_WITH_SELF+...
1.85      paf       304:        if(first_name && *first_name==SELF_ELEMENT_NAME) {
1.54      paf       305:                O($$, OP_WITH_SELF); /* stack: starting context */
1.23      paf       306:                P($$, diving_code, 
                    307:                        /* skip over... */
1.179     paf       308:                        diving_code->size()>=3?3/*OP_VALUE+string+OP_GET_ELEMENTx*/:2/*OP_+string*/);
1.23      paf       309:        } else {
1.157     parser    310:                O($$, OP_WITH_ROOT); /* stack: starting context */
1.23      paf       311:                P($$, diving_code);
                    312:        }
                    313:        /* diving code; stack: current context */
1.156     parser    314: };
1.157     parser    315: name_expr_wdive_write: '.' name_expr_dive_code {
1.156     parser    316:        $$=N(POOL); 
1.157     parser    317:        O($$, OP_WITH_WRITE); /* stack: starting context */
1.156     parser    318:        P($$, $2); /* diving code; stack: context,name */
1.20      paf       319: };
1.155     parser    320: name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P($$, $2) };
1.20      paf       321: 
1.149     parser    322: construct: 
1.163     parser    323:        construct_square
                    324: |      construct_round
                    325: |      construct_curly
1.149     parser    326: ;
                    327: construct_square: '[' any_constructor_code_value ']' {
                    328:        // stack: context, name
                    329:        $$=$2; // stack: context, name, value
1.81      paf       330:        O($$, OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */
                    331: }
                    332: ;
1.149     parser    333: construct_round: '(' expr_value ')' { 
1.182     paf       334:        $$=N(POOL); 
                    335:        O($$, OP_PREPARE_TO_EXPRESSION);
1.149     parser    336:        // stack: context, name
1.182     paf       337:        P($$, $2); // stack: context, name, value
1.163     parser    338:        O($$, OP_CONSTRUCT_EXPR); /* value=pop->as_expr_result; name=pop; context=pop; construct(context,name,value) */
1.81      paf       339: }
1.52      paf       340: ;
1.149     parser    341: construct_curly: '{' maybe_codes '}' {
                    342:        // stack: context, name
                    343:        $$=N(POOL); 
1.186     paf       344:        OA($$, OP_CURLY_CODE__CONSTRUCT, $2); /* code=pop; name=pop; context=pop; construct(context,name,junction(code)) */
1.149     parser    345: };
                    346: 
1.55      paf       347: any_constructor_code_value: 
1.157     parser    348:        void_value /* optimized $var[] case */
1.52      paf       349: |      STRING /* optimized $var[STRING] case */
1.55      paf       350: |      constructor_code_value /* $var[something complex] */
1.1       paf       351: ;
1.55      paf       352: constructor_code_value: constructor_code {
1.25      paf       353:        $$=N(POOL); 
1.186     paf       354:        OA($$, OP_OBJECT_POOL, $1); /* stack: empty write context */
1.183     paf       355:        /* some code that writes to that context */
                    356:        /* context=pop; stack: context.value() */
1.1       paf       357: };
1.55      paf       358: constructor_code: codes__excluding_sole_str_literal;
1.27      paf       359: codes__excluding_sole_str_literal: action | code codes { $$=$1; P($$, $2) };
                    360: 
1.1       paf       361: /* call */
                    362: 
1.66      paf       363: call: call_value {
                    364:        $$=$1; /* stack: value */
1.185     paf       365:        changetail_or_append($$, 
1.186     paf       366:                OP_CALL, true,  /*->*/ OP_CALL__WRITE,
                    367:                /*or */OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */
1.66      paf       368: };
1.177     paf       369: call_value: '^' { 
1.180     paf       370:                                        PC.in_call_value=true; 
1.177     paf       371:                        }
                    372:                        call_name {
1.180     paf       373:                                PC.in_call_value=false;
1.177     paf       374:                        } 
1.154     parser    375:                        store_params EON { /* ^field.$method{vasya} */
                    376:        $$=$3; /* with_xxx,diving code; stack: context,method_junction */
1.123     paf       377: 
1.154     parser    378:        YYSTYPE params_code=$5;
1.186     paf       379:        if(params_code->size()==3) { // probably [] case. [OP_VALUE + Void + STORE_PARAM]
1.123     paf       380:                if(Value *value=LA2V(params_code)) // it is OP_VALUE + value?
1.140     parser    381:                        if(!value->is_defined()) // value is VVoid?
1.123     paf       382:                                params_code=0; // ^zzz[] case. don't append lone empty param.
1.186     paf       383:        }
                    384:        /* stack: context, method_junction */
                    385:        OA($$, OP_CALL, params_code); // method_frame=make frame(pop junction); ncontext=pop; call(ncontext,method_frame) stack: value
1.1       paf       386: };
                    387: 
1.43      paf       388: call_name: name_without_curly_rdive;
1.38      paf       389: 
1.9       paf       390: store_params: store_param | store_params store_param { $$=$1; P($$, $2) };
1.69      paf       391: store_param: 
                    392:        store_square_param
                    393: |      store_round_param
                    394: |      store_curly_param
1.31      paf       395: ;
1.123     paf       396: store_square_param: '[' store_code_param_parts ']' {$$=$2};
1.69      paf       397: store_round_param: '(' store_expr_param_parts ')' {$$=$2};
1.94      paf       398: store_curly_param: '{' store_curly_param_parts '}' {$$=$2};
1.69      paf       399: store_code_param_parts:
                    400:        store_code_param_part
                    401: |      store_code_param_parts ';' store_code_param_part { $$=$1; P($$, $3) }
                    402: ;
                    403: store_expr_param_parts:
                    404:        store_expr_param_part
                    405: |      store_expr_param_parts ';' store_expr_param_part { $$=$1; P($$, $3) }
                    406: ;
1.94      paf       407: store_curly_param_parts:
                    408:        store_curly_param_part
                    409: |      store_curly_param_parts ';' store_curly_param_part { $$=$1; P($$, $3) }
                    410: ;
1.120     paf       411: store_code_param_part: code_param_value {
1.32      paf       412:        $$=$1;
1.54      paf       413:        O($$, OP_STORE_PARAM);
1.120     paf       414: };
1.69      paf       415: store_expr_param_part: write_expr_value {
                    416:        $$=N(POOL); 
1.186     paf       417:        OA($$, OP_EXPR_CODE__STORE_PARAM, $1);
1.69      paf       418: };
1.94      paf       419: store_curly_param_part: maybe_codes {
                    420:        $$=N(POOL); 
1.186     paf       421:        OA($$, OP_CURLY_CODE__STORE_PARAM, $1);
1.94      paf       422: };
1.120     paf       423: code_param_value:
1.157     parser    424:        void_value /* optimized [;...] case */
1.120     paf       425: |      STRING /* optimized [STRING] case */
                    426: |      constructor_code_value /* [something complex] */
                    427: ;
1.90      paf       428: write_expr_value: expr_value {
1.182     paf       429:        $$=N(POOL); 
                    430:        O($$, OP_PREPARE_TO_EXPRESSION);
                    431:        P($$, $1);
1.102     paf       432:        O($$, OP_WRITE_EXPR_RESULT);
1.69      paf       433: };
1.1       paf       434: 
                    435: /* name */
                    436: 
1.20      paf       437: name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P($$, $2) };
1.1       paf       438: 
1.9       paf       439: name_path: name_step | name_path name_step { $$=$1; P($$, $2) };
1.1       paf       440: name_step: name_advance1 '.';
                    441: name_advance1: name_expr_value {
1.177     paf       442:        // we know that name_advance1 not called from ^xxx context
                    443:        // so we'll not check for operator call possibility as we do in name_advance2
                    444: 
1.1       paf       445:        /* stack: context */
                    446:        $$=$1; /* stack: context,name */
1.54      paf       447:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       448: };
                    449: name_advance2: name_expr_value {
                    450:        /* stack: context */
                    451:        $$=$1; /* stack: context,name */
1.179     paf       452:        O($$, OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.1       paf       453: }
1.4       paf       454: |      STRING BOGUS
1.1       paf       455: ;
                    456: name_expr_value: 
1.4       paf       457:        STRING /* subname_is_const */
1.1       paf       458: |      name_expr_subvar_value /* $subname_is_var_value */
1.160     parser    459: |      name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value */
1.166     parser    460: |      name_square_code_value /* [codes] */
1.1       paf       461: ;
                    462: name_expr_subvar_value: '$' subvar_ref_name_rdive {
                    463:        $$=$2;
1.54      paf       464:        O($$, OP_GET_ELEMENT);
1.1       paf       465: };
1.4       paf       466: name_expr_with_subvar_value: STRING subvar_get_writes {
1.183     paf       467:        Array *code;
                    468:        {
                    469:                change_string_literal_to_write_string_literal(code=$1);
                    470:                P(code, $2);
                    471:        }
1.25      paf       472:        $$=N(POOL); 
1.186     paf       473:        OA($$, OP_STRING_POOL, code);
1.1       paf       474: };
1.166     parser    475: name_square_code_value: '[' codes ']' {
1.160     parser    476:        $$=N(POOL); 
1.186     paf       477:        OA($$, OP_OBJECT_POOL, $2); /* stack: empty write context */
1.183     paf       478:        /* some code that writes to that context */
                    479:        /* context=pop; stack: context.value() */
1.160     parser    480: };
1.154     parser    481: subvar_ref_name_rdive: STRING {
1.25      paf       482:        $$=N(POOL); 
1.54      paf       483:        O($$, OP_WITH_READ);
1.9       paf       484:        P($$, $1);
1.1       paf       485: };
1.9       paf       486: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P($$, $2) };
1.1       paf       487: subvar__get_write: '$' subvar_ref_name_rdive {
                    488:        $$=$2;
1.54      paf       489:        O($$, OP_GET_ELEMENT__WRITE);
1.42      paf       490: };
                    491: 
1.154     parser    492: class_prefix:
                    493:        class_static_prefix
                    494: |      class_constructor_prefix
                    495: ;
1.155     parser    496: class_static_prefix: STRING ':' {
                    497:        $$=$1; // stack: class name string
1.189     paf       498:        if(*LA2S($$) == BASE_NAME) { // pseude BASE class
                    499:                if(VStateless_class *base=PC.cclass->base_class()) {
                    500:                        change_string_literal_value($$, base->name());
                    501:                } else {
                    502:                        strcpy(PC.error, "no base class declared");
                    503:                        YYERROR;
                    504:                }
                    505:        }
1.155     parser    506:        O($$, OP_GET_CLASS);
                    507: };
                    508: class_constructor_prefix: class_static_prefix ':' {
1.154     parser    509:        $$=$1;
1.180     paf       510:        if(!PC.in_call_value) {
1.154     parser    511:                strcpy(PC.error, ":: not allowed here");
                    512:                YYERROR;
                    513:        }
1.155     parser    514:        O($$, OP_PREPARE_TO_CONSTRUCT_OBJECT);
1.1       paf       515: };
                    516: 
1.53      paf       517: 
1.56      paf       518: /* expr */
1.53      paf       519: 
1.81      paf       520: expr_value: expr {
1.182     paf       521:        // see OP_PREPARE_TO_EXPRESSION!!
1.81      paf       522:        if(($$=$1)->size()==2) // only one string literal in there?
1.62      paf       523:                change_string_literal_to_double_literal($$); // make that string literal Double
                    524: };
1.56      paf       525: expr: 
1.62      paf       526:        STRING
1.64      paf       527: |      get_value
1.66      paf       528: |      call_value
1.64      paf       529: |      '"' string_inside_quotes_value '"' { $$ = $2; }
1.145     parser    530: |      '\'' string_inside_quotes_value '\'' { $$ = $2; }
1.60      paf       531: |      '(' expr ')' { $$ = $2; }
                    532: /* stack: operand // stack: @operand */
1.202   ! paf       533: |      '-' expr %prec NUNARY { $$=$2;  O($$, OP_NEG) }
        !           534: |      '+' expr %prec NUNARY { $$=$2 }
1.68      paf       535: |      '~' expr { $$=$2;        O($$, OP_INV) }
                    536: |      '!' expr { $$=$2;  O($$, OP_NOT) }
                    537: |      "def" expr { $$=$2;  O($$, OP_DEF) }
                    538: |      "in" expr { $$=$2;  O($$, OP_IN) }
                    539: |      "-f" expr { $$=$2;  O($$, OP_FEXISTS) }
1.127     paf       540: |      "-d" expr { $$=$2;  O($$, OP_DEXISTS) }
1.60      paf       541: /* stack: a,b // stack: a@b */
                    542: |      expr '-' expr { $$=$1;  P($$, $3);  O($$, OP_SUB) }
                    543: |      expr '+' expr { $$=$1;  P($$, $3);  O($$, OP_ADD) }
                    544: |      expr '*' expr { $$=$1;  P($$, $3);  O($$, OP_MUL) }
                    545: |      expr '/' expr { $$=$1;  P($$, $3);  O($$, OP_DIV) }
                    546: |      expr '%' expr { $$=$1;  P($$, $3);  O($$, OP_MOD) }
1.173     paf       547: |      expr '\\' expr { $$=$1;  P($$, $3);  O($$, OP_INTDIV) }
1.195     paf       548: |      expr "<<" expr { $$=$1;  P($$, $3);  O($$, OP_BIN_SL) }
                    549: |      expr ">>" expr { $$=$1;  P($$, $3);  O($$, OP_BIN_SR) }
1.60      paf       550: |      expr '&' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_AND) }
                    551: |      expr '|' expr { $$=$1;  P($$, $3);  O($$, OP_BIN_OR) }
1.193     paf       552: |      expr "!|" expr { $$=$1;  P($$, $3);  O($$, OP_BIN_XOR) }
1.186     paf       553: |      expr "&&" expr { $$=$1;  OA($$, OP_NESTED_CODE, $3);  O($$, OP_LOG_AND) }
                    554: |      expr "||" expr { $$=$1;  OA($$, OP_NESTED_CODE, $3);  O($$, OP_LOG_OR) }
1.193     paf       555: |      expr "!||" expr { $$=$1;  P($$, $3);  O($$, OP_LOG_XOR) }
1.60      paf       556: |      expr '<' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LT) }
                    557: |      expr '>' expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GT) }
                    558: |      expr "<=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_LE) }
                    559: |      expr ">=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_GE) }
                    560: |      expr "==" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_EQ) }
                    561: |      expr "!=" expr { $$=$1;  P($$, $3);  O($$, OP_NUM_NE) }
1.61      paf       562: |      expr "lt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LT) }
                    563: |      expr "gt" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GT) }
                    564: |      expr "le" expr { $$=$1;  P($$, $3);  O($$, OP_STR_LE) }
                    565: |      expr "ge" expr { $$=$1;  P($$, $3);  O($$, OP_STR_GE) }
                    566: |      expr "eq" expr { $$=$1;  P($$, $3);  O($$, OP_STR_EQ) }
                    567: |      expr "ne" expr { $$=$1;  P($$, $3);  O($$, OP_STR_NE) }
1.95      paf       568: |      expr "is" expr { $$=$1;  P($$, $3);  O($$, OP_IS) }
1.56      paf       569: ;
1.55      paf       570: 
1.65      paf       571: string_inside_quotes_value: maybe_codes {
1.64      paf       572:        $$=N(POOL);
1.186     paf       573:        OA($$, OP_STRING_POOL, $1); /* stack: empty write context */
1.183     paf       574:        /* some code that writes to that context */
                    575:        /* context=pop; stack: context.get_string() */
1.53      paf       576: };
1.1       paf       577: 
1.27      paf       578: /* basics */
1.1       paf       579: 
1.81      paf       580: write_string: STRING {
1.102     paf       581:        // optimized from OP_STRING+OP_WRITE_VALUE to OP_STRING__WRITE
1.84      paf       582:        change_string_literal_to_write_string_literal($$=$1)
1.54      paf       583: };
                    584: 
1.157     parser    585: void_value: /* empty */ { $$=VL(NEW VVoid(POOL)) };
1.25      paf       586: empty: /* empty */ { $$=N(POOL) };
1.1       paf       587: 
                    588: %%
1.105     paf       589: #endif
1.1       paf       590: 
                    591: /*
                    592:        000$111(2222)00 
                    593:                000$111{3333}00
1.9       paf       594:        $,^: push,=0
1.1       paf       595:        1:( { break=pop
                    596:        2:( )  pop
                    597:        3:{ }  pop
                    598: 
                    599:        000^111(2222)4444{33333}4000
1.9       paf       600:        $,^: push,=0
1.1       paf       601:        1:( { break=pop
                    602:        2:( )=4
                    603:        3:{ }=4
                    604:                4:[^({]=pop
                    605: */
                    606: 
1.110     paf       607: static int yylex(YYSTYPE *lvalp, void *pc) {
1.177     paf       608:        #define lexical_brackets_nestage PC.brackets_nestages[PC.ls_sp]
1.48      paf       609:        #define RC {result=c; goto break2; }
1.1       paf       610: 
                    611:     register int c;
                    612:     int result;
                    613:        
1.114     paf       614:        if(PC.pending_state) {
                    615:                result=PC.pending_state;
                    616:                PC.pending_state=0;
1.1       paf       617:                return result;
                    618:        }
                    619:        
1.114     paf       620:        const char *begin=PC.source;
1.91      paf       621:        const char *end;
1.114     paf       622:        int begin_line=PC.line;
1.67      paf       623:        int skip_analized=0;
1.50      paf       624:        while(true) {
1.114     paf       625:                c=*(end=(PC.source++));
1.166     parser    626: //             fprintf(stderr, "\nchar: %c %02X; nestage: %d, sp=%d", c, c, lexical_brackets_nestage, PC.sp);
1.1       paf       627: 
1.4       paf       628:                if(c=='\n') {
1.114     paf       629:                        PC.line++;
                    630:                        PC.col=0;
1.10      paf       631:                } else
1.114     paf       632:                        PC.col++;
1.73      paf       633: 
1.171     parser    634:                if(c=='@' && PC.col==0+1) {
1.175     paf       635:                        if(PC.ls==LS_DEF_SPECIAL_BODY) {
                    636:                                // @SPECIAL
                    637:                                // ...
                    638:                                // @<here = 
                    639:                                pop_LS(PC); // exiting from LS_DEF_SPECIAL_BODY state
                    640:                        } // continuing checks
1.171     parser    641:                        if(PC.ls==LS_USER) {
                    642:                                push_LS(PC, LS_DEF_NAME);
                    643:                                RC;
1.175     paf       644:                        } else // @ in first column inside some code [when could that be?]
1.171     parser    645:                                result=BAD_METHOD_DECL_START;
                    646:                        goto break2;
                    647:                } else if(c=='^')
1.169     parser    648:                        switch(PC.ls) {
                    649: case LS_EXPRESSION_VAR_NAME_WITH_COLON:
                    650: case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                    651: case LS_VAR_NAME_SIMPLE_WITH_COLON:
                    652: case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                    653: case LS_VAR_NAME_CURLY:
                    654: case LS_METHOD_NAME:
1.194     paf       655: case LS_USER_COMMENT:
1.169     parser    656: case LS_DEF_COMMENT:
                    657:        // no literals in names, please
                    658:        break;
                    659: default:
1.114     paf       660:                        switch(*PC.source) {
1.165     parser    661:                        // ^escaping some punctuators
1.48      paf       662:                        case '^': case '$': case ';':
1.126     paf       663:                        case '(': case ')':
1.48      paf       664:                        case '[': case ']':
                    665:                        case '{': case '}':
1.172     parser    666:                        case '"':  case ':':
1.40      paf       667:                                if(end!=begin) {
                    668:                                        // append piece till ^
1.121     paf       669:                                        PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line);
1.40      paf       670:                                }
1.63      paf       671:                                // reset piece 'begin' position & line
1.164     parser    672:                                end=begin=PC.source; // ^
1.114     paf       673:                                begin_line=PC.line;
1.164     parser    674:                                if(PC.ls==LS_METHOD_AFTER) {
                    675:                                        pop_LS(PC);
                    676:                                        result=EON;
1.165     parser    677:                                        skip_analized=-1; // return to ^ afterwards to assure it's literality
1.164     parser    678:                                        goto break2;
                    679:                                } else {
                    680:                                        // skip over _ after ^
                    681:                                        PC.source++;  PC.col++;
                    682:                                        // skip analysis = forced literal
                    683:                                        continue;
                    684:                                }
1.114     paf       685: 
                    686:                        // converting ^#HH into char(hex(HH))
                    687:                        case '#':
                    688:                                if(end!=begin) {
                    689:                                        // append piece till ^
1.121     paf       690:                                        PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line);
1.114     paf       691:                                }
                    692:                                // #HH ?
                    693:                                if(PC.source[0]=='#' && PC.source[1] && PC.source[2]) {
                    694:                                        char *hex=(char *)POOL.malloc(1);
                    695:                                        hex[0]=
                    696:                                                hex_value[(unsigned char)PC.source[1]]*0x10+
                    697:                                                hex_value[(unsigned char)PC.source[2]];
                    698:                                        if(hex[0]==0) {
                    699:                                                result=BAD_HEX_LITERAL;
                    700:                                                goto break2; // wrong hex value[no ^#00 chars allowed]: bail out
                    701:                                        }
                    702:                                        // append char(hex(HH))
1.121     paf       703:                                        PC.string->APPEND_CLEAN(hex, 1, PC.file, begin_line);
1.114     paf       704:                                        // skip over ^#HH
                    705:                                        PC.source+=3;
                    706:                                        PC.col+=3;
                    707:                                        // reset piece 'begin' position & line
                    708:                                        begin=PC.source; // ^
                    709:                                        begin_line=PC.line;
                    710:                                        continue;
                    711:                                }
                    712:                                break;
1.1       paf       713:                        }
1.169     parser    714:                        break;
                    715:                }
1.113     paf       716:                // #comment  start skipping
1.114     paf       717:                if(c=='#' && PC.col==1) {
1.112     paf       718:                        if(end!=begin) {
                    719:                                // append piece till #
1.121     paf       720:                                PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line);
1.112     paf       721:                        }
                    722:                        // fall into COMMENT lexical state [wait for \n]
1.194     paf       723:                        push_LS(PC, LS_USER_COMMENT);
1.175     paf       724:                        continue;
1.112     paf       725:                }
1.114     paf       726:                switch(PC.ls) {
1.10      paf       727: 
                    728:                // USER'S = NOT OURS
1.1       paf       729:                case LS_USER:
1.166     parser    730:         case LS_NAME_SQUARE_PART: // name.[here].xxx
1.153     parser    731:                        if(PC.trim_bof)
                    732:                                switch(c) {
                    733:                                case '\n': case ' ': case '\t':
1.152     parser    734:                                        begin=PC.source;
                    735:                                        begin_line=PC.line;
                    736:                                        continue; // skip it
1.153     parser    737:                                default:
                    738:                                        PC.trim_bof=false;
1.152     parser    739:                                }
1.48      paf       740:                        switch(c) {
                    741:                        case '$':
1.166     parser    742:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf       743:                                RC;
                    744:                        case '^':
                    745:                                push_LS(PC, LS_METHOD_NAME);
                    746:                                RC;
1.166     parser    747:                        case ']':
                    748:                                if(PC.ls==LS_NAME_SQUARE_PART)
                    749:                                        if(--lexical_brackets_nestage==0) {// $name.[co<]?>de<]?>
                    750:                                                pop_LS(PC); // $name.[co<]>de<]!>
                    751:                                                RC;
                    752:                                        }
1.160     parser    753:                                break;
1.166     parser    754:                        case '[': // $name.[co<[>de]
                    755:                                if(PC.ls==LS_NAME_SQUARE_PART)
                    756:                                        lexical_brackets_nestage++;
1.161     parser    757:                                break;
1.112     paf       758:                        }
                    759:                        break;
                    760:                        
                    761:                // #COMMENT
1.194     paf       762:                case LS_USER_COMMENT:
1.112     paf       763:                        if(c=='\n') {
                    764:                                // skip comment
1.114     paf       765:                                begin=PC.source;
                    766:                                begin_line=PC.line;
1.112     paf       767: 
                    768:                                pop_LS(PC);
                    769:                                continue;
1.1       paf       770:                        }
1.48      paf       771:                        break;
                    772:                        
                    773:                // STRING IN EXPRESSION
1.145     parser    774:                case LS_EXPRESSION_STRING_QUOTED:
                    775:                case LS_EXPRESSION_STRING_APOSTROFED:
1.48      paf       776:                        switch(c) {
                    777:                        case '"':
1.145     parser    778:                        case '\'':
                    779:                                if(
                    780:                                        PC.ls == LS_EXPRESSION_STRING_QUOTED && c=='"' ||
                    781:                                        PC.ls == LS_EXPRESSION_STRING_APOSTROFED && c=='\'') {
                    782:                                        pop_LS(PC); //"abc". | 'abc'.
                    783:                                        RC;
                    784:                                }
                    785:                                break;
1.48      paf       786:                        case '$':
1.166     parser    787:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf       788:                                RC;
                    789:                        case '^':
1.10      paf       790:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf       791:                                RC;
1.10      paf       792:                        }
                    793:                        break;
                    794: 
                    795:                // METHOD DEFINITION
                    796:                case LS_DEF_NAME:
1.48      paf       797:                        switch(c) {
                    798:                        case '[':
1.114     paf       799:                                PC.ls=LS_DEF_PARAMS;
1.48      paf       800:                                RC;
                    801:                        case '\n':
1.114     paf       802:                                PC.ls=LS_DEF_SPECIAL_BODY;
1.48      paf       803:                                RC;
1.10      paf       804:                        }
                    805:                        break;
1.48      paf       806: 
1.10      paf       807:                case LS_DEF_PARAMS:
1.48      paf       808:                        switch(c) {
1.192     paf       809:                        case '$': // common error
1.193     paf       810:                                result=BAD_METHOD_PARAMETER_NAME_CHARACTER;
                    811:                                goto break2;
1.48      paf       812:                        case ';':
                    813:                                RC;
                    814:                        case ']':
1.114     paf       815:                                PC.ls=*PC.source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf       816:                                RC;
1.49      paf       817:                        case '\n': // wrong. bailing out
1.10      paf       818:                                pop_LS(PC);
1.48      paf       819:                                RC;
1.10      paf       820:                        }
                    821:                        break;
1.48      paf       822: 
1.10      paf       823:                case LS_DEF_LOCALS:
1.48      paf       824:                        switch(c) {
                    825:                        case '[':
                    826:                        case ';':
                    827:                                RC;
                    828:                        case ']':
1.114     paf       829:                                PC.ls=LS_DEF_COMMENT;
1.48      paf       830:                                RC;
                    831:                        case '\n': // wrong. bailing out
1.10      paf       832:                                pop_LS(PC);
1.48      paf       833:                                RC;
1.10      paf       834:                        }
                    835:                        break;
1.48      paf       836: 
1.10      paf       837:                case LS_DEF_COMMENT:
                    838:                        if(c=='\n') {
                    839:                                pop_LS(PC);
1.48      paf       840:                                RC;
1.37      paf       841:                        }
                    842:                        break;
                    843: 
1.48      paf       844:                case LS_DEF_SPECIAL_BODY:
1.175     paf       845:                        if(c=='\n')
1.48      paf       846:                                RC;
                    847:                        break;
                    848: 
                    849:                // (EXPRESSION)
1.69      paf       850:                case LS_VAR_ROUND:
                    851:                case LS_METHOD_ROUND:
1.48      paf       852:                        switch(c) {
                    853:                        case ')':
                    854:                                if(--lexical_brackets_nestage==0)
1.114     paf       855:                                        if(PC.ls==LS_METHOD_ROUND) // method round param ended
                    856:                                                PC.ls=LS_METHOD_AFTER; // look for method end
                    857:                                        else // PC.ls==LS_VAR_ROUND // variable constructor ended
1.69      paf       858:                                                pop_LS(PC); // return to normal life
1.48      paf       859:                                RC;
1.194     paf       860:                        case '#': // comment start skipping
                    861:                                if(end!=begin) {
                    862:                                        // append piece till #
                    863:                                        PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line);
                    864:                                }
                    865:                                // fall into COMMENT lexical state [wait for \n]
                    866:                                push_LS(PC, LS_EXPRESSION_COMMENT);
                    867:                                lexical_brackets_nestage=1;
                    868:                                continue;
1.48      paf       869:                        case '$':
1.166     parser    870:                                push_LS(PC, LS_EXPRESSION_VAR_NAME_WITH_COLON);                         
1.48      paf       871:                                RC;
                    872:                        case '^':
                    873:                                push_LS(PC, LS_METHOD_NAME);
                    874:                                RC;
                    875:                        case '(':
                    876:                                lexical_brackets_nestage++;
                    877:                                RC;
1.67      paf       878:                        case '-':
1.127     paf       879:                                switch(*PC.source) {
                    880:                                case 'f': // -f
1.67      paf       881:                                        skip_analized=1;
                    882:                                        result=FEXISTS;
1.127     paf       883:                                        goto break2;
                    884:                                case 'd': // -d
                    885:                                        skip_analized=1;
                    886:                                        result=DEXISTS;
                    887:                                        goto break2;
1.191     paf       888:                                default: // minus
1.67      paf       889:                                        result=c;
1.127     paf       890:                                        goto break2;
                    891:                                }
1.67      paf       892:                                goto break2;
1.173     paf       893:                        case '+': case '*': case '/': case '%': case '\\':
1.58      paf       894:                        case '~':
1.48      paf       895:                        case ';':
                    896:                                RC;
1.193     paf       897:                        case '&': case '|':
1.114     paf       898:                                if(*PC.source==c) { // && ||
1.193     paf       899:                                        result=c=='&'?LAND:LOR;
1.67      paf       900:                                        skip_analized=1;
1.58      paf       901:                                } else
                    902:                                        result=c;
                    903:                                goto break2;
1.193     paf       904:                        case '!':
                    905:                                switch(PC.source[0]) { 
                    906:                                case '|': // !| !||
                    907:                                        skip_analized=1;
                    908:                                        if(PC.source[1]=='|') {
                    909:                                                skip_analized++;
                    910:                                                result=LXOR;
                    911:                                        } else
                    912:                                                result=NXOR;
                    913:                                        goto break2;
                    914:                                case '=': // !=
                    915:                                        skip_analized=1;
                    916:                                        result=NNE; 
                    917:                                        goto break2;
                    918:                                }
                    919:                                RC;
1.195     paf       920: 
                    921:                        case '<': // <<, <=, <
                    922:                                switch(*PC.source) {
                    923:                                case '<': // <[<]
                    924:                                        skip_analized=1; result=NSL; break;
                    925:                                case '=': // <[=]
                    926:                                        skip_analized=1; result=NLE; break;
                    927:                                default: // <[]
                    928:                                        result=c; break;
                    929:                                }
                    930:                                goto break2;
                    931:                        case '>': // >>, >=, >
                    932:                                switch(*PC.source) {
                    933:                                case '>': // >[>]
                    934:                                        skip_analized=1; result=NSR; break;
                    935:                                case '=': // >[=]
                    936:                                        skip_analized=1; result=NGE; break;
                    937:                                default: // >[]
                    938:                                        result=c; break;
                    939:                                }
                    940:                                goto break2;
                    941:                        case '=': // ==
                    942:                                switch(*PC.source) {
                    943:                                case '=': // =[=]
                    944:                                        skip_analized=1; result=NEQ; break;
                    945:                                default: // =[]
                    946:                                        result=c; break; // not used now
                    947:                                }
1.58      paf       948:                                goto break2;
1.195     paf       949: 
1.48      paf       950:                        case '"':
1.145     parser    951:                                push_LS(PC, LS_EXPRESSION_STRING_QUOTED);
                    952:                                RC;
                    953:                        case '\'':
                    954:                                push_LS(PC, LS_EXPRESSION_STRING_APOSTROFED);
1.48      paf       955:                                RC;
1.50      paf       956:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf       957:                                if(end==begin) // right after whitespace
1.117     paf       958:                                        if(isspace(PC.source[1])) {
                    959:                                                switch(*PC.source) {
                    960:                                                        //                                      case '?': // ok [and bad cases, yacc would bark at them]
                    961:                                                case 't': // lt gt [et nt]
                    962:                                                        result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
                    963:                                                        skip_analized=1;
                    964:                                                        goto break2;
                    965:                                                case 'e': // le ge ne [ee]
                    966:                                                        result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
                    967:                                                        skip_analized=1;
                    968:                                                        goto break2;
                    969:                                                case 'q': // eq [lq gq nq]
                    970:                                                        result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
                    971:                                                        skip_analized=1;
                    972:                                                        goto break2;
                    973:                                                }
1.67      paf       974:                                        }
                    975:                                break;
                    976:                        case 'i':
                    977:                                if(end==begin) // right after whitespace
1.117     paf       978:                                        if(isspace(PC.source[1])) {
                    979:                                                switch(PC.source[0]) {
                    980:                                                case 'n': // in
1.95      paf       981:                                                        skip_analized=1;
                    982:                                                        result=IN;
                    983:                                                        goto break2;
1.117     paf       984:                                                case 's': // is
1.95      paf       985:                                                        skip_analized=1;
                    986:                                                        result=IS;
                    987:                                                        goto break2;
                    988:                                                }
1.67      paf       989:                                        }
                    990:                                break;
                    991:                        case 'd':
                    992:                                if(end==begin) // right after whitespace
1.114     paf       993:                                        if(PC.source[0]=='e' && PC.source[1]=='f') { // def
1.67      paf       994:                                                skip_analized=2;
                    995:                                                result=DEF;
1.58      paf       996:                                                goto break2;
1.50      paf       997:                                        }
1.48      paf       998:                                break;
                    999:                        case ' ': case '\t': case '\n':
1.63      paf      1000:                                if(end!=begin) { // there were a string after previous operator?
                   1001:                                        result=0; // return that string
                   1002:                                        goto break2;
1.48      paf      1003:                                }
1.63      paf      1004:                                // that's a leading|traling space or after-operator-space
                   1005:                                // ignoring it
                   1006:                                // reset piece 'begin' position & line
1.114     paf      1007:                                begin=PC.source; // after whitespace char
                   1008:                                begin_line=PC.line;
1.48      paf      1009:                                continue;
1.1       paf      1010:                        }
                   1011:                        break;
1.194     paf      1012:                case LS_EXPRESSION_COMMENT:
                   1013:                        if(c=='(')
                   1014:                                lexical_brackets_nestage++;
                   1015:                        
                   1016:                        switch(*PC.source) {
                   1017:                        case '\n': case ')':
                   1018:                                if(*PC.source==')')
                   1019:                                        if(--lexical_brackets_nestage!=0)
                   1020:                                                continue;
                   1021: 
                   1022:                                // skip comment
                   1023:                                begin=PC.source;
                   1024:                                begin_line=PC.line;
                   1025: 
                   1026:                                pop_LS(PC);
                   1027:                                continue;
                   1028:                        }
                   1029:                        break;
1.1       paf      1030: 
1.10      paf      1031:                // VARIABLE GET/PUT/WITH
1.166     parser   1032:                case LS_VAR_NAME_SIMPLE_WITH_COLON: 
                   1033:                case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                   1034:                case LS_EXPRESSION_VAR_NAME_WITH_COLON: 
                   1035:                case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                   1036:                        if(
                   1037:                                PC.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON ||
                   1038:                                PC.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.181     paf      1039:                                // name in expr ends also before 
1.48      paf      1040:                                switch(c) {
1.181     paf      1041:                                // expression minus
1.92      paf      1042:                                case '-': 
1.181     paf      1043:                                // expression integer division
                   1044:                                case '\\':
1.48      paf      1045:                                        pop_LS(PC);
1.114     paf      1046:                                        PC.source--;  if(--PC.col<0) { PC.line--;  PC.col=-1; }
1.48      paf      1047:                                        result=EON;
                   1048:                                        goto break2;
                   1049:                                }
                   1050:                        }
1.166     parser   1051:                        if(
                   1052:                                PC.ls==LS_VAR_NAME_SIMPLE_WITHOUT_COLON ||
                   1053:                                PC.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.142     parser   1054:                                // name already has ':', stop before next 
                   1055:                                switch(c) {
                   1056:                                case ':': 
                   1057:                                        pop_LS(PC);
                   1058:                                        PC.source--;  if(--PC.col<0) { PC.line--;  PC.col=-1; }
                   1059:                                        result=EON;
                   1060:                                        goto break2;
                   1061:                                }
                   1062:                        }
1.48      paf      1063:                        switch(c) {
                   1064:                        case 0:
                   1065:                        case ' ': case '\t': case '\n':
                   1066:                        case ';':
1.126     paf      1067:                        case ']': case '}': case ')': 
                   1068:                        case '"': case '\'':
1.139     parser   1069:                        case '<': case '>':  // these stand for HTML brackets AND expression binary ops
1.92      paf      1070:                        case '+': case '*': case '/': case '%': 
                   1071:                        case '&': case '|': 
                   1072:                        case '=': case '!':
1.99      paf      1073:                        // common delimiters
1.190     paf      1074:                        case ',': case '?': case '#':
1.139     parser   1075:                        // before call
                   1076:                        case '^': 
1.1       paf      1077:                                pop_LS(PC);
1.114     paf      1078:                                PC.source--;  if(--PC.col<0) { PC.line--;  PC.col=-1; }
1.13      paf      1079:                                result=EON;
1.1       paf      1080:                                goto break2;
1.48      paf      1081:                        case '[':
1.162     parser   1082:                                // $name.<[>code]
                   1083:                                if(PC.col>1/*not first column*/ && (
1.163     parser   1084:                                        end[-1]=='$'/*was start of get*/ ||
                   1085:                                        end[-1]==':'/*was class name delim */ ||
                   1086:                                        end[-1]=='.'/*was name delim */
1.162     parser   1087:                                        )) {
1.166     parser   1088:                                        push_LS(PC, LS_NAME_SQUARE_PART);
1.162     parser   1089:                                        lexical_brackets_nestage=1;
                   1090:                                        RC;
                   1091:                                }
1.114     paf      1092:                                PC.ls=LS_VAR_SQUARE;
1.1       paf      1093:                                lexical_brackets_nestage=1;
1.48      paf      1094:                                RC;
                   1095:                        case '{':
                   1096:                                if(begin==end) { // ${name}, no need of EON, switching LS
1.114     paf      1097:                                        PC.ls=LS_VAR_NAME_CURLY; 
1.48      paf      1098:                                } else {
1.114     paf      1099:                                        PC.ls=LS_VAR_CURLY;
1.48      paf      1100:                                        lexical_brackets_nestage=1;
                   1101:                                }
1.69      paf      1102: 
1.48      paf      1103:                                RC;
                   1104:                        case '(':
1.114     paf      1105:                                PC.ls=LS_VAR_ROUND;
1.1       paf      1106:                                lexical_brackets_nestage=1;
1.48      paf      1107:                                RC;
                   1108:                        case '.': // name part delim
                   1109:                        case '$': // name part subvar
1.160     parser   1110:                        case ':': // class<:>name
1.166     parser   1111:                                // go to _WITHOUT_COLON state variant...
                   1112:                                if(PC.ls==LS_VAR_NAME_SIMPLE_WITH_COLON)
                   1113:                                        PC.ls=LS_VAR_NAME_SIMPLE_WITHOUT_COLON;
                   1114:                                else if(PC.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON)
                   1115:                                        PC.ls=LS_EXPRESSION_VAR_NAME_WITHOUT_COLON;
                   1116:                                // ...stop before next ':'
1.48      paf      1117:                                RC;
1.1       paf      1118:                        }
                   1119:                        break;
1.48      paf      1120: 
1.1       paf      1121:                case LS_VAR_NAME_CURLY:
1.48      paf      1122:                        switch(c) {
1.162     parser   1123:                        case '[':
1.166     parser   1124:                                // ${name.<[>code]}
                   1125:                                push_LS(PC, LS_NAME_SQUARE_PART);
1.160     parser   1126:                                lexical_brackets_nestage=1;
                   1127:                                RC;
1.48      paf      1128:                        case '}': // ${name} finished, restoring LS
1.1       paf      1129:                                pop_LS(PC);
1.48      paf      1130:                                RC;
                   1131:                        case '.': // name part delim
                   1132:                        case '$': // name part subvar
                   1133:                        case ':': // ':name' or 'class:name'
                   1134:                                RC;
1.1       paf      1135:                        }
                   1136:                        break;
1.48      paf      1137: 
                   1138:                case LS_VAR_SQUARE:
                   1139:                        switch(c) {
                   1140:                        case '$':
1.166     parser   1141:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1142:                                RC;
                   1143:                        case '^':
1.10      paf      1144:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf      1145:                                RC;
                   1146:                        case ']':
1.1       paf      1147:                                if(--lexical_brackets_nestage==0) {
                   1148:                                        pop_LS(PC);
1.48      paf      1149:                                        RC;
1.1       paf      1150:                                }
1.48      paf      1151:                                break;
                   1152:                        case ';': // operator_or_fmt;value delim
                   1153:                                RC;
                   1154:                        case '[':
                   1155:                                lexical_brackets_nestage++;
                   1156:                                break;
1.1       paf      1157:                        }
                   1158:                        break;
1.48      paf      1159: 
1.1       paf      1160:                case LS_VAR_CURLY:
1.48      paf      1161:                        switch(c) {
                   1162:                        case '$':
1.166     parser   1163:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1164:                                RC;
                   1165:                        case '^':
1.10      paf      1166:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf      1167:                                RC;
                   1168:                        case '}':
1.1       paf      1169:                                if(--lexical_brackets_nestage==0) {
                   1170:                                        pop_LS(PC);
1.48      paf      1171:                                        RC;
1.1       paf      1172:                                }
1.48      paf      1173:                                break;
                   1174:                        case '{':
1.1       paf      1175:                                lexical_brackets_nestage++;
1.48      paf      1176:                                break;
                   1177:                        }
1.1       paf      1178:                        break;
                   1179: 
1.10      paf      1180:                // METHOD CALL
1.1       paf      1181:                case LS_METHOD_NAME:
1.48      paf      1182:                        switch(c) {
                   1183:                        case '[':
1.166     parser   1184:                                // ^name.<[>code].xxx
1.162     parser   1185:                                if(PC.col>1/*not first column*/ && (
1.163     parser   1186:                                        end[-1]=='^'/*was start of call*/ || // never, ^[ is literal...
                   1187:                                        end[-1]==':'/*was class name delim */ ||
                   1188:                                        end[-1]=='.'/*was name delim */
1.162     parser   1189:                                        )) {
1.166     parser   1190:                                        push_LS(PC, LS_NAME_SQUARE_PART);
1.162     parser   1191:                                        lexical_brackets_nestage=1;
                   1192:                                        RC;
                   1193:                                }
1.114     paf      1194:                                PC.ls=LS_METHOD_SQUARE;
1.1       paf      1195:                                lexical_brackets_nestage=1;
1.48      paf      1196:                                RC;
                   1197:                        case '{':
1.114     paf      1198:                                PC.ls=LS_METHOD_CURLY;
1.1       paf      1199:                                lexical_brackets_nestage=1;
1.48      paf      1200:                                RC;
1.69      paf      1201:                        case '(':
1.114     paf      1202:                                PC.ls=LS_METHOD_ROUND;
1.69      paf      1203:                                lexical_brackets_nestage=1;
                   1204:                                RC;
1.48      paf      1205:                        case '.': // name part delim 
                   1206:                        case '$': // name part subvar
                   1207:                        case ':': // ':name' or 'class:name'
1.170     parser   1208:                        case '^': // ^abc^xxx wrong. bailing out
                   1209:                        case ']': case '}': case ')': // ^abc]}) wrong. bailing out
1.48      paf      1210:                                RC;
1.1       paf      1211:                        }
                   1212:                        break;
1.48      paf      1213: 
                   1214:                case LS_METHOD_SQUARE:
                   1215:                        switch(c) {
                   1216:                        case '$':
1.166     parser   1217:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1218:                                RC;
                   1219:                        case '^':
1.10      paf      1220:                                push_LS(PC, LS_METHOD_NAME);
1.48      paf      1221:                                RC;
                   1222:                        case ';': // param delim
                   1223:                                RC;
                   1224:                        case ']':
1.1       paf      1225:                                if(--lexical_brackets_nestage==0) {
1.114     paf      1226:                                        PC.ls=LS_METHOD_AFTER;
1.48      paf      1227:                                        RC;
1.1       paf      1228:                                }
1.48      paf      1229:                                break;
                   1230:                        case '[':
1.1       paf      1231:                                lexical_brackets_nestage++;
1.48      paf      1232:                                break;
                   1233:                        }
1.1       paf      1234:                        break;
1.48      paf      1235: 
1.1       paf      1236:                case LS_METHOD_CURLY:
1.48      paf      1237:                        switch(c) {
                   1238:                        case '$':
1.166     parser   1239:                                push_LS(PC, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1240:                                RC;
                   1241:                        case '^':
1.10      paf      1242:                                push_LS(PC, LS_METHOD_NAME);
1.94      paf      1243:                                RC;
                   1244:                        case ';': // param delim
1.48      paf      1245:                                RC;
                   1246:                        case '}':
1.1       paf      1247:                                if(--lexical_brackets_nestage==0) {
1.114     paf      1248:                                        PC.ls=LS_METHOD_AFTER;
1.48      paf      1249:                                        RC;
1.1       paf      1250:                                }
1.48      paf      1251:                                break;
                   1252:                        case '{':
1.1       paf      1253:                                lexical_brackets_nestage++;
1.48      paf      1254:                                break;
                   1255:                        }
1.1       paf      1256:                        break;
1.48      paf      1257: 
1.1       paf      1258:                case LS_METHOD_AFTER:
1.69      paf      1259:                        if(c=='[') {/* ][ }[ )[ */
1.114     paf      1260:                                PC.ls=LS_METHOD_SQUARE;
1.1       paf      1261:                                lexical_brackets_nestage=1;
1.48      paf      1262:                                RC;
1.1       paf      1263:                        }                                          
1.69      paf      1264:                        if(c=='{') {/* ]{ }{ ){ */
1.114     paf      1265:                                PC.ls=LS_METHOD_CURLY;
1.69      paf      1266:                                lexical_brackets_nestage=1;
                   1267:                                RC;
                   1268:                        }                                          
                   1269:                        if(c=='(') {/* ]( }( )( */
1.114     paf      1270:                                PC.ls=LS_METHOD_ROUND;
1.1       paf      1271:                                lexical_brackets_nestage=1;
1.48      paf      1272:                                RC;
1.1       paf      1273:                        }                                          
                   1274:                        pop_LS(PC);
1.114     paf      1275:                        PC.source--;  if(--PC.col<0) { PC.line--;  PC.col=-1; }
1.13      paf      1276:                        result=EON;
1.1       paf      1277:                        goto break2;
                   1278:                }
1.9       paf      1279:                if(c==0) {
1.1       paf      1280:                        result=-1;
                   1281:                        break;
                   1282:                }
                   1283:        }
                   1284: 
                   1285: break2:
1.59      paf      1286:        if(end!=begin) { // there is last piece?
                   1287:                if((c=='@' || c==0) && end[-1]=='\n') { // we are before LS_DEF_NAME or EOF?
                   1288:                        // strip last \n
1.10      paf      1289:                        end--;
1.137     parser   1290:                        if(end!=begin && end[-1]=='\n') // allow one empty line before LS_DEF_NAME
                   1291:                                end--;
1.59      paf      1292:                }
1.194     paf      1293:                if(end!=begin && PC.ls!=LS_USER_COMMENT) { // last piece still alive and not comment?
1.59      paf      1294:                        // append it
1.121     paf      1295:                        PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line/*, start_col*/);
1.30      paf      1296:                }
1.59      paf      1297:        }
1.114     paf      1298:        if(PC.string->size()) { // something accumulated?
1.17      paf      1299:                // create STRING value: array of OP_VALUE+vstring
1.114     paf      1300:                *lvalp=VL(NEW VString(*PC.string));
1.10      paf      1301:                // new pieces storage
1.114     paf      1302:                PC.string=NEW String(POOL);
1.58      paf      1303:                // make current result be pending for next call, return STRING for now
1.114     paf      1304:                PC.pending_state=result;  result=STRING;
1.58      paf      1305:        }
1.67      paf      1306:        if(skip_analized) {
1.114     paf      1307:                PC.source+=skip_analized;  PC.col+=skip_analized;
1.1       paf      1308:        }
1.58      paf      1309:        return result;
1.1       paf      1310: }
                   1311: 
1.110     paf      1312: static int real_yyerror(parse_control *pc, char *s) {  // Called by yyparse on error
1.114     paf      1313:           strncpy(PC.error, s, MAX_STRING);
1.1       paf      1314:           return 1;
1.110     paf      1315: }
1.1       paf      1316: 
1.110     paf      1317: static void yyprint(FILE *file, int type, YYSTYPE value) {
                   1318:        if(type==STRING)
1.120     paf      1319:                fprintf(file, " \"%s\"", LA2S(value)->cstr());
1.110     paf      1320: }

E-mail: