From d72c0763f9dc6298b34eef7d17a14ea76e7c1016 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 29 Jan 2017 20:15:14 -0800 Subject: XPath: Minor parsing refactoring Simplify function argument parsing by folding arg 0 parsing into the main loop, reuse expression parsing logic for unary expression --- src/pugixml.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/pugixml.cpp') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 0818148..906ec04 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -11236,6 +11236,7 @@ PUGI__NS_BEGIN const char_t* value = alloc_string(_lexer.contents()); xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_string_constant, xpath_type_string, value); + _lexer.next(); return n; @@ -11249,6 +11250,7 @@ PUGI__NS_BEGIN throw_error_oom(); xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_number_constant, xpath_type_number, value); + _lexer.next(); return n; @@ -11268,14 +11270,14 @@ PUGI__NS_BEGIN throw_error("Unrecognized function call"); _lexer.next(); - if (_lexer.current() != lex_close_brace) - args[argc++] = parse_expression(); - while (_lexer.current() != lex_close_brace) { - if (_lexer.current() != lex_comma) - throw_error("No comma between function arguments"); - _lexer.next(); + if (argc > 0) + { + if (_lexer.current() != lex_comma) + throw_error("No comma between function arguments"); + _lexer.next(); + } xpath_ast_node* n = parse_expression(); @@ -11561,7 +11563,8 @@ PUGI__NS_BEGIN while (PUGI__IS_CHARTYPE(*state, ct_space)) ++state; - if (*state != '(') return parse_location_path(); + if (*state != '(') + return parse_location_path(); // This looks like a function call; however this still can be a node-test. Check it. if (parse_node_test_type(_lexer.contents()) != nodetest_none) @@ -11594,9 +11597,9 @@ PUGI__NS_BEGIN _lexer.next(); // precedence 7+ - only parses union expressions - xpath_ast_node* expr = parse_expression_rec(parse_path_or_unary_expression(), 7); + xpath_ast_node* n = parse_expression(7); - return new (alloc_node()) xpath_ast_node(ast_op_negate, xpath_type_number, expr); + return new (alloc_node()) xpath_ast_node(ast_op_negate, xpath_type_number, n); } else { @@ -11718,9 +11721,11 @@ PUGI__NS_BEGIN // | MultiplicativeExpr '*' UnaryExpr // | MultiplicativeExpr 'div' UnaryExpr // | MultiplicativeExpr 'mod' UnaryExpr - xpath_ast_node* parse_expression() + xpath_ast_node* parse_expression(int limit = 0) { - return parse_expression_rec(parse_path_or_unary_expression(), 0); + xpath_ast_node* n = parse_path_or_unary_expression(); + + return parse_expression_rec(n, limit); } xpath_parser(const char_t* query, xpath_variable_set* variables, xpath_allocator* alloc, xpath_parse_result* result): _alloc(alloc), _lexer(query), _query(query), _variables(variables), _result(result) -- cgit v1.2.3