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
This commit is contained in:
Daniël Heres 2020-06-11 21:00:35 +02:00 committed by GitHub
parent 0fe3a8ec39
commit 34548e890b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 395 additions and 342 deletions

View file

@ -19,7 +19,7 @@
use std::iter::Peekable;
use std::str::Chars;
use super::dialect::keywords::ALL_KEYWORDS;
use super::dialect::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};
use super::dialect::Dialect;
use std::fmt;
@ -146,15 +146,14 @@ impl Token {
}
pub fn make_word(word: &str, quote_style: Option<char>) -> Self {
let word_uppercase = word.to_uppercase();
let is_keyword =
quote_style == None && ALL_KEYWORDS.binary_search(&word_uppercase.as_str()).is_ok();
Token::Word(Word {
value: word.to_string(),
quote_style,
keyword: if is_keyword {
word_uppercase
keyword: if quote_style == None {
let keyword = ALL_KEYWORDS.binary_search(&word_uppercase.as_str());
keyword.map_or(Keyword::NoKeyword, |x| ALL_KEYWORDS_INDEX[x])
} else {
"".to_string()
Keyword::NoKeyword
},
})
}
@ -172,7 +171,7 @@ pub struct Word {
pub quote_style: Option<char>,
/// If the word was not quoted and it matched one of the known keywords,
/// this will have one of the values from dialect::keywords, otherwise empty
pub keyword: String,
pub keyword: Keyword,
}
impl fmt::Display for Word {