Skip to content

Commit

Permalink
refactor(parser): Split out parser creation
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 28, 2019
1 parent 8e4708d commit d0b9979
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ pub enum Case {
None,
}

#[derive(Debug, Clone)]
pub struct Parser {
words_str: regex::Regex,
words_bytes: regex::bytes::Regex,
}
#[derive(Debug, Clone, Default)]
pub struct ParserBuilder {}

impl Parser {
impl ParserBuilder {
pub fn new() -> Self {
Default::default()
}

pub fn build(self) -> Parser {
let pattern = r#"\b(\p{Alphabetic}|\d|_|')+\b"#;
let words_str = regex::Regex::new(pattern).unwrap();
let words_bytes = regex::bytes::Regex::new(pattern).unwrap();
Self {
Parser {
words_str,
words_bytes,
}
}
}

#[derive(Debug, Clone)]
pub struct Parser {
words_str: regex::Regex,
words_bytes: regex::bytes::Regex,
}

impl Parser {
pub fn new() -> Self {
ParserBuilder::default().build()
}

pub fn parse<'c>(&'c self, content: &'c str) -> impl Iterator<Item = Identifier<'c>> {
self.words_str
Expand All @@ -37,6 +50,12 @@ impl Parser {
}
}

impl Default for Parser {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Identifier<'t> {
token: &'t str,
Expand Down

0 comments on commit d0b9979

Please sign in to comment.