-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add boilerplate for regex AST nodes. #3
Merged
LinZhihao-723
merged 7 commits into
Toplogic-Inc:main
from
Louis-He:basic_data_structure
Oct 26, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d7e5a1
feat: complete basic lexer and parser for regex.
Louis-He 29e2769
fix all formatting issue
Louis-He 7083658
add basic data structures for lexar and parser
Louis-He 09ed315
refactor each node type to individual files
Louis-He d872840
update camal naming
Louis-He 082b6e4
implement new for all nodes and add basic tests
Louis-He 3d1e18c
remove token
Louis-He File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod parser; | ||
|
||
const VERSION: &str = "0.0.1"; | ||
|
||
pub fn version() -> &'static str { | ||
|
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,135 @@ | ||
// #[derive(Debug)] | ||
use super::ast_node_concat::AstNodeConcat; | ||
use super::ast_node_group::AstNodeGroup; | ||
use super::ast_node_literal::AstNodeLiteral; | ||
use super::ast_node_optional::AstNodeOptional; | ||
use super::ast_node_plus::AstNodePlus; | ||
use super::ast_node_star::AstNodeStar; | ||
use super::ast_node_union::AstNodeUnion; | ||
|
||
pub(crate) enum AstNode { | ||
Literal(AstNodeLiteral), | ||
Concat(AstNodeConcat), | ||
Union(AstNodeUnion), | ||
Star(AstNodeStar), | ||
Plus(AstNodePlus), | ||
Optional(AstNodeOptional), | ||
Group(AstNodeGroup), | ||
} | ||
|
||
impl PartialEq for AstNode { | ||
fn eq(&self, other: &Self) -> bool { | ||
match (self, other) { | ||
(AstNode::Literal(lhs), AstNode::Literal(rhs)) => lhs == rhs, | ||
(AstNode::Concat(lhs), AstNode::Concat(rhs)) => lhs == rhs, | ||
(AstNode::Union(lhs), AstNode::Union(rhs)) => lhs == rhs, | ||
(AstNode::Star(lhs), AstNode::Star(rhs)) => lhs == rhs, | ||
(AstNode::Plus(lhs), AstNode::Plus(rhs)) => lhs == rhs, | ||
(AstNode::Optional(lhs), AstNode::Optional(rhs)) => lhs == rhs, | ||
(AstNode::Group(lhs), AstNode::Group(rhs)) => lhs == rhs, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNode { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
AstNode::Literal(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Concat(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Union(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Star(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Plus(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Optional(ast_node) => write!(f, "{:?}", ast_node), | ||
AstNode::Group(ast_node) => write!(f, "{:?}", ast_node), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn ast_node_literal_equality() { | ||
let node1 = AstNode::Literal(AstNodeLiteral::new('a')); | ||
let node2 = AstNode::Literal(AstNodeLiteral::new('a')); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_concat_equality() { | ||
let node1 = AstNode::Concat(AstNodeConcat::new( | ||
AstNode::Literal(AstNodeLiteral::new('a')), | ||
AstNode::Literal(AstNodeLiteral::new('b')), | ||
)); | ||
let node2 = AstNode::Concat(AstNodeConcat::new( | ||
AstNode::Literal(AstNodeLiteral::new('a')), | ||
AstNode::Literal(AstNodeLiteral::new('b')), | ||
)); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_union_equality() { | ||
let node1 = AstNode::Union(AstNodeUnion::new( | ||
AstNode::Literal(AstNodeLiteral::new('a')), | ||
AstNode::Literal(AstNodeLiteral::new('b')), | ||
)); | ||
let node2 = AstNode::Union(AstNodeUnion::new( | ||
AstNode::Literal(AstNodeLiteral::new('a')), | ||
AstNode::Literal(AstNodeLiteral::new('b')), | ||
)); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_star_equality() { | ||
let node1 = AstNode::Star(AstNodeStar::new(AstNode::Literal(AstNodeLiteral::new('a')))); | ||
let node2 = AstNode::Star(AstNodeStar::new(AstNode::Literal(AstNodeLiteral::new('a')))); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_plus_equality() { | ||
let node1 = AstNode::Plus(AstNodePlus::new(AstNode::Literal(AstNodeLiteral::new('a')))); | ||
let node2 = AstNode::Plus(AstNodePlus::new(AstNode::Literal(AstNodeLiteral::new('a')))); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_optional_equality() { | ||
let node1 = AstNode::Optional(AstNodeOptional::new(AstNode::Literal(AstNodeLiteral::new( | ||
'a', | ||
)))); | ||
let node2 = AstNode::Optional(AstNodeOptional::new(AstNode::Literal(AstNodeLiteral::new( | ||
'a', | ||
)))); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_group_equality() { | ||
let node1 = AstNode::Group(AstNodeGroup::new(AstNode::Literal(AstNodeLiteral::new( | ||
'a', | ||
)))); | ||
let node2 = AstNode::Group(AstNodeGroup::new(AstNode::Literal(AstNodeLiteral::new( | ||
'a', | ||
)))); | ||
assert_eq!(node1, node2); | ||
} | ||
|
||
#[test] | ||
fn ast_node_basic_debug() { | ||
let node = AstNode::Concat(AstNodeConcat::new( | ||
AstNode::Star(AstNodeStar::new(AstNode::Union(AstNodeUnion::new( | ||
AstNode::Literal(AstNodeLiteral::new('a')), | ||
AstNode::Literal(AstNodeLiteral::new('b')), | ||
)))), | ||
AstNode::Optional(AstNodeOptional::new(AstNode::Group(AstNodeGroup::new( | ||
AstNode::Plus(AstNodePlus::new(AstNode::Literal(AstNodeLiteral::new('c')))), | ||
)))), | ||
)); | ||
assert_eq!(format!("{:?}", node), "Concat( Star( Union( Literal('a') Literal('b') ) ) Optional( Group( Plus ( Literal('c') ) ) ) )"); | ||
} | ||
} |
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,27 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodeConcat { | ||
m_op1: Box<AstNode>, | ||
m_op2: Box<AstNode>, | ||
} | ||
|
||
impl AstNodeConcat { | ||
pub(crate) fn new(p0: AstNode, p1: AstNode) -> AstNodeConcat { | ||
AstNodeConcat { | ||
m_op1: Box::new(p0), | ||
m_op2: Box::new(p1), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeConcat { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 && self.m_op2 == other.m_op2 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodeConcat { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Concat( {:?} {:?} )", self.m_op1, self.m_op2) | ||
} | ||
} |
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,25 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodeGroup { | ||
m_op1: Box<AstNode>, | ||
} | ||
|
||
impl AstNodeGroup { | ||
pub(crate) fn new(p0: AstNode) -> AstNodeGroup { | ||
AstNodeGroup { | ||
m_op1: Box::new(p0), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeGroup { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodeGroup { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Group( {:?} )", self.m_op1) | ||
} | ||
} |
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,23 @@ | ||
use std::fmt; | ||
|
||
pub(crate) struct AstNodeLiteral { | ||
m_value: char, | ||
} | ||
|
||
impl AstNodeLiteral { | ||
pub(crate) fn new(p0: char) -> AstNodeLiteral { | ||
AstNodeLiteral { m_value: p0 } | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeLiteral { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_value == other.m_value | ||
} | ||
} | ||
|
||
impl fmt::Debug for AstNodeLiteral { | ||
fn fmt(&self, p: &mut fmt::Formatter) -> fmt::Result { | ||
write!(p, "Literal({:?})", self.m_value) | ||
} | ||
} |
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,25 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodeOptional { | ||
m_op1: Box<AstNode>, | ||
} | ||
|
||
impl AstNodeOptional { | ||
pub(crate) fn new(p0: AstNode) -> AstNodeOptional { | ||
AstNodeOptional { | ||
m_op1: Box::new(p0), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeOptional { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodeOptional { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Optional( {:?} )", self.m_op1) | ||
} | ||
} |
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,25 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodePlus { | ||
m_op1: Box<AstNode>, | ||
} | ||
|
||
impl AstNodePlus { | ||
pub(crate) fn new(p0: AstNode) -> AstNodePlus { | ||
AstNodePlus { | ||
m_op1: Box::new(p0), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodePlus { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodePlus { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Plus ( {:?} )", self.m_op1) | ||
} | ||
} |
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,25 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodeStar { | ||
m_op1: Box<AstNode>, | ||
} | ||
|
||
impl AstNodeStar { | ||
pub(crate) fn new(p0: AstNode) -> AstNodeStar { | ||
AstNodeStar { | ||
m_op1: Box::new(p0), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeStar { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodeStar { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Star( {:?} )", self.m_op1) | ||
} | ||
} |
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,27 @@ | ||
use crate::parser::ast_node::ast_node::AstNode; | ||
|
||
pub(crate) struct AstNodeUnion { | ||
m_op1: Box<AstNode>, | ||
m_op2: Box<AstNode>, | ||
} | ||
|
||
impl AstNodeUnion { | ||
pub(crate) fn new(p0: AstNode, p1: AstNode) -> AstNodeUnion { | ||
AstNodeUnion { | ||
m_op1: Box::new(p0), | ||
m_op2: Box::new(p1), | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for AstNodeUnion { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.m_op1 == other.m_op1 && self.m_op2 == other.m_op2 | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for AstNodeUnion { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Union( {:?} {:?} )", self.m_op1, self.m_op2) | ||
} | ||
} |
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,8 @@ | ||
pub mod ast_node; | ||
mod ast_node_concat; | ||
mod ast_node_group; | ||
mod ast_node_literal; | ||
mod ast_node_optional; | ||
mod ast_node_plus; | ||
mod ast_node_star; | ||
mod ast_node_union; | ||
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,2 @@ | ||
// Keep ASTNode private and they will be used by parser in the future | ||
mod ast_node; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need them to be public if we implement nfa in a separate mod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can do this when we need to do this