Skip to content

Commit

Permalink
feat: Add facade wrapper for regex_syntax::ast::parse::Parser to pa…
Browse files Browse the repository at this point in the history
…rse regular expressions into an AST. (#6)
  • Loading branch information
LinZhihao-723 authored Dec 5, 2024
1 parent c9aba92 commit 5f6fae7
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.0.1"
edition = "2021"

[dependencies]
regex-syntax = "0.8.5"
8 changes: 8 additions & 0 deletions src/error_handling/error.rs
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>;
3 changes: 3 additions & 0 deletions src/error_handling/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod error;
pub use error::Error;
pub use error::Result;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod error_handling;
mod nfa;
pub mod parser;

Expand Down
1 change: 1 addition & 0 deletions src/parser/mod.rs
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;
1 change: 1 addition & 0 deletions src/parser/regex_parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod parser;
58 changes: 58 additions & 0 deletions src/parser/regex_parser/parser.rs
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")
};
}
}

0 comments on commit 5f6fae7

Please sign in to comment.