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

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

E-mail: