-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add facade wrapper for
regex_syntax::ast::parse::Parser
to pa…
…rse regular expressions into an AST. (#6)
- Loading branch information
1 parent
c9aba92
commit 5f6fae7
Showing
8 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.0.1" | |
edition = "2021" | ||
|
||
[dependencies] | ||
regex-syntax = "0.8.5" |
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 @@ | ||
use regex_syntax::ast; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
RegexParsingError(ast::Error), | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; |
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,3 @@ | ||
mod error; | ||
pub use error::Error; | ||
pub use error::Result; |
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,4 @@ | ||
mod error_handling; | ||
mod nfa; | ||
pub mod parser; | ||
|
||
|
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,2 +1,3 @@ | ||
// Keep ASTNode private and they will be used by parser in the future | ||
pub(crate) mod ast_node; | ||
mod regex_parser; |
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 @@ | ||
pub mod parser; |
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,58 @@ | ||
use crate::error_handling::{Error, Error::RegexParsingError, Result}; | ||
use regex_syntax::ast::{parse::Parser, Ast}; | ||
|
||
// This is a wrapper of `regex_syntax::ast::parse::Parser`, which can be extended to hold | ||
// program-specific data members. | ||
pub struct RegexParser { | ||
m_parser: Parser, | ||
} | ||
|
||
impl RegexParser { | ||
pub fn new() -> RegexParser { | ||
Self { | ||
m_parser: Parser::new(), | ||
} | ||
} | ||
|
||
pub fn parse_into_ast(&mut self, pattern: &str) -> Result<Ast> { | ||
match self.m_parser.parse(pattern) { | ||
Ok(ast) => Ok(ast), | ||
Err(e) => Err(RegexParsingError(e)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use regex_syntax::ast; | ||
|
||
#[test] | ||
fn test_basic_parsing() { | ||
let mut parser = RegexParser::new(); | ||
let parse_result = parser.parse_into_ast(r"[a-t\d]"); | ||
assert!(parse_result.is_ok()); | ||
let Ast::ClassBracketed(bracket_ast) = &parse_result.unwrap() else { | ||
panic!("Type mismatched") | ||
}; | ||
let ast::ClassSet::Item(item) = &bracket_ast.kind else { | ||
panic!("Type mismatched") | ||
}; | ||
let ast::ClassSetItem::Union(union) = &item else { | ||
panic!("Type mismatched") | ||
}; | ||
let a_to_z_item = &union.items[0]; | ||
let ast::ClassSetItem::Range(range) = &a_to_z_item else { | ||
panic!("Type mismatched") | ||
}; | ||
assert_eq!(range.start.c, 'a'); | ||
assert_eq!(range.end.c, 't'); | ||
let digit_item = &union.items[1]; | ||
let ast::ClassSetItem::Perl(perl) = &digit_item else { | ||
panic!("Type mismatched") | ||
}; | ||
let ast::ClassPerlKind::Digit = &perl.kind else { | ||
panic!("Type mismatched") | ||
}; | ||
} | ||
} |