2010-06-24 19:12:10 +00:00
|
|
|
|
#include <DB/Parsers/IAST.h>
|
|
|
|
|
#include <DB/Parsers/ASTExpressionList.h>
|
|
|
|
|
#include <DB/Parsers/ASTFunction.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/CommonParsers.h>
|
|
|
|
|
#include <DB/Parsers/ExpressionElementParsers.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/ExpressionListParsers.h>
|
2015-12-24 17:14:10 +00:00
|
|
|
|
#include <DB/Parsers/ParserCreateQuery.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
|
|
2014-03-10 14:47:04 +00:00
|
|
|
|
const char * ParserMultiplicativeExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
"*", "multiply",
|
|
|
|
|
"/", "divide",
|
|
|
|
|
"%", "modulo",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char * ParserUnaryMinusExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
"-", "negate",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char * ParserAdditiveExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
"+", "plus",
|
|
|
|
|
"-", "minus",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char * ParserComparisonExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
"==", "equals",
|
|
|
|
|
"!=", "notEquals",
|
|
|
|
|
"<>", "notEquals",
|
|
|
|
|
"<=", "lessOrEquals",
|
|
|
|
|
">=", "greaterOrEquals",
|
|
|
|
|
"<", "less",
|
|
|
|
|
">", "greater",
|
|
|
|
|
"=", "equals",
|
|
|
|
|
"LIKE", "like",
|
|
|
|
|
"NOT LIKE", "notLike",
|
|
|
|
|
"IN", "in",
|
|
|
|
|
"NOT IN", "notIn",
|
2014-03-14 14:52:48 +00:00
|
|
|
|
"GLOBAL IN", "globalIn",
|
|
|
|
|
"GLOBAL NOT IN","globalNotIn",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char * ParserLogicalNotExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
"NOT", "not",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-07 23:18:39 +00:00
|
|
|
|
const char * ParserArrayElementExpression::operators[] =
|
2014-03-10 14:47:04 +00:00
|
|
|
|
{
|
|
|
|
|
"[", "arrayElement",
|
2014-03-10 18:01:26 +00:00
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-07 23:18:39 +00:00
|
|
|
|
const char * ParserTupleElementExpression::operators[] =
|
|
|
|
|
{
|
|
|
|
|
".", "tupleElement",
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-10 14:47:04 +00:00
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2011-08-18 18:48:00 +00:00
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto list = std::make_shared<ASTExpressionList>();
|
2011-08-18 18:48:00 +00:00
|
|
|
|
node = list;
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser->parse(pos, end, elem, max_parsed_pos, expected))
|
2011-08-18 18:48:00 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
list->children.push_back(elem);
|
2014-07-18 19:47:28 +00:00
|
|
|
|
first = false;
|
2011-08-18 18:48:00 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-07-09 06:51:30 +00:00
|
|
|
|
auto prev_pos = pos;
|
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
|
ws.ignore(pos, end);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!separator_parser->ignore(pos, end, max_parsed_pos, expected))
|
2011-08-18 18:48:00 +00:00
|
|
|
|
break;
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser->parse(pos, end, elem, max_parsed_pos, expected))
|
2016-07-09 06:51:30 +00:00
|
|
|
|
{
|
|
|
|
|
pos = prev_pos;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-08-18 18:48:00 +00:00
|
|
|
|
|
|
|
|
|
list->children.push_back(elem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!allow_empty && first)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserLeftAssociativeBinaryOperatorList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
2015-06-05 20:04:54 +00:00
|
|
|
|
Pos begin = pos;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!first_elem_parser->parse(pos, end, elem, max_parsed_pos, expected))
|
2010-06-25 19:55:19 +00:00
|
|
|
|
return false;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
node = elem;
|
2014-07-18 19:47:28 +00:00
|
|
|
|
first = false;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
/// пробуем найти какой-нибудь из допустимых операторов
|
2014-03-10 14:47:04 +00:00
|
|
|
|
|
|
|
|
|
const char ** it;
|
|
|
|
|
for (it = operators; *it; it += 2)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
2014-03-10 14:47:04 +00:00
|
|
|
|
ParserString op(it[0], true, true);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (op.ignore(pos, end, max_parsed_pos, expected))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 14:47:04 +00:00
|
|
|
|
if (!*it)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
/// функция, соответствующая оператору
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
/// аргументы функции
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!(remaining_elem_parser ? remaining_elem_parser : first_elem_parser)->parse(pos, end, elem, max_parsed_pos, expected))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/// первым аргументом функции будет предыдущий элемент, вторым - следующий
|
2016-05-28 15:42:22 +00:00
|
|
|
|
function->range.first = begin;
|
|
|
|
|
function->range.second = pos;
|
|
|
|
|
function->name = it[1];
|
|
|
|
|
function->arguments = exp_list;
|
|
|
|
|
function->children.push_back(exp_list);
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
exp_list->children.push_back(node);
|
|
|
|
|
exp_list->children.push_back(elem);
|
|
|
|
|
exp_list->range.first = begin;
|
|
|
|
|
exp_list->range.second = pos;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
/** специальное исключение для оператора доступа к элементу массива x[y], который
|
|
|
|
|
* содержит инфиксную часть '[' и суффиксную ']' (задаётся в виде '[')
|
|
|
|
|
*/
|
2014-03-10 14:47:04 +00:00
|
|
|
|
if (0 == strcmp(it[0], "["))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
|
|
|
|
ParserString rest_p("]");
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
ws.ignore(pos, end);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!rest_p.ignore(pos, end, max_parsed_pos, expected))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
node = function;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserVariableArityOperatorList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2014-02-13 11:05:51 +00:00
|
|
|
|
{
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
|
|
|
|
|
Pos begin = pos;
|
|
|
|
|
ASTPtr arguments;
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser->parse(pos, end, node, max_parsed_pos, expected))
|
2014-02-13 11:05:51 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!infix_parser.ignore(pos, end, max_parsed_pos, expected))
|
2014-02-13 11:05:51 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
if (!arguments)
|
|
|
|
|
{
|
2014-09-29 13:32:16 +00:00
|
|
|
|
node = makeASTFunction(function_name, node);
|
|
|
|
|
arguments = static_cast<ASTFunction &>(*node).arguments;
|
2014-02-13 11:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser->parse(pos, end, elem, max_parsed_pos, expected))
|
2014-02-13 11:05:51 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
arguments->children.push_back(elem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arguments)
|
|
|
|
|
arguments->range = node->range = StringRange(begin, pos);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2016-04-03 01:15:53 +00:00
|
|
|
|
bool ParserBetweenExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
|
|
|
|
{
|
|
|
|
|
/// Для выражения (subject BETWEEN left AND right)
|
|
|
|
|
/// создаём AST такое же, как для (subject >= left AND subject <= right).
|
|
|
|
|
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
ParserString s_between("BETWEEN", true, true);
|
|
|
|
|
ParserString s_and("AND", true, true);
|
|
|
|
|
|
|
|
|
|
ASTPtr subject;
|
|
|
|
|
ASTPtr left;
|
|
|
|
|
ASTPtr right;
|
|
|
|
|
|
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
|
|
|
|
if (!elem_parser.parse(pos, end, subject, max_parsed_pos, expected))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
if (!s_between.ignore(pos, end, max_parsed_pos, expected))
|
|
|
|
|
node = subject;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
if (!elem_parser.parse(pos, end, left, max_parsed_pos, expected))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
if (!s_and.ignore(pos, end, max_parsed_pos, expected))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
|
|
if (!elem_parser.parse(pos, end, right, max_parsed_pos, expected))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/// функция AND
|
2016-05-28 16:00:04 +00:00
|
|
|
|
auto f_and = std::make_shared<ASTFunction>();
|
|
|
|
|
auto args_and = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
|
|
|
|
|
/// >=
|
2016-05-28 16:00:04 +00:00
|
|
|
|
auto f_ge = std::make_shared<ASTFunction>();
|
|
|
|
|
auto args_ge = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
|
|
|
|
|
/// <=
|
2016-05-28 16:00:04 +00:00
|
|
|
|
auto f_le = std::make_shared<ASTFunction>();
|
|
|
|
|
auto args_le = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
|
|
|
|
|
args_ge->children.emplace_back(subject);
|
|
|
|
|
args_ge->children.emplace_back(left);
|
|
|
|
|
|
|
|
|
|
args_le->children.emplace_back(subject);
|
|
|
|
|
args_le->children.emplace_back(right);
|
|
|
|
|
|
|
|
|
|
f_ge->range.first = begin;
|
|
|
|
|
f_ge->range.second = pos;
|
|
|
|
|
f_ge->name = "greaterOrEquals";
|
2016-05-28 16:00:04 +00:00
|
|
|
|
f_ge->arguments = args_ge;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
f_ge->children.emplace_back(f_ge->arguments);
|
|
|
|
|
|
|
|
|
|
f_le->range.first = begin;
|
|
|
|
|
f_le->range.second = pos;
|
|
|
|
|
f_le->name = "lessOrEquals";
|
2016-05-28 16:00:04 +00:00
|
|
|
|
f_le->arguments = args_le;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
f_le->children.emplace_back(f_le->arguments);
|
|
|
|
|
|
2016-05-28 16:00:04 +00:00
|
|
|
|
args_and->children.emplace_back(f_ge);
|
|
|
|
|
args_and->children.emplace_back(f_le);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
|
|
|
|
|
f_and->range.first = begin;
|
|
|
|
|
f_and->range.second = pos;
|
|
|
|
|
f_and->name = "and";
|
2016-05-28 16:00:04 +00:00
|
|
|
|
f_and->arguments = args_and;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
f_and->children.emplace_back(f_and->arguments);
|
|
|
|
|
|
2016-05-28 16:00:04 +00:00
|
|
|
|
node = f_and;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserTernaryOperatorExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2012-09-23 06:16:42 +00:00
|
|
|
|
{
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
ParserString symbol1("?");
|
|
|
|
|
ParserString symbol2(":");
|
|
|
|
|
|
|
|
|
|
ASTPtr elem_cond;
|
|
|
|
|
ASTPtr elem_then;
|
|
|
|
|
ASTPtr elem_else;
|
|
|
|
|
|
|
|
|
|
Pos begin = pos;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser.parse(pos, end, elem_cond, max_parsed_pos, expected))
|
2012-09-23 06:16:42 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!symbol1.ignore(pos, end, max_parsed_pos, expected))
|
2012-09-23 06:16:42 +00:00
|
|
|
|
node = elem_cond;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser.parse(pos, end, elem_then, max_parsed_pos, expected))
|
2012-09-23 06:16:42 +00:00
|
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-09-23 06:16:42 +00:00
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!symbol2.ignore(pos, end, max_parsed_pos, expected))
|
2012-09-23 06:16:42 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser.parse(pos, end, elem_else, max_parsed_pos, expected))
|
2012-09-23 06:16:42 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/// функция, соответствующая оператору
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2012-09-23 06:16:42 +00:00
|
|
|
|
|
|
|
|
|
/// аргументы функции
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
|
|
|
|
|
|
|
|
|
function->range.first = begin;
|
|
|
|
|
function->range.second = pos;
|
|
|
|
|
function->name = "if";
|
|
|
|
|
function->arguments = exp_list;
|
|
|
|
|
function->children.push_back(exp_list);
|
|
|
|
|
|
|
|
|
|
exp_list->children.push_back(elem_cond);
|
|
|
|
|
exp_list->children.push_back(elem_then);
|
|
|
|
|
exp_list->children.push_back(elem_else);
|
|
|
|
|
exp_list->range.first = begin;
|
|
|
|
|
exp_list->range.second = pos;
|
|
|
|
|
|
|
|
|
|
node = function;
|
2012-09-23 06:16:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserLambdaExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2013-05-08 09:52:02 +00:00
|
|
|
|
{
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
ParserString arrow("->");
|
|
|
|
|
ParserString open("(");
|
|
|
|
|
ParserString close(")");
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
Pos begin = pos;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
ASTPtr inner_arguments;
|
|
|
|
|
ASTPtr expression;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 10:59:21 +00:00
|
|
|
|
bool was_open = false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (open.ignore(pos, end, max_parsed_pos, expected))
|
2013-05-08 10:59:21 +00:00
|
|
|
|
{
|
2015-04-11 03:10:23 +00:00
|
|
|
|
ws.ignore(pos, end, max_parsed_pos, expected);
|
2013-05-08 10:59:21 +00:00
|
|
|
|
was_open = true;
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!ParserList(ParserPtr(new ParserIdentifier), ParserPtr(new ParserString(","))).parse(pos, end, inner_arguments, max_parsed_pos, expected))
|
2013-05-08 09:52:02 +00:00
|
|
|
|
break;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
ws.ignore(pos, end, max_parsed_pos, expected);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 10:59:21 +00:00
|
|
|
|
if (was_open)
|
|
|
|
|
{
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!close.ignore(pos, end, max_parsed_pos, expected))
|
2013-05-08 10:59:21 +00:00
|
|
|
|
break;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
ws.ignore(pos, end, max_parsed_pos, expected);
|
2013-05-08 10:59:21 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!arrow.ignore(pos, end, max_parsed_pos, expected))
|
2013-05-08 09:52:02 +00:00
|
|
|
|
break;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
ws.ignore(pos, end, max_parsed_pos, expected);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser.parse(pos, end, expression, max_parsed_pos, expected))
|
2013-05-08 09:52:02 +00:00
|
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
/// lambda(tuple(inner_arguments), expression)
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto lambda = std::make_shared<ASTFunction>();
|
2013-05-08 09:52:02 +00:00
|
|
|
|
node = lambda;
|
|
|
|
|
lambda->name = "lambda";
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto outer_arguments = std::make_shared<ASTExpressionList>();
|
2013-05-08 09:52:02 +00:00
|
|
|
|
lambda->arguments = outer_arguments;
|
2013-05-08 10:33:37 +00:00
|
|
|
|
lambda->children.push_back(lambda->arguments);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto tuple = std::make_shared<ASTFunction>();
|
2013-05-08 09:52:02 +00:00
|
|
|
|
outer_arguments->children.push_back(tuple);
|
|
|
|
|
tuple->name = "tuple";
|
|
|
|
|
tuple->arguments = inner_arguments;
|
|
|
|
|
tuple->children.push_back(inner_arguments);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
outer_arguments->children.push_back(expression);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
while (false);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-05-08 09:52:02 +00:00
|
|
|
|
pos = begin;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
return elem_parser.parse(pos, end, node, max_parsed_pos, expected);
|
2013-05-08 09:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserPrefixUnaryOperatorExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
|
|
|
|
|
|
/// пробуем найти какой-нибудь из допустимых операторов
|
|
|
|
|
Pos begin = pos;
|
2014-03-10 14:47:04 +00:00
|
|
|
|
const char ** it;
|
|
|
|
|
for (it = operators; *it; it += 2)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
2014-03-10 14:47:04 +00:00
|
|
|
|
ParserString op(it[0], true, true);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (op.ignore(pos, end, max_parsed_pos, expected))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 19:55:19 +00:00
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
2015-05-28 01:41:40 +00:00
|
|
|
|
/// Позволяем парсить цепочки вида NOT NOT x. Это хак.
|
|
|
|
|
/** Так сделано, потому что среди унарных операторов есть только минус и NOT.
|
|
|
|
|
* Но для минуса цепочку из унарных операторов не требуется поддерживать.
|
|
|
|
|
*/
|
|
|
|
|
if (it[0] && 0 == strncmp(it[0], "NOT", 3))
|
|
|
|
|
{
|
|
|
|
|
/// Было ли чётное количество NOT.
|
|
|
|
|
bool even = false;
|
|
|
|
|
|
|
|
|
|
const char ** jt;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
for (jt = operators; *jt; jt += 2)
|
|
|
|
|
{
|
|
|
|
|
ParserString op(jt[0], true, true);
|
|
|
|
|
if (op.ignore(pos, end, max_parsed_pos, expected))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!*jt)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
even = !even;
|
|
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (even)
|
|
|
|
|
it = jt; /// Зануляем результат парсинга первого NOT. Получается, как будто цепочки NOT нет вообще.
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-24 19:37:23 +00:00
|
|
|
|
ASTPtr elem;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (!elem_parser->parse(pos, end, elem, max_parsed_pos, expected))
|
2010-06-24 19:12:10 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2014-03-10 14:47:04 +00:00
|
|
|
|
if (!*it)
|
2010-06-24 19:37:23 +00:00
|
|
|
|
node = elem;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// функция, соответствующая оператору
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2010-06-24 19:37:23 +00:00
|
|
|
|
/// аргументы функции
|
2016-05-28 15:42:22 +00:00
|
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
function->range.first = begin;
|
|
|
|
|
function->range.second = pos;
|
|
|
|
|
function->name = it[1];
|
|
|
|
|
function->arguments = exp_list;
|
|
|
|
|
function->children.push_back(exp_list);
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
exp_list->children.push_back(elem);
|
|
|
|
|
exp_list->range.first = begin;
|
|
|
|
|
exp_list->range.second = pos;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
|
node = function;
|
2010-06-24 19:37:23 +00:00
|
|
|
|
}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserUnaryMinusExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2012-11-20 22:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
/// В качестве исключения, отрицательные числа должны парситься, как литералы, а не как применение оператора.
|
|
|
|
|
|
|
|
|
|
if (pos < end && *pos == '-')
|
|
|
|
|
{
|
|
|
|
|
ParserLiteral lit_p;
|
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
if (lit_p.parse(pos, end, node, max_parsed_pos, expected))
|
2012-11-20 22:48:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
pos = begin;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
return operator_parser.parse(pos, end, node, max_parsed_pos, expected);
|
2012-11-20 22:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-07 23:18:39 +00:00
|
|
|
|
bool ParserArrayElementExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected &expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
2015-01-16 10:17:58 +00:00
|
|
|
|
return ParserLeftAssociativeBinaryOperatorList{
|
|
|
|
|
operators,
|
|
|
|
|
ParserPtr(new ParserExpressionElement),
|
2015-11-08 00:28:12 +00:00
|
|
|
|
ParserPtr(new ParserExpressionWithOptionalAlias(false))
|
2015-04-11 03:10:23 +00:00
|
|
|
|
}.parse(pos, end, node, max_parsed_pos, expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-07 23:18:39 +00:00
|
|
|
|
bool ParserTupleElementExpression::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected &expected)
|
|
|
|
|
{
|
|
|
|
|
return ParserLeftAssociativeBinaryOperatorList{
|
|
|
|
|
operators,
|
|
|
|
|
ParserPtr(new ParserArrayElementExpression),
|
|
|
|
|
ParserPtr(new ParserUnsignedInteger)
|
|
|
|
|
}.parse(pos, end, node, max_parsed_pos, expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-08 00:28:12 +00:00
|
|
|
|
ParserExpressionWithOptionalAlias::ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword)
|
|
|
|
|
: impl(new ParserWithOptionalAlias(ParserPtr(new ParserLambdaExpression), allow_alias_without_as_keyword))
|
2011-11-06 20:47:07 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-24 17:14:10 +00:00
|
|
|
|
ParserExpressionInCastExpression::ParserExpressionInCastExpression(bool allow_alias_without_as_keyword)
|
|
|
|
|
: impl(new ParserCastExpressionWithOptionalAlias(ParserPtr(new ParserLambdaExpression), allow_alias_without_as_keyword))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserExpressionList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
2015-11-08 00:28:12 +00:00
|
|
|
|
return ParserList(ParserPtr(new ParserExpressionWithOptionalAlias(allow_alias_without_as_keyword)), ParserPtr(new ParserString(","))).parse(pos, end, node, max_parsed_pos, expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserNotEmptyExpressionList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
|
{
|
2015-04-11 03:10:23 +00:00
|
|
|
|
return nested_parser.parse(pos, end, node, max_parsed_pos, expected)
|
2014-06-26 00:58:14 +00:00
|
|
|
|
&& !typeid_cast<ASTExpressionList &>(*node).children.empty();
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
bool ParserOrderByExpressionList::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2011-09-04 05:14:52 +00:00
|
|
|
|
{
|
2015-04-11 03:10:23 +00:00
|
|
|
|
return ParserList(ParserPtr(new ParserOrderByElement), ParserPtr(new ParserString(",")), false).parse(pos, end, node, max_parsed_pos, expected);
|
2011-09-04 05:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
}
|