Skip to content

Commit

Permalink
More lexer infrastructure for testing #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 5, 2018
1 parent 04ab16a commit 7e32cbe
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/mango/io/fortest/fromstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/mango/io/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 9 additions & 4 deletions src/mango/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/mango/lexing/code_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mango/util/codeparts/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"(\+|\-|\*|/)"
}
}

Expand Down

0 comments on commit 7e32cbe

Please sign in to comment.