Further simplify parse_compound_identifier (5/8)

This part changes behavior:
- Fail when no identifier is found.
- Avoid rewinding if EOF was hit right after the identifier.
This commit is contained in:
Nickolay Ponomarev 2019-01-13 20:54:11 +03:00
parent 991fd19b87
commit 7bbf69f513
2 changed files with 25 additions and 17 deletions

View file

@ -945,36 +945,37 @@ impl Parser {
}
}
/// Parse one or more identifiers with the specified separator between them
pub fn parse_compound_identifier(&mut self, separator: &Token) -> Result<ASTNode, ParserError> {
let mut idents = vec![];
let mut expect_identifier = true;
loop {
let token = &self.next_token();
match token {
Some(Token::SQLWord(s)) => {
if expect_identifier {
expect_identifier = false;
idents.push(s.to_string());
} else {
self.prev_token();
break;
}
Some(Token::SQLWord(s)) if expect_identifier => {
expect_identifier = false;
idents.push(s.to_string());
}
Some(token) if token == separator => {
if expect_identifier {
return parser_err!(format!("Expecting identifier, found {:?}", token));
} else {
expect_identifier = true;
continue;
}
Some(token) if token == separator && !expect_identifier => {
expect_identifier = true;
continue;
}
_ => {
self.prev_token();
if token.is_some() {
self.prev_token();
}
break;
}
}
}
Ok(ASTNode::SQLCompoundIdentifier(idents))
if expect_identifier {
parser_err!(format!(
"Expecting identifier, found {:?}",
self.peek_token()
))
} else {
Ok(ASTNode::SQLCompoundIdentifier(idents))
}
}
pub fn parse_tablename(&mut self) -> Result<String, ParserError> {