ClickHouse/dbms/src/Parsers/CommonParsers.cpp

155 lines
3.4 KiB
C++
Raw Normal View History

#include <Common/StringUtils.h>
#include <Parsers/CommonParsers.h>
2016-11-12 17:55:40 +00:00
#include <string.h> /// strncmp, strncasecmp
2016-11-12 17:55:40 +00:00
2016-11-20 12:43:20 +00:00
2016-11-12 17:55:40 +00:00
namespace DB {
ParserString::ParserString(const char * s_, bool word_boundary_, bool case_insensitive_)
: s(s_)
, s_size(strlen(s))
, word_boundary(word_boundary_)
, case_insensitive(case_insensitive_)
2016-11-12 17:55:40 +00:00
{
}
const char * ParserString::getName() const
{
return s;
2016-11-12 17:55:40 +00:00
}
bool ParserString::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (static_cast<ssize_t>(s_size) > end - pos || (case_insensitive ? strncasecmp : strncmp)(pos, s, s_size))
return false;
else
{
if (word_boundary && s_size && isWordCharASCII(s[s_size - 1])
&& pos + s_size != end && isWordCharASCII(pos[s_size]))
return false;
2016-11-12 17:55:40 +00:00
pos += s_size;
return true;
}
2016-11-12 17:55:40 +00:00
}
2017-06-15 17:55:57 +00:00
ParserWhitespace::ParserWhitespace(bool allow_newlines_)
: allow_newlines(allow_newlines_)
2016-11-12 17:55:40 +00:00
{
}
2017-06-15 17:55:57 +00:00
const char * ParserWhitespace::getName() const
2016-11-12 17:55:40 +00:00
{
return "white space";
2016-11-12 17:55:40 +00:00
}
2017-06-15 17:55:57 +00:00
bool ParserWhitespace::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
2016-11-12 17:55:40 +00:00
{
Pos begin = pos;
while (pos < end && (*pos == ' ' || *pos == '\t' || (allow_newlines && *pos == '\n') || *pos == '\r' || *pos == '\f'))
++pos;
2016-11-12 17:55:40 +00:00
return pos != begin;
2016-11-12 17:55:40 +00:00
}
const char * ParserCStyleComment::getName() const
{
return "C-style comment";
2016-11-12 17:55:40 +00:00
}
bool ParserCStyleComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (end - pos >= 4 && pos[0] == '/' && pos[1] == '*')
{
pos += 2;
while (end - pos >= 2 && (pos[0] != '*' || pos[1] != '/'))
++pos;
if (end - pos < 2)
{
expected = "closing of C-style comment '*/'";
return false;
}
else
{
pos += 2;
return true;
}
}
else
return false;
2016-11-12 17:55:40 +00:00
}
const char * ParserSQLStyleComment::getName() const
{
return "SQL-style comment";
2016-11-12 17:55:40 +00:00
}
bool ParserSQLStyleComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
if (end - pos >= 2 && pos[0] == '-' && pos[1] == '-')
{
pos += 2;
while (pos != end && *pos != '\n')
++pos;
2016-11-12 17:55:40 +00:00
if (pos != end)
++pos;
return true;
}
else
return false;
2016-11-12 17:55:40 +00:00
}
const char * ParserComment::getName() const
{
return "comment";
2016-11-12 17:55:40 +00:00
}
bool ParserComment::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
ParserCStyleComment p1;
ParserSQLStyleComment p2;
2016-11-12 17:55:40 +00:00
return p1.ignore(pos, end, max_parsed_pos, expected)
|| p2.ignore(pos, end, max_parsed_pos, expected);
2016-11-12 17:55:40 +00:00
}
2017-06-15 17:55:57 +00:00
ParserWhitespaceOrComments::ParserWhitespaceOrComments(bool allow_newlines_outside_comments_)
: allow_newlines_outside_comments(allow_newlines_outside_comments_)
2016-11-12 17:55:40 +00:00
{
}
2017-06-15 17:55:57 +00:00
const char * ParserWhitespaceOrComments::getName() const
2016-11-12 17:55:40 +00:00
{
return "white space or comments";
2016-11-12 17:55:40 +00:00
}
2017-06-15 17:55:57 +00:00
bool ParserWhitespaceOrComments::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
2016-11-12 17:55:40 +00:00
{
2017-06-15 17:55:57 +00:00
ParserWhitespace p1(allow_newlines_outside_comments);
ParserComment p2;
2016-11-12 17:55:40 +00:00
bool res = false;
while (p1.ignore(pos, end, max_parsed_pos, expected) || p2.ignore(pos, end, max_parsed_pos, expected))
res = true;
return res;
2016-11-12 17:55:40 +00:00
}
}