Refactor to use parser's 'parse_block' method
This commit is contained in:
parent
c71c4d2d07
commit
755fe5d899
@ -245,12 +245,7 @@ impl<'src> Parser<'src> {
|
|||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
let condition = Box::new(self.parse_statement(0)?);
|
let condition = Box::new(self.parse_statement(0)?);
|
||||||
let if_body = Box::new(self.parse_statement(0)?);
|
let if_body = Box::new(self.parse_block()?);
|
||||||
|
|
||||||
if let Statement::Block(_) = if_body.inner {
|
|
||||||
} else {
|
|
||||||
return Err(ParseError::ExpectedBlock { actual: *if_body });
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Token::Else = self.current.0 {
|
if let Token::Else = self.current.0 {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
@ -282,14 +277,9 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
else_ifs.push(else_if);
|
else_ifs.push(else_if);
|
||||||
} else {
|
} else {
|
||||||
let else_body = Box::new(self.parse_statement(0)?);
|
let else_body = Box::new(self.parse_block()?);
|
||||||
let else_end = else_body.position.1;
|
let else_end = else_body.position.1;
|
||||||
|
|
||||||
if let Statement::Block(_) = else_body.inner {
|
|
||||||
} else {
|
|
||||||
return Err(ParseError::ExpectedBlock { actual: *else_body });
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(Node::new(
|
return Ok(Node::new(
|
||||||
Statement::IfElseIfElse {
|
Statement::IfElseIfElse {
|
||||||
condition,
|
condition,
|
||||||
@ -302,14 +292,9 @@ impl<'src> Parser<'src> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let else_body = Box::new(self.parse_statement(0)?);
|
let else_body = Box::new(self.parse_block()?);
|
||||||
let else_end = else_body.position.1;
|
let else_end = else_body.position.1;
|
||||||
|
|
||||||
if let Statement::Block(_) = else_body.inner {
|
|
||||||
} else {
|
|
||||||
return Err(ParseError::ExpectedBlock { actual: *else_body });
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Node::new(
|
Ok(Node::new(
|
||||||
Statement::IfElse {
|
Statement::IfElse {
|
||||||
condition,
|
condition,
|
||||||
@ -698,20 +683,21 @@ impl<'src> Parser<'src> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
left: Node<Statement>,
|
left: Node<Statement>,
|
||||||
) -> Result<(Node<Statement>, u8), ParseError> {
|
) -> Result<(Node<Statement>, u8), ParseError> {
|
||||||
if let Token::Semicolon = &self.current.0 {
|
let node = if let Token::Semicolon = &self.current.0 {
|
||||||
self.next_token()?;
|
self.next_token()?;
|
||||||
|
|
||||||
let left_start = left.position.0;
|
let left_start = left.position.0;
|
||||||
let operator_end = self.current.1 .1;
|
let operator_end = self.current.1 .1;
|
||||||
let node = Node::new(Statement::Nil(Box::new(left)), (left_start, operator_end));
|
|
||||||
|
|
||||||
Ok((node, self.current.0.precedence()))
|
Node::new(Statement::Nil(Box::new(left)), (left_start, operator_end))
|
||||||
} else {
|
} else {
|
||||||
Err(ParseError::UnexpectedToken {
|
return Err(ParseError::UnexpectedToken {
|
||||||
actual: self.current.0.to_owned(),
|
actual: self.current.0.to_owned(),
|
||||||
position: self.current.1,
|
position: self.current.1,
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Ok((node, self.current.0.precedence()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_block(&mut self) -> Result<Node<Statement>, ParseError> {
|
fn parse_block(&mut self) -> Result<Node<Statement>, ParseError> {
|
||||||
@ -758,9 +744,6 @@ pub enum ParseError {
|
|||||||
ExpectedAssignment {
|
ExpectedAssignment {
|
||||||
actual: Node<Statement>,
|
actual: Node<Statement>,
|
||||||
},
|
},
|
||||||
ExpectedBlock {
|
|
||||||
actual: Node<Statement>,
|
|
||||||
},
|
|
||||||
ExpectedIdentifier {
|
ExpectedIdentifier {
|
||||||
actual: TokenOwned,
|
actual: TokenOwned,
|
||||||
position: Span,
|
position: Span,
|
||||||
@ -795,7 +778,6 @@ impl ParseError {
|
|||||||
match self {
|
match self {
|
||||||
ParseError::BooleanError { position, .. } => *position,
|
ParseError::BooleanError { position, .. } => *position,
|
||||||
ParseError::ExpectedAssignment { actual } => actual.position,
|
ParseError::ExpectedAssignment { actual } => actual.position,
|
||||||
ParseError::ExpectedBlock { actual } => actual.position,
|
|
||||||
ParseError::ExpectedIdentifier { position, .. } => *position,
|
ParseError::ExpectedIdentifier { position, .. } => *position,
|
||||||
ParseError::ExpectedToken { position, .. } => *position,
|
ParseError::ExpectedToken { position, .. } => *position,
|
||||||
ParseError::FloatError { position, .. } => *position,
|
ParseError::FloatError { position, .. } => *position,
|
||||||
@ -820,7 +802,6 @@ impl Display for ParseError {
|
|||||||
match self {
|
match self {
|
||||||
Self::BooleanError { error, .. } => write!(f, "{}", error),
|
Self::BooleanError { error, .. } => write!(f, "{}", error),
|
||||||
Self::ExpectedAssignment { .. } => write!(f, "Expected assignment"),
|
Self::ExpectedAssignment { .. } => write!(f, "Expected assignment"),
|
||||||
Self::ExpectedBlock { .. } => write!(f, "Expected block"),
|
|
||||||
Self::ExpectedIdentifier { actual, .. } => {
|
Self::ExpectedIdentifier { actual, .. } => {
|
||||||
write!(f, "Expected identifier, found {actual}")
|
write!(f, "Expected identifier, found {actual}")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user