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) 257408498 q std::string result = ""; 267408498 q bool result_valid = true; 287408498 q if (!token.good() || (*token).size() == 0) 290 result_valid = false; 307408498 q else if ((*token)[0] == '"' || (*token)[0] == '\'') 324948456 q std::string::size_type start = 1; 334948456 q std::string::size_type pos = (*token).find('\\'); 345160456 q while (pos != std::string::npos) 3621219 q result += (*token).substr(start, pos - start); 3721219 q if ((*token)[pos + 1] == 'n') 3921219 q else if ((*token)[pos + 1] == 't') 4121219 q else if ((*token)[pos + 1] == 'u') 430 result += decode_to_utf8(*token, pos, error_output); 4721219 q result += (*token)[pos + 1]; 4921219 q pos = (*token).find('\\', start); 514948456 q result += (*token).substr(start, (*token).size() - start - 1); 532460260 q else if (isalpha((*token)[0]) || isdigit((*token)[0]) || (*token)[0] == '_') 5552071 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) 758572584 q std::string result = ""; 768572584 q bool result_valid = true; 788572584 q if (!token.good() || (*token).size() == 0) 790 result_valid = false; 808572584 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); 11937864603 q while (token.good() && *token != target_1) 12137864603 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) 1282072322 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); 1362072322 q while (token.good() && *token != target_1 && *token != target_2) 1382072322 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) 14532457 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); 15332457 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3) 15532457 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) 1832084331 q if (*token != target_1 && *token != target_2 && *token != target_3 1842084331 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); 1932084331 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3 1942084331 q && *token != target_4 && *token != target_5) 1962084331 q process_after(token, error_output, after); 200int operator_priority(const std::string& operator_name, bool unary) 20248892532 q static std::map< std::string, int > priority; 23148776532 q std::map< std::string, int >::const_iterator prio_it = priority.find(operator_name); 23248776532 q if (prio_it != priority.end()) 239Token_Tree::Token_Tree(Tokenizer_Wrapper& token, Error_Output* error_output, bool parenthesis_expected) 2413908532 q tree.push_back(Token_Node("", std::make_pair(0u, 0u))); 24557644532 q while (token.good() && *token != ";" && *token != "->") 24757068532 q if (*token == "(" || *token == "[" || *token == "{") 2499440206 q tree.push_back(Token_Node(*token, token.line_col())); 2509440206 q tree.back().lhs = tree[stack.back()].rhs; 2519440206 q tree[stack.back()].rhs = tree.size()-1; 2529440206 q stack.push_back(tree.size()-1); 25447628532 q else if (*token == ")" || *token == "]" ||*token == "}") 25612768531 q int stack_pos = stack.size()-1; 25722396531 q while (stack_pos >= 0 && tree[stack[stack_pos]].token != "(" 25822396531 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); 27234860532 q int prio = operator_priority(*token, false); 27434860532 q if ((*token).size() >= 2 && (*token)[0] == '-' && tree[stack.back()].rhs) 28213576510 q if (stack.back() < tree.size()) 28413576510 q uint stack_pos = stack.size()-1; 28711872415 q if (operator_priority(tree[stack[stack_pos]].token, false) < prio) 29113576510 q stack.resize(stack_pos+1); 29313576510 q tree.push_back(Token_Node(unary_minus ? "-" : *token, token.line_col())); 29413576510 q tree.back().lhs = tree[stack.back()].rhs; 29513576510 q tree[stack.back()].rhs = tree.size()-1; 29613576510 q stack.push_back(tree.size()-1); 300287 q tree.push_back(Token_Node((*token).substr(1), token.line_col())); 301287 q tree[stack.back()].rhs = tree.size()-1; 30421284531 q else if (!tree[stack.back()].rhs) 30621284531 q tree.push_back(Token_Node(*token, token.line_col())); 30721284531 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); 316576118 q int stack_pos = stack.size()-1; 3172348118 q while (stack_pos >= 0 && tree[stack[stack_pos]].token != "(" 3182348118 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 35237675 q if (tree && pos < tree->tree.size()) 35437675 q if (operator*().token == "(") 3600 return &rhs().lhs()->token; 367bool Token_Node_Ptr::assert_is_function(Error_Output* error_output) const 3695248202 q if (tree && pos < tree->tree.size()) 3715248202 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)); 3815104200 q Token_Node_Ptr lhs_ = lhs(); 3825104200 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)); 39214425 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)); 40214425 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)); 41914425 q Token_Node_Ptr lhs_ = rhs_.lhs(); 42014425 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 4465096200 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 4985036201 q bool has_arguments = operator*().rhs; 4995036201 q if (operator*().token == ".") 50014425 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();