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

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

E-mail: