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