Skip to content

Store keywords in enum ~2x perf. improvement #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,24 @@ macro_rules! define_keywords {
($(
$ident:ident $(= $string_keyword:expr)?
),*) => {
$(kw_def!($ident $(= $string_keyword)?);)*
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
#[allow(non_camel_case_types)]
pub enum Keyword {
NoKeyword,
$($ident),*
}

pub const ALL_KEYWORDS_INDEX: &[Keyword] = &[
$(Keyword::$ident),*
];

$(kw_def!($ident $(= $string_keyword)?);)*
pub const ALL_KEYWORDS: &[&str] = &[
$($ident),*
];
}

};

}

// The following keywords should be sorted to be able to match using binary search
Expand Down Expand Up @@ -434,20 +446,46 @@ define_keywords!(

/// These keywords can't be used as a table alias, so that `FROM table_name alias`
/// can be parsed unambiguously without looking ahead.
pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
// Reserved as both a table and a column alias:
WITH, SELECT, WHERE, GROUP, HAVING, ORDER, TOP, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
ON, JOIN, INNER, CROSS, FULL, LEFT, RIGHT, NATURAL, USING,
// for MSSQL-specific OUTER APPLY (seems reserved in most dialects)
OUTER,
pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
Keyword::CROSS,
Keyword::EXCEPT,
Keyword::FETCH,
Keyword::FULL,
Keyword::GROUP,
Keyword::HAVING,
Keyword::INNER,
Keyword::INTERSECT,
Keyword::JOIN,
Keyword::LEFT,
Keyword::LIMIT,
Keyword::NATURAL,
Keyword::OFFSET,
Keyword::ON,
Keyword::ORDER,
Keyword::OUTER,
Keyword::RIGHT,
Keyword::SELECT,
Keyword::TOP,
Keyword::UNION,
Keyword::USING,
Keyword::WHERE,
Keyword::WITH,
];

/// Can't be used as a column alias, so that `SELECT <expr> alias`
/// can be parsed unambiguously without looking ahead.
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
// Reserved as both a table and a column alias:
WITH, SELECT, WHERE, GROUP, HAVING, ORDER, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
// Reserved only as a column alias in the `SELECT` clause:
FROM,
pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
Keyword::EXCEPT,
Keyword::FETCH,
Keyword::FROM,
Keyword::GROUP,
Keyword::HAVING,
Keyword::INTERSECT,
Keyword::LIMIT,
Keyword::OFFSET,
Keyword::ORDER,
Keyword::SELECT,
Keyword::UNION,
Keyword::WHERE,
Keyword::WITH,
];
Loading