2011-09-04 05:14:52 +00:00
|
|
|
|
#pragma once
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
2010-06-25 16:36:13 +00:00
|
|
|
|
#include <list>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/assign/list_of.hpp>
|
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/IParserBase.h>
|
|
|
|
|
#include <DB/Parsers/CommonParsers.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2010-06-25 16:36:13 +00:00
|
|
|
|
/** Оператор и соответствующая ему функция. Например, "+" -> "plus"
|
|
|
|
|
* Не std::map, так как порядок парсинга операторов задаётся явно и может отличаться от алфавитного.
|
|
|
|
|
*/
|
|
|
|
|
typedef std::list<std::pair<String, String> > Operators_t;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
|
/** Список элементов, разделённых чем-либо. */
|
|
|
|
|
class ParserList : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ParserList(ParserPtr elem_parser_, ParserPtr separator_parser_, bool allow_empty_ = true)
|
|
|
|
|
: elem_parser(elem_parser_), separator_parser(separator_parser_), allow_empty(allow_empty_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "list of elements (" + elem_parser->getName() + ")"; }
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserPtr separator_parser;
|
|
|
|
|
bool allow_empty;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
/** Выражение с инфиксным бинарным лево-ассоциативным оператором.
|
|
|
|
|
* Например, a + b - c + d.
|
|
|
|
|
* NOTE: если оператор словесный (например, OR), то после него не требуется границы слова.
|
|
|
|
|
* то есть, можно написать a = b ORx = y.
|
|
|
|
|
*/
|
|
|
|
|
class ParserLeftAssociativeBinaryOperatorList : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
2010-06-24 19:37:23 +00:00
|
|
|
|
Operators_t operators;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** operators_ - допустимые операторы и соответствующие им функции
|
|
|
|
|
*/
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList(const Operators_t & operators_, ParserPtr elem_parser_)
|
|
|
|
|
: operators(operators_), elem_parser(elem_parser_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "list, delimited by binary operators"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Выражение с префиксным унарным оператором.
|
|
|
|
|
* Например, NOT x.
|
|
|
|
|
* NOTE: если оператор словесный (например, NOT), то после него не требуется границы слова.
|
|
|
|
|
* то есть, можно написать NOTx.
|
|
|
|
|
*/
|
|
|
|
|
class ParserPrefixUnaryOperatorExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
2010-06-24 19:37:23 +00:00
|
|
|
|
Operators_t operators;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** operators_ - допустимые операторы и соответствующие им функции
|
|
|
|
|
*/
|
|
|
|
|
ParserPrefixUnaryOperatorExpression(const Operators_t & operators_, ParserPtr elem_parser_)
|
|
|
|
|
: operators(operators_), elem_parser(elem_parser_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "expression with prefix unary operator"; }
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserAccessExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserAccessExpression();
|
|
|
|
|
|
|
|
|
|
protected:
|
2010-06-24 19:37:23 +00:00
|
|
|
|
String getName() { return "access expression"; }
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserUnaryMinusExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserPrefixUnaryOperatorExpression operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserUnaryMinusExpression()
|
|
|
|
|
: elem_parser(new ParserAccessExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of("-", "negate"), elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "unary minus expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserMultiplicativeExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserMultiplicativeExpression()
|
|
|
|
|
: elem_parser(new ParserUnaryMinusExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of
|
|
|
|
|
("*", "multiply")
|
|
|
|
|
("/", "divide")
|
|
|
|
|
("%", "modulo"),
|
|
|
|
|
elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "multiplicative expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserAdditiveExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserAdditiveExpression()
|
|
|
|
|
: elem_parser(new ParserMultiplicativeExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of
|
|
|
|
|
("+", "plus")
|
|
|
|
|
("-", "minus"),
|
|
|
|
|
elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "additive expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserComparisonExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserComparisonExpression()
|
|
|
|
|
: elem_parser(new ParserAdditiveExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of
|
2011-10-17 08:28:39 +00:00
|
|
|
|
("==", "equals")
|
|
|
|
|
("!=", "notEquals")
|
|
|
|
|
("<>", "notEquals")
|
|
|
|
|
("<=", "lessOrEquals")
|
|
|
|
|
(">=", "greaterOrEquals")
|
|
|
|
|
("<", "less")
|
|
|
|
|
(">", "greater")
|
|
|
|
|
("=", "equals")
|
|
|
|
|
("LIKE", "like")
|
|
|
|
|
("NOT LIKE", "notLike"),
|
2010-06-24 19:12:10 +00:00
|
|
|
|
elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "comparison expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserLogicalNotExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserPrefixUnaryOperatorExpression operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserLogicalNotExpression()
|
|
|
|
|
: elem_parser(new ParserComparisonExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of("NOT", "not"), elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "logical-NOT expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserLogicalAndExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserLogicalAndExpression()
|
|
|
|
|
: elem_parser(new ParserLogicalNotExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of("AND", "and"), elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "logical-AND expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserLogicalOrExpression : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserPtr elem_parser;
|
|
|
|
|
ParserLeftAssociativeBinaryOperatorList operator_parser;
|
|
|
|
|
public:
|
|
|
|
|
ParserLogicalOrExpression()
|
|
|
|
|
: elem_parser(new ParserLogicalAndExpression),
|
|
|
|
|
operator_parser(boost::assign::map_list_of("OR", "or"), elem_parser)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "logical-OR expression"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return operator_parser.parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-11-06 20:47:07 +00:00
|
|
|
|
class ParserExpressionWithOptionalAlias : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ParserExpressionWithOptionalAlias();
|
|
|
|
|
protected:
|
|
|
|
|
ParserPtr impl;
|
|
|
|
|
|
|
|
|
|
String getName() { return "expression with optional alias"; }
|
|
|
|
|
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
|
{
|
|
|
|
|
return impl->parse(pos, end, node, expected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
/** Список выражений, разделённых запятыми, возможно пустой. */
|
|
|
|
|
class ParserExpressionList : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "list of expressions"; }
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParserNotEmptyExpressionList : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ParserExpressionList nested_parser;
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "not empty list of expressions"; }
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-09-04 05:14:52 +00:00
|
|
|
|
class ParserOrderByExpressionList : public IParserBase
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
String getName() { return "order by expression"; }
|
|
|
|
|
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected);
|
|
|
|
|
};
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
|
2011-09-04 05:14:52 +00:00
|
|
|
|
}
|