1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "tokenizer_utils.h" 22std::string get_text_token(Tokenizer_Wrapper& token, Error_Output* error_output, 23 std::string type_of_token) 258544569 q std::string result = ""; 268544569 q bool result_valid = true; 288544569 q if (!token.good() || (*token).size() == 0) 290 result_valid = false; 308544569 q else if ((*token)[0] == '"' || (*token)[0] == '\'') 325524514 q std::string::size_type start = 1; 335524514 q std::string::size_type pos = (*token).find('\\'); 345904514 q while (pos != std::string::npos) 3638021 q result += (*token).substr(start, pos - start); 3738021 q if ((*token)[pos + 1] == 'n') 3938021 q else if ((*token)[pos + 1] == 't') 4138021 q else if ((*token)[pos + 1] == 'u') 430 result += decode_to_utf8(*token, pos, error_output); 4738021 q result += (*token)[pos + 1]; 4938021 q pos = (*token).find('\\', start); 515524514 q result += (*token).substr(start, (*token).size() - start - 1); 533020305 q else if (isalpha((*token)[0]) || isdigit((*token)[0]) || (*token)[0] == '_') 55122 q else if ((*token)[0] == '-' && (*token).size() > 1 && isdigit((*token)[1])) 580 result_valid = false; 650 error_output->add_parse_error(type_of_token + " expected - '" + *token + "' found.", token.line_col().first); 72std::string get_identifier_token(Tokenizer_Wrapper& token, Error_Output* error_output, 73 std::string type_of_token) 759680657 q std::string result = ""; 769680657 q bool result_valid = true; 789680657 q if (!token.good() || (*token).size() == 0) 790 result_valid = false; 809680657 q else if (isalpha((*token)[0]) || (*token)[0] == '_') 830 result_valid = false; 900 error_output->add_parse_error(type_of_token + " expected - '" + *token + "' found.", token.line_col().first); 97void process_after(Tokenizer_Wrapper& token, Error_Output* error_output, bool after) 1020 error_output->add_parse_error("Unexpected end of input.", token.line_col().first); 109void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output, 110 std::string target_1, bool after) 1150 error_output->add_parse_error(std::string("'") + target_1 + "' expected - '" 1160 + *token + "' found.", token.line_col().first); 11943200683 q while (token.good() && *token != target_1) 12143200683 q process_after(token, error_output, after); 125void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output, 126 std::string target_1, std::string target_2, bool after) 1282384368 q if (*token != target_1 && *token != target_2) 1310 error_output->add_parse_error 1320 (std::string("'") + target_1 + "' or '" + target_2 + "' expected - '" 1330 + *token + "' found.", token.line_col().first); 1362384368 q while (token.good() && *token != target_1 && *token != target_2) 1382384368 q process_after(token, error_output, after); 142void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output, 143 std::string target_1, std::string target_2, std::string target_3, bool after) 14541268 q if (*token != target_1 && *token != target_2 && *token != target_3) 1480 error_output->add_parse_error 1490 (std::string("'") + target_1 + "', '" + target_2 + "', or '" + target_3 + "' expected - '" 1500 + *token + "' found.", token.line_col().first); 15341268 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3) 15541268 q process_after(token, error_output, after); 159void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output, 160 std::string target_1, std::string target_2, std::string target_3, std::string target_4, 1630 if (*token != target_1 && *token != target_2 && *token != target_3 && *token != target_4) 1660 error_output->add_parse_error 1670 (std::string("'") + target_1 + "', '" + target_2 + "', '" + target_3 + "', or '" 1680 + target_4 + "' expected - '" 1690 + *token + "' found.", token.line_col().first); 1720 while (token.good() && *token != target_1 && *token != target_2 && *token != target_3 1730 && *token != target_4) 1750 process_after(token, error_output, after); 179void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output, 180 std::string target_1, std::string target_2, std::string target_3, std::string target_4, 181 std::string target_5, bool after) 1832268368 q if (*token != target_1 && *token != target_2 && *token != target_3 1842268368 q && *token != target_4 && *token != target_5) 1870 error_output->add_parse_error 1880 (std::string("'") + target_1 + "', '" + target_2 + "', '" + target_3+ "', '" + target_4 1890 + "', or '" + target_5 + "' expected - '" 1900 + *token + "' found.", token.line_col().first); 1932268368 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3 1942268368 q && *token != target_4 && *token != target_5) 1962268368 q process_after(token, error_output, after); 200int operator_priority(const std::string& operator_name, bool unary) 20254542605 q static std::map< std::string, int > priority; 23154366605 q std::map< std::string, int >::const_iterator prio_it = priority.find(operator_name); 23254366605 q if (prio_it != priority.end()) 239Token_Tree::Token_Tree(Tokenizer_Wrapper& token, Error_Output* error_output, bool parenthesis_expected) 2414564605 q tree.push_back(Token_Node("", std::make_pair(0u, 0u))); 24563640605 q while (token.good() && *token != ";" && *token != "->") 24763008605 q if (*token == "(" || *token == "[" || *token == "{") 24910112224 q tree.push_back(Token_Node(*token, token.line_col())); 25010112224 q tree.back().lhs = tree[stack.back()].rhs; 25110112224 q tree[stack.back()].rhs = tree.size()-1; 25210112224 q stack.push_back(tree.size()-1); 25452896605 q else if (*token == ")" || *token == "]" ||*token == "}") 25614040604 q int stack_pos = stack.size()-1; 25724784604 q while (stack_pos >= 0 && tree[stack[stack_pos]].token != "(" 25824784604 q && tree[stack[stack_pos]].token != "[" && tree[stack[stack_pos]].token != "{") 2650 error_output->add_parse_error(std::string("Unmatched ") + *token, token.line_col().first); 27238856605 q int prio = operator_priority(*token, false); 27438856605 q if ((*token).size() >= 2 && (*token)[0] == '-' && tree[stack.back()].rhs) 28215228576 q if (stack.back() < tree.size()) 28415228576 q uint stack_pos = stack.size()-1; 28713336463 q if (operator_priority(tree[stack[stack_pos]].token, false) < prio) 29115228576 q stack.resize(stack_pos+1); 29315228576 q tree.push_back(Token_Node(unary_minus ? "-" : *token, token.line_col())); 29415228576 q tree.back().lhs = tree[stack.back()].rhs; 29515228576 q tree[stack.back()].rhs = tree.size()-1; 29615228576 q stack.push_back(tree.size()-1); 300246 q tree.push_back(Token_Node((*token).substr(1), token.line_col())); 301246 q tree[stack.back()].rhs = tree.size()-1; 30423628604 q else if (!tree[stack.back()].rhs) 30623628604 q tree.push_back(Token_Node(*token, token.line_col())); 30723628604 q tree[stack.back()].rhs = tree.size()-1; 3100 error_output->add_parse_error(std::string("Operator expected, but \"") + *token + "\"found.", 3110 token.line_col().first); 316632127 q int stack_pos = stack.size()-1; 3172592127 q while (stack_pos >= 0 && tree[stack[stack_pos]].token != "(" 3182592127 q && tree[stack[stack_pos]].token != "[" && tree[stack[stack_pos]].token != "{") 32141 q error_output->add_parse_error(std::string("Left ") + tree[stack[stack_pos]].token 32241 q + " not closed.", token.line_col().first); 326uint Token_Tree::add_tree_node(const Token_Node_Ptr& root, const Token_Node_Ptr& exclude) 32982 q return root->rhs ? add_tree_node(root.rhs(), exclude) : 0u; 332242 q Token_Node vertex(root->token, root->line_col); 333242 q vertex.lhs = root->lhs ? add_tree_node(root.lhs(), exclude) : 0u; 334242 q vertex.rhs = root->rhs ? add_tree_node(root.rhs(), exclude) : 0u; 335242 q tree.push_back(vertex); 336242 q return tree.size()-1; 342Token_Tree::Token_Tree(const Token_Node_Ptr& root, const Token_Node_Ptr& exclude) 34482 q tree.push_back(Token_Node("", std::make_pair(0u, 0u))); 34582 q uint result = add_tree_node(root, exclude); 350const std::string* Token_Node_Ptr::function_name() const 35245286 q if (tree && pos < tree->tree.size()) 35445286 q if (operator*().token == "(") 3600 return &rhs().lhs()->token; 367bool Token_Node_Ptr::assert_is_function(Error_Output* error_output) const 3695588223 q if (tree && pos < tree->tree.size()) 3715588223 q if (operator*().token == "(") 3760 error_output->add_parse_error("Function expected, but no function name found", 3770 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 3815448221 q Token_Node_Ptr lhs_ = lhs(); 3825448221 q if (lhs_->lhs || lhs_->rhs) 3850 error_output->add_parse_error("Function name expected, compound expression found", 3860 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 39214027 q else if (operator*().token == ".") 3970 error_output->add_parse_error("Function expected, but token right of \".\" is void", 3980 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 40214027 q Token_Node_Ptr rhs_ = rhs(); 4060 error_output->add_parse_error(std::string("Function left parenthesis expected, but ") 4070 + "\"" + rhs_->token + "\" found.", rhs_->line_col.first); 4140 error_output->add_parse_error("Function expected, but no function name found", 4150 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 41914027 q Token_Node_Ptr lhs_ = rhs_.lhs(); 42014027 q if (lhs_->lhs || lhs_->rhs) 4230 error_output->add_parse_error("Function name expected, compound expression found", 4240 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 4330 error_output->add_parse_error(std::string("Function expected, but ") 4340 + (tree && pos < tree->tree.size() ? 4350 "\"" + operator*().token + "\"" : "void token") + " found.", 4360 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 442bool Token_Node_Ptr::assert_has_input_set(Error_Output* error_output, bool expected) const 444 // We assume that this is a function to avoid double checking 4465436221 q if (operator*().token == "(") 4510 error_output->add_parse_error("Input set expected, but only function name found", 4520 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 4580 else if (operator*().token == ".") 4600 if (!operator*().lhs) 4630 error_output->add_parse_error("Input set expected, but token left of \".\" is void", 4640 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 4700 Token_Node_Ptr lhs_ = lhs(); 4710 if (lhs_->lhs || lhs_->rhs) 4740 error_output->add_parse_error("Input set expected, compound expression found", 4750 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 4820 const std::string* func_name = function_name(); 4840 error_output->add_parse_error((func_name ? *func_name + "(...)" : "Void function") + " cannot have an input set", 4850 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 494bool Token_Node_Ptr::assert_has_arguments(Error_Output* error_output, bool expected) const 496 // We assume that this is a function to avoid double checking 4985296222 q bool has_arguments = operator*().rhs; 4995296222 q if (operator*().token == ".") 50014027 q has_arguments = rhs()->rhs; 50782 q const std::string* func_name = function_name(); 50982 q error_output->add_parse_error((func_name ? *func_name + "(...)" : "Void function") 51082 q + " does not accept any arguments", 51182 q (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 519123 q const std::string* func_name = function_name(); 521123 q error_output->add_parse_error((func_name ? *func_name + "(...)" : "Void function") 522123 q + " must have one or more arguments", 523123 q (tree && pos < tree->tree.size() ? operator*().line_col.first : 0)); 529Token_Node_Ptr find_leftmost_token(Token_Node_Ptr tree_it) 538Token_Node_Ptr find_leftmost_but_one_token(Token_Node_Ptr tree_it) 540162 q while (tree_it->lhs && tree_it.lhs()->lhs) 54182 q tree_it = tree_it.lhs();