From 6d657ef8b5a8865370909163cb9e66b73f749667 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 15 May 2018 22:03:55 +0200 Subject: [PATCH] Add a fake io reader to test lexing #56 --- src/lib.rs | 1 + src/mango/io/fortest/fromstr.rs | 29 +++++++++++++++++++++++++++++ src/mango/io/fortest/mod.rs | 2 ++ src/mango/io/mod.rs | 3 +++ src/mango/io/typ.rs | 14 ++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 src/mango/io/fortest/fromstr.rs create mode 100644 src/mango/io/fortest/mod.rs create mode 100644 src/mango/io/mod.rs create mode 100644 src/mango/io/typ.rs diff --git a/src/lib.rs b/src/lib.rs index c7fe180e..163b6c11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate derive_new; pub mod mango { // Utilities pub mod cli; + pub mod io; pub mod jit; pub mod util; diff --git a/src/mango/io/fortest/fromstr.rs b/src/mango/io/fortest/fromstr.rs new file mode 100644 index 00000000..3a39b80c --- /dev/null +++ b/src/mango/io/fortest/fromstr.rs @@ -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 { + unimplemented!() // TODO + } +} diff --git a/src/mango/io/fortest/mod.rs b/src/mango/io/fortest/mod.rs new file mode 100644 index 00000000..9aa88ab0 --- /dev/null +++ b/src/mango/io/fortest/mod.rs @@ -0,0 +1,2 @@ +pub mod fromstr; +pub use self::fromstr::*; diff --git a/src/mango/io/mod.rs b/src/mango/io/mod.rs new file mode 100644 index 00000000..6dbbd816 --- /dev/null +++ b/src/mango/io/mod.rs @@ -0,0 +1,3 @@ +pub mod typ; + +pub mod fortest; diff --git a/src/mango/io/typ.rs b/src/mango/io/typ.rs new file mode 100644 index 00000000..00aa6f7a --- /dev/null +++ b/src/mango/io/typ.rs @@ -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; +} + +pub trait Writer { + // TODO +}