Skip to content

Commit

Permalink
Add the match pattern parser to the global parser
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed May 23, 2024
1 parent 6571980 commit 185a797
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use search_path::SearchPath;
use self::{
braille::{braille_chars, chars_to_dots, BrailleChars},
multipass::Test,
match_rule::Pattern,
};

pub use braille::dots_to_unicode;
Expand Down Expand Up @@ -103,6 +104,8 @@ pub enum ParseError {
MultipassActionExpected,
#[error("Invalid multipass test: {0}")]
InvalidMultipassTest(#[from] multipass::ParseError),
#[error("Invalid match pattern: {0}")]
InvalidMatchPattern(#[from] match_rule::ParseError),
#[error("Match pre-pattern expected")]
MatchPreExpected,
#[error("Match post-pattern expected")]
Expand Down Expand Up @@ -672,9 +675,9 @@ pub enum Rule {
},

Match {
pre: String,
pre: Pattern,
chars: String,
post: String,
post: Pattern,
dots: Braille,
constraints: Constraints,
matches: Option<WithMatches>,
Expand Down Expand Up @@ -1183,18 +1186,26 @@ impl<'a> RuleParser<'a> {
.map(|s| s.to_string())
}

fn match_pre(&mut self) -> Result<String, ParseError> {
fn match_pre(&mut self) -> Result<Pattern, ParseError> {
self.tokens
.next()
.ok_or(ParseError::MatchPreExpected)
.map(|s| s.to_string())
.map(|s| {
match_rule::PatternParser::new(&s)
.pattern()
.map_err(ParseError::InvalidMatchPattern)
})?
}

fn match_post(&mut self) -> Result<String, ParseError> {
fn match_post(&mut self) -> Result<Pattern, ParseError> {
self.tokens
.next()
.ok_or(ParseError::MatchPostExpected)
.map(|s| s.to_string())
.map(|s| {
match_rule::PatternParser::new(&s)
.pattern()
.map_err(ParseError::InvalidMatchPattern)
})?
}

pub fn rule(&mut self) -> Result<Rule, ParseError> {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/match_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum Attribute {
}

#[derive(Debug, PartialEq)]
enum Pattern {
pub enum Pattern {
Characters(String),
Any,
Set(HashSet<char>),
Expand Down

0 comments on commit 185a797

Please sign in to comment.