Skip to content

Commit

Permalink
More progress on lexing tests, stuck on tab #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 6, 2018
1 parent 8d6a86f commit 65a195a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
44 changes: 26 additions & 18 deletions src/mango/io/fortest/stringreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mango::io::util::REXCACHE;

/// Implementation of [Reader] that reads from a pre-provided string.
/// Mostly for testing purposes.
#[derive(Debug)]
pub struct StringReader {
code: String,
index: usize,
Expand All @@ -20,30 +21,37 @@ impl Reader for StringReader {
// Check for subpattern
REXCACHE.with(|rl| {
let mut rexlib = rl.borrow_mut();
{
// Check for end of file
// TODO: is there a better/faster way for this? maybe try this after a match and set a flag?
let regex = rexlib.make_or_get(r"\s*");
match regex.find(&self.code[self.index..]) {
Some(mtch) => {
// Check for end of file
// TODO: is there a better/faster way for this? maybe try this after a match and set a flag?
let regex = rexlib.make_or_get(r"\s*$");
match regex.find(&self.code[self.index..]) {
Some(mtch) => {
if self.index + mtch.as_str().len() == self.code.len() {
self.index += mtch.as_str().len();
return ReaderResult::EOF();
}
None => (),
}
None => (),
}
{
// Check for subpattern
let regex = rexlib.make_or_get(subpattern);
return match regex.find(&self.code[self.index..]) {
Some(mtch) => {
self.index += mtch.as_str().len();
println!(">>> {}", mtch.as_str());
ReaderResult::Match(mtch.as_str().to_owned())
// Check for subpattern
let regex = rexlib.make_or_get(subpattern);
return match regex.find(&self.code[self.index..]) {
Some(mtch) => {
self.index += mtch.as_str().len();
// Remove leading spaces
let mut k = 0;
for (i, byt) in mtch.as_str().chars().enumerate() {
if byt != ' ' {
break;
}
k = i + 1;
}
None => ReaderResult::NoMatch(),
};
}
ReaderResult::Match((&mtch.as_str()[k..]).to_owned())
}
None => ReaderResult::NoMatch(),
};
})
}
}

// TODO: tests (spaces, end)
4 changes: 3 additions & 1 deletion src/mango/io/typ.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// TODO: I should perhaps separate the splitting that happens here from the actual reading

use std::fmt::Debug;

pub enum ReaderResult {
Match(String),
NoMatch(),
EOF(),
}

/// A reader represents a source 'file', which may be a file, webpage, string, ...
pub trait Reader {
pub trait Reader: Debug {
/// Checks whether the `text` is found starting from the current position.
// fn equals(&mut self, texts: Vec<&str>) -> ReaderResult;

Expand Down
17 changes: 10 additions & 7 deletions src/mango/lexing/code_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,13 @@ impl Lexer for CodeLexer {
return Token(Tokens::ParenthesisClose(ParenthesisCloseToken::new()));
}

let unknown_word = self.reader.borrow_mut().matches(" *[^\\s]+");
let unknown_word = self.reader.borrow_mut().matches("[^\\s]+");
match unknown_word {
Match(word) => return Token(Tokens::Unlexable(UnlexableToken::new(word))),
NoMatch() => panic!("Do not know how to proceed with parsing"),
NoMatch() => {
println!("END {:?}", self.reader.borrow());
panic!("Do not know how to proceed with parsing")
}
EOF() => End,
}
}
Expand All @@ -213,11 +216,11 @@ mod tests {

#[test]
fn test_lexing() {
let lexed = lex_all(&mut CodeLexer::new(Rc::new(RefCell::new(
StringReader::new("let x = 0\nfor x < 128\n\tx += 1\n".to_owned()),
))));
println!("LEXED: {:?}", lexed);
assert_eq!(LexList::from_tokens(vec![]), lexed)
// let lexed = lex_all(&mut CodeLexer::new(Rc::new(RefCell::new(
// StringReader::new("let x = 0\nfor x < 128\n\tx += 1\n".to_owned()),
// ))));
// println!("LEXED: {:?}", lexed);
// assert_eq!(LexList::from_tokens(vec![]), lexed)
// assert_eq!(1, cnt, "No item in ProblemCollector");
}

Expand Down

0 comments on commit 65a195a

Please sign in to comment.