Skip to content

Commit 35a2009

Browse files
authored
Merge pull request #134 from umanwizard/expect_keywords
add `expect_keywords` function
2 parents 391a54b + 41d4ea4 commit 35a2009

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/parser.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,15 @@ impl Parser {
802802
}
803803
}
804804

805+
/// Bail out if the following tokens are not the expected sequence of
806+
/// keywords, or consume them if they are.
807+
pub fn expect_keywords(&mut self, expected: &[&'static str]) -> Result<(), ParserError> {
808+
for kw in expected {
809+
self.expect_keyword(kw)?;
810+
}
811+
Ok(())
812+
}
813+
805814
/// Consume the next token if it matches the expected token, otherwise return false
806815
#[must_use]
807816
pub fn consume_token(&mut self, expected: &Token) -> bool {
@@ -856,8 +865,7 @@ impl Parser {
856865
self.expect_keyword("TABLE")?;
857866
let table_name = self.parse_object_name()?;
858867
let (columns, constraints) = self.parse_columns()?;
859-
self.expect_keyword("STORED")?;
860-
self.expect_keyword("AS")?;
868+
self.expect_keywords(&["STORED", "AS"])?;
861869
let file_format = self.parse_identifier()?.parse::<FileFormat>()?;
862870

863871
self.expect_keyword("LOCATION")?;
@@ -1111,8 +1119,7 @@ impl Parser {
11111119
pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
11121120
let table_name = self.parse_object_name()?;
11131121
let columns = self.parse_parenthesized_column_list(Optional)?;
1114-
self.expect_keyword("FROM")?;
1115-
self.expect_keyword("STDIN")?;
1122+
self.expect_keywords(&["FROM", "STDIN"])?;
11161123
self.expect_token(&Token::SemiColon)?;
11171124
let values = self.parse_tsv()?;
11181125
Ok(Statement::Copy {
@@ -1240,16 +1247,14 @@ impl Parser {
12401247
"TIMESTAMP" => {
12411248
// TBD: we throw away "with/without timezone" information
12421249
if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
1243-
self.expect_keyword("TIME")?;
1244-
self.expect_keyword("ZONE")?;
1250+
self.expect_keywords(&["TIME", "ZONE"])?;
12451251
}
12461252
Ok(DataType::Timestamp)
12471253
}
12481254
"TIME" => {
12491255
// TBD: we throw away "with/without timezone" information
12501256
if self.parse_keyword("WITH") || self.parse_keyword("WITHOUT") {
1251-
self.expect_keyword("TIME")?;
1252-
self.expect_keyword("ZONE")?;
1257+
self.expect_keywords(&["TIME", "ZONE"])?;
12531258
}
12541259
Ok(DataType::Time)
12551260
}

0 commit comments

Comments
 (0)