| 1 | /* |
| 2 | www.sourceforge.net/projects/tinyxpath |
| 3 | Copyright (c) 2002-2004 Yves Berquin ([email protected]) |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any |
| 7 | damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any |
| 10 | purpose, including commercial applications, and to alter it and |
| 11 | redistribute it freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must |
| 14 | not claim that you wrote the original software. If you use this |
| 15 | software in a product, an acknowledgment in the product documentation |
| 16 | would be appreciated but is not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and |
| 19 | must not be misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source |
| 22 | distribution. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | \file xpath_syntax.cpp |
| 27 | \author Yves Berquin |
| 28 | XPath Syntax analyzer for TinyXPath project |
| 29 | */ |
| 30 | |
| 31 | #include <cstdio> |
| 32 | #include "xpath_syntax.h" |
| 33 | |
| 34 | namespace TinyXPath |
| 35 | { |
| 36 | |
| 37 | /// Decodes the syntax of an XPath expression. On entry, the lexical analysis has already |
| 38 | /// produced a list of basic tokens. |
| 39 | /// \n Throws : syntax_error, syntax_overflow |
| 40 | void token_syntax_decoder::v_syntax_decode () |
| 41 | { |
| 42 | bool o_res; |
| 43 | |
| 44 | // group double tokens ('!=', '::', ...) |
| 45 | v_tokenize_expression (); |
| 46 | |
| 47 | // reset list start |
| 48 | v_set_current_top (); |
| 49 | |
| 50 | u_nb_recurs = 0; |
| 51 | |
| 52 | // the XPath expression, well, ..., must be an xpath_expr |
| 53 | o_res = o_recognize (xpath_expr, true); |
| 54 | if (! o_res) |
| 55 | throw syntax_error ("main level"); |
| 56 | #ifdef DUMP_SYNTAX |
| 57 | if (ltp_get (0)) |
| 58 | printf ("!!! Still to decode : %s !!!\n", cp_disp_class_lex (ltp_get (0) -> lex_get_value ())); |
| 59 | else |
| 60 | printf ("Completely parsed\n"); |
| 61 | printf ("%d recursions\n", u_nb_recurs); |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | /// Recognize one XPath construction |
| 66 | /// \n This function throws exceptions every time there's a failure in a backtracking attempt. |
| 67 | /// This should only happen when o_final is false, otherwise we have a syntax error |
| 68 | bool token_syntax_decoder::o_recognize ( |
| 69 | xpath_construct xc_current, ///< XPath construction to recognize |
| 70 | bool o_final) ///< true if we need to go on, false if it's just a trial |
| 71 | /// in the backtracking |
| 72 | { |
| 73 | lex_token * ltp_freeze; |
| 74 | bool o_empty, o_found, o_location_path, o_qname, o_temp; |
| 75 | unsigned u_nb_argument, u_nb_predicate; |
| 76 | int i_action_counter; |
| 77 | bool o_test_more; |
| 78 | |
| 79 | u_nb_recurs++; |
| 80 | if (u_nb_recurs > 10000) |
| 81 | throw syntax_overflow (); |
| 82 | ltp_freeze = NULL; |
| 83 | |
| 84 | switch (xc_current) |
| 85 | { |
| 86 | case xpath_location_path : |
| 87 | // |
| 88 | // [1] LocationPath ::= RelativeLocationPath |
| 89 | // | AbsoluteLocationPath |
| 90 | // |
| 91 | if (! ltp_get (0)) |
| 92 | return false; |
| 93 | switch (ltp_get (0) -> lex_get_value ()) |
| 94 | { |
| 95 | case lex_slash : |
| 96 | case lex_2_slash : |
| 97 | if (! o_recognize (xpath_absolute_location_path, o_final)) |
| 98 | return false; |
| 99 | if (o_final) |
| 100 | v_action (xpath_location_path, xpath_location_path_abs); |
| 101 | break; |
| 102 | default : |
| 103 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 104 | return false; |
| 105 | if (o_final) |
| 106 | v_action (xpath_location_path, xpath_location_path_rel); |
| 107 | break; |
| 108 | } |
| 109 | break; |
| 110 | |
| 111 | case xpath_absolute_location_path : |
| 112 | // [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 113 | // | AbbreviatedAbsoluteLocationPath |
| 114 | if (! ltp_get (0)) |
| 115 | return false; |
| 116 | i_action_counter = i_get_action_counter (); |
| 117 | switch (ltp_get (0) -> lex_get_value ()) |
| 118 | { |
| 119 | case lex_slash : |
| 120 | v_inc_current (1); |
| 121 | ltp_freeze = ltp_get (0); |
| 122 | o_temp = o_recognize (xpath_relative_location_path, false); |
| 123 | if (o_temp) |
| 124 | { |
| 125 | v_set_current (ltp_freeze); |
| 126 | o_recognize (xpath_relative_location_path, o_final); |
| 127 | if (o_final) |
| 128 | v_action (xpath_absolute_location_path, xpath_absolute_location_path_slash_rel, i_action_counter); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | v_set_current (ltp_freeze); |
| 133 | if (o_final) |
| 134 | v_action (xpath_absolute_location_path, xpath_absolute_location_path_slash, i_action_counter); |
| 135 | } |
| 136 | break; |
| 137 | case lex_2_slash : |
| 138 | if (! o_recognize (xpath_abbreviated_absolute_location_path, o_final)) |
| 139 | return false; |
| 140 | if (o_final) |
| 141 | v_action (xpath_absolute_location_path, xpath_absolute_location_path_abbrev, i_action_counter); |
| 142 | break; |
| 143 | default : |
| 144 | return false; |
| 145 | } |
| 146 | break; |
| 147 | |
| 148 | case xpath_relative_location_path : |
| 149 | // |
| 150 | // [3] RelativeLocationPath ::= Step |
| 151 | // | RelativeLocationPath '/' Step |
| 152 | // | AbbreviatedRelativeLocationPath |
| 153 | // [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 154 | // |
| 155 | i_action_counter = i_get_action_counter (); |
| 156 | if (! o_recognize (xpath_step, o_final)) |
| 157 | return false; |
| 158 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_slash) |
| 159 | { |
| 160 | v_inc_current (1); |
| 161 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 162 | return false; |
| 163 | if (o_final) |
| 164 | v_action (xpath_relative_location_path, xpath_relative_location_path_rel_step, i_action_counter); |
| 165 | } |
| 166 | else |
| 167 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_2_slash) |
| 168 | { |
| 169 | v_inc_current (1); |
| 170 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 171 | return false; |
| 172 | if (o_final) |
| 173 | v_action (xpath_relative_location_path, xpath_relative_location_path_rel_double_slash_step, i_action_counter); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | if (o_final) |
| 178 | v_action (xpath_relative_location_path, xpath_relative_location_path_step, i_action_counter); |
| 179 | } |
| 180 | break; |
| 181 | |
| 182 | case xpath_step : |
| 183 | // [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 184 | // | AbbreviatedStep |
| 185 | if (! ltp_get (0)) |
| 186 | return false; |
| 187 | switch (ltp_get (0) -> lex_get_value ()) |
| 188 | { |
| 189 | case lex_dot : |
| 190 | case lex_2_dot : |
| 191 | if (! o_recognize (xpath_abbrieviated_step, o_final)) |
| 192 | return false; |
| 193 | if (o_final) |
| 194 | v_action (xpath_step, xpath_step_abbrev); |
| 195 | break; |
| 196 | default : |
| 197 | if (! o_recognize (xpath_axis_specifier, o_final)) |
| 198 | return false; |
| 199 | if (! o_recognize (xpath_node_test, o_final)) |
| 200 | return false; |
| 201 | o_found = true; |
| 202 | u_nb_predicate = 0; |
| 203 | while (o_found && ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_obrack) |
| 204 | { |
| 205 | ltp_freeze = ltp_get (0); |
| 206 | if (! o_recognize (xpath_predicate, false)) |
| 207 | o_found = false; |
| 208 | else |
| 209 | { |
| 210 | v_set_current (ltp_freeze); |
| 211 | o_recognize (xpath_predicate, o_final); |
| 212 | u_nb_predicate++; |
| 213 | } |
| 214 | } |
| 215 | if (o_final) |
| 216 | v_action (xpath_step, xpath_step_full, u_nb_predicate); |
| 217 | break; |
| 218 | } |
| 219 | break; |
| 220 | |
| 221 | case xpath_axis_specifier : |
| 222 | // |
| 223 | // [5] AxisSpecifier ::= AxisName '::' |
| 224 | // | AbbreviatedAxisSpecifier |
| 225 | // |
| 226 | // [13] AbbreviatedAxisSpecifier ::= '@'? |
| 227 | o_empty = false; |
| 228 | if (ltp_get (0)) |
| 229 | { |
| 230 | switch (ltp_get (0) -> lex_get_value ()) |
| 231 | { |
| 232 | case lex_at : |
| 233 | v_inc_current (1); |
| 234 | if (o_final) |
| 235 | v_action (xpath_axis_specifier, xpath_axis_specifier_at); |
| 236 | break; |
| 237 | default : |
| 238 | if (o_is_axis_name (ltp_get (0) -> lex_get_value ())) |
| 239 | { |
| 240 | if (! o_recognize (xpath_axis_name, o_final)) |
| 241 | return false; |
| 242 | if (! ltp_get (0)) |
| 243 | return false; |
| 244 | if (ltp_get (0) -> lex_get_value () != lex_2_colon) |
| 245 | return false; |
| 246 | v_inc_current (1); |
| 247 | if (o_final) |
| 248 | v_action (xpath_axis_specifier, xpath_axis_specifier_axis_name); |
| 249 | } |
| 250 | else |
| 251 | o_empty = true; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | else |
| 256 | o_empty = true; |
| 257 | if (o_empty) |
| 258 | { |
| 259 | if (o_final) |
| 260 | { |
| 261 | v_action (xpath_abbreviated_axis_specifier, 1); |
| 262 | v_action (xpath_axis_specifier, xpath_axis_specifier_empty); |
| 263 | } |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case xpath_axis_name : |
| 268 | // [6] AxisName ::= 'ancestor' |
| 269 | // | 'ancestor-or-self' |
| 270 | // | 'attribute' |
| 271 | // | 'child' |
| 272 | // | 'descendant' |
| 273 | // | 'descendant-or-self' |
| 274 | // | 'following' |
| 275 | // | 'following-sibling' |
| 276 | // | 'namespace' |
| 277 | // | 'parent' |
| 278 | // | 'preceding' |
| 279 | // | 'preceding-sibling' |
| 280 | // | 'self' |
| 281 | |
| 282 | if (! ltp_get (0)) |
| 283 | return false; |
| 284 | if (! o_is_axis_name (ltp_get (0) -> lex_get_value ())) |
| 285 | return false; |
| 286 | if (o_final) |
| 287 | v_action (xpath_axis_name, 0, ltp_get (0) -> lex_get_value ()); |
| 288 | v_inc_current (1); |
| 289 | break; |
| 290 | |
| 291 | case xpath_node_test : |
| 292 | // [7] NodeTest ::= NameTest |
| 293 | // | NodeType '(' ')' |
| 294 | // | 'processing-instruction' '(' Literal ')' |
| 295 | // [38] NodeType ::= 'comment' |
| 296 | // | 'text' |
| 297 | // | 'processing-instruction' |
| 298 | // | 'node' |
| 299 | if (! ltp_get (0)) |
| 300 | return false; |
| 301 | switch (ltp_get (0) -> lex_get_value ()) |
| 302 | { |
| 303 | case lex_comment : |
| 304 | case lex_text : |
| 305 | case lex_node : |
| 306 | if (o_final) |
| 307 | v_action (xpath_node_test, xpath_node_test_reserved_keyword, ltp_get (0) -> lex_get_value ()); |
| 308 | v_inc_current (3); |
| 309 | break; |
| 310 | case lex_processing_instruction : |
| 311 | if (ltp_get (2) && ltp_get (2) -> lex_get_value () == lex_cparen) |
| 312 | { |
| 313 | // single |
| 314 | v_inc_current (3); |
| 315 | if (o_final) |
| 316 | v_action (xpath_node_test, xpath_node_test_pi, lex_processing_instruction); |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | // with literal |
| 321 | v_inc_current (3); |
| 322 | if (o_final) |
| 323 | v_action (xpath_node_test, xpath_node_test_pi_lit, lex_processing_instruction, ltp_get (0) -> cp_get_literal ()); |
| 324 | v_inc_current (1); |
| 325 | } |
| 326 | break; |
| 327 | default : |
| 328 | if (! o_recognize (xpath_name_test, o_final)) |
| 329 | return false; |
| 330 | if (o_final) |
| 331 | v_action (xpath_node_test, xpath_node_test_name_test); |
| 332 | } |
| 333 | break; |
| 334 | |
| 335 | case xpath_predicate : |
| 336 | // [8] Predicate ::= '[' PredicateExpr ']' |
| 337 | if (! ltp_get (1)) |
| 338 | return false; |
| 339 | if (ltp_get (0) -> lex_get_value () != lex_obrack) |
| 340 | return false; |
| 341 | v_inc_current (1); |
| 342 | if (! o_recognize (xpath_predicate_expr, o_final)) |
| 343 | return false; |
| 344 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_cbrack) |
| 345 | return false; |
| 346 | v_inc_current (1); |
| 347 | if (o_final) |
| 348 | v_action (xpath_predicate, 0); |
| 349 | break; |
| 350 | |
| 351 | case xpath_predicate_expr : |
| 352 | // [9] PredicateExpr ::= Expr |
| 353 | if (! o_recognize (xpath_expr, o_final)) |
| 354 | return false; |
| 355 | if (o_final) |
| 356 | v_action (xpath_predicate_expr, 0); |
| 357 | break; |
| 358 | |
| 359 | case xpath_abbreviated_absolute_location_path : |
| 360 | // [10] AbbreviatedAbsoluteLocationPath ::= '//' RelativeLocationPath |
| 361 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_2_slash) |
| 362 | return false; |
| 363 | v_inc_current (1); |
| 364 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 365 | return false; |
| 366 | if (o_final) |
| 367 | v_action (xpath_abbreviated_absolute_location_path, 0); |
| 368 | break; |
| 369 | |
| 370 | // Note : [11] is processed by [3] |
| 371 | |
| 372 | case xpath_abbrieviated_step : |
| 373 | // [12] AbbreviatedStep ::= '.' | '..' |
| 374 | if (! ltp_get (0)) |
| 375 | return false; |
| 376 | switch (ltp_get (0) -> lex_get_value ()) |
| 377 | { |
| 378 | case lex_dot : |
| 379 | v_inc_current (1); |
| 380 | if (o_final) |
| 381 | v_action (xpath_abbrieviated_step, 0); |
| 382 | break; |
| 383 | case lex_2_dot : |
| 384 | v_inc_current (1); |
| 385 | if (o_final) |
| 386 | v_action (xpath_abbrieviated_step, 1); |
| 387 | break; |
| 388 | default : |
| 389 | return false; |
| 390 | } |
| 391 | break; |
| 392 | |
| 393 | // Note : [13] processed by [5] |
| 394 | |
| 395 | case xpath_expr : |
| 396 | // |
| 397 | // [14] Expr ::= OrExpr |
| 398 | // |
| 399 | if (! ltp_get (0)) |
| 400 | return false; |
| 401 | if (! o_recognize (xpath_or_expr, o_final)) |
| 402 | return false; |
| 403 | if (o_final) |
| 404 | v_action (xpath_expr, 0); |
| 405 | break; |
| 406 | |
| 407 | case xpath_primary_expr : |
| 408 | // [15] PrimaryExpr ::= VariableReference |
| 409 | // | '(' Expr ')' |
| 410 | // | Literal |
| 411 | // | Number |
| 412 | // | FunctionCall |
| 413 | if (! ltp_get (0)) |
| 414 | return false; |
| 415 | switch (ltp_get (0) -> lex_get_value ()) |
| 416 | { |
| 417 | case lex_dollar : |
| 418 | if (! o_recognize (xpath_variable_reference, o_final)) |
| 419 | return false; |
| 420 | if (o_final) |
| 421 | v_action (xpath_primary_expr, xpath_primary_expr_variable); |
| 422 | break; |
| 423 | case lex_oparen : |
| 424 | v_inc_current (1); |
| 425 | if (! o_recognize (xpath_expr, o_final)) |
| 426 | return false; |
| 427 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_cparen) |
| 428 | return false; |
| 429 | v_inc_current (1); |
| 430 | if (o_final) |
| 431 | v_action (xpath_primary_expr, xpath_primary_expr_paren_expr); |
| 432 | break; |
| 433 | case lex_literal : |
| 434 | if (o_final) |
| 435 | v_action (xpath_primary_expr, xpath_primary_expr_literal, 0, ltp_get (0) -> cp_get_literal ()); |
| 436 | v_inc_current (1); |
| 437 | break; |
| 438 | case lex_number : |
| 439 | if (o_final) |
| 440 | v_action (xpath_primary_expr, xpath_primary_expr_number, 0, ltp_get (0) -> cp_get_literal ()); |
| 441 | v_inc_current (1); |
| 442 | break; |
| 443 | default : |
| 444 | if (! o_recognize (xpath_function_call, o_final)) |
| 445 | return false; |
| 446 | if (o_final) |
| 447 | v_action (xpath_primary_expr, xpath_primary_expr_function_call); |
| 448 | break; |
| 449 | } |
| 450 | break; |
| 451 | |
| 452 | case xpath_function_call : |
| 453 | // [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument )* )? ')' |
| 454 | // [35] FunctionName ::= QName - NodeType |
| 455 | if (! ltp_get (0)) |
| 456 | return false; |
| 457 | switch (ltp_get (0) -> lex_get_value ()) |
| 458 | { |
| 459 | case lex_comment : |
| 460 | case lex_text : |
| 461 | case lex_node : |
| 462 | case lex_processing_instruction : |
| 463 | if (o_final) |
| 464 | { |
| 465 | v_action (xpath_xml_local_part, 0, 0, ltp_get (0) -> cp_get_literal ()); |
| 466 | v_action (xpath_xml_q_name, xpath_xml_q_name_simple); |
| 467 | } |
| 468 | v_inc_current (1); |
| 469 | break; |
| 470 | default : |
| 471 | if (! o_recognize (xpath_xml_q_name, o_final)) |
| 472 | return false; |
| 473 | } |
| 474 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_oparen) |
| 475 | return false; |
| 476 | v_inc_current (1); |
| 477 | if (! ltp_get (0)) |
| 478 | return false; |
| 479 | if (ltp_get (0) -> lex_get_value () == lex_cparen) |
| 480 | { |
| 481 | v_inc_current (1); |
| 482 | if (o_final) |
| 483 | v_action (xpath_function_call, 0); |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | ltp_freeze = ltp_get (0); |
| 488 | o_found = true; |
| 489 | u_nb_argument = 0; |
| 490 | while (o_found) |
| 491 | { |
| 492 | if (u_nb_argument) |
| 493 | { |
| 494 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_comma) |
| 495 | o_found = false; |
| 496 | else |
| 497 | v_inc_current (1); |
| 498 | } |
| 499 | if (o_found) |
| 500 | { |
| 501 | if (! o_recognize (xpath_argument, o_final)) |
| 502 | o_found = false; |
| 503 | else |
| 504 | { |
| 505 | u_nb_argument++; |
| 506 | ltp_freeze = ltp_get (0); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | v_set_current (ltp_freeze); |
| 511 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_cparen) |
| 512 | return false; |
| 513 | v_inc_current (1); |
| 514 | if (o_final) |
| 515 | v_action (xpath_function_call, 1, u_nb_argument); |
| 516 | } |
| 517 | break; |
| 518 | |
| 519 | case xpath_argument : |
| 520 | // [17] Argument ::= Expr |
| 521 | if (! o_recognize (xpath_expr, o_final)) |
| 522 | return false; |
| 523 | if (o_final) |
| 524 | v_action (xpath_argument, 0); |
| 525 | break; |
| 526 | |
| 527 | case xpath_union_expr : |
| 528 | // |
| 529 | // [18] UnionExpr ::= PathExpr |
| 530 | // | UnionExpr '|' PathExpr |
| 531 | // |
| 532 | if (! o_recognize (xpath_path_expr, o_final)) |
| 533 | return false; |
| 534 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_orchar) |
| 535 | { |
| 536 | v_inc_current (1); |
| 537 | if (! o_recognize (xpath_union_expr, o_final)) |
| 538 | return false; |
| 539 | if (o_final) |
| 540 | v_action (xpath_union_expr, xpath_union_expr_union); |
| 541 | } |
| 542 | else |
| 543 | if (o_final) |
| 544 | v_action (xpath_union_expr, xpath_union_expr_simple); |
| 545 | break; |
| 546 | |
| 547 | case xpath_path_expr : |
| 548 | // |
| 549 | // [19] PathExpr ::= LocationPath |
| 550 | // | FilterExpr |
| 551 | // | FilterExpr '/' RelativeLocationPath |
| 552 | // | FilterExpr '//' RelativeLocationPath |
| 553 | // |
| 554 | ltp_freeze = ltp_get (0); |
| 555 | o_location_path = false; |
| 556 | |
| 557 | if (! o_recognize (xpath_filter_expr, false)) |
| 558 | o_location_path = true; |
| 559 | else |
| 560 | { |
| 561 | v_set_current (ltp_freeze); |
| 562 | o_recognize (xpath_filter_expr, o_final); |
| 563 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_slash) |
| 564 | { |
| 565 | v_inc_current (1); |
| 566 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 567 | o_location_path = true; |
| 568 | else |
| 569 | if (o_final) |
| 570 | v_action (xpath_path_expr, xpath_path_expr_slash); |
| 571 | } |
| 572 | else |
| 573 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_2_slash) |
| 574 | { |
| 575 | v_inc_current (1); |
| 576 | if (! o_recognize (xpath_relative_location_path, o_final)) |
| 577 | o_location_path = true; |
| 578 | else |
| 579 | if (o_final) |
| 580 | v_action (xpath_path_expr, xpath_path_expr_2_slash); |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | if (o_final) |
| 585 | v_action (xpath_path_expr, xpath_path_expr_filter); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (o_location_path) |
| 590 | { |
| 591 | v_set_current (ltp_freeze); |
| 592 | if (! o_recognize (xpath_location_path, o_final)) |
| 593 | return false; |
| 594 | if (o_final) |
| 595 | v_action (xpath_path_expr, xpath_path_expr_location_path); |
| 596 | } |
| 597 | break; |
| 598 | |
| 599 | case xpath_filter_expr : |
| 600 | // |
| 601 | // [20] FilterExpr ::= PrimaryExpr |
| 602 | // | FilterExpr Predicate |
| 603 | // |
| 604 | if (! o_recognize (xpath_primary_expr, o_final)) |
| 605 | return false; |
| 606 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_obrack) |
| 607 | { |
| 608 | if (! o_recognize (xpath_predicate, o_final)) |
| 609 | return false; |
| 610 | if (o_final) |
| 611 | v_action (xpath_filter_expr, xpath_filter_expr_predicate); |
| 612 | } |
| 613 | else |
| 614 | if (o_final) |
| 615 | v_action (xpath_filter_expr, xpath_filter_expr_primary); |
| 616 | break; |
| 617 | |
| 618 | case xpath_or_expr : |
| 619 | // |
| 620 | // [21] OrExpr ::= AndExpr |
| 621 | // | OrExpr 'or' AndExpr |
| 622 | // |
| 623 | if (! o_recognize (xpath_and_expr, o_final)) |
| 624 | return false; |
| 625 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_or) |
| 626 | { |
| 627 | v_inc_current (1); |
| 628 | if (! o_recognize (xpath_and_expr, o_final)) |
| 629 | return false; |
| 630 | if (o_final) |
| 631 | v_action (xpath_or_expr, xpath_or_expr_or); |
| 632 | } |
| 633 | else |
| 634 | if (o_final) |
| 635 | v_action (xpath_or_expr, xpath_or_expr_simple); |
| 636 | break; |
| 637 | |
| 638 | case xpath_and_expr : |
| 639 | // |
| 640 | // [22] AndExpr ::= EqualityExpr |
| 641 | // | AndExpr 'and' EqualityExpr |
| 642 | // |
| 643 | if (! o_recognize (xpath_equality_expr, o_final)) |
| 644 | return false; |
| 645 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_and) |
| 646 | { |
| 647 | v_inc_current (1); |
| 648 | if (! o_recognize (xpath_equality_expr, o_final)) |
| 649 | return false; |
| 650 | if (o_final) |
| 651 | v_action (xpath_and_expr, xpath_and_expr_and); |
| 652 | } |
| 653 | else |
| 654 | if (o_final) |
| 655 | v_action (xpath_and_expr, xpath_and_expr_simple); |
| 656 | break; |
| 657 | |
| 658 | case xpath_equality_expr : |
| 659 | // |
| 660 | // [23] EqualityExpr ::= RelationalExpr |
| 661 | // | EqualityExpr '=' RelationalExpr |
| 662 | // | EqualityExpr '!=' RelationalExpr |
| 663 | // |
| 664 | if (! o_recognize (xpath_relational_expr, o_final)) |
| 665 | return false; |
| 666 | if (ltp_get (0)) |
| 667 | { |
| 668 | switch (ltp_get (0) -> lex_get_value ()) |
| 669 | { |
| 670 | case lex_equal : |
| 671 | v_inc_current (1); |
| 672 | if (! o_recognize (xpath_relational_expr, o_final)) |
| 673 | return false; |
| 674 | if (o_final) |
| 675 | v_action (xpath_equality_expr, xpath_equality_expr_equal); |
| 676 | break; |
| 677 | |
| 678 | case lex_not_equal : |
| 679 | v_inc_current (1); |
| 680 | if (! o_recognize (xpath_relational_expr, o_final)) |
| 681 | return false; |
| 682 | if (o_final) |
| 683 | v_action (xpath_equality_expr, xpath_equality_expr_not_equal); |
| 684 | break; |
| 685 | |
| 686 | default : |
| 687 | if (o_final) |
| 688 | v_action (xpath_equality_expr, xpath_equality_expr_simple); |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | else |
| 693 | { |
| 694 | if (o_final) |
| 695 | v_action (xpath_equality_expr, xpath_equality_expr_simple); |
| 696 | } |
| 697 | break; |
| 698 | |
| 699 | case xpath_relational_expr : |
| 700 | // |
| 701 | // [24] RelationalExpr ::= AdditiveExpr |
| 702 | // | RelationalExpr '<' AdditiveExpr |
| 703 | // | RelationalExpr '>' AdditiveExpr |
| 704 | // | RelationalExpr '<=' AdditiveExpr |
| 705 | // | RelationalExpr '>=' AdditiveExpr |
| 706 | // |
| 707 | if (! o_recognize (xpath_additive_expr, o_final)) |
| 708 | return false; |
| 709 | if (ltp_get (0)) |
| 710 | { |
| 711 | switch (ltp_get (0) -> lex_get_value ()) |
| 712 | { |
| 713 | case lex_lt : |
| 714 | v_inc_current (1); |
| 715 | if (! o_recognize (xpath_additive_expr, o_final)) |
| 716 | return false; |
| 717 | if (o_final) |
| 718 | v_action (xpath_relational_expr, xpath_relational_expr_lt); |
| 719 | break; |
| 720 | case lex_gt : |
| 721 | v_inc_current (1); |
| 722 | if (! o_recognize (xpath_additive_expr, o_final)) |
| 723 | return false; |
| 724 | if (o_final) |
| 725 | v_action (xpath_relational_expr, xpath_relational_expr_gt); |
| 726 | break; |
| 727 | case lex_lt_equal : |
| 728 | v_inc_current (1); |
| 729 | if (! o_recognize (xpath_additive_expr, o_final)) |
| 730 | return false; |
| 731 | if (o_final) |
| 732 | v_action (xpath_relational_expr, xpath_relational_expr_lte); |
| 733 | break; |
| 734 | case lex_gt_equal : |
| 735 | v_inc_current (1); |
| 736 | if (! o_recognize (xpath_additive_expr, o_final)) |
| 737 | return false; |
| 738 | if (o_final) |
| 739 | v_action (xpath_relational_expr, xpath_relational_expr_gte); |
| 740 | break; |
| 741 | default : |
| 742 | if (o_final) |
| 743 | v_action (xpath_relational_expr, xpath_relational_expr_simple); |
| 744 | break; |
| 745 | } // switch |
| 746 | } |
| 747 | else |
| 748 | if (o_final) |
| 749 | v_action (xpath_relational_expr, xpath_relational_expr_simple); |
| 750 | break; |
| 751 | |
| 752 | case xpath_additive_expr : |
| 753 | // |
| 754 | // [25] AdditiveExpr ::= MultiplicativeExpr |
| 755 | // | AdditiveExpr '+' MultiplicativeExpr |
| 756 | // | AdditiveExpr '-' MultiplicativeExpr |
| 757 | // |
| 758 | if (! o_recognize (xpath_multiplicative_expr, o_final)) |
| 759 | return false; |
| 760 | o_test_more = false; |
| 761 | if (ltp_get (0)) |
| 762 | { |
| 763 | switch (ltp_get (0) -> lex_get_value ()) |
| 764 | { |
| 765 | case lex_plus : |
| 766 | v_inc_current (1); |
| 767 | if (! o_recognize (xpath_multiplicative_expr, o_final)) |
| 768 | return false; |
| 769 | if (o_final) |
| 770 | v_action (xpath_additive_expr, xpath_additive_expr_plus); |
| 771 | o_test_more = true; |
| 772 | break; |
| 773 | case lex_minus : |
| 774 | v_inc_current (1); |
| 775 | if (! o_recognize (xpath_multiplicative_expr, o_final)) |
| 776 | return false; |
| 777 | if (o_final) |
| 778 | v_action (xpath_additive_expr, xpath_additive_expr_minus); |
| 779 | o_test_more = true; |
| 780 | break; |
| 781 | default : |
| 782 | if (o_final) |
| 783 | v_action (xpath_additive_expr, xpath_additive_expr_simple); |
| 784 | break; |
| 785 | } // switch |
| 786 | } |
| 787 | else |
| 788 | if (o_final) |
| 789 | v_action (xpath_additive_expr, xpath_additive_expr_simple); |
| 790 | if (o_test_more) |
| 791 | { |
| 792 | while (ltp_get (0) && (ltp_get (0) -> lex_get_value () == lex_plus || ltp_get (0) -> lex_get_value () == lex_minus)) |
| 793 | { |
| 794 | if (ltp_get (0) -> lex_get_value () == lex_plus) |
| 795 | { |
| 796 | v_inc_current (1); |
| 797 | if (! o_recognize (xpath_multiplicative_expr, o_final)) |
| 798 | return false; |
| 799 | if (o_final) |
| 800 | v_action (xpath_additive_expr, xpath_additive_expr_more_plus); |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | v_inc_current (1); |
| 805 | if (! o_recognize (xpath_multiplicative_expr, o_final)) |
| 806 | return false; |
| 807 | if (o_final) |
| 808 | v_action (xpath_additive_expr, xpath_additive_expr_more_minus); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | break; |
| 813 | |
| 814 | case xpath_multiplicative_expr : |
| 815 | // |
| 816 | // [26] MultiplicativeExpr ::= UnaryExpr |
| 817 | // | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 818 | // | MultiplicativeExpr 'div' UnaryExpr |
| 819 | // | MultiplicativeExpr 'mod' UnaryExpr |
| 820 | // |
| 821 | if (! o_recognize (xpath_unary_expr, o_final)) |
| 822 | return false; |
| 823 | if (ltp_get (0)) |
| 824 | { |
| 825 | switch (ltp_get (0) -> lex_get_value ()) |
| 826 | { |
| 827 | case lex_star : |
| 828 | // |
| 829 | // [34] MultiplyOperator ::= '*' |
| 830 | // |
| 831 | v_inc_current (1); |
| 832 | if (! o_recognize (xpath_unary_expr, o_final)) |
| 833 | return false; |
| 834 | if (o_final) |
| 835 | v_action (xpath_multiplicative_expr, xpath_multiplicative_expr_star); |
| 836 | break; |
| 837 | case lex_div : |
| 838 | v_inc_current (1); |
| 839 | if (! o_recognize (xpath_unary_expr, o_final)) |
| 840 | return false; |
| 841 | if (o_final) |
| 842 | v_action (xpath_multiplicative_expr, xpath_multiplicative_expr_div); |
| 843 | break; |
| 844 | case lex_mod : |
| 845 | v_inc_current (1); |
| 846 | if (! o_recognize (xpath_unary_expr, o_final)) |
| 847 | return false; |
| 848 | if (o_final) |
| 849 | v_action (xpath_multiplicative_expr, xpath_multiplicative_expr_mod); |
| 850 | break; |
| 851 | default : |
| 852 | if (o_final) |
| 853 | v_action (xpath_multiplicative_expr, xpath_multiplicative_expr_simple); |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | else |
| 858 | if (o_final) |
| 859 | v_action (xpath_multiplicative_expr, xpath_multiplicative_expr_simple); |
| 860 | break; |
| 861 | |
| 862 | case xpath_unary_expr : |
| 863 | // |
| 864 | // [27] UnaryExpr ::= UnionExpr |
| 865 | // | '-' UnaryExpr |
| 866 | // |
| 867 | if (ltp_get (0) && ltp_get (0) -> lex_get_value () == lex_minus) |
| 868 | { |
| 869 | v_inc_current (1); |
| 870 | if (! o_recognize (xpath_unary_expr, o_final)) |
| 871 | return false; |
| 872 | if (o_final) |
| 873 | v_action (xpath_unary_expr, xpath_unary_expr_minus); |
| 874 | } |
| 875 | else |
| 876 | { |
| 877 | if (! o_recognize (xpath_union_expr, o_final)) |
| 878 | return false; |
| 879 | if (o_final) |
| 880 | v_action (xpath_unary_expr, xpath_unary_expr_simple); |
| 881 | } |
| 882 | break; |
| 883 | |
| 884 | case xpath_variable_reference : |
| 885 | // [36] VariableReference ::= '$' QName |
| 886 | if (! ltp_get (0) || ! ltp_get (1)) |
| 887 | return false; |
| 888 | if (ltp_get (0) -> lex_get_value () != lex_dollar) |
| 889 | return false; |
| 890 | v_inc_current (1); |
| 891 | if (! o_recognize (xpath_xml_q_name, o_final)) |
| 892 | return false; |
| 893 | if (o_final) |
| 894 | v_action (xpath_variable_reference, 0); |
| 895 | break; |
| 896 | |
| 897 | case xpath_name_test : |
| 898 | // [37] NameTest ::= '*' |
| 899 | // | NCName ':' '*' |
| 900 | // | QName |
| 901 | if (! ltp_get (0)) |
| 902 | return false; |
| 903 | switch (ltp_get (0) -> lex_get_value ()) |
| 904 | { |
| 905 | case lex_star : |
| 906 | v_inc_current (1); |
| 907 | if (o_final) |
| 908 | v_action (xpath_name_test, xpath_name_test_star); |
| 909 | break; |
| 910 | case lex_ncname : |
| 911 | o_qname = false; |
| 912 | if (ltp_get (1) && ltp_get (2) && |
| 913 | ltp_get (1) -> lex_get_value () == lex_colon) |
| 914 | { |
| 915 | if (ltp_get (2) -> lex_get_value () == lex_star) |
| 916 | { |
| 917 | v_inc_current (3); |
| 918 | if (o_final) |
| 919 | v_action (xpath_name_test, xpath_name_test_ncname); |
| 920 | } |
| 921 | else |
| 922 | o_qname = true; |
| 923 | } |
| 924 | else |
| 925 | o_qname = true; |
| 926 | if (o_qname) |
| 927 | { |
| 928 | if (! o_recognize (xpath_xml_q_name, o_final)) |
| 929 | return false; |
| 930 | if (o_final) |
| 931 | v_action (xpath_name_test, xpath_name_test_qname); |
| 932 | } |
| 933 | break; |
| 934 | |
| 935 | // jan 21th, 07 - neoneurone: silently ignore the other cases |
| 936 | default: |
| 937 | break; |
| 938 | } |
| 939 | break; |
| 940 | |
| 941 | case xpath_xml_q_name : |
| 942 | // [Namespace XML : 6] QName ::= (Prefix ':')? LocalPart |
| 943 | // [Namespace XML : 7] Prefix ::= NCName |
| 944 | // [Namespace XML : 8] LocalPart ::= NCName |
| 945 | if (! ltp_get (0) || ltp_get (0) -> lex_get_value () != lex_ncname) |
| 946 | return false; |
| 947 | if (ltp_get (1) && ltp_get (1) -> lex_get_value () == lex_colon) |
| 948 | { |
| 949 | if (! ltp_get (2) || ltp_get (2) -> lex_get_value () != lex_ncname) |
| 950 | return false; |
| 951 | if (o_final) |
| 952 | { |
| 953 | v_action (xpath_xml_prefix, 0, 0, ltp_get (0) -> cp_get_literal ()); |
| 954 | v_action (xpath_xml_local_part, 0, 0, ltp_get (2) -> cp_get_literal ()); |
| 955 | v_action (xpath_xml_q_name, xpath_xml_q_name_colon); |
| 956 | } |
| 957 | v_inc_current (3); |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | if (o_final) |
| 962 | { |
| 963 | v_action (xpath_xml_local_part, 0, 0, ltp_get (0) -> cp_get_literal ()); |
| 964 | v_action (xpath_xml_q_name, xpath_xml_q_name_simple); |
| 965 | } |
| 966 | v_inc_current (1); |
| 967 | } |
| 968 | break; |
| 969 | |
| 970 | default : |
| 971 | if (o_final) |
| 972 | v_action (xpath_unknown, 0); |
| 973 | return false; |
| 974 | break; |
| 975 | } |
| 976 | return true; |
| 977 | } // v_recognize () |
| 978 | |
| 979 | } |