Overpass API regression suite coverage (Saxony) / overpass_api/frontend/tokenizer_utils.cc

overpass_api/frontend/tokenizer_utils.cc

268 of 428 instrumented lines run by the suite (62.6%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
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.
9 *
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.
14 *
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/>.
17 */
19#include "tokenizer_utils.h"
22std::string get_text_token(Tokenizer_Wrapper& token, Error_Output* error_output,
23 std::string type_of_token)
248544569 q{
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] == '\'')
315524514 q {
325524514 q std::string::size_type start = 1;
335524514 q std::string::size_type pos = (*token).find('\\');
345904514 q while (pos != std::string::npos)
3538021 q {
3638021 q result += (*token).substr(start, pos - start);
3738021 q if ((*token)[pos + 1] == 'n')
380 result += '\n';
3938021 q else if ((*token)[pos + 1] == 't')
400 result += '\t';
4138021 q else if ((*token)[pos + 1] == 'u')
420 {
430 result += decode_to_utf8(*token, pos, error_output);
440 pos -= 2;
450 }
4638021 q else
4738021 q result += (*token)[pos + 1];
4838021 q start = pos + 2;
4938021 q pos = (*token).find('\\', start);
5038021 q }
515524514 q result += (*token).substr(start, (*token).size() - start - 1);
525524514 q }
533020305 q else if (isalpha((*token)[0]) || isdigit((*token)[0]) || (*token)[0] == '_')
543008305 q result = *token;
55122 q else if ((*token)[0] == '-' && (*token).size() > 1 && isdigit((*token)[1]))
56122 q result = *token;
570 else
580 result_valid = false;
608544569 q if (result_valid)
618544569 q ++token;
620 else
630 {
640 if (error_output)
650 error_output->add_parse_error(type_of_token + " expected - '" + *token + "' found.", token.line_col().first);
660 }
688544569 q return result;
698544569 q}
72std::string get_identifier_token(Tokenizer_Wrapper& token, Error_Output* error_output,
73 std::string type_of_token)
749680657 q{
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] == '_')
819680657 q result = *token;
820 else
830 result_valid = false;
859680657 q if (result_valid)
869680657 q ++token;
870 else
880 {
890 if (error_output)
900 error_output->add_parse_error(type_of_token + " expected - '" + *token + "' found.", token.line_col().first);
910 }
939680657 q return result;
949680657 q}
97void process_after(Tokenizer_Wrapper& token, Error_Output* error_output, bool after)
9848264684 q{
9948264684 q if (!token.good())
1000 {
1010 if (error_output)
1020 error_output->add_parse_error("Unexpected end of input.", token.line_col().first);
1030 }
10448264684 q else if (after)
10542892683 q ++token;
10648264684 q}
109void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output,
110 std::string target_1, bool after)
11143200683 q{
11243200683 q if (*token != target_1)
1130 {
1140 if (error_output)
1150 error_output->add_parse_error(std::string("'") + target_1 + "' expected - '"
1160 + *token + "' found.", token.line_col().first);
1170 ++token;
1180 }
11943200683 q while (token.good() && *token != target_1)
1200 ++token;
12143200683 q process_after(token, error_output, after);
12243200683 q}
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)
1290 {
1300 if (error_output)
1310 error_output->add_parse_error
1320 (std::string("'") + target_1 + "' or '" + target_2 + "' expected - '"
1330 + *token + "' found.", token.line_col().first);
1340 ++token;
1350 }
1362384368 q while (token.good() && *token != target_1 && *token != target_2)
1370 ++token;
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)
1460 {
1470 if (error_output)
1480 error_output->add_parse_error
1490 (std::string("'") + target_1 + "', '" + target_2 + "', or '" + target_3 + "' expected - '"
1500 + *token + "' found.", token.line_col().first);
1510 ++token;
1520 }
15341268 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3)
1540 ++token;
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,
161 bool after)
1620{
1630 if (*token != target_1 && *token != target_2 && *token != target_3 && *token != target_4)
1640 {
1650 if (error_output)
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);
1700 ++token;
1710 }
1720 while (token.good() && *token != target_1 && *token != target_2 && *token != target_3
1730 && *token != target_4)
1740 ++token;
1750 process_after(token, error_output, after);
1760}
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)
1850 {
1860 if (error_output)
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);
1910 ++token;
1920 }
1932268368 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3
1942268368 q && *token != target_4 && *token != target_5)
1950 ++token;
1962268368 q process_after(token, error_output, after);
200int operator_priority(const std::string& operator_name, bool unary)
20154542605 q{
20254542605 q static std::map< std::string, int > priority;
20354542605 q if (priority.empty())
2042420605 q {
2052420605 q priority[","] = 1;
2062420605 q priority["="] = 2;
2072420605 q priority["?"] = 3;
2082420605 q priority[":"] = 4;
2092420605 q priority["||"] = 5;
2102420605 q priority["&&"] = 6;
2112420605 q priority["=="] = 7;
2122420605 q priority["!="] = 7;
2132420605 q priority["<"] = 8;
2142420605 q priority["<="] = 8;
2152420605 q priority[">"] = 8;
2162420605 q priority[">="] = 8;
2172420605 q priority["+"] = 10;
2182420605 q priority["-"] = 10;
2192420605 q priority["*"] = 11;
2202420605 q priority["/"] = 11;
2212420605 q priority["%"] = 11;
2222420605 q priority["."] = 12;
2232420605 q priority["->"] = 12;
2242420605 q priority["!"] = 13;
2252420605 q priority["::"] = 15;
2262420605 q }
22854542605 q if (unary)
22917642 q return 13;
23154366605 q std::map< std::string, int >::const_iterator prio_it = priority.find(operator_name);
23254366605 q if (prio_it != priority.end())
23327654576 q return prio_it->second;
23526712604 q return 0;
23654366605 q}
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)));
2424564605 q std::vector< uint > stack;
2434564605 q stack.push_back(0);
24563640605 q while (token.good() && *token != ";" && *token != "->")
24663008605 q {
24763008605 q if (*token == "(" || *token == "[" || *token == "{")
24810112224 q {
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);
25310112224 q }
25452896605 q else if (*token == ")" || *token == "]" ||*token == "}")
25514040604 q {
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 != "{")
25910744600 q --stack_pos;
26014040604 q if (stack_pos < 0)
2613932582 q {
2623932582 q if (parenthesis_expected)
2633932582 q return;
2640 else
2650 error_output->add_parse_error(std::string("Unmatched ") + *token, token.line_col().first);
2663932582 q }
26710108224 q else
26810108224 q stack.resize(stack_pos);
26914040604 q }
27038856605 q else
27138856605 q {
27238856605 q int prio = operator_priority(*token, false);
27338856605 q bool unary_minus = false;
27438856605 q if ((*token).size() >= 2 && (*token)[0] == '-' && tree[stack.back()].rhs)
275246 q {
276246 q prio = 10;
277246 q unary_minus = true;
278246 q }
28038856605 q if (prio > 0)
28115228576 q {
28215228576 q if (stack.back() < tree.size())
28315228576 q {
28415228576 q uint stack_pos = stack.size()-1;
28522304576 q while (stack_pos > 0)
28613336463 q {
28713336463 q if (operator_priority(tree[stack[stack_pos]].token, false) < prio)
2886260201 q break;
2897076444 q --stack_pos;
2907076444 q }
29115228576 q stack.resize(stack_pos+1);
29215228576 q }
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);
29815228576 q if (unary_minus)
299246 q {
300246 q tree.push_back(Token_Node((*token).substr(1), token.line_col()));
301246 q tree[stack.back()].rhs = tree.size()-1;
302246 q }
30315228576 q }
30423628604 q else if (!tree[stack.back()].rhs)
30523628604 q {
30623628604 q tree.push_back(Token_Node(*token, token.line_col()));
30723628604 q tree[stack.back()].rhs = tree.size()-1;
30823628604 q }
3090 else
3100 error_output->add_parse_error(std::string("Operator expected, but \"") + *token + "\"found.",
3110 token.line_col().first);
31238856605 q }
31359076605 q ++token;
31459076605 q }
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 != "{")
3191960127 q --stack_pos;
320632127 q if (stack_pos > 0)
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)
328322 q if (root == exclude)
32982 q return root->rhs ? add_tree_node(root.rhs(), exclude) : 0u;
330242 q else
331242 q {
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;
337242 q }
3380 return 0u;
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);
34682 q tree[0].rhs = result;
350const std::string* Token_Node_Ptr::function_name() const
35245286 q if (tree && pos < tree->tree.size())
35345286 q {
35445286 q if (operator*().token == "(")
35545286 q {
35645286 q if (operator*().lhs)
35745286 q return &lhs()->token;
35845286 q }
3600 return &rhs().lhs()->token;
36145286 q }
3630 return 0;
367bool Token_Node_Ptr::assert_is_function(Error_Output* error_output) const
3695588223 q if (tree && pos < tree->tree.size())
3705588223 q {
3715588223 q if (operator*().token == "(")
3725448221 q {
3735448221 q if (!operator*().lhs)
3740 {
3750 if (error_output)
3760 error_output->add_parse_error("Function expected, but no function name found",
3770 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0));
3780 return false;
3790 }
3815448221 q Token_Node_Ptr lhs_ = lhs();
3825448221 q if (lhs_->lhs || lhs_->rhs)
3830 {
3840 if (error_output)
3850 error_output->add_parse_error("Function name expected, compound expression found",
3860 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0));
3870 return false;
3880 }
3905448221 q return true;
3915448221 q }
39214027 q else if (operator*().token == ".")
39314027 q {
39414027 q if (!operator*().rhs)
3950 {
3960 if (error_output)
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));
3990 return false;
4000 }
40214027 q Token_Node_Ptr rhs_ = rhs();
40314027 q if (rhs_->token != "(")
4040 {
4050 if (error_output)
4060 error_output->add_parse_error(std::string("Function left parenthesis expected, but ")
4070 + "\"" + rhs_->token + "\" found.", rhs_->line_col.first);
4080 return false;
4090 }
41114027 q if (!rhs_->lhs)
4120 {
4130 if (error_output)
4140 error_output->add_parse_error("Function expected, but no function name found",
4150 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0));
4160 return false;
4170 }
41914027 q Token_Node_Ptr lhs_ = rhs_.lhs();
42014027 q if (lhs_->lhs || lhs_->rhs)
4210 {
4220 if (error_output)
4230 error_output->add_parse_error("Function name expected, compound expression found",
4240 + (tree && pos < tree->tree.size() ? operator*().line_col.first : 0));
4250 return false;
4260 }
42814027 q return true;
42914027 q }
4305588223 q }
4320 if (error_output)
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));
4380 return false;
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 == "(")
4475436221 q {
4485436221 q if (expected)
4490 {
4500 if (error_output)
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));
4530 return false;
4540 }
4555436221 q else
4565436221 q return true;
4575436221 q }
4580 else if (operator*().token == ".")
4590 {
4600 if (!operator*().lhs)
4610 {
4620 if (error_output)
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));
4650 return false;
4660 }
4680 if (expected)
4690 {
4700 Token_Node_Ptr lhs_ = lhs();
4710 if (lhs_->lhs || lhs_->rhs)
4720 {
4730 if (error_output)
4740 error_output->add_parse_error("Input set expected, compound expression found",
4750 (tree && pos < tree->tree.size() ? operator*().line_col.first : 0));
4760 return false;
4770 }
4780 return true;
4790 }
4800 else
4810 {
4820 const std::string* func_name = function_name();
4830 if (error_output)
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));
4860 return false;
4870 }
4880 }
4900 return false;
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;
5025296222 q if (has_arguments)
5033040175 q {
5043040175 q if (expected)
5053032174 q return true;
50782 q const std::string* func_name = function_name();
50882 q if (error_output)
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));
51282 q return false;
5133040175 q }
5142256182 q else
5152256182 q {
5162256182 q if (!expected)
5172244182 q return true;
519123 q const std::string* func_name = function_name();
520123 q if (error_output)
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));
524123 q return false;
5252256182 q }
529Token_Node_Ptr find_leftmost_token(Token_Node_Ptr tree_it)
53110264570 q while (tree_it->lhs)
5326616533 q tree_it = tree_it.lhs();
5343648570 q return 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();
54382 q return tree_it;