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