Use the same pattern to parse comma-separated lists of things

This commit is contained in:
Nickolay Ponomarev 2019-06-09 05:45:46 +03:00
parent ab88e02f0d
commit b6dac5099d

View file

@ -822,15 +822,10 @@ impl Parser {
)); ));
}; };
let if_exists = self.parse_keywords(vec!["IF", "EXISTS"]); let if_exists = self.parse_keywords(vec!["IF", "EXISTS"]);
let mut names = vec![self.parse_object_name()?]; let mut names = vec![];
loop { loop {
let token = &self.next_token(); names.push(self.parse_object_name()?);
if let Some(Token::Comma) = token { if !self.consume_token(&Token::Comma) {
names.push(self.parse_object_name()?)
} else {
if token.is_some() {
self.prev_token();
}
break; break;
} }
} }
@ -1014,10 +1009,9 @@ impl Parser {
self.expect_token(&Token::Eq)?; self.expect_token(&Token::Eq)?;
let value = self.parse_value()?; let value = self.parse_value()?;
options.push(SQLOption { name, value }); options.push(SQLOption { name, value });
match self.peek_token() { if !self.consume_token(&Token::Comma) {
Some(Token::Comma) => self.next_token(), break;
_ => break, }
};
} }
self.expect_token(&Token::RParen)?; self.expect_token(&Token::RParen)?;
Ok(options) Ok(options)
@ -1279,30 +1273,14 @@ impl Parser {
/// Parse one or more identifiers with the specified separator between them /// Parse one or more identifiers with the specified separator between them
pub fn parse_list_of_ids(&mut self, separator: &Token) -> Result<Vec<SQLIdent>, ParserError> { pub fn parse_list_of_ids(&mut self, separator: &Token) -> Result<Vec<SQLIdent>, ParserError> {
let mut idents = vec![]; let mut idents = vec![];
let mut expect_identifier = true;
loop { loop {
let token = &self.next_token(); idents.push(self.parse_identifier()?);
match token { if !self.consume_token(separator) {
Some(Token::SQLWord(s)) if expect_identifier => {
expect_identifier = false;
idents.push(s.as_sql_ident());
}
Some(token) if token == separator && !expect_identifier => {
expect_identifier = true;
continue;
}
_ => {
self.prev_token();
break; break;
} }
} }
}
if expect_identifier {
self.expected("identifier", self.peek_token())
} else {
Ok(idents) Ok(idents)
} }
}
/// Parse a possibly qualified, possibly quoted identifier, e.g. /// Parse a possibly qualified, possibly quoted identifier, e.g.
/// `foo` or `myschema."table"` /// `foo` or `myschema."table"`
@ -1844,10 +1822,9 @@ impl Parser {
self.expect_token(&Token::LParen)?; self.expect_token(&Token::LParen)?;
values.push(self.parse_expr_list()?); values.push(self.parse_expr_list()?);
self.expect_token(&Token::RParen)?; self.expect_token(&Token::RParen)?;
match self.peek_token() { if !self.consume_token(&Token::Comma) {
Some(Token::Comma) => self.next_token(), break;
_ => break, }
};
} }
Ok(SQLValues(values)) Ok(SQLValues(values))
} }