pub struct Parser<'a> { /* private fields */ }
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn parse_sql(
dialect: &dyn Dialect,
sql: &str,
) -> Result<Vec<Statement>, ParserError>
pub fn parse_sql( dialect: &dyn Dialect, sql: &str, ) -> Result<Vec<Statement>, ParserError>
Parse a SQL statement and produce an Abstract Syntax Tree (AST)
Sourcepub fn parse_statement(&mut self) -> Result<Statement, ParserError>
pub fn parse_statement(&mut self) -> Result<Statement, ParserError>
Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.), stopping before the statement separator, if any.
pub fn parse_msck(&mut self) -> Result<Msck, ParserError>
pub fn parse_truncate(&mut self) -> Result<Truncate, ParserError>
pub fn parse_analyze(&mut self) -> Result<Analyze, ParserError>
Sourcepub fn parse_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_expr(&mut self) -> Result<Expr, ParserError>
Parse a new expression
Sourcepub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError>
pub fn parse_subexpr(&mut self, precedence: u8) -> Result<Expr, ParserError>
Parse tokens until the precedence changes
pub fn parse_assert(&mut self) -> Result<Assert, ParserError>
Sourcepub fn parse_prefix(&mut self) -> Result<Expr, ParserError>
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError>
Parse an expression prefix
pub fn parse_function(&mut self, name: ObjectName) -> Result<Expr, ParserError>
pub fn parse_window_frame_units( &mut self, ) -> Result<WindowFrameUnits, ParserError>
pub fn parse_window_frame(&mut self) -> Result<WindowFrame, ParserError>
Sourcepub fn parse_window_frame_bound(
&mut self,
) -> Result<WindowFrameBound, ParserError>
pub fn parse_window_frame_bound( &mut self, ) -> Result<WindowFrameBound, ParserError>
Parse CURRENT ROW
or { <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }
pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError>
Sourcepub fn parse_cast_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_cast_expr(&mut self) -> Result<Expr, ParserError>
Parse a SQL CAST function e.g. CAST(expr AS FLOAT)
Sourcepub fn parse_try_cast_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_try_cast_expr(&mut self) -> Result<Expr, ParserError>
Parse a SQL TRY_CAST function e.g. TRY_CAST(expr AS FLOAT)
Sourcepub fn parse_exists_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_exists_expr(&mut self) -> Result<Expr, ParserError>
Parse a SQL EXISTS expression e.g. WHERE EXISTS(SELECT ...)
.
pub fn parse_extract_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError>
Sourcepub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError>
TRIM (WHERE ‘text’ FROM ‘text’)
TRIM (‘text’)
pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError>
Sourcepub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError>
pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError>
Parse a SQL LISTAGG expression, e.g. LISTAGG(...) WITHIN GROUP (ORDER BY ...)
.
pub fn parse_date_time_field(&mut self) -> Result<DateTimeField, ParserError>
Sourcepub fn parse_literal_interval(&mut self) -> Result<Expr, ParserError>
pub fn parse_literal_interval(&mut self) -> Result<Expr, ParserError>
Parse an INTERVAL literal.
Some syntactically valid intervals:
INTERVAL '1' DAY
INTERVAL '1-1' YEAR TO MONTH
INTERVAL '1' SECOND
INTERVAL '1:1:1.1' HOUR (5) TO SECOND (5)
INTERVAL '1.1' SECOND (2, 2)
INTERVAL '1:1' HOUR (5) TO MINUTE (5)
Note that we do not currently attempt to parse the quoted value.
Sourcepub fn parse_infix(
&mut self,
expr: Expr,
precedence: u8,
) -> Result<Expr, ParserError>
pub fn parse_infix( &mut self, expr: Expr, precedence: u8, ) -> Result<Expr, ParserError>
Parse an operator following an expression
pub fn parse_map_access(&mut self, expr: Expr) -> Result<Expr, ParserError>
Sourcepub fn parse_in(
&mut self,
expr: Expr,
negated: bool,
) -> Result<Expr, ParserError>
pub fn parse_in( &mut self, expr: Expr, negated: bool, ) -> Result<Expr, ParserError>
Parses the parens following the [ NOT ] IN
operator
Sourcepub fn parse_between(
&mut self,
expr: Expr,
negated: bool,
) -> Result<Expr, ParserError>
pub fn parse_between( &mut self, expr: Expr, negated: bool, ) -> Result<Expr, ParserError>
Parses BETWEEN <low> AND <high>
, assuming the BETWEEN
keyword was already consumed
Sourcepub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError>
pub fn parse_pg_cast(&mut self, expr: Expr) -> Result<Expr, ParserError>
Parse a postgresql casting style which is in the form of expr::datatype
Sourcepub fn get_next_precedence(&self) -> Result<u8, ParserError>
pub fn get_next_precedence(&self) -> Result<u8, ParserError>
Get the precedence of the next token
Sourcepub fn peek_token(&self) -> Token
pub fn peek_token(&self) -> Token
Return the first non-whitespace token that has not yet been processed (or None if reached end-of-file)
Sourcepub fn peek_nth_token(&self, n: usize) -> Token
pub fn peek_nth_token(&self, n: usize) -> Token
Return nth non-whitespace token that has not yet been processed
Sourcepub fn next_token(&mut self) -> Token
pub fn next_token(&mut self) -> Token
Return the first non-whitespace token that has not yet been processed (or None if reached end-of-file) and mark it as processed. OK to call repeatedly after reaching EOF.
Sourcepub fn next_token_no_skip(&mut self) -> Option<&Token>
pub fn next_token_no_skip(&mut self) -> Option<&Token>
Return the first unprocessed token, possibly whitespace.
Sourcepub fn prev_token(&mut self)
pub fn prev_token(&mut self)
Push back the last one non-whitespace token. Must be called after
next_token()
, otherwise might panic. OK to call after
next_token()
indicates an EOF.
Sourcepub fn parse_keyword(&mut self, expected: Keyword) -> bool
pub fn parse_keyword(&mut self, expected: Keyword) -> bool
Look for an expected keyword and consume it if it exists
Sourcepub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool
pub fn parse_keywords(&mut self, keywords: &[Keyword]) -> bool
Look for an expected sequence of keywords and consume them if they exist
Sourcepub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword>
pub fn parse_one_of_keywords(&mut self, keywords: &[Keyword]) -> Option<Keyword>
Look for one of the given keywords and return the one that matches.
Sourcepub fn expect_one_of_keywords(
&mut self,
keywords: &[Keyword],
) -> Result<Keyword, ParserError>
pub fn expect_one_of_keywords( &mut self, keywords: &[Keyword], ) -> Result<Keyword, ParserError>
Bail out if the current token is not one of the expected keywords, or consume it if it is
Sourcepub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError>
pub fn expect_keyword(&mut self, expected: Keyword) -> Result<(), ParserError>
Bail out if the current token is not an expected keyword, or consume it if it is
Sourcepub fn expect_keywords(
&mut self,
expected: &[Keyword],
) -> Result<(), ParserError>
pub fn expect_keywords( &mut self, expected: &[Keyword], ) -> Result<(), ParserError>
Bail out if the following tokens are not the expected sequence of keywords, or consume them if they are.
Sourcepub fn consume_token(&mut self, expected: &Token) -> bool
pub fn consume_token(&mut self, expected: &Token) -> bool
Consume the next token if it matches the expected token, otherwise return false
Sourcepub fn expect_token(&mut self, expected: &Token) -> Result<(), ParserError>
pub fn expect_token(&mut self, expected: &Token) -> Result<(), ParserError>
Bail out if the current token is not an expected keyword, or consume it if it is
Sourcepub fn parse_comma_separated<T, F>(
&mut self,
f: F,
) -> Result<Vec<T>, ParserError>
pub fn parse_comma_separated<T, F>( &mut self, f: F, ) -> Result<Vec<T>, ParserError>
Parse a comma-separated list of 1+ items accepted by F
Sourcepub fn parse_all_or_distinct(&mut self) -> Result<bool, ParserError>
pub fn parse_all_or_distinct(&mut self) -> Result<bool, ParserError>
Parse either ALL
or DISTINCT
. Returns true
if DISTINCT
is parsed and results in a
ParserError
if both ALL
and DISTINCT
are fround.
Sourcepub fn parse_create(&mut self) -> Result<Statement, ParserError>
pub fn parse_create(&mut self) -> Result<Statement, ParserError>
Parse a SQL CREATE statement
Sourcepub fn parse_create_virtual_table(
&mut self,
) -> Result<CreateVirtualTable, ParserError>
pub fn parse_create_virtual_table( &mut self, ) -> Result<CreateVirtualTable, ParserError>
SQLite-specific CREATE VIRTUAL TABLE
pub fn parse_create_schema(&mut self) -> Result<CreateSchema, ParserError>
pub fn parse_create_database(&mut self) -> Result<CreateDatabase, ParserError>
pub fn parse_create_external_table( &mut self, or_replace: bool, ) -> Result<CreateTable, ParserError>
pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError>
pub fn parse_create_view( &mut self, or_replace: bool, ) -> Result<CreateView, ParserError>
pub fn parse_drop(&mut self) -> Result<Drop, ParserError>
pub fn parse_create_index( &mut self, unique: bool, ) -> Result<CreateIndex, ParserError>
pub fn parse_hive_distribution( &mut self, ) -> Result<HiveDistributionStyle, ParserError>
pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError>
pub fn parse_row_format(&mut self) -> Result<HiveRowFormat, ParserError>
pub fn parse_create_table( &mut self, or_replace: bool, temporary: bool, ) -> Result<CreateTable, ParserError>
pub fn parse_optional_column_option( &mut self, ) -> Result<Option<ColumnOption>, ParserError>
pub fn parse_referential_action( &mut self, ) -> Result<ReferentialAction, ParserError>
pub fn parse_optional_table_constraint( &mut self, ) -> Result<Option<TableConstraint>, ParserError>
pub fn parse_options( &mut self, keyword: Keyword, ) -> Result<Vec<SqlOption>, ParserError>
pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError>
pub fn parse_alter(&mut self) -> Result<AlterTable, ParserError>
Sourcepub fn parse_copy(&mut self) -> Result<Copy, ParserError>
pub fn parse_copy(&mut self) -> Result<Copy, ParserError>
Parse a copy statement
pub fn parse_number_value(&mut self) -> Result<Value, ParserError>
Sourcepub fn parse_literal_uint(&mut self) -> Result<u64, ParserError>
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError>
Parse an unsigned literal integer/long
Sourcepub fn parse_literal_string(&mut self) -> Result<String, ParserError>
pub fn parse_literal_string(&mut self) -> Result<String, ParserError>
Parse a literal string
Sourcepub fn parse_data_type(&mut self) -> Result<DataType, ParserError>
pub fn parse_data_type(&mut self) -> Result<DataType, ParserError>
Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
Sourcepub fn parse_optional_alias(
&mut self,
reserved_kwds: &[Keyword],
) -> Result<Option<Ident>, ParserError>
pub fn parse_optional_alias( &mut self, reserved_kwds: &[Keyword], ) -> Result<Option<Ident>, ParserError>
Parse AS identifier
(or simply identifier
if it’s not a reserved keyword)
Some examples with aliases: SELECT 1 foo
, SELECT COUNT(*) AS cnt
,
SELECT ... FROM t1 foo, t2 bar
, SELECT ... FROM (...) AS bar
Sourcepub fn parse_optional_table_alias(
&mut self,
reserved_kwds: &[Keyword],
) -> Result<Option<TableAlias>, ParserError>
pub fn parse_optional_table_alias( &mut self, reserved_kwds: &[Keyword], ) -> Result<Option<TableAlias>, ParserError>
Parse AS identifier
when the AS is describing a table-valued object,
like in ... FROM generate_series(1, 10) AS t (col)
. In this case
the alias is allowed to optionally name the columns in the table, in
addition to the table itself.
Sourcepub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError>
pub fn parse_object_name(&mut self) -> Result<ObjectName, ParserError>
Parse a possibly qualified, possibly quoted identifier, e.g.
foo
or `myschema.“table”
Sourcepub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError>
pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError>
Parse identifiers
Sourcepub fn parse_identifier(&mut self) -> Result<Ident, ParserError>
pub fn parse_identifier(&mut self) -> Result<Ident, ParserError>
Parse a simple one-word identifier (possibly quoted, possibly a keyword)
Sourcepub fn parse_parenthesized_column_list(
&mut self,
optional: IsOptional,
) -> Result<Vec<Ident>, ParserError>
pub fn parse_parenthesized_column_list( &mut self, optional: IsOptional, ) -> Result<Vec<Ident>, ParserError>
Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError>
pub fn parse_optional_precision_scale( &mut self, ) -> Result<(Option<u64>, Option<u64>), ParserError>
pub fn parse_delete(&mut self) -> Result<Delete, ParserError>
pub fn parse_explain(&mut self) -> Result<Explain, ParserError>
Sourcepub fn parse_query(&mut self) -> Result<Query, ParserError>
pub fn parse_query(&mut self) -> Result<Query, ParserError>
Parse a query expression, i.e. a SELECT
statement optionally
preceeded with some WITH
CTE declarations and optionally followed
by ORDER BY
. Unlike some other parse_… methods, this one doesn’t
expect the initial keyword to be already consumed
Sourcepub fn parse_select(&mut self) -> Result<Select, ParserError>
pub fn parse_select(&mut self) -> Result<Select, ParserError>
Parse a restricted SELECT
statement (no CTEs / UNION
/ ORDER BY
),
assuming the initial SELECT
was already consumed
pub fn parse_set(&mut self) -> Result<Statement, ParserError>
pub fn parse_show(&mut self) -> Result<Statement, ParserError>
pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError>
Sourcepub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError>
pub fn parse_table_factor(&mut self) -> Result<TableFactor, ParserError>
A table name or a parenthesized subquery, followed by optional [AS] alias
pub fn parse_derived_table_factor( &mut self, lateral: IsLateral, ) -> Result<TableFactor, ParserError>
Sourcepub fn parse_insert(&mut self) -> Result<Statement, ParserError>
pub fn parse_insert(&mut self) -> Result<Statement, ParserError>
Parse an INSERT statement
pub fn parse_update(&mut self) -> Result<Update, ParserError>
Sourcepub fn parse_assignment(&mut self) -> Result<Assignment, ParserError>
pub fn parse_assignment(&mut self) -> Result<Assignment, ParserError>
Parse a var = expr
assignment, used in an UPDATE statement
pub fn parse_optional_args(&mut self) -> Result<Vec<FunctionArg>, ParserError>
Sourcepub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError>
pub fn parse_select_item(&mut self) -> Result<SelectItem, ParserError>
Parse a comma-delimited list of projections after SELECT
Sourcepub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError>
pub fn parse_order_by_expr(&mut self) -> Result<OrderByExpr, ParserError>
Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
Sourcepub fn parse_top(&mut self) -> Result<Top, ParserError>
pub fn parse_top(&mut self) -> Result<Top, ParserError>
Parse a TOP clause, MSSQL equivalent of LIMIT, that follows after SELECT [DISTINCT].
Sourcepub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError>
pub fn parse_limit(&mut self) -> Result<Option<Expr>, ParserError>
Parse a LIMIT clause
Sourcepub fn parse_offset(&mut self) -> Result<Offset, ParserError>
pub fn parse_offset(&mut self) -> Result<Offset, ParserError>
Parse an OFFSET clause
Sourcepub fn parse_fetch(&mut self) -> Result<Fetch, ParserError>
pub fn parse_fetch(&mut self) -> Result<Fetch, ParserError>
Parse a FETCH clause