Skip to content

Commit 34548e8

Browse files
authored
Change Word::keyword to a enum (#193)
This improves performance and paves the way to future API enhancements as discussed in the PR https://github.com/andygrove/sqlparser-rs/pull/193
1 parent 0fe3a8e commit 34548e8

File tree

5 files changed

+395
-342
lines changed

5 files changed

+395
-342
lines changed

src/ast/mod.rs

-16
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,6 @@ impl fmt::Display for WindowFrameUnits {
388388
}
389389
}
390390

391-
impl FromStr for WindowFrameUnits {
392-
type Err = ParserError;
393-
394-
fn from_str(s: &str) -> Result<Self, Self::Err> {
395-
match s {
396-
"ROWS" => Ok(WindowFrameUnits::Rows),
397-
"RANGE" => Ok(WindowFrameUnits::Range),
398-
"GROUPS" => Ok(WindowFrameUnits::Groups),
399-
_ => Err(ParserError::ParserError(format!(
400-
"Expected ROWS, RANGE, or GROUPS, found: {}",
401-
s
402-
))),
403-
}
404-
}
405-
}
406-
407391
/// Specifies [WindowFrame]'s `start_bound` and `end_bound`
408392
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
409393
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/dialect/keywords.rs

+54-10
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,24 @@ macro_rules! define_keywords {
4141
($(
4242
$ident:ident $(= $string_keyword:expr)?
4343
),*) => {
44-
$(kw_def!($ident $(= $string_keyword)?);)*
44+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
45+
#[allow(non_camel_case_types)]
46+
pub enum Keyword {
47+
NoKeyword,
48+
$($ident),*
49+
}
4550

51+
pub const ALL_KEYWORDS_INDEX: &[Keyword] = &[
52+
$(Keyword::$ident),*
53+
];
54+
55+
$(kw_def!($ident $(= $string_keyword)?);)*
4656
pub const ALL_KEYWORDS: &[&str] = &[
4757
$($ident),*
4858
];
49-
}
59+
60+
};
61+
5062
}
5163

5264
// The following keywords should be sorted to be able to match using binary search
@@ -434,20 +446,52 @@ define_keywords!(
434446

435447
/// These keywords can't be used as a table alias, so that `FROM table_name alias`
436448
/// can be parsed unambiguously without looking ahead.
437-
pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
449+
pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
438450
// Reserved as both a table and a column alias:
439-
WITH, SELECT, WHERE, GROUP, HAVING, ORDER, TOP, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
451+
Keyword::WITH,
452+
Keyword::SELECT,
453+
Keyword::WHERE,
454+
Keyword::GROUP,
455+
Keyword::HAVING,
456+
Keyword::ORDER,
457+
Keyword::TOP,
458+
Keyword::LIMIT,
459+
Keyword::OFFSET,
460+
Keyword::FETCH,
461+
Keyword::UNION,
462+
Keyword::EXCEPT,
463+
Keyword::INTERSECT,
440464
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
441-
ON, JOIN, INNER, CROSS, FULL, LEFT, RIGHT, NATURAL, USING,
465+
Keyword::ON,
466+
Keyword::JOIN,
467+
Keyword::INNER,
468+
Keyword::CROSS,
469+
Keyword::FULL,
470+
Keyword::LEFT,
471+
Keyword::RIGHT,
472+
Keyword::NATURAL,
473+
Keyword::USING,
442474
// for MSSQL-specific OUTER APPLY (seems reserved in most dialects)
443-
OUTER,
475+
Keyword::OUTER,
444476
];
445477

446478
/// Can't be used as a column alias, so that `SELECT <expr> alias`
447479
/// can be parsed unambiguously without looking ahead.
448-
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
480+
pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
449481
// Reserved as both a table and a column alias:
450-
WITH, SELECT, WHERE, GROUP, HAVING, ORDER, LIMIT, OFFSET, FETCH, UNION, EXCEPT, INTERSECT,
451-
// Reserved only as a column alias in the `SELECT` clause:
452-
FROM,
482+
Keyword::WITH,
483+
Keyword::SELECT,
484+
Keyword::WHERE,
485+
Keyword::GROUP,
486+
Keyword::HAVING,
487+
Keyword::ORDER,
488+
Keyword::TOP,
489+
Keyword::LIMIT,
490+
Keyword::OFFSET,
491+
Keyword::FETCH,
492+
Keyword::UNION,
493+
Keyword::EXCEPT,
494+
Keyword::INTERSECT,
495+
// Reserved only as a column alias in the `SELECT` clause
496+
Keyword::FROM,
453497
];

0 commit comments

Comments
 (0)