Make StructVariableInitializer an AST node

This commit is contained in:
Pranav Gaddamadugu 2023-08-09 17:22:52 -04:00
parent 6808d90545
commit 9125c84cd1
2 changed files with 10 additions and 4 deletions

View File

@ -27,10 +27,14 @@ pub struct StructVariableInitializer {
/// The expression to initialize the field with.
/// When `None`, a binding, in scope, with the name will be used instead.
pub expression: Option<Expression>,
/// The span of the node.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
crate::simple_node_impl!(StructVariableInitializer);
impl fmt::Display for StructVariableInitializer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(expr) = &self.expression {

View File

@ -571,14 +571,16 @@ impl ParserContext<'_> {
self.expect_identifier()?
};
let expression = if self.eat(&Token::Colon) {
let (expression, span) = if self.eat(&Token::Colon) {
// Parse individual struct variable declarations.
Some(self.parse_expression()?)
let expression = self.parse_expression()?;
let span = identifier.span + expression.span();
(Some(expression), span)
} else {
None
(None, identifier.span)
};
Ok(StructVariableInitializer { identifier, expression, id: NodeID::default() })
Ok(StructVariableInitializer { identifier, expression, id: NodeID::default(), span })
}
/// Returns an [`Expression`] AST node if the next tokens represent a