Skip to content

Commit

Permalink
Make StringReader works #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 6, 2018
1 parent 7e32cbe commit 8d6a86f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 68 deletions.
6 changes: 0 additions & 6 deletions dev/playground/src/enumhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ fn get_test_hash(x: &MyEnum) -> u64 {
}

fn main() {
<<<<<<< Updated upstream
let a1: MyEnum = MyEnum::A(Alpha { val: "Hello World".to_owned() });
let a2: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_owned() });
let a3: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_owned() });
=======
let a1: MyEnum = MyEnum::A(Alpha { val: "Hello World".to_string() });
let a2: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_string() });
let a3: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_string() });
>>>>>>> Stashed changes
let b: MyEnum = MyEnum::B(Beta { nr: 8, f: 2 });
let mut m = HashMap::new();
println!("{:?} {:?}", a1.to_text(), b.to_text());
Expand Down
37 changes: 0 additions & 37 deletions src/mango/io/fortest/fromstr.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/mango/io/fortest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod fromstr;
pub use self::fromstr::*;
pub mod stringreader;
pub use self::stringreader::*;
49 changes: 49 additions & 0 deletions src/mango/io/fortest/stringreader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use mango::io::typ::Reader;
use mango::io::typ::ReaderResult;
use mango::io::util::REXCACHE;

/// Implementation of [Reader] that reads from a pre-provided string.
/// Mostly for testing purposes.
pub struct StringReader {
code: String,
index: usize,
}

impl StringReader {
pub fn new(code: String) -> Self {
StringReader { code, index: 0 }
}
}

impl Reader for StringReader {
fn matches(&mut self, subpattern: &str) -> ReaderResult {
// 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) => {
self.index += mtch.as_str().len();
return ReaderResult::EOF();
}
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())
}
None => ReaderResult::NoMatch(),
};
}
})
}
}
7 changes: 4 additions & 3 deletions src/mango/io/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use regex::Error;
use regex::Regex;
use std::cell::RefCell;
use std::collections::HashMap;
Expand All @@ -18,12 +17,14 @@ impl RegexCache {
pub fn make_or_get(&mut self, subpattern: &str) -> &Regex {
if !self.cache.contains_key(subpattern) {
match Regex::new(&format!("^ *{}", subpattern)) {
Err(err) => panic!(format!("Invalid regular expression while adding to library; this is a bug:\n{:?}", err)),
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);
}
}

}
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 @@ -84,7 +84,7 @@ impl Lexer for CodeLexer {
match delegated_token {
End => {
// Swap back from delegation to direct mode.
let reader = delegate.get_reader().clone();
// let reader = delegate.get_reader().clone();
self.reader_or_delegate = ReaderOrDelegate::Reader();
self.lex()
}
Expand Down Expand Up @@ -207,19 +207,17 @@ impl Lexer for CodeLexer {
mod tests {
use super::CodeLexer;
use mango::io::fortest::StringReader;
use mango::io::typ::Reader;
use mango::lexing::util::lex_all::{lex_all, LexList};
use std::cell::RefCell;
use std::rc::Rc;

#[test]
fn test_lexing() {
assert_eq!(
LexList::from_tokens(vec![]),
lex_all(Rc::new(RefCell::new(StringReader::new(
"let x = 0\nfor x < 128\n\tx += 1\n".to_owned(),
))))
)
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
12 changes: 3 additions & 9 deletions src/mango/lexing/util/lex_all.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use mango::io::typ::Reader;
use mango::lexing::code_lexer::CodeLexer;
use mango::lexing::typ::Lexer;
use mango::lexing::typ::MaybeToken;
use mango::token::Token;
use mango::token::Tokens;
use std::cell::RefCell;
use std::rc::Rc;

/// Represents all the lex tokens in a source.
#[derive(PartialEq, Eq, Debug)]
Expand All @@ -18,14 +13,13 @@ impl LexList {
LexList { tokens }
}

pub fn from_reader(reader: Rc<RefCell<Reader>>) -> Self {
lex_all(reader)
pub fn from_reader(lexer: &mut Lexer) -> Self {
lex_all(lexer)
}
}

pub fn lex_all(reader: Rc<RefCell<Reader>>) -> LexList {
pub fn lex_all(lexer: &mut Lexer) -> LexList {
let mut list = Vec::with_capacity(512);
let mut lexer = CodeLexer::new(reader);
while let MaybeToken::Token(token) = lexer.lex() {
list.push(token)
}
Expand Down
4 changes: 2 additions & 2 deletions src/mango/towasm/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use mango::towasm::arithmetic::Add;
use mango::towasm::collect::datatype::Value;
use mango::towasm::collect::typ::Wasm;
use mango::towasm::collect::Type;
use mango::towasm::control::BranchIf;
use mango::towasm::control::Label;
Expand All @@ -18,6 +17,7 @@ use mango::towasm::values::Const;
use mango::towasm::values::DeclareLocal;

#[test]
#[allow(unused_variables)]
fn test_example_1() {
let param_n = Parameter::new(Name::new("n".to_owned()).unwrap(), Type::Int32);
let var_n = param_n.local();
Expand Down Expand Up @@ -62,5 +62,5 @@ fn test_example_1() {
},
)]);

println!("WAT:\n{}\n", module.as_wat());
// println!("WAT:\n{}\n", module.as_wat());
}
1 change: 0 additions & 1 deletion src/mango/util/strslice/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn charslice<S: Into<String>>(text: S, start: isize, end: isize) -> String {
);
length = end as usize - from;
}
println!("from: {}, length: {}", from, length);
stext.chars().skip(from).take(length).collect()
}

Expand Down

0 comments on commit 8d6a86f

Please sign in to comment.