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

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

E-mail: