From e08a7cf1aa342ec0b96179ee847cee1a0adabe56 Mon Sep 17 00:00:00 2001 From: Almann Goo Date: Thu, 3 Jun 2021 22:42:07 -0700 Subject: [PATCH] Fixes an ambiguity with identifiers and keywords. (#32) Previously, `SELECTY` would be parsed as keyword `SELECT` identifier `Y`. This fixes the negative look-ahead assertion for keywords to make sure a non-quoted identifier character follows and similarly adds a look-ahead assertion for the prefix of identifiers to not be keywords (which includes the previous assertion). Also fixes a unit test name for identifiers. --- partiql-parser/src/partiql.pest | 6 ++++-- partiql-parser/src/scanner.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/partiql-parser/src/partiql.pest b/partiql-parser/src/partiql.pest index 986f117c..cf2716fe 100644 --- a/partiql-parser/src/partiql.pest +++ b/partiql-parser/src/partiql.pest @@ -155,7 +155,7 @@ StringContent = { Identifier = { NonQuotedIdentifier | QuotedIdentifier } NonQuotedIdentifier = @{ - !Keyword ~ NonQuotedIdentifierStart ~ NonQuotedIdentifierCont* + !(Keyword) ~ NonQuotedIdentifierStart ~ NonQuotedIdentifierCont* } NonQuotedIdentifierStart = _{ "_" | "$" | 'a'..'z' | 'A'..'Z' } @@ -173,7 +173,9 @@ QuotedIdentifierContent = { // Keywords // -Keyword = { SqlKeyword | PartiqlKeyword } +Keyword = @{ + (SqlKeyword | PartiqlKeyword) ~ !NonQuotedIdentifierCont +} SqlKeyword = _{ Absolute_ diff --git a/partiql-parser/src/scanner.rs b/partiql-parser/src/scanner.rs index 0b50cc6c..6492237f 100644 --- a/partiql-parser/src/scanner.rs +++ b/partiql-parser/src/scanner.rs @@ -509,7 +509,7 @@ mod test { "WHERE" => keyword("WHERE") ] )] - #[case::some_keywords( + #[case::some_identifiers( scanner_test_case![ "moo_cow_1999" => identifier("moo_cow_1999"), " ", @@ -518,6 +518,13 @@ mod test { "$$$$" => identifier("$$$$") ] )] + #[case::identifiers_with_keywords_in_them( + scanner_test_case![ + "moowhere" => identifier("moowhere"), + " ", + "selecty" => identifier("selecty"), + ] + )] #[case::quoted_identifiers( scanner_test_case![ " ",