From d0b9979c36861b7a7625e6ea2079fef32f4f2150 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 23 Jul 2019 20:42:20 -0600 Subject: [PATCH] refactor(parser): Split out parser creation --- src/tokens.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/tokens.rs b/src/tokens.rs index 46ce93e..23d1d1d 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -6,6 +6,25 @@ pub enum Case { None, } +#[derive(Debug, Clone, Default)] +pub struct ParserBuilder {} + +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(); + Parser { + words_str, + words_bytes, + } + } +} + #[derive(Debug, Clone)] pub struct Parser { words_str: regex::Regex, @@ -14,13 +33,7 @@ pub struct Parser { impl Parser { pub fn new() -> Self { - 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 { - words_str, - words_bytes, - } + ParserBuilder::default().build() } pub fn parse<'c>(&'c self, content: &'c str) -> impl Iterator> { @@ -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,