Skip to content

Commit

Permalink
Add a fake io reader to test lexing #56
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 15, 2018
1 parent bfc94fd commit 6d657ef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate derive_new;
pub mod mango {
// Utilities
pub mod cli;
pub mod io;
pub mod jit;
pub mod util;

Expand Down
29 changes: 29 additions & 0 deletions src/mango/io/fortest/fromstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use mango::io::typ::Reader;
use regex::Regex;

/// 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 equals(&mut self, text: &str) -> bool {
if &self.code[self.index..self.index + text.len()] == text {
self.index += text.len();
return true;
}
false
}

fn matches(&mut self, pattern: Regex) -> Option<String> {
unimplemented!() // TODO
}
}
2 changes: 2 additions & 0 deletions src/mango/io/fortest/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod fromstr;
pub use self::fromstr::*;
3 changes: 3 additions & 0 deletions src/mango/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod typ;

pub mod fortest;
14 changes: 14 additions & 0 deletions src/mango/io/typ.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use regex::Regex;

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

/// Checks whether the code from the current position matches a regex pattern.
fn matches(&mut self, pattern: Regex) -> Option<String>;
}

pub trait Writer {
// TODO
}

0 comments on commit 6d657ef

Please sign in to comment.