From 7e32cbea6a696839e3654a1126f64a122be7acd0 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 5 Jun 2018 19:43:52 +0200 Subject: [PATCH] More lexer infrastructure for testing #52 --- src/mango/io/fortest/fromstr.rs | 6 +++--- src/mango/io/typ.rs | 2 +- src/mango/io/util.rs | 13 +++++++++---- src/mango/lexing/code_lexer.rs | 14 ++++++-------- src/mango/util/codeparts/operator.rs | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/mango/io/fortest/fromstr.rs b/src/mango/io/fortest/fromstr.rs index c4ee12b7..8ed0c9db 100644 --- a/src/mango/io/fortest/fromstr.rs +++ b/src/mango/io/fortest/fromstr.rs @@ -29,9 +29,9 @@ impl Reader for StringReader { fn matches(&mut self, subpattern: &str) -> ReaderResult { REXCACHE.with(|rl| { let mut rexlib = rl.borrow_mut(); - let rex = rexlib.make_or_get(subpattern); - println!("{:?}", rex); + let regex = rexlib.make_or_get(subpattern); + println!("{:?}", regex); }); - ReaderResult::NoMatch() // TODO + ReaderResult::EOF() // TODO } } diff --git a/src/mango/io/typ.rs b/src/mango/io/typ.rs index 2c83c5ca..13b4e0ba 100644 --- a/src/mango/io/typ.rs +++ b/src/mango/io/typ.rs @@ -13,7 +13,7 @@ pub trait Reader { /// Checks whether the code from the current position matches a regex pattern. /// - /// This has to eventually return EOF, after which it should not be called again. + /// This has to eventually return EOF, and keep returning EOF forever after that. fn matches(&mut self, subpattern: &str) -> ReaderResult; } diff --git a/src/mango/io/util.rs b/src/mango/io/util.rs index 66e1053d..022ec795 100644 --- a/src/mango/io/util.rs +++ b/src/mango/io/util.rs @@ -15,12 +15,17 @@ impl RegexCache { } } - pub fn make_or_get(&mut self, subpattern: &str) -> Result<&Regex, Error> { + pub fn make_or_get(&mut self, subpattern: &str) -> &Regex { if !self.cache.contains_key(subpattern) { - let regex = Regex::new(&format!("^ *{}", subpattern))?; - self.cache.insert(subpattern.to_owned(), regex); + match Regex::new(&format!("^ *{}", subpattern)) { + Err(err) => panic!(format!("Invalid regular expression while adding to library; this is a bug:\n{:?}", err)), + Ok(regex) => { + self.cache.insert(subpattern.to_owned(), regex); + } + } + } - Result::Ok(self.cache.get(subpattern).unwrap()) + self.cache.get(subpattern).unwrap() } } diff --git a/src/mango/lexing/code_lexer.rs b/src/mango/lexing/code_lexer.rs index bbf2bf86..44293df7 100644 --- a/src/mango/lexing/code_lexer.rs +++ b/src/mango/lexing/code_lexer.rs @@ -178,20 +178,18 @@ impl Lexer for CodeLexer { return Token(Tokens::Operator(OperatorToken::from_str(&token).unwrap())); } // Grouping symbols - if let Match(_) = self.reader.borrow_mut().matches("(") { + if let Match(_) = self.reader.borrow_mut().matches(r"\(") { return Token(Tokens::ParenthesisOpen(ParenthesisOpenToken::new())); } - if let Match(_) = self.reader.borrow_mut().matches(")") { + if let Match(_) = self.reader.borrow_mut().matches(r"\)") { return Token(Tokens::ParenthesisClose(ParenthesisCloseToken::new())); } - // TODO: specify the unlexable word let unknown_word = self.reader.borrow_mut().matches(" *[^\\s]+"); - if let Match(word) = unknown_word { - return Token(Tokens::Unlexable(UnlexableToken::new(word))); - } else { - // todo: handle better someday - panic!("Do not know how to proceed with parsing"); + match unknown_word { + Match(word) => return Token(Tokens::Unlexable(UnlexableToken::new(word))), + NoMatch() => panic!("Do not know how to proceed with parsing"), + EOF() => End, } } } diff --git a/src/mango/util/codeparts/operator.rs b/src/mango/util/codeparts/operator.rs index 71d37b33..1a7e59fd 100644 --- a/src/mango/util/codeparts/operator.rs +++ b/src/mango/util/codeparts/operator.rs @@ -30,7 +30,7 @@ impl Symbol { /// Generate an eager subpattern to match tokens, that can be composed in a regular expression. pub fn subpattern() -> &'static str { - r"(\+|\-|\*|\/)" + r"(\+|\-|\*|/)" } }