Overpass API regression suite coverage (Nova Scotia) / 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)
247408498 q{
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] == '\'')
314948456 q {
324948456 q std::string::size_type start = 1;
334948456 q std::string::size_type pos = (*token).find('\\');
345160456 q while (pos != std::string::npos)
3521219 q {
3621219 q result += (*token).substr(start, pos - start);
3721219 q if ((*token)[pos + 1] == 'n')
380 result += '\n';
3921219 q else if ((*token)[pos + 1] == 't')
400 result += '\t';
4121219 q else if ((*token)[pos + 1] == 'u')
420 {
430 result += decode_to_utf8(*token, pos, error_output);
440 pos -= 2;
450 }
4621219 q else
4721219 q result += (*token)[pos + 1];
4821219 q start = pos + 2;
4921219 q pos = (*token).find('\\', start);
5021219 q }
514948456 q result += (*token).substr(start, (*token).size() - start - 1);
524948456 q }
532460260 q else if (isalpha((*token)[0]) || isdigit((*token)[0]) || (*token)[0] == '_')
541940260 q result = *token;
5552071 q else if ((*token)[0] == '-' && (*token).size() > 1 && isdigit((*token)[1]))
5652071 q result = *token;
570 else
580 result_valid = false;
607408498 q if (result_valid)
617408498 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 }
687408498 q return result;
697408498 q}
72std::string get_identifier_token(Tokenizer_Wrapper& token, Error_Output* error_output,
73 std::string type_of_token)
748572584 q{
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] == '_')
818572584 q result = *token;
820 else
830 result_valid = false;
858572584 q if (result_valid)
868572584 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 }
938572584 q return result;
948572584 q}
97void process_after(Tokenizer_Wrapper& token, Error_Output* error_output, bool after)
9842344604 q{
9942344604 q if (!token.good())
1000 {
1010 if (error_output)
1020 error_output->add_parse_error("Unexpected end of input.", token.line_col().first);
1030 }
10442344604 q else if (after)
10537608603 q ++token;
10642344604 q}
109void clear_until_after(Tokenizer_Wrapper& token, Error_Output* error_output,
110 std::string target_1, bool after)
11137864603 q{
11237864603 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 }
11937864603 q while (token.good() && *token != target_1)
1200 ++token;
12137864603 q process_after(token, error_output, after);
12237864603 q}
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)
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 }
1362072322 q while (token.good() && *token != target_1 && *token != target_2)
1370 ++token;
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)
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 }
15332457 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3)
1540 ++token;
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,
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)
1832084331 q if (*token != target_1 && *token != target_2 && *token != target_3
1842084331 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 }
1932084331 q while (token.good() && *token != target_1 && *token != target_2 && *token != target_3
1942084331 q && *token != target_4 && *token != target_5)
1950 ++token;
1962084331 q process_after(token, error_output, after);
200int operator_priority(const std::string& operator_name, bool unary)
20148892532 q{
20248892532 q static std::map< std::string, int > priority;
20348892532 q if (priority.empty())
2042128532 q {
2052128532 q priority[","] = 1;
2062128532 q priority["="] = 2;
2072128532 q priority["?"] = 3;
2082128532 q priority[":"] = 4;
2092128532 q priority["||"] = 5;
2102128532 q priority["&&"] = 6;
2112128532 q priority["=="] = 7;
2122128532 q priority["!="] = 7;
2132128532 q priority["<"] = 8;
2142128532 q priority["<="] = 8;
2152128532 q priority[">"] = 8;
2162128532 q priority[">="] = 8;
2172128532 q priority["+"] = 10;
2182128532 q priority["-"] = 10;
2192128532 q priority["*"] = 11;
2202128532 q priority["/"] = 11;
2212128532 q priority["%"] = 11;
2222128532 q priority["."] = 12;
2232128532 q priority["->"] = 12;
2242128532 q priority["!"] = 13;
2252128532 q priority["::"] = 15;
2262128532 q }
22848892532 q if (unary)
22911634 q return 13;
23148776532 q std::map< std::string, int >::const_iterator prio_it = priority.find(operator_name);
23248776532 q if (prio_it != priority.end())
23324560510 q return prio_it->second;
23524216531 q return 0;
23648776532 q}
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)));
2423908532 q std::vector< uint > stack;
2433908532 q stack.push_back(0);
24557644532 q while (token.good() && *token != ";" && *token != "->")
24657068532 q {
24757068532 q if (*token == "(" || *token == "[" || *token == "{")
2489440206 q {
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);
2539440206 q }
25447628532 q else if (*token == ")" || *token == "]" ||*token == "}")
25512768531 q {
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 != "{")
2599628527 q --stack_pos;
26012768531 q if (stack_pos < 0)
2613332509 q {
2623332509 q if (parenthesis_expected)
2633332509 q return;
2640 else
2650 error_output->add_parse_error(std::string("Unmatched ") + *token, token.line_col().first);
2663332509 q }
2679436206 q else
2689436206 q stack.resize(stack_pos);
26912768531 q }
27034860532 q else
27134860532 q {
27234860532 q int prio = operator_priority(*token, false);
27334860532 q bool unary_minus = false;
27434860532 q if ((*token).size() >= 2 && (*token)[0] == '-' && tree[stack.back()].rhs)
275287 q {
276287 q prio = 10;
277287 q unary_minus = true;
278287 q }
28034860532 q if (prio > 0)
28113576510 q {
28213576510 q if (stack.back() < tree.size())
28313576510 q {
28413576510 q uint stack_pos = stack.size()-1;
28519648510 q while (stack_pos > 0)
28611872415 q {
28711872415 q if (operator_priority(tree[stack[stack_pos]].token, false) < prio)
2885800190 q break;
2896072395 q --stack_pos;
2906072395 q }
29113576510 q stack.resize(stack_pos+1);
29213576510 q }
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);
29813576510 q if (unary_minus)
299287 q {
300287 q tree.push_back(Token_Node((*token).substr(1), token.line_col()));
301287 q tree[stack.back()].rhs = tree.size()-1;
302287 q }
30313576510 q }
30421284531 q else if (!tree[stack.back()].rhs)
30521284531 q {
30621284531 q tree.push_back(Token_Node(*token, token.line_col()));
30721284531 q tree[stack.back()].rhs = tree.size()-1;
30821284531 q }
3090 else
3100 error_output->add_parse_error(std::string("Operator expected, but \"") + *token + "\"found.",
3110 token.line_col().first);
31234860532 q }
31353736532 q ++token;
31453736532 q }
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 != "{")
3191772118 q --stack_pos;
320576118 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
35237675 q if (tree && pos < tree->tree.size())
35337675 q {
35437675 q if (operator*().token == "(")
35537675 q {
35637675 q if (operator*().lhs)
35737675 q return &lhs()->token;
35837675 q }
3600 return &rhs().lhs()->token;
36137675 q }
3630 return 0;
367bool Token_Node_Ptr::assert_is_function(Error_Output* error_output) const
3695248202 q if (tree && pos < tree->tree.size())
3705248202 q {
3715248202 q if (operator*().token == "(")
3725104200 q {
3735104200 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 }
3815104200 q Token_Node_Ptr lhs_ = lhs();
3825104200 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 }
3905104200 q return true;
3915104200 q }
39214425 q else if (operator*().token == ".")
39314425 q {
39414425 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 }
40214425 q Token_Node_Ptr rhs_ = rhs();
40314425 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 }
41114425 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 }
41914425 q Token_Node_Ptr lhs_ = rhs_.lhs();
42014425 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 }
42814425 q return true;
42914425 q }
4305248202 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
4465096200 q if (operator*().token == "(")
4475096200 q {
4485096200 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 }
4555096200 q else
4565096200 q return true;
4575096200 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
4985036201 q bool has_arguments = operator*().rhs;
4995036201 q if (operator*().token == ".")
50014425 q has_arguments = rhs()->rhs;
5025036201 q if (has_arguments)
5032932152 q {
5042932152 q if (expected)
5052924151 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;
5132932152 q }
5142104166 q else
5152104166 q {
5162104166 q if (!expected)
5172092165 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;
5252104166 q }
529Token_Node_Ptr find_leftmost_token(Token_Node_Ptr tree_it)
5318784497 q while (tree_it->lhs)
5325700471 q tree_it = tree_it.lhs();
5343084497 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;