-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::ast; | ||
use crate::ast::{AstNode, NodeId}; | ||
|
||
/// A provider of 'fresh' [`NodeId`]s. | ||
pub trait IdGenerator { | ||
/// Provides a 'fresh' [`NodeId`]. | ||
fn id(&mut self) -> NodeId; | ||
} | ||
|
||
/// Auto-incrementing [`IdGenerator`] | ||
pub struct AutoNodeIdGenerator { | ||
next_id: ast::NodeId, | ||
} | ||
|
||
impl Default for AutoNodeIdGenerator { | ||
fn default() -> Self { | ||
AutoNodeIdGenerator { next_id: NodeId(1) } | ||
} | ||
} | ||
|
||
impl IdGenerator for AutoNodeIdGenerator { | ||
#[inline] | ||
fn id(&mut self) -> NodeId { | ||
let mut next = NodeId(&self.next_id.0 + 1); | ||
std::mem::swap(&mut self.next_id, &mut next); | ||
next | ||
} | ||
} | ||
|
||
/// A provider of [`NodeId`]s that are always `0`; Useful for testing | ||
#[derive(Default)] | ||
pub struct NullIdGenerator {} | ||
|
||
impl IdGenerator for NullIdGenerator { | ||
fn id(&mut self) -> NodeId { | ||
NodeId(0) | ||
} | ||
} | ||
|
||
/// A Builder for [`AstNode`]s that uses a [`IdGenerator`] to assign [`NodeId`]s | ||
pub struct NodeBuilder<Id: IdGenerator> { | ||
/// Generator for 'fresh' [`NodeId`]s | ||
pub id_gen: Id, | ||
} | ||
|
||
impl<Id> NodeBuilder<Id> | ||
where | ||
Id: IdGenerator, | ||
{ | ||
pub fn new(id_gen: Id) -> Self { | ||
Self { id_gen } | ||
} | ||
|
||
pub fn node<T>(&mut self, node: T) -> AstNode<T> { | ||
let id = self.id_gen.id(); | ||
AstNode { id, node } | ||
} | ||
} | ||
|
||
impl<T> Default for NodeBuilder<T> | ||
where | ||
T: IdGenerator + Default, | ||
{ | ||
fn default() -> Self { | ||
Self::new(T::default()) | ||
} | ||
} | ||
|
||
/// A [`NodeBuilder`] whose 'fresh' [`NodeId`]s are Auto-incrementing. | ||
pub type NodeBuilderWithAutoId = NodeBuilder<AutoNodeIdGenerator>; | ||
|
||
/// A [`NodeBuilder`] whose 'fresh' [`NodeId`]s are always `0`; Useful for testing | ||
pub type NodeBuilderWithNullId = NodeBuilder<NullIdGenerator>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ pub mod ast; | |
|
||
pub mod pretty; | ||
|
||
pub mod builder; | ||
|
||
pub mod visit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters