Refactor NodeIdentity to use unique IDs
The `NodeIdentity` struct has been refactored to use a unique `id` field generated by an atomic counter. This ensures that each `NodeIdentity` instance is distinct, even if it has the same `SourceLocation`. The `location` field is now optional, allowing for anonymous nodes. This change improves the reliability of identity comparisons and provides a more robust way to manage AST node identities.
This commit is contained in:
+7
-21
@@ -29,9 +29,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
pub fn parse_expression(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token_loc = self.current_token.location;
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: token_loc,
|
||||
});
|
||||
let identity = NodeIdentity::new(token_loc);
|
||||
|
||||
match self.peek() {
|
||||
TokenKind::LeftParen => self.parse_list(),
|
||||
@@ -93,9 +91,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn parse_atom(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let token = self.advance()?;
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
});
|
||||
let identity = NodeIdentity::new(token.location);
|
||||
|
||||
let kind = match token.kind {
|
||||
TokenKind::Integer(n) => UntypedKind::Constant(Value::Int(n)),
|
||||
@@ -123,9 +119,7 @@ impl<'a> Parser<'a> {
|
||||
|
||||
fn parse_list(&mut self) -> Result<Node<UntypedKind>, String> {
|
||||
let start_loc = self.advance()?.location; // consume '('
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: start_loc,
|
||||
});
|
||||
let identity = NodeIdentity::new(start_loc);
|
||||
|
||||
if *self.peek() == TokenKind::RightParen {
|
||||
return Err(format!(
|
||||
@@ -272,18 +266,14 @@ impl<'a> Parser<'a> {
|
||||
_ => unreachable!(),
|
||||
};
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
identity: NodeIdentity::new(token.location),
|
||||
kind: UntypedKind::Parameter(sym),
|
||||
ty: (),
|
||||
})
|
||||
}
|
||||
TokenKind::LeftBracket => {
|
||||
let token = self.advance()?;
|
||||
let identity = Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
});
|
||||
let identity = NodeIdentity::new(token.location);
|
||||
|
||||
let mut elements = Vec::new();
|
||||
while *self.peek() != TokenKind::RightBracket {
|
||||
@@ -344,9 +334,7 @@ impl<'a> Parser<'a> {
|
||||
self.expect(TokenKind::RightBracket)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
identity: NodeIdentity::new(token.location),
|
||||
kind: UntypedKind::Tuple { elements },
|
||||
ty: (),
|
||||
})
|
||||
@@ -380,9 +368,7 @@ impl<'a> Parser<'a> {
|
||||
self.expect(TokenKind::RightBrace)?;
|
||||
|
||||
Ok(Node {
|
||||
identity: Rc::new(NodeIdentity {
|
||||
location: token.location,
|
||||
}),
|
||||
identity: NodeIdentity::new(token.location),
|
||||
kind: UntypedKind::Record { fields },
|
||||
ty: (),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user