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

1.168     parser      1: %{
1.105     paf         2: /** @file
1.106     paf         3:        Parser: compiler(lexical parser and grammar).
                      4: 
1.299     moko        5:        Copyright (c) 2001-2024 Art. Lebedev Studio (http://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.301   ! moko       11: volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.300 2024/12/01 15:19:59 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.271     moko       82: %pure-parser
                     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.206     paf       677:        $$=N(); 
1.267     misha     678: #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    679:        if(!maybe_append_simple_diving_code(*$$, *$3))
                    680: #endif
                    681:        {
1.281     moko      682:                OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */
                    683:                /* some code that writes to that context */
                    684:                /* context=pop; stack: context.value() */
1.267     misha     685:        }
1.160     parser    686: };
1.301   ! moko      687: name_round_expr_value: '(' expr_value ')' {
        !           688:        $$ = N();
        !           689:        P(*$$, *$2);
        !           690: };
1.154     parser    691: subvar_ref_name_rdive: STRING {
1.206     paf       692:        $$=N(); 
1.229     misha     693:        O(*$$, OP::OP_WITH_READ);
1.206     paf       694:        P(*$$, *$1);
1.1       paf       695: };
1.259     misha     696: subvar_get_writes: subvar__get_write | subvar_get_writes subvar__get_write { $$=$1; P(*$$, *$2); };
1.1       paf       697: subvar__get_write: '$' subvar_ref_name_rdive {
                    698:        $$=$2;
1.229     misha     699:        O(*$$, OP::OP_GET_ELEMENT__WRITE);
1.42      paf       700: };
                    701: 
1.154     parser    702: class_prefix:
                    703:        class_static_prefix
                    704: |      class_constructor_prefix
                    705: ;
1.155     parser    706: class_static_prefix: STRING ':' {
                    707:        $$=$1; // stack: class name string
1.282     moko      708:        OP::OPCODE code = OP::OP_VALUE__GET_CLASS;
1.206     paf       709:        if(*LA2S(*$$) == BASE_NAME) { // pseudo BASE class
                    710:                if(VStateless_class* base=PC.cclass->base_class()) {
1.275     moko      711:                        change_string_literal_value(*$$, *new String(base->type()));
1.189     paf       712:                } else {
1.292     moko      713:                        YYERROR1("no base class declared");
1.189     paf       714:                }
1.282     moko      715:                code = OP::OP_VALUE__GET_BASE_CLASS;
                    716:        } else {
1.283     moko      717:                // can't use get_class because it will call @autouse[] if the class wasn't loaded
                    718:                VStateless_class* base=PC.request.classes().get(*LA2S(*$$));
                    719:                if(base && PC.cclass->derived_from(*base))
1.282     moko      720:                        code = OP::OP_VALUE__GET_BASE_CLASS;
1.189     paf       721:        }
1.238     misha     722:        // optimized OP_VALUE+origin+string+OP_GET_CLASS => OP_VALUE__GET_CLASS+origin+string
1.282     moko      723:        change_first(*$$, OP::OP_VALUE, code);
1.155     parser    724: };
                    725: class_constructor_prefix: class_static_prefix ':' {
1.154     parser    726:        $$=$1;
1.292     moko      727:        if(!PC.in_call_value)
                    728:                YYERROR1(":: not allowed here");
1.229     misha     729:        O(*$$, OP::OP_PREPARE_TO_CONSTRUCT_OBJECT);
1.1       paf       730: };
                    731: 
1.53      paf       732: 
1.56      paf       733: /* expr */
1.53      paf       734: 
1.214     paf       735: expr_value: expr;
1.56      paf       736: expr: 
1.214     paf       737:        double_or_STRING
1.216     paf       738: |   true_value
                    739: |   false_value
1.64      paf       740: |      get_value
1.66      paf       741: |      call_value
1.259     misha     742: |      '"' string_inside_quotes_value '"' { $$ = $2; }
                    743: |      '\'' string_inside_quotes_value '\'' { $$ = $2; }
1.60      paf       744: |      '(' expr ')' { $$ = $2; }
                    745: /* stack: operand // stack: @operand */
1.259     misha     746: |      '-' expr %prec NUNARY { $$=$2;  O(*$$, OP::OP_NEG); }
                    747: |      '+' expr %prec NUNARY { $$=$2; }
                    748: |      '~' expr { $$=$2;        O(*$$, OP::OP_INV); }
                    749: |      '!' expr { $$=$2;  O(*$$, OP::OP_NOT); }
                    750: |      "def" expr { $$=$2;  O(*$$, OP::OP_DEF); }
                    751: |      "in" expr { $$=$2;  O(*$$, OP::OP_IN); }
                    752: |      "-f" expr { $$=$2;  O(*$$, OP::OP_FEXISTS); }
                    753: |      "-d" expr { $$=$2;  O(*$$, OP::OP_DEXISTS); }
1.60      paf       754: /* stack: a,b // stack: a@b */
1.259     misha     755: |      expr '-' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_SUB); }
                    756: |      expr '+' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_ADD); }
                    757: |      expr '*' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_MUL); }
                    758: |      expr '/' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_DIV); }
                    759: |      expr '%' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_MOD); }
                    760: |      expr '\\' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_INTDIV); }
                    761: |      expr "<<" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_SL); }
                    762: |      expr ">>" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_SR); }
                    763: |      expr '&' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_AND); }
                    764: |      expr '|' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_OR); }
                    765: |      expr "!|" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_BIN_XOR); }
                    766: |      expr "&&" expr { $$=$1;  OA(*$$, OP::OP_NESTED_CODE, $3);  O(*$$, OP::OP_LOG_AND); }
                    767: |      expr "||" expr { $$=$1;  OA(*$$, OP::OP_NESTED_CODE, $3);  O(*$$, OP::OP_LOG_OR); }
                    768: |      expr "!||" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_LOG_XOR); }
                    769: |      expr '<' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_LT); }
                    770: |      expr '>' expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_GT); }
                    771: |      expr "<=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_LE); }
                    772: |      expr ">=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_GE); }
                    773: |      expr "==" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_EQ); }
                    774: |      expr "!=" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_NUM_NE); }
                    775: |      expr "lt" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_LT); }
                    776: |      expr "gt" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_GT); }
                    777: |      expr "le" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_LE); }
                    778: |      expr "ge" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_GE); }
                    779: |      expr "eq" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_EQ); }
                    780: |      expr "ne" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_STR_NE); }
                    781: |      expr "is" expr { $$=$1;  P(*$$, *$3);  O(*$$, OP::OP_IS); }
1.56      paf       782: ;
1.214     paf       783: 
                    784: double_or_STRING: STRING {
1.238     misha     785:        // optimized OP_STRING => OP_VALUE for doubles
1.214     paf       786:        maybe_change_string_literal_to_double_literal(*($$=$1));
                    787: };
1.55      paf       788: 
1.65      paf       789: string_inside_quotes_value: maybe_codes {
1.238     misha     790: #ifdef OPTIMIZE_BYTECODE_STRING_POOL
                    791:        // it brakes ^if(" 09 "){...}
                    792:        YYSTYPE code=$1;
                    793:        $$=N();
1.267     misha     794:        if(code->count()==3 && change_first(*code, OP::OP_STRING__WRITE, OP::OP_VALUE)){
1.238     misha     795:                // optimized OP_STRING__WRITE+origin+value => OP_VALUE+origin+value without starting OP_STRING_POOL
                    796:                P(*$$, *code);
                    797:        } else {
                    798:                OA(*$$, OP::OP_STRING_POOL, code); /* stack: empty write context */
                    799:        }
                    800: #else
1.206     paf       801:        $$=N();
1.229     misha     802:        OA(*$$, OP::OP_STRING_POOL, $1); /* stack: empty write context */
1.238     misha     803: #endif
1.183     paf       804:        /* some code that writes to that context */
                    805:        /* context=pop; stack: context.get_string() */
1.53      paf       806: };
1.1       paf       807: 
1.27      paf       808: /* basics */
1.1       paf       809: 
1.81      paf       810: write_string: STRING {
1.238     misha     811:        // optimized OP_STRING+OP_WRITE_VALUE => OP_STRING__WRITE
1.259     misha     812:        change_string_literal_to_write_string_literal(*($$=$1));
1.54      paf       813: };
                    814: 
1.261     moko      815: empty_value: /* empty */ { $$=VL(/*we know that we will not change it*/const_cast<VString*>(&vempty), 0, 0, 0); }
1.259     misha     816: true_value: "true" { $$ = VL(/*we know that we will not change it*/const_cast<VBool*>(&vtrue), 0, 0, 0); }
                    817: false_value: "false" { $$ = VL(/*we know that we will not change it*/const_cast<VBool*>(&vfalse), 0, 0, 0); }
1.216     paf       818: 
1.259     misha     819: empty: /* empty */ { $$=N(); };
1.1       paf       820: 
                    821: %%
1.105     paf       822: #endif
1.1       paf       823: 
                    824: /*
                    825:        000$111(2222)00 
                    826:                000$111{3333}00
1.9       paf       827:        $,^: push,=0
1.1       paf       828:        1:( { break=pop
                    829:        2:( )  pop
                    830:        3:{ }  pop
                    831: 
                    832:        000^111(2222)4444{33333}4000
1.9       paf       833:        $,^: push,=0
1.1       paf       834:        1:( { break=pop
                    835:        2:( )=4
                    836:        3:{ }=4
                    837:                4:[^({]=pop
                    838: */
                    839: 
1.206     paf       840: inline void ungetc(Parse_control& pc, uint last_line_end_col) {
                    841:        pc.source--;
                    842:        if(pc.pos.col==0) {
                    843:                --pc.pos.line; pc.pos.col=last_line_end_col;
                    844:        } else
                    845:                --pc.pos.col;
                    846: 
                    847: }
                    848: static int yylex(YYSTYPE *lvalp, void *apc) {
1.295     moko      849:        Parse_control& pc=*static_cast<Parse_control*>(apc);
1.206     paf       850: 
                    851:        #define lexical_brackets_nestage pc.brackets_nestages[pc.ls_sp]
1.48      paf       852:        #define RC {result=c; goto break2; }
1.1       paf       853: 
1.295     moko      854:        int c;
1.206     paf       855:        int result;
1.1       paf       856:        
1.206     paf       857:        if(pc.pending_state) {
                    858:                result=pc.pending_state;
                    859:                pc.pending_state=0;
1.1       paf       860:                return result;
                    861:        }
                    862:        
1.206     paf       863:        const char *begin=pc.source;
                    864:        Pos begin_pos=pc.pos;
1.91      paf       865:        const char *end;
1.67      paf       866:        int skip_analized=0;
1.50      paf       867:        while(true) {
1.206     paf       868:                c=*(end=(pc.source++));
                    869: //             fprintf(stderr, "\nchar: %c %02X; nestage: %d, sp=%d", c, c, lexical_brackets_nestage, pc.sp);
1.1       paf       870: 
1.208     paf       871:                if(c=='\n')
1.206     paf       872:                        pc.pos_next_line();
1.208     paf       873:                else
1.206     paf       874:                        pc.pos_next_c(c);
1.208     paf       875: //             fprintf(stderr, "\nchar: %c file(%d:%d)", c, pc.pos.line, pc.pos.col);
1.73      paf       876: 
1.208     paf       877:                if(pc.pos.col==0+1 && c=='@') {
1.206     paf       878:                        if(pc.ls==LS_DEF_SPECIAL_BODY) {
1.175     paf       879:                                // @SPECIAL
                    880:                                // ...
                    881:                                // @<here = 
1.206     paf       882:                                pop_LS(pc); // exiting from LS_DEF_SPECIAL_BODY state
1.175     paf       883:                        } // continuing checks
1.206     paf       884:                        if(pc.ls==LS_USER) {
                    885:                                push_LS(pc, LS_DEF_NAME);
1.171     parser    886:                                RC;
1.175     paf       887:                        } else // @ in first column inside some code [when could that be?]
1.171     parser    888:                                result=BAD_METHOD_DECL_START;
                    889:                        goto break2;
1.209     paf       890:                }
                    891:                if(c=='^') {
1.206     paf       892:                        if(pc.ls==LS_METHOD_AFTER) {
1.203     paf       893:                                // handle after-method situation
1.206     paf       894:                                pop_LS(pc);
1.203     paf       895:                                result=EON;
                    896:                                skip_analized=-1; // return to punctuation afterwards to assure it's literality
                    897:                                goto break2;
                    898:                        }
1.206     paf       899:                        switch(pc.ls) {
1.169     parser    900: case LS_EXPRESSION_VAR_NAME_WITH_COLON:
                    901: case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                    902: case LS_VAR_NAME_SIMPLE_WITH_COLON:
                    903: case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                    904: case LS_VAR_NAME_CURLY:
                    905: case LS_METHOD_NAME:
1.194     paf       906: case LS_USER_COMMENT:
1.169     parser    907: case LS_DEF_COMMENT:
                    908:        // no literals in names, please
                    909:        break;
                    910: default:
1.206     paf       911:                        switch(*pc.source) {
1.165     parser    912:                        // ^escaping some punctuators
1.228     misha     913:                        case '^': case '$': case ';': case '@':
1.126     paf       914:                        case '(': case ')':
1.48      paf       915:                        case '[': case ']':
                    916:                        case '{': case '}':
1.172     parser    917:                        case '"':  case ':':
1.40      paf       918:                                if(end!=begin) {
1.206     paf       919:                                        if(!pc.string_start)
                    920:                                                pc.string_start=begin_pos;
1.40      paf       921:                                        // append piece till ^
1.206     paf       922:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.40      paf       923:                                }
1.63      paf       924:                                // reset piece 'begin' position & line
1.206     paf       925:                                begin=pc.source; // ->punctuation
                    926:                                begin_pos=pc.pos;
1.203     paf       927:                                // skip over _ after ^
1.206     paf       928:                                pc.source++;  pc.pos.col++;
1.203     paf       929:                                // skip analysis = forced literal
                    930:                                continue;
1.114     paf       931: 
                    932:                        // converting ^#HH into char(hex(HH))
                    933:                        case '#':
                    934:                                if(end!=begin) {
1.206     paf       935:                                        if(!pc.string_start)
                    936:                                                pc.string_start=begin_pos;
1.114     paf       937:                                        // append piece till ^
1.206     paf       938:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.114     paf       939:                                }
                    940:                                // #HH ?
1.228     misha     941:                                if(pc.source[1] && isxdigit(pc.source[1]) && pc.source[2] && isxdigit(pc.source[2])) {
1.218     paf       942:                                        char c=(char)(
1.206     paf       943:                                                hex_value[(unsigned char)pc.source[1]]*0x10+
1.218     paf       944:                                                hex_value[(unsigned char)pc.source[2]]);
1.206     paf       945:                                        if(c==0) {
1.114     paf       946:                                                result=BAD_HEX_LITERAL;
                    947:                                                goto break2; // wrong hex value[no ^#00 chars allowed]: bail out
                    948:                                        }
                    949:                                        // append char(hex(HH))
1.206     paf       950:                                        pc.string.append(c);
1.114     paf       951:                                        // skip over ^#HH
1.206     paf       952:                                        pc.source+=3;
                    953:                                        pc.pos.col+=3;
1.114     paf       954:                                        // reset piece 'begin' position & line
1.206     paf       955:                                        begin=pc.source; // ->after ^#HH
                    956:                                        begin_pos=pc.pos;
1.203     paf       957:                                        // skip analysis = forced literal
1.114     paf       958:                                        continue;
                    959:                                }
1.228     misha     960:                                // just escaped char
                    961:                                // reset piece 'begin' position & line
                    962:                                begin=pc.source;
                    963:                                begin_pos=pc.pos;
                    964:                                // skip over _ after ^
                    965:                                pc.source++;  pc.pos.col++;
                    966:                                // skip analysis = forced literal
                    967:                                continue;
1.1       paf       968:                        }
1.169     parser    969:                        break;
1.203     paf       970:                        }
1.169     parser    971:                }
1.113     paf       972:                // #comment  start skipping
1.206     paf       973:                if(c=='#' && pc.pos.col==1) {
1.112     paf       974:                        if(end!=begin) {
1.206     paf       975:                                if(!pc.string_start)
                    976:                                        pc.string_start=begin_pos;
1.112     paf       977:                                // append piece till #
1.206     paf       978:                                pc.string.append_strdup_know_length(begin, end-begin);
1.112     paf       979:                        }
                    980:                        // fall into COMMENT lexical state [wait for \n]
1.206     paf       981:                        push_LS(pc, LS_USER_COMMENT);
1.175     paf       982:                        continue;
1.112     paf       983:                }
1.206     paf       984:                switch(pc.ls) {
1.10      paf       985: 
                    986:                // USER'S = NOT OURS
1.1       paf       987:                case LS_USER:
1.206     paf       988:                case LS_NAME_SQUARE_PART: // name.[here].xxx
                    989:                        if(pc.trim_bof)
1.153     parser    990:                                switch(c) {
                    991:                                case '\n': case ' ': case '\t':
1.206     paf       992:                                        begin=pc.source;
                    993:                                        begin_pos=pc.pos;
1.152     parser    994:                                        continue; // skip it
1.153     parser    995:                                default:
1.206     paf       996:                                        pc.trim_bof=false;
1.152     parser    997:                                }
1.48      paf       998:                        switch(c) {
                    999:                        case '$':
1.206     paf      1000:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1001:                                RC;
                   1002:                        case '^':
1.206     paf      1003:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1004:                                RC;
1.166     parser   1005:                        case ']':
1.206     paf      1006:                                if(pc.ls==LS_NAME_SQUARE_PART)
1.166     parser   1007:                                        if(--lexical_brackets_nestage==0) {// $name.[co<]?>de<]?>
1.206     paf      1008:                                                pop_LS(pc); // $name.[co<]>de<]!>
1.166     parser   1009:                                                RC;
                   1010:                                        }
1.160     parser   1011:                                break;
1.166     parser   1012:                        case '[': // $name.[co<[>de]
1.206     paf      1013:                                if(pc.ls==LS_NAME_SQUARE_PART)
1.166     parser   1014:                                        lexical_brackets_nestage++;
1.161     parser   1015:                                break;
1.112     paf      1016:                        }
1.209     paf      1017:                        if(pc.explicit_result && c)
                   1018:                                switch(c) {
1.276     moko     1019:                                default:
                   1020:                                        pc.string.append(c);
1.209     paf      1021:                                case '\n': case ' ': case '\t':
                   1022:                                        begin=pc.source;
                   1023:                                        begin_pos=pc.pos;
1.276     moko     1024:                                        continue;
1.209     paf      1025:                                }
1.112     paf      1026:                        break;
                   1027:                        
                   1028:                // #COMMENT
1.194     paf      1029:                case LS_USER_COMMENT:
1.112     paf      1030:                        if(c=='\n') {
                   1031:                                // skip comment
1.206     paf      1032:                                begin=pc.source;
                   1033:                                begin_pos=pc.pos;
1.112     paf      1034: 
1.206     paf      1035:                                pop_LS(pc);
1.112     paf      1036:                                continue;
1.1       paf      1037:                        }
1.48      paf      1038:                        break;
                   1039:                        
                   1040:                // STRING IN EXPRESSION
1.145     parser   1041:                case LS_EXPRESSION_STRING_QUOTED:
                   1042:                case LS_EXPRESSION_STRING_APOSTROFED:
1.48      paf      1043:                        switch(c) {
                   1044:                        case '"':
1.145     parser   1045:                        case '\'':
                   1046:                                if(
1.270     moko     1047:                                        (pc.ls == LS_EXPRESSION_STRING_QUOTED && c=='"') ||
                   1048:                                        (pc.ls == LS_EXPRESSION_STRING_APOSTROFED && c=='\'') ) {
1.206     paf      1049:                                        pop_LS(pc); //"abc". | 'abc'.
1.145     parser   1050:                                        RC;
                   1051:                                }
                   1052:                                break;
1.48      paf      1053:                        case '$':
1.206     paf      1054:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1055:                                RC;
                   1056:                        case '^':
1.206     paf      1057:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1058:                                RC;
1.10      paf      1059:                        }
                   1060:                        break;
                   1061: 
                   1062:                // METHOD DEFINITION
                   1063:                case LS_DEF_NAME:
1.48      paf      1064:                        switch(c) {
                   1065:                        case '[':
1.206     paf      1066:                                pc.ls=LS_DEF_PARAMS;
1.48      paf      1067:                                RC;
                   1068:                        case '\n':
1.206     paf      1069:                                pc.ls=LS_DEF_SPECIAL_BODY;
1.48      paf      1070:                                RC;
1.10      paf      1071:                        }
                   1072:                        break;
1.48      paf      1073: 
1.10      paf      1074:                case LS_DEF_PARAMS:
1.48      paf      1075:                        switch(c) {
1.192     paf      1076:                        case '$': // common error
1.193     paf      1077:                                result=BAD_METHOD_PARAMETER_NAME_CHARACTER;
                   1078:                                goto break2;
1.48      paf      1079:                        case ';':
                   1080:                                RC;
                   1081:                        case ']':
1.206     paf      1082:                                pc.ls=*pc.source=='['?LS_DEF_LOCALS:LS_DEF_COMMENT;
1.48      paf      1083:                                RC;
1.49      paf      1084:                        case '\n': // wrong. bailing out
1.206     paf      1085:                                pop_LS(pc);
1.48      paf      1086:                                RC;
1.10      paf      1087:                        }
                   1088:                        break;
1.48      paf      1089: 
1.10      paf      1090:                case LS_DEF_LOCALS:
1.48      paf      1091:                        switch(c) {
                   1092:                        case '[':
                   1093:                        case ';':
                   1094:                                RC;
                   1095:                        case ']':
1.206     paf      1096:                                pc.ls=LS_DEF_COMMENT;
1.48      paf      1097:                                RC;
                   1098:                        case '\n': // wrong. bailing out
1.206     paf      1099:                                pop_LS(pc);
1.48      paf      1100:                                RC;
1.10      paf      1101:                        }
                   1102:                        break;
1.48      paf      1103: 
1.10      paf      1104:                case LS_DEF_COMMENT:
                   1105:                        if(c=='\n') {
1.206     paf      1106:                                pop_LS(pc);
1.48      paf      1107:                                RC;
1.37      paf      1108:                        }
                   1109:                        break;
                   1110: 
1.48      paf      1111:                case LS_DEF_SPECIAL_BODY:
1.175     paf      1112:                        if(c=='\n')
1.48      paf      1113:                                RC;
                   1114:                        break;
                   1115: 
                   1116:                // (EXPRESSION)
1.69      paf      1117:                case LS_VAR_ROUND:
                   1118:                case LS_METHOD_ROUND:
1.48      paf      1119:                        switch(c) {
                   1120:                        case ')':
1.270     moko     1121:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1122:                                        if(pc.ls==LS_METHOD_ROUND) // method round param ended
                   1123:                                                pc.ls=LS_METHOD_AFTER; // look for method end
                   1124:                                        else // pc.ls==LS_VAR_ROUND // variable constructor ended
                   1125:                                                pop_LS(pc); // return to normal life
1.270     moko     1126:                                }
1.48      paf      1127:                                RC;
1.194     paf      1128:                        case '#': // comment start skipping
                   1129:                                if(end!=begin) {
1.206     paf      1130:                                        if(!pc.string_start)
                   1131:                                                pc.string_start=begin_pos;
1.194     paf      1132:                                        // append piece till #
1.206     paf      1133:                                        pc.string.append_strdup_know_length(begin, end-begin);
1.194     paf      1134:                                }
                   1135:                                // fall into COMMENT lexical state [wait for \n]
1.206     paf      1136:                                push_LS(pc, LS_EXPRESSION_COMMENT);
1.194     paf      1137:                                lexical_brackets_nestage=1;
                   1138:                                continue;
1.48      paf      1139:                        case '$':
1.206     paf      1140:                                push_LS(pc, LS_EXPRESSION_VAR_NAME_WITH_COLON);                         
1.48      paf      1141:                                RC;
                   1142:                        case '^':
1.206     paf      1143:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1144:                                RC;
                   1145:                        case '(':
                   1146:                                lexical_brackets_nestage++;
                   1147:                                RC;
1.67      paf      1148:                        case '-':
1.206     paf      1149:                                switch(*pc.source) {
1.127     paf      1150:                                case 'f': // -f
1.67      paf      1151:                                        skip_analized=1;
                   1152:                                        result=FEXISTS;
1.127     paf      1153:                                        goto break2;
                   1154:                                case 'd': // -d
                   1155:                                        skip_analized=1;
                   1156:                                        result=DEXISTS;
                   1157:                                        goto break2;
1.191     paf      1158:                                default: // minus
1.67      paf      1159:                                        result=c;
1.127     paf      1160:                                        goto break2;
                   1161:                                }
1.67      paf      1162:                                goto break2;
1.173     paf      1163:                        case '+': case '*': case '/': case '%': case '\\':
1.58      paf      1164:                        case '~':
1.48      paf      1165:                        case ';':
                   1166:                                RC;
1.193     paf      1167:                        case '&': case '|':
1.206     paf      1168:                                if(*pc.source==c) { // && ||
1.193     paf      1169:                                        result=c=='&'?LAND:LOR;
1.67      paf      1170:                                        skip_analized=1;
1.58      paf      1171:                                } else
                   1172:                                        result=c;
                   1173:                                goto break2;
1.193     paf      1174:                        case '!':
1.206     paf      1175:                                switch(pc.source[0]) { 
1.193     paf      1176:                                case '|': // !| !||
                   1177:                                        skip_analized=1;
1.206     paf      1178:                                        if(pc.source[1]=='|') {
1.193     paf      1179:                                                skip_analized++;
                   1180:                                                result=LXOR;
                   1181:                                        } else
                   1182:                                                result=NXOR;
                   1183:                                        goto break2;
                   1184:                                case '=': // !=
                   1185:                                        skip_analized=1;
                   1186:                                        result=NNE; 
                   1187:                                        goto break2;
                   1188:                                }
                   1189:                                RC;
1.195     paf      1190: 
                   1191:                        case '<': // <<, <=, <
1.206     paf      1192:                                switch(*pc.source) {
1.195     paf      1193:                                case '<': // <[<]
                   1194:                                        skip_analized=1; result=NSL; break;
                   1195:                                case '=': // <[=]
                   1196:                                        skip_analized=1; result=NLE; break;
                   1197:                                default: // <[]
                   1198:                                        result=c; break;
                   1199:                                }
                   1200:                                goto break2;
                   1201:                        case '>': // >>, >=, >
1.206     paf      1202:                                switch(*pc.source) {
1.195     paf      1203:                                case '>': // >[>]
                   1204:                                        skip_analized=1; result=NSR; break;
                   1205:                                case '=': // >[=]
                   1206:                                        skip_analized=1; result=NGE; break;
                   1207:                                default: // >[]
                   1208:                                        result=c; break;
                   1209:                                }
                   1210:                                goto break2;
                   1211:                        case '=': // ==
1.206     paf      1212:                                switch(*pc.source) {
1.195     paf      1213:                                case '=': // =[=]
                   1214:                                        skip_analized=1; result=NEQ; break;
                   1215:                                default: // =[]
                   1216:                                        result=c; break; // not used now
                   1217:                                }
1.58      paf      1218:                                goto break2;
1.195     paf      1219: 
1.48      paf      1220:                        case '"':
1.206     paf      1221:                                push_LS(pc, LS_EXPRESSION_STRING_QUOTED);
1.145     parser   1222:                                RC;
                   1223:                        case '\'':
1.206     paf      1224:                                push_LS(pc, LS_EXPRESSION_STRING_APOSTROFED);
1.48      paf      1225:                                RC;
1.50      paf      1226:                        case 'l': case 'g': case 'e': case 'n':
1.51      paf      1227:                                if(end==begin) // right after whitespace
1.206     paf      1228:                                        if(isspace(pc.source[1])) {
                   1229:                                                switch(*pc.source) {
1.117     paf      1230:                                                        //                                      case '?': // ok [and bad cases, yacc would bark at them]
                   1231:                                                case 't': // lt gt [et nt]
                   1232:                                                        result=c=='l'?SLT:c=='g'?SGT:BAD_STRING_COMPARISON_OPERATOR;
                   1233:                                                        skip_analized=1;
                   1234:                                                        goto break2;
                   1235:                                                case 'e': // le ge ne [ee]
                   1236:                                                        result=c=='l'?SLE:c=='g'?SGE:c=='n'?SNE:BAD_STRING_COMPARISON_OPERATOR;
                   1237:                                                        skip_analized=1;
                   1238:                                                        goto break2;
                   1239:                                                case 'q': // eq [lq gq nq]
                   1240:                                                        result=c=='e'?SEQ:BAD_STRING_COMPARISON_OPERATOR;
                   1241:                                                        skip_analized=1;
                   1242:                                                        goto break2;
                   1243:                                                }
1.67      paf      1244:                                        }
                   1245:                                break;
                   1246:                        case 'i':
                   1247:                                if(end==begin) // right after whitespace
1.206     paf      1248:                                        if(isspace(pc.source[1])) {
                   1249:                                                switch(pc.source[0]) {
1.117     paf      1250:                                                case 'n': // in
1.95      paf      1251:                                                        skip_analized=1;
                   1252:                                                        result=IN;
                   1253:                                                        goto break2;
1.117     paf      1254:                                                case 's': // is
1.95      paf      1255:                                                        skip_analized=1;
                   1256:                                                        result=IS;
                   1257:                                                        goto break2;
                   1258:                                                }
1.67      paf      1259:                                        }
                   1260:                                break;
                   1261:                        case 'd':
                   1262:                                if(end==begin) // right after whitespace
1.206     paf      1263:                                        if(pc.source[0]=='e' && pc.source[1]=='f') { // def
1.225     misha    1264:                                                switch(pc.source[2]){
                   1265:                                                case ' ': case '\t': case '\n': case '"': case '\'': case '^': case '$': // non-quoted string without whitespace after 'def' is not allowed
                   1266:                                                        skip_analized=2;
                   1267:                                                        result=DEF;
                   1268:                                                        goto break2;
                   1269:                                                }
                   1270:                                                // error: incorrect char after 'def'
1.50      paf      1271:                                        }
1.48      paf      1272:                                break;
1.216     paf      1273:                        case 't':
                   1274:                                if(end==begin) // right after whitespace
1.225     misha    1275:                                        if(pc.source[0]=='r' && pc.source[1]=='u' && pc.source[2]=='e') { // true
1.216     paf      1276:                                                skip_analized=3;
                   1277:                                                result=LITERAL_TRUE;
                   1278:                                                goto break2;
                   1279:                                        }
                   1280:                                break;
                   1281:                        case 'f':
                   1282:                                if(end==begin) // right after whitespace
1.225     misha    1283:                                        if(pc.source[0]=='a' && pc.source[1]=='l' && pc.source[2]=='s' && pc.source[3]=='e') { // false
1.216     paf      1284:                                                skip_analized=4;
                   1285:                                                result=LITERAL_FALSE;
                   1286:                                                goto break2;
                   1287:                                        }
                   1288:                                break;
1.48      paf      1289:                        case ' ': case '\t': case '\n':
1.63      paf      1290:                                if(end!=begin) { // there were a string after previous operator?
                   1291:                                        result=0; // return that string
                   1292:                                        goto break2;
1.48      paf      1293:                                }
1.63      paf      1294:                                // that's a leading|traling space or after-operator-space
                   1295:                                // ignoring it
                   1296:                                // reset piece 'begin' position & line
1.206     paf      1297:                                begin=pc.source; // after whitespace char
                   1298:                                begin_pos=pc.pos;
1.48      paf      1299:                                continue;
1.1       paf      1300:                        }
                   1301:                        break;
1.194     paf      1302:                case LS_EXPRESSION_COMMENT:
                   1303:                        if(c=='(')
                   1304:                                lexical_brackets_nestage++;
                   1305:                        
1.206     paf      1306:                        switch(*pc.source) {
1.194     paf      1307:                        case '\n': case ')':
1.206     paf      1308:                                if(*pc.source==')')
1.194     paf      1309:                                        if(--lexical_brackets_nestage!=0)
                   1310:                                                continue;
                   1311: 
                   1312:                                // skip comment
1.206     paf      1313:                                begin=pc.source;
                   1314:                                begin_pos=pc.pos;
1.194     paf      1315: 
1.206     paf      1316:                                pop_LS(pc);
1.194     paf      1317:                                continue;
                   1318:                        }
                   1319:                        break;
1.1       paf      1320: 
1.10      paf      1321:                // VARIABLE GET/PUT/WITH
1.166     parser   1322:                case LS_VAR_NAME_SIMPLE_WITH_COLON: 
                   1323:                case LS_VAR_NAME_SIMPLE_WITHOUT_COLON:
                   1324:                case LS_EXPRESSION_VAR_NAME_WITH_COLON: 
                   1325:                case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON:
                   1326:                        if(
1.206     paf      1327:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON ||
                   1328:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.181     paf      1329:                                // name in expr ends also before 
1.48      paf      1330:                                switch(c) {
1.181     paf      1331:                                // expression minus
1.92      paf      1332:                                case '-': 
1.181     paf      1333:                                // expression integer division
                   1334:                                case '\\':
1.206     paf      1335:                                        pop_LS(pc);
                   1336:                                        pc.ungetc();
1.48      paf      1337:                                        result=EON;
                   1338:                                        goto break2;
                   1339:                                }
                   1340:                        }
1.166     parser   1341:                        if(
1.206     paf      1342:                                pc.ls==LS_VAR_NAME_SIMPLE_WITHOUT_COLON ||
                   1343:                                pc.ls==LS_EXPRESSION_VAR_NAME_WITHOUT_COLON) {
1.142     parser   1344:                                // name already has ':', stop before next 
                   1345:                                switch(c) {
                   1346:                                case ':': 
1.206     paf      1347:                                        pop_LS(pc);
                   1348:                                        pc.ungetc();
1.142     parser   1349:                                        result=EON;
                   1350:                                        goto break2;
                   1351:                                }
                   1352:                        }
1.48      paf      1353:                        switch(c) {
                   1354:                        case 0:
                   1355:                        case ' ': case '\t': case '\n':
                   1356:                        case ';':
1.126     paf      1357:                        case ']': case '}': case ')': 
                   1358:                        case '"': case '\'':
1.139     parser   1359:                        case '<': case '>':  // these stand for HTML brackets AND expression binary ops
1.253     misha    1360:                        case '+': case '*': case '/': case '\\': case '%': 
1.92      paf      1361:                        case '&': case '|': 
                   1362:                        case '=': case '!':
1.99      paf      1363:                        // common delimiters
1.190     paf      1364:                        case ',': case '?': case '#':
1.220     paf      1365:                        // mysql column separators
                   1366:                        case '`':
1.139     parser   1367:                        // before call
                   1368:                        case '^': 
1.206     paf      1369:                                pop_LS(pc);
                   1370:                                pc.ungetc();
1.13      paf      1371:                                result=EON;
1.1       paf      1372:                                goto break2;
1.48      paf      1373:                        case '[':
1.162     parser   1374:                                // $name.<[>code]
1.301   ! moko     1375:                                if(     end[-1]=='$' /* was start of get */ ||
        !          1376:                                        end[-1]==':' /* was class name delim */ ||
        !          1377:                                        end[-1]=='.' /* was name delim */
1.300     moko     1378:                                        ) {
1.206     paf      1379:                                        push_LS(pc, LS_NAME_SQUARE_PART);
1.162     parser   1380:                                        lexical_brackets_nestage=1;
                   1381:                                        RC;
                   1382:                                }
1.206     paf      1383:                                pc.ls=LS_VAR_SQUARE;
1.1       paf      1384:                                lexical_brackets_nestage=1;
1.48      paf      1385:                                RC;
                   1386:                        case '{':
1.300     moko     1387:                                if(end[-1]=='$') { // ${name}, no need of EON, switching LS, not begin==end as $[a]{$b} will fit
1.206     paf      1388:                                        pc.ls=LS_VAR_NAME_CURLY; 
1.48      paf      1389:                                } else {
1.206     paf      1390:                                        pc.ls=LS_VAR_CURLY;
1.48      paf      1391:                                        lexical_brackets_nestage=1;
                   1392:                                }
1.69      paf      1393: 
1.48      paf      1394:                                RC;
                   1395:                        case '(':
1.301   ! moko     1396:                                // $name.<(>expr)
        !          1397:                                if(     // end[-1]=='$' /* $() excluded */ ||
        !          1398:                                        end[-1]==':' /* was class name delim */ ||
        !          1399:                                        end[-1]=='.' /* was name delim */
        !          1400:                                        ) {
        !          1401:                                        push_LS(pc, LS_VAR_ROUND);
        !          1402:                                        lexical_brackets_nestage=1;
        !          1403:                                        RC;
        !          1404:                                }
1.206     paf      1405:                                pc.ls=LS_VAR_ROUND;
1.1       paf      1406:                                lexical_brackets_nestage=1;
1.48      paf      1407:                                RC;
                   1408:                        case '.': // name part delim
                   1409:                        case '$': // name part subvar
1.160     parser   1410:                        case ':': // class<:>name
1.166     parser   1411:                                // go to _WITHOUT_COLON state variant...
1.206     paf      1412:                                if(pc.ls==LS_VAR_NAME_SIMPLE_WITH_COLON)
                   1413:                                        pc.ls=LS_VAR_NAME_SIMPLE_WITHOUT_COLON;
                   1414:                                else if(pc.ls==LS_EXPRESSION_VAR_NAME_WITH_COLON)
                   1415:                                        pc.ls=LS_EXPRESSION_VAR_NAME_WITHOUT_COLON;
1.166     parser   1416:                                // ...stop before next ':'
1.48      paf      1417:                                RC;
1.1       paf      1418:                        }
                   1419:                        break;
1.48      paf      1420: 
1.1       paf      1421:                case LS_VAR_NAME_CURLY:
1.48      paf      1422:                        switch(c) {
1.162     parser   1423:                        case '[':
1.166     parser   1424:                                // ${name.<[>code]}
1.206     paf      1425:                                push_LS(pc, LS_NAME_SQUARE_PART);
1.160     parser   1426:                                lexical_brackets_nestage=1;
                   1427:                                RC;
1.301   ! moko     1428:                        case '(':
        !          1429:                                // ${name.<(>expr)}
        !          1430:                                push_LS(pc, LS_VAR_ROUND);
        !          1431:                                lexical_brackets_nestage=1;
        !          1432:                                RC;
1.48      paf      1433:                        case '}': // ${name} finished, restoring LS
1.206     paf      1434:                                pop_LS(pc);
1.48      paf      1435:                                RC;
                   1436:                        case '.': // name part delim
                   1437:                        case '$': // name part subvar
                   1438:                        case ':': // ':name' or 'class:name'
                   1439:                                RC;
1.1       paf      1440:                        }
                   1441:                        break;
1.48      paf      1442: 
                   1443:                case LS_VAR_SQUARE:
                   1444:                        switch(c) {
                   1445:                        case '$':
1.206     paf      1446:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1447:                                RC;
                   1448:                        case '^':
1.206     paf      1449:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1450:                                RC;
                   1451:                        case ']':
1.1       paf      1452:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1453:                                        pop_LS(pc);
1.48      paf      1454:                                        RC;
1.1       paf      1455:                                }
1.48      paf      1456:                                break;
                   1457:                        case ';': // operator_or_fmt;value delim
                   1458:                                RC;
                   1459:                        case '[':
                   1460:                                lexical_brackets_nestage++;
                   1461:                                break;
1.1       paf      1462:                        }
                   1463:                        break;
1.48      paf      1464: 
1.1       paf      1465:                case LS_VAR_CURLY:
1.48      paf      1466:                        switch(c) {
                   1467:                        case '$':
1.206     paf      1468:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1469:                                RC;
                   1470:                        case '^':
1.206     paf      1471:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1472:                                RC;
                   1473:                        case '}':
1.1       paf      1474:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1475:                                        pop_LS(pc);
1.48      paf      1476:                                        RC;
1.1       paf      1477:                                }
1.48      paf      1478:                                break;
                   1479:                        case '{':
1.1       paf      1480:                                lexical_brackets_nestage++;
1.48      paf      1481:                                break;
                   1482:                        }
1.1       paf      1483:                        break;
                   1484: 
1.10      paf      1485:                // METHOD CALL
1.1       paf      1486:                case LS_METHOD_NAME:
1.48      paf      1487:                        switch(c) {
                   1488:                        case '[':
1.301   ! moko     1489:                                // ^name.<[>code]
        !          1490:                                if(     // end[-1]=='^' /* never, ^[ is literal */ ||
        !          1491:                                        end[-1]==':' /* was class name delim */ ||
        !          1492:                                        end[-1]=='.' /* was name delim */
        !          1493:                                        ) {
1.206     paf      1494:                                        push_LS(pc, LS_NAME_SQUARE_PART);
1.162     parser   1495:                                        lexical_brackets_nestage=1;
                   1496:                                        RC;
                   1497:                                }
1.206     paf      1498:                                pc.ls=LS_METHOD_SQUARE;
1.1       paf      1499:                                lexical_brackets_nestage=1;
1.48      paf      1500:                                RC;
                   1501:                        case '{':
1.206     paf      1502:                                pc.ls=LS_METHOD_CURLY;
1.1       paf      1503:                                lexical_brackets_nestage=1;
1.48      paf      1504:                                RC;
1.69      paf      1505:                        case '(':
1.301   ! moko     1506:                                // ^name.<(>expr)
        !          1507:                                if(     // end[-1]=='^' /* never, ^( is literal */ ||
        !          1508:                                        end[-1]==':' /* was class name delim */ ||
        !          1509:                                        end[-1]=='.' /* was name delim */
        !          1510:                                        ) {
        !          1511:                                        push_LS(pc, LS_VAR_ROUND);
        !          1512:                                        lexical_brackets_nestage=1;
        !          1513:                                        RC;
        !          1514:                                }
1.206     paf      1515:                                pc.ls=LS_METHOD_ROUND;
1.69      paf      1516:                                lexical_brackets_nestage=1;
                   1517:                                RC;
1.48      paf      1518:                        case '.': // name part delim 
                   1519:                        case '$': // name part subvar
                   1520:                        case ':': // ':name' or 'class:name'
1.170     parser   1521:                        case '^': // ^abc^xxx wrong. bailing out
                   1522:                        case ']': case '}': case ')': // ^abc]}) wrong. bailing out
1.207     paf      1523:                        case ' ': // ^if ( wrong. bailing out
1.48      paf      1524:                                RC;
1.1       paf      1525:                        }
                   1526:                        break;
1.48      paf      1527: 
                   1528:                case LS_METHOD_SQUARE:
                   1529:                        switch(c) {
                   1530:                        case '$':
1.206     paf      1531:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1532:                                RC;
                   1533:                        case '^':
1.206     paf      1534:                                push_LS(pc, LS_METHOD_NAME);
1.48      paf      1535:                                RC;
                   1536:                        case ';': // param delim
                   1537:                                RC;
                   1538:                        case ']':
1.1       paf      1539:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1540:                                        pc.ls=LS_METHOD_AFTER;
1.48      paf      1541:                                        RC;
1.1       paf      1542:                                }
1.48      paf      1543:                                break;
                   1544:                        case '[':
1.1       paf      1545:                                lexical_brackets_nestage++;
1.48      paf      1546:                                break;
                   1547:                        }
1.1       paf      1548:                        break;
1.48      paf      1549: 
1.1       paf      1550:                case LS_METHOD_CURLY:
1.48      paf      1551:                        switch(c) {
                   1552:                        case '$':
1.206     paf      1553:                                push_LS(pc, LS_VAR_NAME_SIMPLE_WITH_COLON);
1.48      paf      1554:                                RC;
                   1555:                        case '^':
1.206     paf      1556:                                push_LS(pc, LS_METHOD_NAME);
1.94      paf      1557:                                RC;
                   1558:                        case ';': // param delim
1.48      paf      1559:                                RC;
                   1560:                        case '}':
1.1       paf      1561:                                if(--lexical_brackets_nestage==0) {
1.206     paf      1562:                                        pc.ls=LS_METHOD_AFTER;
1.48      paf      1563:                                        RC;
1.1       paf      1564:                                }
1.48      paf      1565:                                break;
                   1566:                        case '{':
1.1       paf      1567:                                lexical_brackets_nestage++;
1.48      paf      1568:                                break;
                   1569:                        }
1.209     paf      1570:                        if(pc.explicit_result && c)
                   1571:                                switch(c) {
1.276     moko     1572:                                default:
                   1573:                                        pc.string.append(c);
1.209     paf      1574:                                case '\n': case ' ': case '\t':
                   1575:                                        begin=pc.source;
                   1576:                                        begin_pos=pc.pos;
1.276     moko     1577:                                        continue;
1.209     paf      1578:                                }
1.1       paf      1579:                        break;
1.48      paf      1580: 
1.1       paf      1581:                case LS_METHOD_AFTER:
1.69      paf      1582:                        if(c=='[') {/* ][ }[ )[ */
1.206     paf      1583:                                pc.ls=LS_METHOD_SQUARE;
1.1       paf      1584:                                lexical_brackets_nestage=1;
1.48      paf      1585:                                RC;
1.281     moko     1586:                        }
1.69      paf      1587:                        if(c=='{') {/* ]{ }{ ){ */
1.206     paf      1588:                                pc.ls=LS_METHOD_CURLY;
1.69      paf      1589:                                lexical_brackets_nestage=1;
                   1590:                                RC;
1.281     moko     1591:                        }
1.69      paf      1592:                        if(c=='(') {/* ]( }( )( */
1.206     paf      1593:                                pc.ls=LS_METHOD_ROUND;
1.1       paf      1594:                                lexical_brackets_nestage=1;
1.48      paf      1595:                                RC;
1.281     moko     1596:                        }
1.206     paf      1597:                        pop_LS(pc);
                   1598:                        pc.ungetc();
1.13      paf      1599:                        result=EON;
1.1       paf      1600:                        goto break2;
                   1601:                }
1.9       paf      1602:                if(c==0) {
1.1       paf      1603:                        result=-1;
                   1604:                        break;
                   1605:                }
                   1606:        }
                   1607: 
                   1608: break2:
1.59      paf      1609:        if(end!=begin) { // there is last piece?
1.258     misha    1610:                if(c=='@' || c==0) // we are before LS_DEF_NAME or EOF?
                   1611:                        while(end!=begin && end[-1]=='\n') // trim all empty lines before LS_DEF_NAME and EOF
1.137     parser   1612:                                end--;
1.206     paf      1613:                if(end!=begin && pc.ls!=LS_USER_COMMENT) { // last piece still alive and not comment?
                   1614:                        if(!pc.string_start)
                   1615:                                pc.string_start=begin_pos;
1.59      paf      1616:                        // append it
1.206     paf      1617:                        pc.string.append_strdup_know_length(begin, end-begin);
1.30      paf      1618:                }
1.59      paf      1619:        }
1.206     paf      1620:        if(!pc.string.is_empty()) { // something accumulated?
                   1621:                // create STRING value: array of OP_VALUE+origin+vstring
1.278     moko     1622: #ifdef SYMBOLS_CACHING
                   1623:                Value *lookup=symbols->get(pc.string);
                   1624: #else
                   1625:                Value *lookup=0;
                   1626: #endif
1.281     moko     1627:                *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      1628:                // new pieces storage
1.206     paf      1629:                pc.string.clear();
                   1630:                pc.string_start.clear();
1.58      paf      1631:                // make current result be pending for next call, return STRING for now
1.206     paf      1632:                pc.pending_state=result;  result=STRING;
1.58      paf      1633:        }
1.67      paf      1634:        if(skip_analized) {
1.206     paf      1635:                pc.source+=skip_analized;  pc.pos.col+=skip_analized;
1.1       paf      1636:        }
1.58      paf      1637:        return result;
1.1       paf      1638: }
                   1639: 
1.264     moko     1640: static int real_yyerror(Parse_control *pc, const char *s) {  // Called by yyparse on error
1.293     moko     1641:        PC.error=pa_strdup(s);
1.281     moko     1642:        return 1;
1.110     paf      1643: }
1.1       paf      1644: 
1.110     paf      1645: static void yyprint(FILE *file, int type, YYSTYPE value) {
                   1646:        if(type==STRING)
1.206     paf      1647:                fprintf(file, " \"%s\"", LA2S(*value)->cstr());
1.110     paf      1648: }

E-mail: