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

1.168     parser      1: %{
1.105     paf         2: /** @file
1.106     paf         3:        Parser: compiler(lexical parser and grammar).
                      4: 
1.304   ! moko        5:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.294     moko        6:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.217     paf         7: 
1.262     moko        8:        
1.88      paf         9: */
                     10: 
1.304   ! moko       11: volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.303 2026/04/24 13:45:12 moko Exp $";
1.262     moko       12: 
1.106     paf        13: /**
                     14:        @todo parser4: 
                     15:        - cache compiled code from request to request. to do that...
                     16:                -#:     make method definitions, @CLASS, @BASE, @USE instructions,
1.88      paf        17:                        which would be executed afterwards, and actions
                     18:                        now performed at compile time would be delayed to run time.
1.106     paf        19:                -#:     make cache expiration on time and on disk-change of class source
                     20:                -#:     in apache use subpools for compiled class storage
                     21:                -#:     in iis make up specialized Pool object for that
1.24      paf        22: */
                     23: 
1.206     paf        24: #define YYSTYPE  ArrayOperation* 
1.9       paf        25: #define YYDEBUG  1
1.271     moko       26: #define YYERROR_VERBOSE  1
                     27: #define yyerror(pc, msg)  real_yyerror(pc, msg)
1.9       paf        28: #define YYPRINT(file, type, value)  yyprint(file, type, value)
1.269     moko       29: #define YYMALLOC pa_malloc
                     30: #define YYFREE pa_free
1.1       paf        31: 
1.206     paf        32: // includes
                     33: 
1.1       paf        34: #include "compile_tools.h"
1.8       paf        35: #include "pa_value.h"
1.12      paf        36: #include "pa_request.h"
1.39      paf        37: #include "pa_vobject.h"
1.52      paf        38: #include "pa_vdouble.h"
1.98      paf        39: #include "pa_globals.h"
1.200     paf        40: #include "pa_vmethod_frame.h"
1.39      paf        41: 
1.206     paf        42: // defines
                     43: 
1.280     moko       44: #define CLASS_NAME "CLASS"
1.97      paf        45: #define USE_CONTROL_METHOD_NAME "USE"
1.226     misha      46: #define OPTIONS_CONTROL_METHOD_NAME "OPTIONS"
1.1       paf        47: 
1.206     paf        48: // forwards
                     49: 
1.264     moko       50: static int real_yyerror(Parse_control* pc, const char* s);
1.206     paf        51: static void yyprint(FILE* file, int type, YYSTYPE value);
                     52: static int yylex(YYSTYPE* lvalp, void* pc);
1.1       paf        53: 
1.216     paf        54: static const VBool vfalse(false);
                     55: static const VBool vtrue(true);
1.261     moko       56: static const VString vempty;
1.216     paf        57: 
1.8       paf        58: // local convinient inplace typecast & var
1.205     paf        59: #undef PC
1.271     moko       60: #define PC  (*pc)
1.206     paf        61: #undef POOL
1.114     paf        62: #define POOL  (*PC.pool)
1.107     paf        63: #ifndef DOXYGEN
1.265     moko       64: 
1.296     moko       65: #define CLASS_ADD if(!PC.class_add()){                                         \
1.292     moko       66:        PC.error=pa_strcat(PC.cclass->type(), " - class is already defined");   \
                     67:        YYERROR;                                                                \
1.281     moko       68: }
                     69: 
1.292     moko       70: #define YYERROR3(header, value, footer) {              \
                     71:        PC.error=pa_strcat(header, value, footer);      \
                     72:        YYERROR;                                        \
                     73: }
                     74: 
                     75: #define YYERROR1(value) {                              \
                     76:        PC.error=value;                                 \
                     77:        YYERROR;                                        \
1.265     moko       78: }
                     79: 
1.1       paf        80: %}
                     81: 
1.303     moko       82: %define api.pure
1.271     moko       83: %lex-param {Parse_control* pc}
                     84: %parse-param {Parse_control* pc}
1.1       paf        85: 
1.13      paf        86: %token EON
1.4       paf        87: %token STRING
1.1       paf        88: %token BOGUS
1.55      paf        89: 
1.58      paf        90: %token BAD_STRING_COMPARISON_OPERATOR
1.114     paf        91: %token BAD_HEX_LITERAL
1.171     parser     92: %token BAD_METHOD_DECL_START
1.193     paf        93: %token BAD_METHOD_PARAMETER_NAME_CHARACTER
1.58      paf        94: 
1.59      paf        95: %token LAND "&&"
1.58      paf        96: %token LOR "||"
1.193     paf        97: %token LXOR "!||"
                     98: %token NXOR "!|"
1.58      paf        99: 
                    100: %token NLE "<="
                    101: %token NGE ">="
                    102: %token NEQ "=="
                    103: %token NNE "!="
1.195     paf       104: %token NSL "<<"
                    105: %token NSR ">>"
1.58      paf       106: 
                    107: %token SLT "lt"
                    108: %token SGT "gt"
                    109: %token SLE "le"
                    110: %token SGE "ge"
                    111: %token SEQ "eq"
                    112: %token SNE "ne"
                    113: 
1.67      paf       114: %token DEF "def"
                    115: %token IN "in"
                    116: %token FEXISTS "-f"
1.127     paf       117: %token DEXISTS "-d"
1.95      paf       118: %token IS "is"
1.67      paf       119: 
1.216     paf       120: %token LITERAL_TRUE "true"
                    121: %token LITERAL_FALSE "false"
                    122: 
1.57      paf       123: /* logical */
1.193     paf       124: %left "!||"
1.57      paf       125: %left "||"
                    126: %left "&&"
1.131     paf       127: %left '<' '>' "<=" ">="   "lt" "gt" "le" "ge"
                    128: %left "==" "!="  "eq" "ne"
                    129: %left "is" "def" "in" "-f" "-d"
1.57      paf       130: 
                    131: /* bitwise */
1.193     paf       132: %left "!|"
1.131     paf       133: %left '|'
                    134: %left '&' 
1.202     paf       135: %left "<<"  ">>"
1.57      paf       136: 
1.56      paf       137: /* numerical */
1.202     paf       138: %left '+' '-'
                    139: %left '*' '/' '\\' '%'
                    140: %left NUNARY   /* unary - + */
                    141: 
                    142: /* out-of-group */
                    143: %left '~' /* bitwise */
                    144: %left '!'  /* logical */
1.1       paf       145: 
                    146: %%
1.195     paf       147: all:
1.10      paf       148:        one_big_piece {
1.284     moko      149:        Method* method=new Method(Method::CT_ANY, 0, 0 /*min, max numbered_params_count*/, 0 /*param_names*/, 0 /*local_names*/, $1 /*parser_code*/, 0 /*native_code*/, PC.cclass->is_vars_local());
1.251     misha     150:        PC.cclass->set_method(PC.alias_method(main_method_name), method);
1.10      paf       151: }
                    152: |      methods;
                    153: 
                    154: methods: method | methods method;
                    155: one_big_piece: maybe_codes;
                    156: 
1.34      paf       157: method: control_method | code_method;
                    158: 
                    159: control_method: '@' STRING '\n' 
1.132     paf       160:                                maybe_control_strings {
1.260     misha     161:        const String& command=LA2S(*$2)->trim(String::TRIM_END);
1.34      paf       162:        YYSTYPE strings_code=$4;
1.292     moko      163:        if(strings_code->count()<1*OPERATIONS_PER_OPVALUE)
                    164:                YYERROR3("@", command.cstr(), " is empty");
1.74      paf       165:        if(command==CLASS_NAME) {
1.206     paf       166:                if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) {
1.265     moko      167:                        CLASS_ADD;
1.73      paf       168:                        // new class' name
1.260     misha     169:                        const String& name=LA2S(*strings_code)->trim(String::TRIM_END);
1.73      paf       170:                        // creating the class
1.288     moko      171:                        VStateless_class* cclass=new VClass(name.cstr(), PC.request.get_used_filespec(PC.file_no));
1.226     misha     172:                        PC.cclass_new=cclass;
1.266     misha     173:                        PC.append=false;
1.73      paf       174:                } else {
1.298     moko      175:                        YYERROR1("@" CLASS_NAME " must contain only one line with class name (contains more than one)");
1.34      paf       176:                }
1.130     paf       177:        } else if(command==USE_CONTROL_METHOD_NAME) {
1.265     moko      178:                CLASS_ADD;
1.273     moko      179:                for(size_t i=0; i<strings_code->count(); i+=OPERATIONS_PER_OPVALUE){
1.288     moko      180:                        PC.request.use_file(LA2S(*strings_code, i)->trim(String::TRIM_END), PC.request.get_used_filespec(PC.file_no), strings_code->get(i+1).origin);
1.273     moko      181:                }
1.130     paf       182:        } else if(command==BASE_NAME) {
1.292     moko      183:                if(PC.append)
                    184:                        YYERROR3("can't set base while appending methods to class '", PC.cclass->type(), "'");
1.265     moko      185:                CLASS_ADD;
1.292     moko      186:                if(PC.cclass->base_class()) // already changed from default?
                    187:                        YYERROR3("class already have a base '", PC.cclass->base_class()->type(), "'");
1.206     paf       188:                if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) {
1.260     misha     189:                        const String& base_name=LA2S(*strings_code)->trim(String::TRIM_END);
1.283     moko      190:                        if(VStateless_class *base_class=PC.request.get_class(base_name)) {
1.206     paf       191:                                // @CLASS == @BASE sanity check
1.292     moko      192:                                if(PC.cclass==base_class)
                    193:                                        YYERROR1("@" CLASS_NAME " equals @" BASE_NAME);
1.283     moko      194:                                PC.cclass->get_class()->set_base(base_class);
1.206     paf       195:                        } else {
1.292     moko      196:                                YYERROR3("'", base_name.cstr(), "': undefined class in @" BASE_NAME);
1.73      paf       197:                        }
1.34      paf       198:                } else {
1.292     moko      199:                        YYERROR1("@" BASE_NAME " must contain sole name");
1.34      paf       200:                }
1.223     misha     201:        } else if(command==OPTIONS_CONTROL_METHOD_NAME) {
                    202:                for(size_t i=0; i<strings_code->count(); i+=OPERATIONS_PER_OPVALUE) {
1.260     misha     203:                        const String& option=LA2S(*strings_code, i)->trim(String::TRIM_END);
1.279     moko      204:                        if(option==Symbols::LOCALS_SYMBOL){
1.226     misha     205:                                PC.set_all_vars_local();
1.279     moko      206:                        } else if(option==Symbols::PARTIAL_SYMBOL){
1.230     misha     207:                                if(PC.cclass_new){
                    208:                                        if(VStateless_class* existed=PC.get_existed_class(PC.cclass_new)){
1.292     moko      209:                                                if(!PC.reuse_existed_class(existed))
                    210:                                                        YYERROR3("can't append methods to '", PC.cclass_new->type(), "' - the class wasn't marked as partial");
1.226     misha     211:                                        } else {
1.256     misha     212:                                                // marks the new class as partial. we will be able to add methods here later.
1.230     misha     213:                                                PC.cclass_new->set_partial();
1.226     misha     214:                                        }
1.230     misha     215:                                } else {
1.292     moko      216:                                        YYERROR1("'partial' option should be used straight after @" CLASS_NAME);
1.226     misha     217:                                }
1.279     moko      218:                        } else if(option==Symbols::STATIC_SYMBOL){
1.256     misha     219:                                PC.set_methods_call_type(Method::CT_STATIC);
1.279     moko      220:                        } else if(option==Symbols::DYNAMIC_SYMBOL){
1.256     misha     221:                                PC.set_methods_call_type(Method::CT_DYNAMIC);
1.223     misha     222:                        } else {
1.292     moko      223:                                YYERROR3("'", option.cstr(), "' invalid option. valid options are 'partial', 'locals', 'static' and 'dynamic'");
1.223     misha     224:                        }
                    225:                }
1.130     paf       226:        } else {
1.292     moko      227:                YYERROR3("'", command.cstr(), "' invalid special name. valid names are '" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'.");
1.34      paf       228:        }
                    229: };
1.132     paf       230: maybe_control_strings: empty | control_strings;
1.259     misha     231: control_strings: control_string | control_strings control_string { $$=$1; P(*$$, *$2); };
1.37      paf       232: control_string: maybe_string '\n';
                    233: maybe_string: empty | STRING;
1.34      paf       234: 
1.222     misha     235: code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' { 
1.265     moko      236:        CLASS_ADD;
1.208     paf       237:        PC.explicit_result=false;
1.10      paf       238: 
                    239:        YYSTYPE params_names_code=$3;
1.206     paf       240:        ArrayString* params_names=0;
                    241:        if(int size=params_names_code->count()) {
                    242:                params_names=new ArrayString;
                    243:                for(int i=0; i<size; i+=OPERATIONS_PER_OPVALUE)
                    244:                        *params_names+=LA2S(*params_names_code, i);
1.75      paf       245:        }
1.10      paf       246: 
                    247:        YYSTYPE locals_names_code=$4;
1.206     paf       248:        ArrayString* locals_names=0;
1.284     moko      249:        bool all_vars_local=PC.cclass->is_vars_local();
                    250: 
1.206     paf       251:        if(int size=locals_names_code->count()) {
                    252:                locals_names=new ArrayString;
1.208     paf       253:                for(int i=0; i<size; i+=OPERATIONS_PER_OPVALUE) {
                    254:                        const String* local_name=LA2S(*locals_names_code, i);
1.279     moko      255:                        if(SYMBOLS_EQ(*local_name,RESULT_SYMBOL))
1.208     paf       256:                                PC.explicit_result=true;
1.279     moko      257:                        else if(SYMBOLS_EQ(*local_name,LOCALS_SYMBOL))
1.223     misha     258:                                all_vars_local=true;
1.208     paf       259:                        else
                    260:                                *locals_names+=local_name;
                    261:                }
1.75      paf       262:        }
1.10      paf       263: 
1.208     paf       264:        Method* method=new Method(
1.206     paf       265:                //name, 
1.257     misha     266:                GetMethodCallType(PC, *$2),
1.81      paf       267:                0, 0/*min,max numbered_params_count*/, 
1.76      paf       268:                params_names, locals_names, 
1.223     misha     269:                0/*to be filled later in next {} */, 0, all_vars_local);
1.221     misha     270: 
1.208     paf       271:        *reinterpret_cast<Method**>(&$$)=method;
1.222     misha     272: } maybe_codes {
1.251     misha     273:                Method* method=reinterpret_cast<Method*>($7);
1.231     misha     274:                // fill in the code
1.251     misha     275:                method->parser_code=$8;
1.231     misha     276: 
                    277:                // register in class
                    278:                const String& name=*LA2S(*$2);
1.251     misha     279:                PC.cclass->set_method(PC.alias_method(name), method);
1.8       paf       280: };
1.10      paf       281: 
                    282: maybe_bracketed_strings: empty | bracketed_maybe_strings;
1.259     misha     283: bracketed_maybe_strings: '[' maybe_strings ']' {$$=$2;};
1.10      paf       284: maybe_strings: empty | strings;
1.259     misha     285: strings: STRING | strings ';' STRING { $$=$1; P(*$$, *$3); };
1.10      paf       286: 
                    287: maybe_comment: empty | STRING;
1.1       paf       288: 
                    289: /* codes */
                    290: 
1.10      paf       291: maybe_codes: empty | codes;
                    292: 
1.259     misha     293: codes: code | codes code { $$=$1; P(*$$, *$2); };
1.81      paf       294: code: write_string | action;
1.209     paf       295: action: get | put | call;
1.1       paf       296: 
                    297: /* get */
                    298: 
1.64      paf       299: get: get_value {
1.238     misha     300:        $$=N();
                    301:        YYSTYPE code=$1;
1.247     misha     302:        size_t count=code->count();
1.245     misha     303: 
1.235     misha     304: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1.281     moko      305:        if(count!=3 || !change_first(*code, OP::OP_VALUE__GET_ELEMENT, /*=>*/OP::OP_VALUE__GET_ELEMENT__WRITE) )
1.238     misha     306: #endif
1.247     misha     307: 
1.245     misha     308: #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT
1.281     moko      309:        if(count!=3 || !change_first(*code, OP::OP_WITH_SELF__VALUE__GET_ELEMENT, /*=>*/OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE) )
1.245     misha     310: #endif
                    311: 
1.239     misha     312: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.281     moko      313:        if(count!=5 || !change_first(*code, OP::OP_GET_OBJECT_ELEMENT, /*=>*/OP::OP_GET_OBJECT_ELEMENT__WRITE) )
1.239     misha     314: #endif
                    315: 
                    316: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.281     moko      317:        if(count!=5 || !change_first(*code, OP::OP_GET_OBJECT_VAR_ELEMENT, /*=>*/OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE) )
1.238     misha     318: #endif
1.267     misha     319: 
                    320: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
1.281     moko      321:        if(!change(*code, count-1/* last */, OP::OP_GET_ELEMENT__SPECIAL, /*=>*/OP::OP_GET_ELEMENT__SPECIAL__WRITE) )
1.267     misha     322: #endif
                    323: 
1.247     misha     324:        {
1.281     moko      325:                change_or_append(*code, count-1 /* last */, OP::OP_GET_ELEMENT, /*=>*/OP::OP_GET_ELEMENT__WRITE, /*or */OP::OP_WRITE_VALUE ); /* value=pop; wcontext.write(value) */
1.246     misha     326:        }
1.247     misha     327: 
1.238     misha     328:        P(*$$, *code);
1.1       paf       329: };
1.259     misha     330: get_value: '$' get_name_value { $$=$2; };
1.64      paf       331: get_name_value: name_without_curly_rdive EON | name_in_curly_rdive;
1.259     misha     332: name_in_curly_rdive: '{' name_without_curly_rdive '}' { $$=$2; };
1.44      paf       333: name_without_curly_rdive: 
                    334:        name_without_curly_rdive_read 
                    335: |      name_without_curly_rdive_class;
1.19      paf       336: name_without_curly_rdive_read: name_without_curly_rdive_code {
1.206     paf       337:        $$=N(); 
1.238     misha     338:        YYSTYPE diving_code=$1;
1.239     misha     339:        size_t count=diving_code->count();
1.247     misha     340: 
                    341:        if(maybe_make_self(*$$, *diving_code, count)) {
                    342:                // $self.
                    343:        } else
1.239     misha     344: 
                    345: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
1.247     misha     346:        if(maybe_make_get_object_element(*$$, *diving_code, count)){
1.245     misha     347:                // optimization for $object.field + ^object.method[
1.247     misha     348:        } else
1.238     misha     349: #endif
1.239     misha     350: 
                    351: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
1.247     misha     352:        if(maybe_make_get_object_var_element(*$$, *diving_code, count)){
1.245     misha     353:                // optimization for $object.$var
1.247     misha     354:        } else
1.238     misha     355: #endif
1.239     misha     356: 
                    357: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1.281     moko      358:        if(count>=4 && (*diving_code)[0].code==OP::OP_VALUE && (*diving_code)[3].code==OP::OP_GET_ELEMENT ){
1.248     misha     359:                 // optimization
1.239     misha     360:                O(*$$,
1.247     misha     361:                        (PC.in_call_value && count==4)
1.239     misha     362:                        ? OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR // ^object[ : OP_VALUE+origin+string+OP_GET_ELEMENT => OP_VALUE__GET_ELEMENT_OR_OPERATOR+origin+string
                    363:                        : OP::OP_VALUE__GET_ELEMENT             // $object  : OP_VALUE+origin+string+OP_GET_ELEMENT => OP_VALUE__GET_ELEMENT+origin+string
                    364:                );
                    365:                P(*$$, *diving_code, 1/*offset*/, 2/*limit*/); // copy origin+value
1.247     misha     366:                if(count>4)
                    367:                        P(*$$, *diving_code, 4); // copy tail
1.239     misha     368:        } else {
                    369:                O(*$$, OP::OP_WITH_READ); /* stack: starting context */
                    370:                P(*$$, *diving_code);
                    371:        }
1.233     misha     372: #else
1.247     misha     373:        {
1.238     misha     374:                O(*$$, OP::OP_WITH_READ); /* stack: starting context */
1.179     paf       375: 
1.238     misha     376:                // ^if OP_ELEMENT => ^if OP_ELEMENT_OR_OPERATOR
                    377:                // optimized OP_VALUE+origin+string+OP_GET_ELEMENT. => OP_VALUE+origin+string+OP_GET_ELEMENT_OR_OPERATOR.
1.239     misha     378:                if(PC.in_call_value && count==4)
                    379:                        diving_code->put(count-1, OP::OP_GET_ELEMENT_OR_OPERATOR);
1.206     paf       380:                P(*$$, *diving_code);
1.239     misha     381:        }
1.233     misha     382: #endif
1.22      paf       383:        /* diving code; stack: current context */
1.1       paf       384: };
1.259     misha     385: name_without_curly_rdive_class: class_prefix name_without_curly_rdive_code { $$=$1; P(*$$, *$2); };
                    386: name_without_curly_rdive_code: name_advance2 | name_path name_advance2 { $$=$1; P(*$$, *$2); };
1.1       paf       387: 
                    388: /* put */
                    389: 
1.81      paf       390: put: '$' name_expr_wdive construct {
1.241     misha     391:        $$=N();
1.244     misha     392: #ifdef OPTIMIZE_BYTECODE_CONSTRUCT
                    393:        if(maybe_optimize_construct(*$$, *$2, *$3)){
1.246     misha     394:                // $a(expr), $.a(expr), $a[value], $.a[value], $self.a[value], $self.a(expr)
1.241     misha     395:        } else 
                    396: #endif
                    397:        {
                    398:                P(*$$, *$2); /* stack: context,name */
                    399:                P(*$$, *$3); /* stack: context,name,constructor_value */
                    400:        }
1.20      paf       401: };
1.44      paf       402: name_expr_wdive: 
1.157     parser    403:        name_expr_wdive_root
                    404: |      name_expr_wdive_write
1.44      paf       405: |      name_expr_wdive_class;
1.157     parser    406: name_expr_wdive_root: name_expr_dive_code {
1.206     paf       407:        $$=N();
1.238     misha     408:        YYSTYPE diving_code=$1;
1.247     misha     409:        size_t count=diving_code->count();
                    410: 
                    411:        if(maybe_make_self(*$$, *diving_code, count)) {
                    412:                // $self.
                    413:        } else
                    414: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1.281     moko      415:        if(count>=4 && (*diving_code)[0].code==OP::OP_VALUE && (*diving_code)[3].code==OP::OP_GET_ELEMENT ){
1.247     misha     416:                O(*$$, OP::OP_WITH_ROOT__VALUE__GET_ELEMENT);
                    417:                P(*$$, *diving_code, 1/*offset*/, 2/*limit*/); // copy origin+value
                    418:                if(count>4)
                    419:                        P(*$$, *diving_code, 4); // tail
                    420:        } else
                    421: #endif
                    422:        {
1.229     misha     423:                O(*$$, OP::OP_WITH_ROOT); /* stack: starting context */
1.206     paf       424:                P(*$$, *diving_code);
1.23      paf       425:        }
                    426:        /* diving code; stack: current context */
1.156     parser    427: };
1.157     parser    428: name_expr_wdive_write: '.' name_expr_dive_code {
1.206     paf       429:        $$=N(); 
1.229     misha     430:        O(*$$, OP::OP_WITH_WRITE); /* stack: starting context */
1.206     paf       431:        P(*$$, *$2); /* diving code; stack: context,name */
1.20      paf       432: };
1.259     misha     433: name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P(*$$, *$2); };
1.20      paf       434: 
1.244     misha     435: construct:
1.163     parser    436:        construct_square
                    437: |      construct_round
                    438: |      construct_curly
1.149     parser    439: ;
1.211     paf       440: construct_square: '[' {
                    441:        // allow $result_or_other_variable[ letters here any time ]
                    442:        *reinterpret_cast<bool*>(&$$)=PC.explicit_result; PC.explicit_result=false;
1.297     moko      443:        PC.array=false; // no need to save current value as if() is right after PC.array=true;
                    444: } any_constructor_code_values {
1.213     paf       445:        PC.explicit_result=*reinterpret_cast<bool*>(&$2);
1.211     paf       446: } ']' {
1.149     parser    447:        // stack: context, name
1.297     moko      448:        if(!PC.array){
                    449:                $$=$3; // stack: context, name, value
                    450:                O(*$$, OP::OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */
                    451:        } else {
                    452:                $$ = N();
                    453:                OA(*$$, OP::OP_CONSTRUCT_ARRAY, $3);
                    454:                PC.array=false;
                    455:        }
1.81      paf       456: }
                    457: ;
1.297     moko      458: any_constructor_code_values:
                    459:        any_constructor_code_value { $$ = $1; }
                    460:        | any_constructor_code_values ';' any_constructor_code_value { $$ = $1; P(*$$, *$3); PC.array=true; };
                    461: ;
1.149     parser    462: construct_round: '(' expr_value ')' { 
1.206     paf       463:        $$=N(); 
1.149     parser    464:        // stack: context, name
1.206     paf       465:        P(*$$, *$2); // stack: context, name, value
1.229     misha     466:        O(*$$, OP::OP_CONSTRUCT_EXPR); /* value=pop->as_expr_result; name=pop; context=pop; construct(context,name,value) */
1.81      paf       467: }
1.52      paf       468: ;
1.149     parser    469: construct_curly: '{' maybe_codes '}' {
                    470:        // stack: context, name
1.206     paf       471:        $$=N(); 
1.229     misha     472:        OA(*$$, OP::OP_CURLY_CODE__CONSTRUCT, $2); /* code=pop; name=pop; context=pop; construct(context,name,junction(code)) */
1.149     parser    473: };
                    474: 
1.55      paf       475: any_constructor_code_value: 
1.261     moko      476:        empty_value /* optimized $var[] case */
1.52      paf       477: |      STRING /* optimized $var[STRING] case */
1.55      paf       478: |      constructor_code_value /* $var[something complex] */
1.1       paf       479: ;
1.55      paf       480: constructor_code_value: constructor_code {
1.206     paf       481:        $$=N(); 
1.229     misha     482:        OA(*$$, OP::OP_OBJECT_POOL, $1); /* stack: empty write context */
1.183     paf       483:        /* some code that writes to that context */
                    484:        /* context=pop; stack: context.value() */
1.1       paf       485: };
1.55      paf       486: constructor_code: codes__excluding_sole_str_literal;
1.259     misha     487: codes__excluding_sole_str_literal: action | code codes { $$=$1; P(*$$, *$2); };
1.27      paf       488: 
1.1       paf       489: /* call */
                    490: 
1.66      paf       491: call: call_value {
1.267     misha     492:        size_t count=$1->count();
1.238     misha     493: #ifdef OPTIMIZE_BYTECODE_CUT_REM_OPERATOR
1.267     misha     494:        if(count)
1.238     misha     495: #endif
                    496:        {
                    497:                $$=$1; /* stack: value */
1.281     moko      498:                if(!change_first(*$$, OP::OP_CONSTRUCT_OBJECT, /*=>*/ OP::OP_CONSTRUCT_OBJECT__WRITE))
                    499:                        change_or_append(*$$, count-2 /* second last */, OP::OP_CALL, /*=>*/ OP::OP_CALL__WRITE, /*or */ OP::OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */
1.238     misha     500:        }
1.66      paf       501: };
1.177     paf       502: call_value: '^' { 
1.281     moko      503:        PC.in_call_value=true; 
                    504: }
                    505: call_name {
                    506:        PC.in_call_value=false;
                    507: } 
                    508: store_params EON { /* ^field.$method{vasya} */
1.238     misha     509: #ifdef OPTIMIZE_BYTECODE_CUT_REM_OPERATOR
1.242     misha     510: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT
1.238     misha     511:        const String* operator_name=LA2S(*$3, 0, OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR);
1.242     misha     512: #else
                    513:        const String* operator_name=LA2S(*$3, 1);
                    514: #endif
1.279     moko      515:        if(operator_name && SYMBOLS_EQ(*operator_name,REM_SYMBOL)){
1.238     misha     516:                $$=N();
                    517:        } else 
                    518: #endif
                    519:                {
                    520:                        YYSTYPE params_code=$5;
                    521:                        if(params_code->count()==3) { // probably [] case. [OP::OP_VALUE+origin+Void]
                    522:                                if(Value* value=LA2V(*params_code)) // it is OP_VALUE+origin+value?
1.261     moko      523:                                        if(const String * string=value->get_string())
                    524:                                                if(string->is_empty()) // value is empty string?
                    525:                                                        params_code=0; // ^zzz[] case. don't append lone empty param.
1.238     misha     526:                        }
                    527:                        /* stack: context, method_junction */
1.249     misha     528: 
                    529:                        YYSTYPE var_code=$3;
                    530:                        if(
                    531:                                var_code->count()==8
1.282     moko      532:                                && ( (*var_code)[0].code==OP::OP_VALUE__GET_CLASS || (*var_code)[0].code==OP::OP_VALUE__GET_BASE_CLASS )
1.249     misha     533:                                && (*var_code)[3].code==OP::OP_PREPARE_TO_CONSTRUCT_OBJECT
                    534:                                && (*var_code)[4].code==OP::OP_VALUE
1.272     moko      535: #ifdef FEATURE_GET_ELEMENT4CALL
                    536:                                && (*var_code)[7].code==OP::OP_GET_ELEMENT4CALL
                    537: #else
1.249     misha     538:                                && (*var_code)[7].code==OP::OP_GET_ELEMENT
1.272     moko      539: #endif
1.249     misha     540:                        ){
1.267     misha     541:                                $$=N();
1.249     misha     542:                                O(*$$, OP::OP_CONSTRUCT_OBJECT);
                    543:                                P(*$$, *var_code, 1/*offset*/, 2/*limit*/); // class name
                    544:                                P(*$$, *var_code, 5/*offset*/, 2/*limit*/); // constructor name
                    545:                                OA(*$$, params_code);
                    546:                        } else 
                    547:                                {
                    548:                                        $$=var_code; /* with_xxx,diving code; stack: context,method_junction */
                    549:                                        OA(*$$, OP::OP_CALL, params_code); // method_frame=make frame(pop junction); ncontext=pop; call(ncontext,method_frame) stack: value
                    550:                                }
1.238     misha     551:                }
1.1       paf       552: };
                    553: 
1.272     moko      554: call_name: name_without_curly_rdive {
                    555: #ifdef FEATURE_GET_ELEMENT4CALL
                    556:        size_t count=$1->count();
                    557:        if(count){
                    558:                $$=$1;
                    559: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT
                    560:                !(count==5 && change_first(*$$, OP::OP_GET_OBJECT_ELEMENT, OP::OP_GET_OBJECT_ELEMENT4CALL)) &&
                    561: #endif
                    562: #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT
                    563:                !(count==5 && change_first(*$$, OP::OP_GET_OBJECT_VAR_ELEMENT, OP::OP_GET_OBJECT_VAR_ELEMENT4CALL)) &&
                    564: #endif
                    565:                !change(*$$, count-1, OP::OP_GET_ELEMENT, OP::OP_GET_ELEMENT4CALL);
                    566:        }
                    567: #endif
                    568: };
1.38      paf       569: 
1.259     misha     570: store_params: store_param | store_params store_param { $$=$1; P(*$$, *$2); };
1.69      paf       571: store_param: 
                    572:        store_square_param
                    573: |      store_round_param
                    574: |      store_curly_param
1.31      paf       575: ;
1.210     paf       576: store_square_param: '[' {
1.211     paf       577:        // allow ^call[ letters here any time ]
1.210     paf       578:        *reinterpret_cast<bool*>(&$$)=PC.explicit_result; PC.explicit_result=false;
                    579: } store_code_param_parts {
1.213     paf       580:        PC.explicit_result=*reinterpret_cast<bool*>(&$2);
1.259     misha     581: } ']' {$$=$3;};
                    582: store_round_param: '(' store_expr_param_parts ')' {$$=$2;};
                    583: store_curly_param: '{' store_curly_param_parts '}' {$$=$2;};
1.69      paf       584: store_code_param_parts:
                    585:        store_code_param_part
1.259     misha     586: |      store_code_param_parts ';' store_code_param_part { $$=$1; P(*$$, *$3); }
1.69      paf       587: ;
                    588: store_expr_param_parts:
                    589:        store_expr_param_part
1.259     misha     590: |      store_expr_param_parts ';' store_expr_param_part { $$=$1; P(*$$, *$3); }
1.69      paf       591: ;
1.94      paf       592: store_curly_param_parts:
                    593:        store_curly_param_part
1.259     misha     594: |      store_curly_param_parts ';' store_curly_param_part { $$=$1; P(*$$, *$3); }
1.94      paf       595: ;
1.120     paf       596: store_code_param_part: code_param_value {
1.32      paf       597:        $$=$1;
1.120     paf       598: };
1.214     paf       599: store_expr_param_part: expr_value {
                    600:        YYSTYPE expr_code=$1;
1.215     paf       601:        if(expr_code->count()==3
1.234     misha     602:                && (*expr_code)[0].code==OP::OP_VALUE) { // optimizing (double/bool/incidently 'string' too) case. [OP::OP_VALUE+origin+Double]. no evaluating
1.214     paf       603:                $$=expr_code; 
                    604:        } else {
1.238     misha     605:                YYSTYPE code=N();
1.214     paf       606:                P(*code, *expr_code);
1.229     misha     607:                O(*code, OP::OP_WRITE_EXPR_RESULT);
1.214     paf       608:                $$=N(); 
1.229     misha     609:                OA(*$$, OP::OP_EXPR_CODE__STORE_PARAM, code);
1.214     paf       610:        }
1.69      paf       611: };
1.94      paf       612: store_curly_param_part: maybe_codes {
1.206     paf       613:        $$=N(); 
1.229     misha     614:        OA(*$$, OP::OP_CURLY_CODE__STORE_PARAM, $1);
1.94      paf       615: };
1.120     paf       616: code_param_value:
1.261     moko      617:        empty_value /* optimized [;...] case */
1.120     paf       618: |      STRING /* optimized [STRING] case */
                    619: |      constructor_code_value /* [something complex] */
                    620: ;
1.1       paf       621: 
                    622: /* name */
                    623: 
1.259     misha     624: name_expr_dive_code: name_expr_value | name_path name_expr_value { $$=$1; P(*$$, *$2); };
1.1       paf       625: 
1.259     misha     626: name_path: name_step | name_path name_step { $$=$1; P(*$$, *$2); };
1.1       paf       627: name_step: name_advance1 '.';
                    628: name_advance1: name_expr_value {
1.177     paf       629:        // we know that name_advance1 not called from ^xxx context
                    630:        // so we'll not check for operator call possibility as we do in name_advance2
                    631: 
1.1       paf       632:        /* stack: context */
                    633:        $$=$1; /* stack: context,name */
1.267     misha     634: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    635:        O(*$$, is_special_element(*$$) ? OP::OP_GET_ELEMENT__SPECIAL : OP::OP_GET_ELEMENT);
                    636: #else
1.229     misha     637:        O(*$$, OP::OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.267     misha     638: #endif
1.1       paf       639: };
                    640: name_advance2: name_expr_value {
                    641:        /* stack: context */
                    642:        $$=$1; /* stack: context,name */
1.267     misha     643: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    644:        O(*$$, is_special_element(*$$) ? OP::OP_GET_ELEMENT__SPECIAL : OP::OP_GET_ELEMENT);
                    645: #else
1.229     misha     646:        O(*$$, OP::OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */
1.267     misha     647: #endif
1.1       paf       648: }
1.4       paf       649: |      STRING BOGUS
1.1       paf       650: ;
                    651: name_expr_value: 
1.4       paf       652:        STRING /* subname_is_const */
1.1       paf       653: |      name_expr_subvar_value /* $subname_is_var_value */
1.160     parser    654: |      name_expr_with_subvar_value /* xxx$part_of_subname_is_var_value */
1.166     parser    655: |      name_square_code_value /* [codes] */
1.301     moko      656: |      name_round_expr_value /* (expr) */
1.1       paf       657: ;
                    658: name_expr_subvar_value: '$' subvar_ref_name_rdive {
                    659:        $$=$2;
1.229     misha     660:        O(*$$, OP::OP_GET_ELEMENT);
1.1       paf       661: };
1.4       paf       662: name_expr_with_subvar_value: STRING subvar_get_writes {
1.238     misha     663:        YYSTYPE code;
1.183     paf       664:        {
1.206     paf       665:                change_string_literal_to_write_string_literal(*(code=$1));
                    666:                P(*code, *$2);
1.183     paf       667:        }
1.206     paf       668:        $$=N(); 
1.229     misha     669:        OA(*$$, OP::OP_STRING_POOL, code);
1.1       paf       670: };
1.212     paf       671: name_square_code_value: '[' {
                    672:        // allow $result_or_other_variable[ letters here any time ]
                    673:        *reinterpret_cast<bool*>(&$$)=PC.explicit_result; PC.explicit_result=false;
                    674: } codes {
1.213     paf       675:        PC.explicit_result=*reinterpret_cast<bool*>(&$2);
1.212     paf       676: } ']' {
1.302     moko      677:        $$=N();
                    678:        if(!maybe_append_simple_diving_code(*$$, *$3)) {
1.281     moko      679:                OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */
                    680:                /* some code that writes to that context */
                    681:                /* context=pop; stack: context.value() */
1.267     misha     682:        }
1.160     parser    683: };
1.301     moko      684: name_round_expr_value: '(' expr_value ')' {
                    685:        $$ = N();
                    686:        P(*$$, *$2);
                    687: };
1.154     parser    688: subvar_ref_name_rdive: STRING {
1.206     paf       689:        $$=N(); 
1.229     misha     690:        O(*$$, OP::OP_WITH_READ);
1.206     paf       691:        P(*$$, *$1);
1.1       paf       692: };
1.259     misha     693: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P(*$$, *$2); };
1.1       paf       694: subvar__get_write: '$' subvar_ref_name_rdive {
                    695:        $$=$2;
1.229     misha     696:        O(*$$, OP::OP_GET_ELEMENT__WRITE);
1.42      paf       697: };
                    698: 
1.154     parser    699: class_prefix:
                    700:        class_static_prefix
                    701: |      class_constructor_prefix
                    702: ;
1.155     parser    703: class_static_prefix: STRING ':' {
                    704:        $$=$1; // stack: class name string
1.282     moko      705:        OP::OPCODE code = OP::OP_VALUE__GET_CLASS;
1.206     paf       706:        if(*LA2S(*$$) == BASE_NAME) { // pseudo BASE class
                    707:                if(VStateless_class* base=PC.cclass->base_class()) {
1.275     moko      708:                        change_string_literal_value(*$$, *new String(base->type()));
1.189     paf       709:                } else {
1.292     moko      710:                        YYERROR1("no base class declared");
1.189     paf       711:                }
1.282     moko      712:                code = OP::OP_VALUE__GET_BASE_CLASS;
                    713:        } else {
1.283     moko      714:                // can't use get_class because it will call @autouse[] if the class wasn't loaded
                    715:                VStateless_class* base=PC.request.classes().get(*LA2S(*$$));
                    716:                if(base && PC.cclass->derived_from(*base))
1.282     moko      717:                        code = OP::OP_VALUE__GET_BASE_CLASS;
1.189     paf       718:        }
1.238     misha     719:        // optimized OP_VALUE+origin+string+OP_GET_CLASS => OP_VALUE__GET_CLASS+origin+string
1.282     moko      720:        change_first(*$$, OP::OP_VALUE, code);
1.155     parser    721: };
                    722: class_constructor_prefix: class_static_prefix ':' {
1.154     parser    723:        $$=$1;
1.292     moko      724:        if(!PC.in_call_value)
                    725:                YYERROR1(":: not allowed here");
1.229     misha     726:        O(*$$, OP::OP_PREPARE_TO_CONSTRUCT_OBJECT);
1.1       paf       727: };
                    728: 
1.53      paf       729: 
1.56      paf       730: /* expr */
1.53      paf       731: 
1.214     paf       732: expr_value: expr;
1.56      paf       733: expr: 
1.214     paf       734:        double_or_STRING
1.216     paf       735: |   true_value
                    736: |   false_value
1.64      paf       737: |      get_value
1.66      paf       738: |      call_value
1.259     misha     739: |      '"' string_inside_quotes_value '"' { $$ = $2; }
                    740: |      '\'' string_inside_quotes_value '\'' { $$ = $2; }
1.60      paf       741: |      '(' expr ')' { $$ = $2; }
                    742: /* stack: operand // stack: @operand */
1.259     misha     743: |      '-' expr %prec NUNARY { $$=$2;  O(*$$, OP::OP_NEG); }
                    744: |      '+' expr %prec NUNARY { $$=$2; }
                    745: |      '~' expr { $$=$2;        O(*$$, OP::OP_INV); }
                    746: |      '!' expr { $$=$2;  O(*$$, OP::OP_NOT); }
                    747: |      "def" expr { $$=$2;  O(*$$, OP::OP_DEF); }
                    748: |      "in" expr { $$=$2;  O(*$$, OP::OP_IN); }
                    749: |      "-f" expr { $$=$2;  O(*$$, OP::OP_FEXISTS); }
                    750: |      "-d" expr { $$=$2;  O(*$$, OP::OP_DEXISTS); }
1.60      paf       751: /* stack: a,b // stack: a@b */
1.259     misha     752: |      expr '-' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_SUB); }
                    753: |      expr '+' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_ADD); }
                    754: |      expr '*' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_MUL); }
                    755: |      expr '/' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_DIV); }
                    756: |      expr '%' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_MOD); }
                    757: |      expr '\\' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_INTDIV); }
                    758: |      expr "<<" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_SL); }
                    759: |      expr ">>" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_SR); }
                    760: |      expr '&' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_AND); }
                    761: |      expr '|' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_OR); }
                    762: |      expr "!|" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_XOR); }
                    763: |      expr "&&" expr { $$=$1;  OA(*$$, OP::OP_NESTED_CODE, $3);  O(*$$, OP::OP_LOG_AND); }
                    764: |      expr "||" expr { $$=$1;  OA(*$$, OP::OP_NESTED_CODE, $3);  O(*$$, OP::OP_LOG_OR); }
                    765: |      expr "!||" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_LOG_XOR); }
                    766: |      expr '<' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_LT); }
                    767: |      expr '>' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_GT); }
                    768: |      expr "<=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_LE); }
                    769: |      expr ">=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_GE); }
                    770: |      expr "==" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_EQ); }
                    771: |      expr "!=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_NE); }
                    772: |      expr "lt" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_LT); }
                    773: |      expr "gt" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_GT); }
                    774: |      expr "le" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_LE); }
                    775: |      expr "ge" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_GE); }
                    776: |      expr "eq" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_EQ); }
                    777: |      expr "ne" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_NE); }
                    778: |      expr "is" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_IS); }
1.56      paf       779: ;
1.214     paf       780: 
                    781: double_or_STRING: STRING {
1.238     misha     782:        // optimized OP_STRING => OP_VALUE for doubles
1.214     paf       783:        maybe_change_string_literal_to_double_literal(*($$=$1));
                    784: };
1.55      paf       785: 
1.65      paf       786: string_inside_quotes_value: maybe_codes {
1.238     misha     787: #ifdef OPTIMIZE_BYTECODE_STRING_POOL
                    788:        // it brakes ^if(" 09 "){...}
                    789:        YYSTYPE code=$1;
                    790:        $$=N();
1.267     misha     791:        if(code->count()==3 && change_first(*code, OP::OP_STRING__WRITE, OP::OP_VALUE)){
1.238     misha     792:                // optimized OP_STRING__WRITE+origin+value => OP_VALUE+origin+value without starting OP_STRING_POOL
                    793:                P(*$$, *code);
                    794:        } else {
                    795:                OA(*$$, OP::OP_STRING_POOL, code); /* stack: empty write context */
                    796:        }
                    797: #else
1.206     paf       798:        $$=N();
1.229     misha     799:        OA(*$$, OP::OP_STRING_POOL, $1); /* stack: empty write context */
1.238     misha     800: #endif
1.183     paf       801:        /* some code that writes to that context */
                    802:        /* context=pop; stack: context.get_string() */
1.53      paf       803: };
1.1       paf       804: 
1.27      paf       805: /* basics */
1.1       paf       806: 
1.81      paf       807: write_string: STRING {
1.238     misha     808:        // optimized OP_STRING+OP_WRITE_VALUE => OP_STRING__WRITE
1.259     misha     809:        change_string_literal_to_write_string_literal(*($$=$1));
1.54      paf       810: };
                    811: 
1.261     moko      812: empty_value: /* empty */ { $$=VL(/*we know that we will not change it*/const_cast<VString*>(&vempty), 0, 0, 0); }
1.259     misha     813: true_value: "true" { $$ = VL(/*we know that we will not change it*/const_cast<VBool*>(&vtrue), 0, 0, 0); }
                    814: false_value: "false" { $$ = VL(/*we know that we will not change it*/const_cast<VBool*>(&vfalse), 0, 0, 0); }
1.216     paf       815: 
1.259     misha     816: empty: /* empty */ { $$=N(); };
1.1       paf       817: 
                    818: %%
1.105     paf       819: #endif
1.1       paf       820: 
                    821: /*
                    822:        000$111(2222)00 
                    823:                000$111{3333}00
1.9       paf       824:        $,^: push,=0
1.1       paf       825:        1:( { break=pop
                    826:        2:( )  pop
                    827:        3:{ }  pop
                    828: 
                    829:        000^111(2222)4444{33333}4000
1.9       paf       830:        $,^: push,=0
1.1       paf       831:        1:( { break=pop
                    832:        2:( )=4
                    833:        3:{ }=4
                    834:                4:[^({]=pop
                    835: */
                    836: 
1.206     paf       837: inline void ungetc(Parse_control& pc, uint last_line_end_col) {
                    838:        pc.source--;
                    839:        if(pc.pos.col==0) {
                    840:                --pc.pos.line; pc.pos.col=last_line_end_col;
                    841:        } else
                    842:                --pc.pos.col;
                    843: 
                    844: }
                    845: static int yylex(YYSTYPE *lvalp, void *apc) {
1.295     moko      846:        Parse_control& pc=*static_cast<Parse_control*>(apc);
1.206     paf       847: 
                    848:        #define lexical_brackets_nestage pc.brackets_nestages[pc.ls_sp]
1.48      paf       849:        #define RC {result=c; goto break2; }
1.1       paf       850: 
1.295     moko      851:        int c;
1.206     paf       852:        int result;
1.1       paf       853:        
1.206     paf       854:        if(pc.pending_state) {
                    855:                result=pc.pending_state;
                    856:                pc.pending_state=0;
1.1       paf       857:                return result;
                    858:        }
                    859:        
1.206     paf       860:        const char *begin=pc.source;
                    861:        Pos begin_pos=pc.pos;
1.91      paf       862:        const char *end;
1.67      paf       863:        int skip_analized=0;
1.50      paf       864:        while(true) {
1.206     paf       865:                c=*(end=(pc.source++));
                    866: //             fprintf(stderr, "\nchar: %c %02X; nestage: %d, sp=%d", c, c, lexical_brackets_nestage, pc.sp);
1.1       paf       867: 
1.208     paf       868:                if(c=='\n')
1.206     paf       869:                        pc.pos_next_line();
1.208     paf       870:                else
1.206     paf       871:                        pc.pos_next_c(c);
1.208     paf       872: //             fprintf(stderr, "\nchar: %c file(%d:%d)", c, pc.pos.line, pc.pos.col);
1.73      paf       873: 
1.208     paf       874:                if(pc.pos.col==0+1 && c=='@') {
1.206     paf       875:                        if(pc.ls==LS_DEF_SPECIAL_BODY) {
1.175     paf       876:                                // @SPECIAL
                    877:                                // ...
                    878:                                // @<here = 
1.206     paf       879:                                pop_LS(pc); // exiting from LS_DEF_SPECIAL_BODY state
1.175     paf       880:                        } // continuing checks
1.206     paf       881:                        if(pc.ls==LS_USER) {
                    882:                                push_LS(pc, LS_DEF_NAME);
1.171     parser    883:                                RC;
1.175     paf       884:                        } else // @ in first column inside some code [when could that be?]
1.171     parser    885:                                result=BAD_METHOD_DECL_START;
                    886:                        goto break2;
1.209     paf       887:                }
                    888:                if(c=='^') {
1.206     paf       889:                        if(pc.ls==LS_METHOD_AFTER) {
1.203     paf       890:                                // handle after-method situation
1.206     paf       891:                                pop_LS(pc);
1.203     paf       892:                                result=EON;
                    893:                                skip_analized=-1; // return to punctuation afterwards to assure it's literality
                    894:                                goto break2;
                    895:                        }
1.206     paf       896:                        switch(pc.ls) {
1.169     parser    897: case LS_EXPRESSION_VAR_NAME_WITH_COLON:
                    898: case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                    899: case LS_VAR_NAME_SIMPLE_WITH_COLON:
                    900: case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                    901: case LS_VAR_NAME_CURLY:
                    902: case LS_METHOD_NAME:
1.194     paf       903: case LS_USER_COMMENT:
1.169     parser    904: case LS_DEF_COMMENT:
                    905:        // no literals in names, please
                    906:        break;
                    907: default:
1.206     paf       908:                        switch(*pc.source) {
1.165     parser    909:                        // ^escaping some punctuators
1.228     misha     910:                        case '^': case '$': case ';': case '@':
1.126     paf       911:                        case '(': case ')':
1.48      paf       912:                        case '[': case ']':
                    913:                        case '{': case '}':
1.172     parser    914:                        case '"':  case ':':
1.40      paf       915:                                if(end!=begin) {
1.206     paf       916:                                        if(!pc.string_start)
                    917:                                                pc.string_start=begin_pos;
1.40      paf       918:                                        // append piece till ^
1.206     paf       919:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.40      paf       920:                                }
1.63      paf       921:                                // reset piece 'begin' position & line
1.206     paf       922:                                begin=pc.source; // ->punctuation
                    923:                                begin_pos=pc.pos;
1.203     paf       924:                                // skip over _ after ^
1.206     paf       925:                                pc.source++;  pc.pos.col++;
1.203     paf       926:                                // skip analysis = forced literal
                    927:                                continue;
1.114     paf       928: 
                    929:                        // converting ^#HH into char(hex(HH))
                    930:                        case '#':
                    931:                                if(end!=begin) {
1.206     paf       932:                                        if(!pc.string_start)
                    933:                                                pc.string_start=begin_pos;
1.114     paf       934:                                        // append piece till ^
1.206     paf       935:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.114     paf       936:                                }
                    937:                                // #HH ?
1.228     misha     938:                                if(pc.source[1] && isxdigit(pc.source[1]) && pc.source[2] && isxdigit(pc.source[2])) {
1.218     paf       939:                                        char c=(char)(
1.206     paf       940:                                                hex_value[(unsigned char)pc.source[1]]*0x10+
1.218     paf       941:                                                hex_value[(unsigned char)pc.source[2]]);
1.206     paf       942:                                        if(c==0) {
1.114     paf       943:                                                result=BAD_HEX_LITERAL;
                    944:                                                goto break2; // wrong hex value[no ^#00 chars allowed]: bail out
                    945:                                        }
                    946:                                        // append char(hex(HH))
1.206     paf       947:                                        pc.string.append(c);
1.114     paf       948:                                        // skip over ^#HH
1.206     paf       949:                                        pc.source+=3;
                    950:                                        pc.pos.col+=3;
1.114     paf       951:                                        // reset piece 'begin' position & line
1.206     paf       952:                                        begin=pc.source; // ->after ^#HH
                    953:                                        begin_pos=pc.pos;
1.203     paf       954:                                        // skip analysis = forced literal
1.114     paf       955:                                        continue;
                    956:                                }
1.228     misha     957:                                // just escaped char
                    958:                                // reset piece 'begin' position & line
                    959:                                begin=pc.source;
                    960:                                begin_pos=pc.pos;
                    961:                                // skip over _ after ^
                    962:                                pc.source++;  pc.pos.col++;
                    963:                                // skip analysis = forced literal
                    964:                                continue;
1.1       paf       965:                        }
1.169     parser    966:                        break;
1.203     paf       967:                        }
1.169     parser    968:                }
1.113     paf       969:                // #comment  start skipping
1.206     paf       970:                if(c=='#' && pc.pos.col==1) {
1.112     paf       971:                        if(end!=begin) {
1.206     paf       972:                                if(!pc.string_start)
                    973:                                        pc.string_start=begin_pos;
1.112     paf       974:                                // append piece till #
1.206     paf       975:                                pc.string.append_strdup_know_length(begin, end-begin);
1.112     paf       976:                        }
                    977:                        // fall into COMMENT lexical state [wait for \n]
1.206     paf       978:                        push_LS(pc, LS_USER_COMMENT);
1.175     paf       979:                        continue;
1.112     paf       980:                }
1.206     paf       981:                switch(pc.ls) {
1.10      paf       982: 
                    983:                // USER'S = NOT OURS
1.1       paf       984:                case LS_USER:
1.206     paf       985:                case LS_NAME_SQUARE_PART: // name.[here].xxx
                    986:                        if(pc.trim_bof)
1.153     parser    987:                                switch(c) {
                    988:                                case '\n': case ' ': case '\t':
1.206     paf       989:                                        begin=pc.source;
                    990:                                        begin_pos=pc.pos;
1.152     parser    991:                                        continue; // skip it
1.153     parser    992:                                default:
1.206     paf       993:                                        pc.trim_bof=false;
1.152     parser    994:                                }
1.48      paf       995:                        switch(c) {
                    996:                        case '$':
1.206     paf       997:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf       998:                                RC;
                    999:                        case '^':
1.206     paf      1000:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1001:                                RC;
1.166     parser   1002:                        case ']':
1.206     paf      1003:                                if(pc.ls==LS_NAME_SQUARE_PART)
1.166     parser   1004:                                        if(--lexical_brackets_nestage==0) {// $name.[co<]?>de<]?>
1.206     paf      1005:                                                pop_LS(pc); // $name.[co<]>de<]!>
1.166     parser   1006:                                                RC;
                   1007:                                        }
1.160     parser   1008:                                break;
1.166     parser   1009:                        case '[': // $name.[co<[>de]
1.206     paf      1010:                                if(pc.ls==LS_NAME_SQUARE_PART)
1.166     parser   1011:                                        lexical_brackets_nestage++;
1.161     parser   1012:                                break;
1.112     paf      1013:                        }
1.209     paf      1014:                        if(pc.explicit_result && c)
                   1015:                                switch(c) {
1.276     moko     1016:                                default:
                   1017:                                        pc.string.append(c);
1.209     paf      1018:                                case '\n': case ' ': case '\t':
                   1019:                                        begin=pc.source;
                   1020:                                        begin_pos=pc.pos;
1.276     moko     1021:                                        continue;
1.209     paf      1022:                                }
1.112     paf      1023:                        break;
                   1024:                        
                   1025:                // #COMMENT
1.194     paf      1026:                case LS_USER_COMMENT:
1.112     paf      1027:                        if(c=='\n') {
                   1028:                                // skip comment
1.206     paf      1029:                                begin=pc.source;
                   1030:                                begin_pos=pc.pos;
1.112     paf      1031: 
1.206     paf      1032:                                pop_LS(pc);
1.112     paf      1033:                                continue;
1.1       paf      1034:                        }
1.48      paf      1035:                        break;
                   1036:                        
                   1037:                // STRING IN EXPRESSION
1.145     parser   1038:                case LS_EXPRESSION_STRING_QUOTED:
                   1039:                case LS_EXPRESSION_STRING_APOSTROFED:
1.48      paf      1040:                        switch(c) {
                   1041:                        case '"':
1.145     parser   1042:                        case '\'':
                   1043:                                if(
1.270     moko     1044:                                        (pc.ls == LS_EXPRESSION_STRING_QUOTED && c=='"') ||
                   1045:                                        (pc.ls == LS_EXPRESSION_STRING_APOSTROFED && c=='\'') ) {
1.206     paf      1046:                                        pop_LS(pc); //"abc". | 'abc'.
1.145     parser   1047:                                        RC;
                   1048:                                }
                   1049:                                break;
1.48      paf      1050:                        case '$':
1.206     paf      1051:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1052:                                RC;
                   1053:                        case '^':
1.206     paf      1054:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1055:                                RC;
1.10      paf      1056:                        }
                   1057:                        break;
                   1058: 
                   1059:                // METHOD DEFINITION
                   1060:                case LS_DEF_NAME:
1.48      paf      1061:                        switch(c) {
                   1062:                        case '[':
1.206     paf      1063:                                pc.ls=LS_DEF_PARAMS;
1.48      paf      1064:                                RC;
                   1065:                        case '\n':
1.206     paf      1066:                                pc.ls=LS_DEF_SPECIAL_BODY;
1.48      paf      1067:                                RC;
1.10      paf      1068:                        }
                   1069:                        break;
1.48      paf      1070: 
1.10      paf      1071:                case LS_DEF_PARAMS:
1.48      paf      1072:                        switch(c) {
1.192     paf      1073:                        case '$': // common error
1.193     paf      1074:                                result=BAD_METHOD_PARAMETER_NAME_CHARACTER;
                   1075:                                goto break2;
1.48      paf      1076:                        case ';':
                   1077:                                RC;
                   1078:                        case ']':
1.206     paf      1079:                                pc.ls=*pc.source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf      1080:                                RC;
1.49      paf      1081:                        case '\n': // wrong. bailing out
1.206     paf      1082:                                pop_LS(pc);
1.48      paf      1083:                                RC;
1.10      paf      1084:                        }
                   1085:                        break;
1.48      paf      1086: 
1.10      paf      1087:                case LS_DEF_LOCALS:
1.48      paf      1088:                        switch(c) {
                   1089:                        case '[':
                   1090:                        case ';':
                   1091:                                RC;
                   1092:                        case ']':
1.206     paf      1093:                                pc.ls=LS_DEF_COMMENT;
1.48      paf      1094:                                RC;
                   1095:                        case '\n': // wrong. bailing out
1.206     paf      1096:                                pop_LS(pc);
1.48      paf      1097:                                RC;
1.10      paf      1098:                        }
                   1099:                        break;
1.48      paf      1100: 
1.10      paf      1101:                case LS_DEF_COMMENT:
                   1102:                        if(c=='\n') {
1.206     paf      1103:                                pop_LS(pc);
1.48      paf      1104:                                RC;
1.37      paf      1105:                        }
                   1106:                        break;
                   1107: 
1.48      paf      1108:                case LS_DEF_SPECIAL_BODY:
1.175     paf      1109:                        if(c=='\n')
1.48      paf      1110:                                RC;
                   1111:                        break;
                   1112: 
                   1113:                // (EXPRESSION)
1.69      paf      1114:                case LS_VAR_ROUND:
                   1115:                case LS_METHOD_ROUND:
1.48      paf      1116:                        switch(c) {
                   1117:                        case ')':
1.270     moko     1118:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1119:                                        if(pc.ls==LS_METHOD_ROUND) // method round param ended
                   1120:                                                pc.ls=LS_METHOD_AFTER; // look for method end
                   1121:                                        else // pc.ls==LS_VAR_ROUND // variable constructor ended
                   1122:                                                pop_LS(pc); // return to normal life
1.270     moko     1123:                                }
1.48      paf      1124:                                RC;
1.194     paf      1125:                        case '#': // comment start skipping
                   1126:                                if(end!=begin) {
1.206     paf      1127:                                        if(!pc.string_start)
                   1128:                                                pc.string_start=begin_pos;
1.194     paf      1129:                                        // append piece till #
1.206     paf      1130:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.194     paf      1131:                                }
                   1132:                                // fall into COMMENT lexical state [wait for \n]
1.206     paf      1133:                                push_LS(pc, LS_EXPRESSION_COMMENT);
1.194     paf      1134:                                lexical_brackets_nestage=1;
                   1135:                                continue;
1.48      paf      1136:                        case '$':
1.206     paf      1137:                                push_LS(pc, LS_EXPRESSION_VAR_NAME_WITH_COLON);                         
1.48      paf      1138:                                RC;
                   1139:                        case '^':
1.206     paf      1140:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1141:                                RC;
                   1142:                        case '(':
                   1143:                                lexical_brackets_nestage++;
                   1144:                                RC;
1.67      paf      1145:                        case '-':
1.206     paf      1146:                                switch(*pc.source) {
1.127     paf      1147:                                case 'f': // -f
1.67      paf      1148:                                        skip_analized=1;
                   1149:                                        result=FEXISTS;
1.127     paf      1150:                                        goto break2;
                   1151:                                case 'd': // -d
                   1152:                                        skip_analized=1;
                   1153:                                        result=DEXISTS;
                   1154:                                        goto break2;
1.191     paf      1155:                                default: // minus
1.67      paf      1156:                                        result=c;
1.127     paf      1157:                                        goto break2;
                   1158:                                }
1.67      paf      1159:                                goto break2;
1.173     paf      1160:                        case '+': case '*': case '/': case '%': case '\\':
1.58      paf      1161:                        case '~':
1.48      paf      1162:                        case ';':
                   1163:                                RC;
1.193     paf      1164:                        case '&': case '|':
1.206     paf      1165:                                if(*pc.source==c) { // && ||
1.193     paf      1166:                                        result=c=='&'?LAND:LOR;
1.67      paf      1167:                                        skip_analized=1;
1.58      paf      1168:                                } else
                   1169:                                        result=c;
                   1170:                                goto break2;
1.193     paf      1171:                        case '!':
1.206     paf      1172:                                switch(pc.source[0]) { 
1.193     paf      1173:                                case '|': // !| !||
                   1174:                                        skip_analized=1;
1.206     paf      1175:                                        if(pc.source[1]=='|') {
1.193     paf      1176:                                                skip_analized++;
                   1177:                                                result=LXOR;
                   1178:                                        } else
                   1179:                                                result=NXOR;
                   1180:                                        goto break2;
                   1181:                                case '=': // !=
                   1182:                                        skip_analized=1;
                   1183:                                        result=NNE; 
                   1184:                                        goto break2;
                   1185:                                }
                   1186:                                RC;
1.195     paf      1187: 
                   1188:                        case '<': // <<, <=, <
1.206     paf      1189:                                switch(*pc.source) {
1.195     paf      1190:                                case '<': // <[<]
                   1191:                                        skip_analized=1; result=NSL; break;
                   1192:                                case '=': // <[=]
                   1193:                                        skip_analized=1; result=NLE; break;
                   1194:                                default: // <[]
                   1195:                                        result=c; break;
                   1196:                                }
                   1197:                                goto break2;
                   1198:                        case '>': // >>, >=, >
1.206     paf      1199:                                switch(*pc.source) {
1.195     paf      1200:                                case '>': // >[>]
                   1201:                                        skip_analized=1; result=NSR; break;
                   1202:                                case '=': // >[=]
                   1203:                                        skip_analized=1; result=NGE; break;
                   1204:                                default: // >[]
                   1205:                                        result=c; break;
                   1206:                                }
                   1207:                                goto break2;
                   1208:                        case '=': // ==
1.206     paf      1209:                                switch(*pc.source) {
1.195     paf      1210:                                case '=': // =[=]
                   1211:                                        skip_analized=1; result=NEQ; break;
                   1212:                                default: // =[]
                   1213:                                        result=c; break; // not used now
                   1214:                                }
1.58      paf      1215:                                goto break2;
1.195     paf      1216: 
1.48      paf      1217:                        case '"':
1.206     paf      1218:                                push_LS(pc, LS_EXPRESSION_STRING_QUOTED);
1.145     parser   1219:                                RC;
                   1220:                        case '\'':
1.206     paf      1221:                                push_LS(pc, LS_EXPRESSION_STRING_APOSTROFED);
1.48      paf      1222:                                RC;
1.50      paf      1223:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf      1224:                                if(end==begin) // right after whitespace
1.206     paf      1225:                                        if(isspace(pc.source[1])) {
                   1226:                                                switch(*pc.source) {
1.117     paf      1227:                                                        //                                      case '?': // ok [and bad cases, yacc would bark at them]
                   1228:                                                case 't': // lt gt [et nt]
                   1229:                                                        result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
                   1230:                                                        skip_analized=1;
                   1231:                                                        goto break2;
                   1232:                                                case 'e': // le ge ne [ee]
                   1233:                                                        result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
                   1234:                                                        skip_analized=1;
                   1235:                                                        goto break2;
                   1236:                                                case 'q': // eq [lq gq nq]
                   1237:                                                        result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
                   1238:                                                        skip_analized=1;
                   1239:                                                        goto break2;
                   1240:                                                }
1.67      paf      1241:                                        }
                   1242:                                break;
                   1243:                        case 'i':
                   1244:                                if(end==begin) // right after whitespace
1.206     paf      1245:                                        if(isspace(pc.source[1])) {
                   1246:                                                switch(pc.source[0]) {
1.117     paf      1247:                                                case 'n': // in
1.95      paf      1248:                                                        skip_analized=1;
                   1249:                                                        result=IN;
                   1250:                                                        goto break2;
1.117     paf      1251:                                                case 's': // is
1.95      paf      1252:                                                        skip_analized=1;
                   1253:                                                        result=IS;
                   1254:                                                        goto break2;
                   1255:                                                }
1.67      paf      1256:                                        }
                   1257:                                break;
                   1258:                        case 'd':
                   1259:                                if(end==begin) // right after whitespace
1.206     paf      1260:                                        if(pc.source[0]=='e' && pc.source[1]=='f') { // def
1.225     misha    1261:                                                switch(pc.source[2]){
                   1262:                                                case ' ': case '\t': case '\n': case '"': case '\'': case '^': case '$': // non-quoted string without whitespace after 'def' is not allowed
                   1263:                                                        skip_analized=2;
                   1264:                                                        result=DEF;
                   1265:                                                        goto break2;
                   1266:                                                }
                   1267:                                                // error: incorrect char after 'def'
1.50      paf      1268:                                        }
1.48      paf      1269:                                break;
1.216     paf      1270:                        case 't':
                   1271:                                if(end==begin) // right after whitespace
1.225     misha    1272:                                        if(pc.source[0]=='r' && pc.source[1]=='u' && pc.source[2]=='e') { // true
1.216     paf      1273:                                                skip_analized=3;
                   1274:                                                result=LITERAL_TRUE;
                   1275:                                                goto break2;
                   1276:                                        }
                   1277:                                break;
                   1278:                        case 'f':
                   1279:                                if(end==begin) // right after whitespace
1.225     misha    1280:                                        if(pc.source[0]=='a' && pc.source[1]=='l' && pc.source[2]=='s' && pc.source[3]=='e') { // false
1.216     paf      1281:                                                skip_analized=4;
                   1282:                                                result=LITERAL_FALSE;
                   1283:                                                goto break2;
                   1284:                                        }
                   1285:                                break;
1.48      paf      1286:                        case ' ': case '\t': case '\n':
1.63      paf      1287:                                if(end!=begin) { // there were a string after previous operator?
                   1288:                                        result=0; // return that string
                   1289:                                        goto break2;
1.48      paf      1290:                                }
1.63      paf      1291:                                // that's a leading|traling space or after-operator-space
                   1292:                                // ignoring it
                   1293:                                // reset piece 'begin' position & line
1.206     paf      1294:                                begin=pc.source; // after whitespace char
                   1295:                                begin_pos=pc.pos;
1.48      paf      1296:                                continue;
1.1       paf      1297:                        }
                   1298:                        break;
1.194     paf      1299:                case LS_EXPRESSION_COMMENT:
                   1300:                        if(c=='(')
                   1301:                                lexical_brackets_nestage++;
                   1302:                        
1.206     paf      1303:                        switch(*pc.source) {
1.194     paf      1304:                        case '\n': case ')':
1.206     paf      1305:                                if(*pc.source==')')
1.194     paf      1306:                                        if(--lexical_brackets_nestage!=0)
                   1307:                                                continue;
                   1308: 
                   1309:                                // skip comment
1.206     paf      1310:                                begin=pc.source;
                   1311:                                begin_pos=pc.pos;
1.194     paf      1312: 
1.206     paf      1313:                                pop_LS(pc);
1.194     paf      1314:                                continue;
                   1315:                        }
                   1316:                        break;
1.1       paf      1317: 
1.10      paf      1318:                // VARIABLE GET/PUT/WITH
1.166     parser   1319:                case LS_VAR_NAME_SIMPLE_WITH_COLON: 
                   1320:                case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                   1321:                case LS_EXPRESSION_VAR_NAME_WITH_COLON: 
                   1322:                case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                   1323:                        if(
1.206     paf      1324:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON ||
                   1325:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.181     paf      1326:                                // name in expr ends also before 
1.48      paf      1327:                                switch(c) {
1.181     paf      1328:                                // expression minus
1.92      paf      1329:                                case '-': 
1.181     paf      1330:                                // expression integer division
                   1331:                                case '\\':
1.206     paf      1332:                                        pop_LS(pc);
                   1333:                                        pc.ungetc();
1.48      paf      1334:                                        result=EON;
                   1335:                                        goto break2;
                   1336:                                }
                   1337:                        }
1.166     parser   1338:                        if(
1.206     paf      1339:                                pc.ls==LS_VAR_NAME_SIMPLE_WITHOUT_COLON ||
                   1340:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.142     parser   1341:                                // name already has ':', stop before next 
                   1342:                                switch(c) {
                   1343:                                case ':': 
1.206     paf      1344:                                        pop_LS(pc);
                   1345:                                        pc.ungetc();
1.142     parser   1346:                                        result=EON;
                   1347:                                        goto break2;
                   1348:                                }
                   1349:                        }
1.48      paf      1350:                        switch(c) {
                   1351:                        case 0:
                   1352:                        case ' ': case '\t': case '\n':
                   1353:                        case ';':
1.126     paf      1354:                        case ']': case '}': case ')': 
                   1355:                        case '"': case '\'':
1.139     parser   1356:                        case '<': case '>':  // these stand for HTML brackets AND expression binary ops
1.253     misha    1357:                        case '+': case '*': case '/': case '\\': case '%': 
1.92      paf      1358:                        case '&': case '|': 
                   1359:                        case '=': case '!':
1.99      paf      1360:                        // common delimiters
1.190     paf      1361:                        case ',': case '?': case '#':
1.220     paf      1362:                        // mysql column separators
                   1363:                        case '`':
1.139     parser   1364:                        // before call
                   1365:                        case '^': 
1.206     paf      1366:                                pop_LS(pc);
                   1367:                                pc.ungetc();
1.13      paf      1368:                                result=EON;
1.1       paf      1369:                                goto break2;
1.48      paf      1370:                        case '[':
1.162     parser   1371:                                // $name.<[>code]
1.301     moko     1372:                                if(     end[-1]=='$' /* was start of get */ ||
                   1373:                                        end[-1]==':' /* was class name delim */ ||
                   1374:                                        end[-1]=='.' /* was name delim */
1.300     moko     1375:                                        ) {
1.206     paf      1376:                                        push_LS(pc, LS_NAME_SQUARE_PART);
1.162     parser   1377:                                        lexical_brackets_nestage=1;
                   1378:                                        RC;
                   1379:                                }
1.206     paf      1380:                                pc.ls=LS_VAR_SQUARE;
1.1       paf      1381:                                lexical_brackets_nestage=1;
1.48      paf      1382:                                RC;
                   1383:                        case '{':
1.300     moko     1384:                                if(end[-1]=='$') { // ${name}, no need of EON, switching LS, not begin==end as $[a]{$b} will fit
1.206     paf      1385:                                        pc.ls=LS_VAR_NAME_CURLY; 
1.48      paf      1386:                                } else {
1.206     paf      1387:                                        pc.ls=LS_VAR_CURLY;
1.48      paf      1388:                                        lexical_brackets_nestage=1;
                   1389:                                }
1.69      paf      1390: 
1.48      paf      1391:                                RC;
                   1392:                        case '(':
1.301     moko     1393:                                // $name.<(>expr)
                   1394:                                if(     // end[-1]=='$' /* $() excluded */ ||
                   1395:                                        end[-1]==':' /* was class name delim */ ||
                   1396:                                        end[-1]=='.' /* was name delim */
                   1397:                                        ) {
                   1398:                                        push_LS(pc, LS_VAR_ROUND);
                   1399:                                        lexical_brackets_nestage=1;
                   1400:                                        RC;
                   1401:                                }
1.206     paf      1402:                                pc.ls=LS_VAR_ROUND;
1.1       paf      1403:                                lexical_brackets_nestage=1;
1.48      paf      1404:                                RC;
                   1405:                        case '.': // name part delim
                   1406:                        case '$': // name part subvar
1.160     parser   1407:                        case ':': // class<:>name
1.166     parser   1408:                                // go to _WITHOUT_COLON state variant...
1.206     paf      1409:                                if(pc.ls==LS_VAR_NAME_SIMPLE_WITH_COLON)
                   1410:                                        pc.ls=LS_VAR_NAME_SIMPLE_WITHOUT_COLON;
                   1411:                                else if(pc.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON)
                   1412:                                        pc.ls=LS_EXPRESSION_VAR_NAME_WITHOUT_COLON;
1.166     parser   1413:                                // ...stop before next ':'
1.48      paf      1414:                                RC;
1.1       paf      1415:                        }
                   1416:                        break;
1.48      paf      1417: 
1.1       paf      1418:                case LS_VAR_NAME_CURLY:
1.48      paf      1419:                        switch(c) {
1.162     parser   1420:                        case '[':
1.166     parser   1421:                                // ${name.<[>code]}
1.206     paf      1422:                                push_LS(pc, LS_NAME_SQUARE_PART);
1.160     parser   1423:                                lexical_brackets_nestage=1;
                   1424:                                RC;
1.301     moko     1425:                        case '(':
                   1426:                                // ${name.<(>expr)}
                   1427:                                push_LS(pc, LS_VAR_ROUND);
                   1428:                                lexical_brackets_nestage=1;
                   1429:                                RC;
1.48      paf      1430:                        case '}': // ${name} finished, restoring LS
1.206     paf      1431:                                pop_LS(pc);
1.48      paf      1432:                                RC;
                   1433:                        case '.': // name part delim
                   1434:                        case '$': // name part subvar
                   1435:                        case ':': // ':name' or 'class:name'
                   1436:                                RC;
1.1       paf      1437:                        }
                   1438:                        break;
1.48      paf      1439: 
                   1440:                case LS_VAR_SQUARE:
                   1441:                        switch(c) {
                   1442:                        case '$':
1.206     paf      1443:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1444:                                RC;
                   1445:                        case '^':
1.206     paf      1446:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1447:                                RC;
                   1448:                        case ']':
1.1       paf      1449:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1450:                                        pop_LS(pc);
1.48      paf      1451:                                        RC;
1.1       paf      1452:                                }
1.48      paf      1453:                                break;
                   1454:                        case ';': // operator_or_fmt;value delim
                   1455:                                RC;
                   1456:                        case '[':
                   1457:                                lexical_brackets_nestage++;
                   1458:                                break;
1.1       paf      1459:                        }
                   1460:                        break;
1.48      paf      1461: 
1.1       paf      1462:                case LS_VAR_CURLY:
1.48      paf      1463:                        switch(c) {
                   1464:                        case '$':
1.206     paf      1465:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1466:                                RC;
                   1467:                        case '^':
1.206     paf      1468:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1469:                                RC;
                   1470:                        case '}':
1.1       paf      1471:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1472:                                        pop_LS(pc);
1.48      paf      1473:                                        RC;
1.1       paf      1474:                                }
1.48      paf      1475:                                break;
                   1476:                        case '{':
1.1       paf      1477:                                lexical_brackets_nestage++;
1.48      paf      1478:                                break;
                   1479:                        }
1.1       paf      1480:                        break;
                   1481: 
1.10      paf      1482:                // METHOD CALL
1.1       paf      1483:                case LS_METHOD_NAME:
1.48      paf      1484:                        switch(c) {
                   1485:                        case '[':
1.301     moko     1486:                                // ^name.<[>code]
                   1487:                                if(     // end[-1]=='^' /* never, ^[ is literal */ ||
                   1488:                                        end[-1]==':' /* was class name delim */ ||
                   1489:                                        end[-1]=='.' /* was name delim */
                   1490:                                        ) {
1.206     paf      1491:                                        push_LS(pc, LS_NAME_SQUARE_PART);
1.162     parser   1492:                                        lexical_brackets_nestage=1;
                   1493:                                        RC;
                   1494:                                }
1.206     paf      1495:                                pc.ls=LS_METHOD_SQUARE;
1.1       paf      1496:                                lexical_brackets_nestage=1;
1.48      paf      1497:                                RC;
                   1498:                        case '{':
1.206     paf      1499:                                pc.ls=LS_METHOD_CURLY;
1.1       paf      1500:                                lexical_brackets_nestage=1;
1.48      paf      1501:                                RC;
1.69      paf      1502:                        case '(':
1.301     moko     1503:                                // ^name.<(>expr)
                   1504:                                if(     // end[-1]=='^' /* never, ^( is literal */ ||
                   1505:                                        end[-1]==':' /* was class name delim */ ||
                   1506:                                        end[-1]=='.' /* was name delim */
                   1507:                                        ) {
                   1508:                                        push_LS(pc, LS_VAR_ROUND);
                   1509:                                        lexical_brackets_nestage=1;
                   1510:                                        RC;
                   1511:                                }
1.206     paf      1512:                                pc.ls=LS_METHOD_ROUND;
1.69      paf      1513:                                lexical_brackets_nestage=1;
                   1514:                                RC;
1.48      paf      1515:                        case '.': // name part delim 
                   1516:                        case '$': // name part subvar
                   1517:                        case ':': // ':name' or 'class:name'
1.170     parser   1518:                        case '^': // ^abc^xxx wrong. bailing out
                   1519:                        case ']': case '}': case ')': // ^abc]}) wrong. bailing out
1.207     paf      1520:                        case ' ': // ^if ( wrong. bailing out
1.48      paf      1521:                                RC;
1.1       paf      1522:                        }
                   1523:                        break;
1.48      paf      1524: 
                   1525:                case LS_METHOD_SQUARE:
                   1526:                        switch(c) {
                   1527:                        case '$':
1.206     paf      1528:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1529:                                RC;
                   1530:                        case '^':
1.206     paf      1531:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1532:                                RC;
                   1533:                        case ';': // param delim
                   1534:                                RC;
                   1535:                        case ']':
1.1       paf      1536:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1537:                                        pc.ls=LS_METHOD_AFTER;
1.48      paf      1538:                                        RC;
1.1       paf      1539:                                }
1.48      paf      1540:                                break;
                   1541:                        case '[':
1.1       paf      1542:                                lexical_brackets_nestage++;
1.48      paf      1543:                                break;
                   1544:                        }
1.1       paf      1545:                        break;
1.48      paf      1546: 
1.1       paf      1547:                case LS_METHOD_CURLY:
1.48      paf      1548:                        switch(c) {
                   1549:                        case '$':
1.206     paf      1550:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1551:                                RC;
                   1552:                        case '^':
1.206     paf      1553:                                push_LS(pc, LS_METHOD_NAME);
1.94      paf      1554:                                RC;
                   1555:                        case ';': // param delim
1.48      paf      1556:                                RC;
                   1557:                        case '}':
1.1       paf      1558:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1559:                                        pc.ls=LS_METHOD_AFTER;
1.48      paf      1560:                                        RC;
1.1       paf      1561:                                }
1.48      paf      1562:                                break;
                   1563:                        case '{':
1.1       paf      1564:                                lexical_brackets_nestage++;
1.48      paf      1565:                                break;
                   1566:                        }
1.209     paf      1567:                        if(pc.explicit_result && c)
                   1568:                                switch(c) {
1.276     moko     1569:                                default:
                   1570:                                        pc.string.append(c);
1.209     paf      1571:                                case '\n': case ' ': case '\t':
                   1572:                                        begin=pc.source;
                   1573:                                        begin_pos=pc.pos;
1.276     moko     1574:                                        continue;
1.209     paf      1575:                                }
1.1       paf      1576:                        break;
1.48      paf      1577: 
1.1       paf      1578:                case LS_METHOD_AFTER:
1.69      paf      1579:                        if(c=='[') {/* ][ }[ )[ */
1.206     paf      1580:                                pc.ls=LS_METHOD_SQUARE;
1.1       paf      1581:                                lexical_brackets_nestage=1;
1.48      paf      1582:                                RC;
1.281     moko     1583:                        }
1.69      paf      1584:                        if(c=='{') {/* ]{ }{ ){ */
1.206     paf      1585:                                pc.ls=LS_METHOD_CURLY;
1.69      paf      1586:                                lexical_brackets_nestage=1;
                   1587:                                RC;
1.281     moko     1588:                        }
1.69      paf      1589:                        if(c=='(') {/* ]( }( )( */
1.206     paf      1590:                                pc.ls=LS_METHOD_ROUND;
1.1       paf      1591:                                lexical_brackets_nestage=1;
1.48      paf      1592:                                RC;
1.281     moko     1593:                        }
1.206     paf      1594:                        pop_LS(pc);
                   1595:                        pc.ungetc();
1.13      paf      1596:                        result=EON;
1.1       paf      1597:                        goto break2;
                   1598:                }
1.9       paf      1599:                if(c==0) {
1.1       paf      1600:                        result=-1;
                   1601:                        break;
                   1602:                }
                   1603:        }
                   1604: 
                   1605: break2:
1.59      paf      1606:        if(end!=begin) { // there is last piece?
1.258     misha    1607:                if(c=='@' || c==0) // we are before LS_DEF_NAME or EOF?
                   1608:                        while(end!=begin && end[-1]=='\n') // trim all empty lines before LS_DEF_NAME and EOF
1.137     parser   1609:                                end--;
1.206     paf      1610:                if(end!=begin && pc.ls!=LS_USER_COMMENT) { // last piece still alive and not comment?
                   1611:                        if(!pc.string_start)
                   1612:                                pc.string_start=begin_pos;
1.59      paf      1613:                        // append it
1.206     paf      1614:                        pc.string.append_strdup_know_length(begin, end-begin);
1.30      paf      1615:                }
1.59      paf      1616:        }
1.206     paf      1617:        if(!pc.string.is_empty()) { // something accumulated?
                   1618:                // create STRING value: array of OP_VALUE+origin+vstring
1.278     moko     1619: #ifdef SYMBOLS_CACHING
                   1620:                Value *lookup=symbols->get(pc.string);
                   1621: #else
                   1622:                Value *lookup=0;
                   1623: #endif
1.281     moko     1624:                *lvalp=VL(lookup ? lookup : new VString(*new String(pc.string, String::L_CLEAN)), pc.file_no, pc.string_start.line, pc.string_start.col);
1.10      paf      1625:                // new pieces storage
1.206     paf      1626:                pc.string.clear();
                   1627:                pc.string_start.clear();
1.58      paf      1628:                // make current result be pending for next call, return STRING for now
1.206     paf      1629:                pc.pending_state=result;  result=STRING;
1.58      paf      1630:        }
1.67      paf      1631:        if(skip_analized) {
1.206     paf      1632:                pc.source+=skip_analized;  pc.pos.col+=skip_analized;
1.1       paf      1633:        }
1.58      paf      1634:        return result;
1.1       paf      1635: }
                   1636: 
1.264     moko     1637: static int real_yyerror(Parse_control *pc, const char *s) {  // Called by yyparse on error
1.293     moko     1638:        PC.error=pa_strdup(s);
1.281     moko     1639:        return 1;
1.110     paf      1640: }
1.1       paf      1641: 
1.110     paf      1642: static void yyprint(FILE *file, int type, YYSTYPE value) {
                   1643:        if(type==STRING)
1.206     paf      1644:                fprintf(file, " \"%s\"", LA2S(*value)->cstr());
1.110     paf      1645: }

E-mail: