2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
#include <Common/StringUtils.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[] =
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
"*", "multiply",
|
|
|
|
"/", "divide",
|
|
|
|
"%", "modulo",
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * ParserUnaryMinusExpression::operators[] =
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
"-", "negate",
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * ParserAdditiveExpression::operators[] =
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
"+", "plus",
|
|
|
|
"-", "minus",
|
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * ParserComparisonExpression::operators[] =
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
"==", "equals",
|
|
|
|
"!=", "notEquals",
|
|
|
|
"<>", "notEquals",
|
|
|
|
"<=", "lessOrEquals",
|
|
|
|
">=", "greaterOrEquals",
|
|
|
|
"<", "less",
|
|
|
|
">", "greater",
|
|
|
|
"=", "equals",
|
|
|
|
"LIKE", "like",
|
|
|
|
"NOT LIKE", "notLike",
|
|
|
|
"IN", "in",
|
|
|
|
"NOT IN", "notIn",
|
|
|
|
"GLOBAL IN", "globalIn",
|
|
|
|
"GLOBAL NOT IN", "globalNotIn",
|
2017-04-01 07:20:54 +00:00
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char * ParserLogicalNotExpression::operators[] =
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
"NOT", "not",
|
|
|
|
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
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
"[", "arrayElement",
|
2017-04-01 07:20:54 +00:00
|
|
|
nullptr
|
2014-03-10 14:47:04 +00:00
|
|
|
};
|
|
|
|
|
2015-11-07 23:18:39 +00:00
|
|
|
const char * ParserTupleElementExpression::operators[] =
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
".", "tupleElement",
|
2017-04-01 07:20:54 +00:00
|
|
|
nullptr
|
2015-11-07 23:18:39 +00:00
|
|
|
};
|
|
|
|
|
2014-03-10 14:47:04 +00:00
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-08-18 18:48:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
auto list = std::make_shared<ASTExpressionList>();
|
|
|
|
node = list;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
list->children.push_back(elem);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto prev_pos = pos;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!separator_parser->ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
pos = prev_pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->children.push_back(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_empty && first)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2011-08-18 18:48:00 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
|
2017-07-13 04:20:56 +00:00
|
|
|
static bool parseOperator(IParser::Pos & pos, const char * op, Expected & expected)
|
2017-07-10 03:28:12 +00:00
|
|
|
{
|
|
|
|
if (isWordCharASCII(*op))
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
return ParserKeyword(op).ignore(pos, expected);
|
2017-07-10 03:28:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strlen(op) == pos->size() && 0 == memcmp(op, pos->begin, pos->size()))
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserLeftAssociativeBinaryOperatorList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool first = true;
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!first_elem_parser->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
node = elem;
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// try to find any of the valid operators
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const char ** it;
|
|
|
|
for (it = operators; *it; it += 2)
|
2017-07-13 04:20:56 +00:00
|
|
|
if (parseOperator(pos, *it, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!*it)
|
|
|
|
break;
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// the function corresponding to the operator
|
2017-04-01 07:20:54 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>();
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// function arguments
|
2017-04-01 07:20:54 +00:00
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!(remaining_elem_parser ? remaining_elem_parser : first_elem_parser)->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// the first argument of the function is the previous element, the second is the next one
|
2017-07-10 03:28:12 +00:00
|
|
|
function->range.first = begin->begin;
|
|
|
|
function->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
function->name = it[1];
|
|
|
|
function->arguments = exp_list;
|
|
|
|
function->children.push_back(exp_list);
|
|
|
|
|
|
|
|
exp_list->children.push_back(node);
|
|
|
|
exp_list->children.push_back(elem);
|
2017-07-10 03:28:12 +00:00
|
|
|
exp_list->range.first = begin->begin;
|
|
|
|
exp_list->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** special exception for the access operator to the element of the array `x[y]`, which
|
2017-07-10 03:28:12 +00:00
|
|
|
* contains the infix part '[' and the suffix ''] '(specified as' [')
|
|
|
|
*/
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == strcmp(it[0], "["))
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingSquareBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node = function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserVariableArityOperatorList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
|
|
|
ASTPtr arguments;
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
if (!parseOperator(pos, infix, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!arguments)
|
|
|
|
{
|
|
|
|
node = makeASTFunction(function_name, node);
|
|
|
|
arguments = static_cast<ASTFunction &>(*node).arguments;
|
|
|
|
}
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
arguments->children.push_back(elem);
|
|
|
|
}
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments)
|
|
|
|
arguments->range = node->range = StringRange(begin, pos);
|
2014-02-13 11:05:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2014-02-13 11:05:51 +00:00
|
|
|
}
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserBetweenExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-04-03 01:15:53 +00:00
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// For the expression (subject BETWEEN left AND right)
|
|
|
|
/// create an AST the same as for (subject> = left AND subject <= right).
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_between("BETWEEN");
|
|
|
|
ParserKeyword s_and("AND");
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr subject;
|
|
|
|
ASTPtr left;
|
|
|
|
ASTPtr right;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, subject, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_between.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
node = subject;
|
|
|
|
else
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, left, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_and.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, right, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// AND function
|
2017-04-01 07:20:54 +00:00
|
|
|
auto f_and = std::make_shared<ASTFunction>();
|
|
|
|
auto args_and = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// >=
|
|
|
|
auto f_ge = std::make_shared<ASTFunction>();
|
|
|
|
auto args_ge = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// <=
|
|
|
|
auto f_le = std::make_shared<ASTFunction>();
|
|
|
|
auto args_le = std::make_shared<ASTExpressionList>();
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
args_ge->children.emplace_back(subject);
|
|
|
|
args_ge->children.emplace_back(left);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
args_le->children.emplace_back(subject);
|
|
|
|
args_le->children.emplace_back(right);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
f_ge->range.first = begin->begin;
|
|
|
|
f_ge->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
f_ge->name = "greaterOrEquals";
|
|
|
|
f_ge->arguments = args_ge;
|
|
|
|
f_ge->children.emplace_back(f_ge->arguments);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
f_le->range.first = begin->begin;
|
|
|
|
f_le->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
f_le->name = "lessOrEquals";
|
|
|
|
f_le->arguments = args_le;
|
|
|
|
f_le->children.emplace_back(f_le->arguments);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
args_and->children.emplace_back(f_ge);
|
|
|
|
args_and->children.emplace_back(f_le);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
f_and->range.first = begin->begin;
|
|
|
|
f_and->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
f_and->name = "and";
|
|
|
|
f_and->arguments = args_and;
|
|
|
|
f_and->children.emplace_back(f_and->arguments);
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
node = f_and;
|
|
|
|
}
|
2016-04-03 01:15:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-04-03 01:15:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserTernaryOperatorExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2012-09-23 06:16:42 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken symbol1(TokenType::QuestionMark);
|
|
|
|
ParserToken symbol2(TokenType::Colon);
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr elem_cond;
|
|
|
|
ASTPtr elem_then;
|
|
|
|
ASTPtr elem_else;
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, elem_cond, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!symbol1.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
node = elem_cond;
|
|
|
|
else
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, elem_then, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!symbol2.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, elem_else, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// the function corresponding to the operator
|
2017-04-01 07:20:54 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// function arguments
|
2017-04-01 07:20:54 +00:00
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
2016-05-28 15:42:22 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
function->range.first = begin->begin;
|
|
|
|
function->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
function->name = "if";
|
|
|
|
function->arguments = exp_list;
|
|
|
|
function->children.push_back(exp_list);
|
2016-05-28 15:42:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
exp_list->children.push_back(elem_cond);
|
|
|
|
exp_list->children.push_back(elem_then);
|
|
|
|
exp_list->children.push_back(elem_else);
|
2017-07-10 03:28:12 +00:00
|
|
|
exp_list->range.first = begin->begin;
|
|
|
|
exp_list->range.second = pos->begin;
|
2016-05-28 15:42:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
node = function;
|
|
|
|
}
|
2012-09-23 06:16:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2012-09-23 06:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserLambdaExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2013-05-08 09:52:02 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserToken arrow(TokenType::Arrow);
|
|
|
|
ParserToken open(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken close(TokenType::ClosingRoundBracket);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ASTPtr inner_arguments;
|
|
|
|
ASTPtr expression;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool was_open = false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (open.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
was_open = true;
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserList(std::make_unique<ParserIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma)).parse(pos, inner_arguments, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (was_open)
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!close.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!arrow.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser.parse(pos, expression, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// lambda(tuple(inner_arguments), expression)
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto lambda = std::make_shared<ASTFunction>();
|
|
|
|
node = lambda;
|
|
|
|
lambda->name = "lambda";
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto outer_arguments = std::make_shared<ASTExpressionList>();
|
|
|
|
lambda->arguments = outer_arguments;
|
|
|
|
lambda->children.push_back(lambda->arguments);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto tuple = std::make_shared<ASTFunction>();
|
|
|
|
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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
outer_arguments->children.push_back(expression);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
while (false);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
pos = begin;
|
2017-07-10 03:28:12 +00:00
|
|
|
return elem_parser.parse(pos, node, expected);
|
2013-05-08 09:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserPrefixUnaryOperatorExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// try to find any of the valid operators
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
|
|
|
const char ** it;
|
|
|
|
for (it = operators; *it; it += 2)
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
if (parseOperator(pos, *it, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Let's parse chains of the form `NOT NOT x`. This is hack.
|
|
|
|
/** This is done, because among the unary operators there is only a minus and NOT.
|
|
|
|
* But for a minus the chain of unary operators does not need to be supported.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
if (it[0] && 0 == strncmp(it[0], "NOT", 3))
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Was there an even number of NOTs.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool even = false;
|
|
|
|
|
|
|
|
const char ** jt;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
for (jt = operators; *jt; jt += 2)
|
2017-07-13 04:20:56 +00:00
|
|
|
if (parseOperator(pos, *jt, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!*jt)
|
|
|
|
break;
|
|
|
|
|
|
|
|
even = !even;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (even)
|
2017-04-02 17:37:49 +00:00
|
|
|
it = jt; /// Zero the result of parsing the first NOT. It turns out, as if there is no `NOT` chain at all.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!*it)
|
|
|
|
node = elem;
|
|
|
|
else
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// the function corresponding to the operator
|
2017-04-01 07:20:54 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>();
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// function arguments
|
2017-04-01 07:20:54 +00:00
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
function->range.first = begin->begin;
|
|
|
|
function->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
function->name = it[1];
|
|
|
|
function->arguments = exp_list;
|
|
|
|
function->children.push_back(exp_list);
|
|
|
|
|
|
|
|
exp_list->children.push_back(elem);
|
2017-07-10 03:28:12 +00:00
|
|
|
exp_list->range.first = begin->begin;
|
|
|
|
exp_list->range.second = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
node = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserUnaryMinusExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2012-11-20 22:48:38 +00:00
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// As an exception, negative numbers should be parsed as literals, and not as an application of the operator.
|
2012-11-20 22:48:38 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::Minus)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
ParserLiteral lit_p;
|
|
|
|
Pos begin = pos;
|
2012-11-20 22:48:38 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (lit_p.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2012-11-20 22:48:38 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
pos = begin;
|
|
|
|
}
|
2012-11-20 22:48:38 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
return operator_parser.parse(pos, node, expected);
|
2012-11-20 22:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserArrayElementExpression::parseImpl(Pos & pos, ASTPtr & node, Expected &expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return ParserLeftAssociativeBinaryOperatorList{
|
|
|
|
operators,
|
|
|
|
std::make_unique<ParserExpressionElement>(),
|
|
|
|
std::make_unique<ParserExpressionWithOptionalAlias>(false)
|
2017-07-10 03:28:12 +00:00
|
|
|
}.parse(pos, node, expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserTupleElementExpression::parseImpl(Pos & pos, ASTPtr & node, Expected &expected)
|
2015-11-07 23:18:39 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return ParserLeftAssociativeBinaryOperatorList{
|
|
|
|
operators,
|
|
|
|
std::make_unique<ParserArrayElementExpression>(),
|
|
|
|
std::make_unique<ParserUnsignedInteger>()
|
2017-07-10 03:28:12 +00:00
|
|
|
}.parse(pos, node, expected);
|
2015-11-07 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-08 00:28:12 +00:00
|
|
|
ParserExpressionWithOptionalAlias::ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword)
|
2017-07-12 02:40:28 +00:00
|
|
|
: impl(std::make_unique<ParserWithOptionalAlias>(std::make_unique<ParserExpression>(), 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)
|
2017-07-12 02:40:28 +00:00
|
|
|
: impl(std::make_unique<ParserCastExpressionWithOptionalAlias>(std::make_unique<ParserExpression>(), allow_alias_without_as_keyword))
|
2015-12-24 17:14:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return ParserList(
|
|
|
|
std::make_unique<ParserExpressionWithOptionalAlias>(allow_alias_without_as_keyword),
|
2017-07-10 03:28:12 +00:00
|
|
|
std::make_unique<ParserToken>(TokenType::Comma))
|
|
|
|
.parse(pos, node, expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNotEmptyExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
return nested_parser.parse(pos, node, expected)
|
2017-04-01 07:20:54 +00:00
|
|
|
&& !typeid_cast<ASTExpressionList &>(*node).children.empty();
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserOrderByExpressionList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-09-04 05:14:52 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
return ParserList(std::make_unique<ParserOrderByElement>(), std::make_unique<ParserToken>(TokenType::Comma), false)
|
|
|
|
.parse(pos, node, expected);
|
2011-09-04 05:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNullityChecking::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-07-18 16:12:29 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr node_comp;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserComparisonExpression{}.parse(pos, node_comp, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
Pos begin = pos;
|
|
|
|
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_is{"IS"};
|
|
|
|
ParserKeyword s_not{"NOT"};
|
|
|
|
ParserKeyword s_null{"NULL"};
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_is.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
bool is_not = false;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_not.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
is_not = true;
|
2017-07-10 03:28:12 +00:00
|
|
|
|
|
|
|
if (!s_null.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto args = std::make_shared<ASTExpressionList>();
|
|
|
|
args->children.push_back(node_comp);
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>(StringRange{begin, pos});
|
2017-04-01 07:20:54 +00:00
|
|
|
function->name = is_not ? "isNotNull" : "isNull";
|
|
|
|
function->arguments = args;
|
|
|
|
function->children.push_back(function->arguments);
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
node = function;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
node = node_comp;
|
2016-07-18 16:12:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-07-18 16:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|