Merge pull request #134 from umanwizard/expect_keywords

add `expect_keywords` function
This commit is contained in:
Nikhil Benesch 2019-08-13 16:00:38 -04:00 committed by GitHub
commit 35a20091ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -802,6 +802,15 @@ impl Parser {
} }
} }
/// Bail out if the following tokens are not the expected sequence of
/// keywords, or consume them if they are.
pub fn expect_keywords(&mut self, expected: &[&'static str]) -> Result<(), ParserError> {
for kw in expected {
self.expect_keyword(kw)?;
}
Ok(())
}
/// Consume the next token if it matches the expected token, otherwise return false /// Consume the next token if it matches the expected token, otherwise return false
#[must_use] #[must_use]
pub fn consume_token(&mut self, expected: &Token) -> bool { pub fn consume_token(&mut self, expected: &Token) -> bool {
@ -856,8 +865,7 @@ impl Parser {
self.expect_keyword("TABLE")?; self.expect_keyword("TABLE")?;
let table_name = self.parse_object_name()?; let table_name = self.parse_object_name()?;
let (columns, constraints) = self.parse_columns()?; let (columns, constraints) = self.parse_columns()?;
self.expect_keyword("STORED")?; self.expect_keywords(&["STORED", "AS"])?;
self.expect_keyword("AS")?;
let file_format = self.parse_identifier()?.parse::<FileFormat>()?; let file_format = self.parse_identifier()?.parse::<FileFormat>()?;
self.expect_keyword("LOCATION")?; self.expect_keyword("LOCATION")?;
@ -1111,8 +1119,7 @@ impl Parser {
pub fn parse_copy(&mut self) -> Result<Statement, ParserError> { pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
let table_name = self.parse_object_name()?; let table_name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional)?; let columns = self.parse_parenthesized_column_list(Optional)?;
self.expect_keyword("FROM")?; self.expect_keywords(&["FROM", "STDIN"])?;
self.expect_keyword("STDIN")?;
self.expect_token(&Token::SemiColon)?; self.expect_token(&Token::SemiColon)?;
let values = self.parse_tsv()?; let values = self.parse_tsv()?;
Ok(Statement::Copy { Ok(Statement::Copy {
@ -1240,16 +1247,14 @@ impl Parser {
"TIMESTAMP" => { "TIMESTAMP" => {
// TBD: we throw away "with/without timezone" information // TBD: we throw away "with/without timezone" information
if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") { if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
self.expect_keyword("TIME")?; self.expect_keywords(&["TIME", "ZONE"])?;
self.expect_keyword("ZONE")?;
} }
Ok(DataType::Timestamp) Ok(DataType::Timestamp)
} }
"TIME" => { "TIME" => {
// TBD: we throw away "with/without timezone" information // TBD: we throw away "with/without timezone" information
if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") { if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
self.expect_keyword("TIME")?; self.expect_keywords(&["TIME", "ZONE"])?;
self.expect_keyword("ZONE")?;
} }
Ok(DataType::Time) Ok(DataType::Time)
} }