-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from mangolang/lexer
MWE lexer #52
- Loading branch information
Showing
64 changed files
with
1,734 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ before_script: | |
sudo: false | ||
cache: cargo | ||
script: | ||
- cargo +nightly fmt --all -- --check | ||
- cargo test --all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
reorder_extern_crates = true | ||
reorder_extern_crates_in_group = true | ||
reorder_imports = true | ||
reorder_imports_in_group = true | ||
reorder_imported_names = true | ||
max_width = 140 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod stringreader; | ||
pub use self::stringreader::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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. | ||
#[derive(Debug)] | ||
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) => { | ||
if self.index + mtch.as_str().len() == self.code.len() { | ||
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(); | ||
// Remove leading spaces | ||
let mut k = 0; | ||
for (i, byt) in mtch.as_str().chars().enumerate() { | ||
if byt != ' ' { | ||
break; | ||
} | ||
k = i + 1; | ||
} | ||
ReaderResult::Match((&mtch.as_str()[k..]).to_owned()) | ||
} | ||
None => ReaderResult::NoMatch(), | ||
}; | ||
}) | ||
} | ||
|
||
fn get_progress(&self) -> usize { | ||
self.index | ||
} | ||
} | ||
|
||
// TODO: tests (spaces, end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod typ; | ||
|
||
pub mod fortest; | ||
|
||
pub mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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: Debug { | ||
/// Checks whether the `text` is found starting from the current position. | ||
// fn equals(&mut self, texts: Vec<&str>) -> ReaderResult; | ||
|
||
/// Checks whether the code from the current position matches a regex pattern. | ||
/// | ||
/// This has to eventually return EOF, and keep returning EOF forever after that. | ||
fn matches(&mut self, subpattern: &str) -> ReaderResult; | ||
|
||
/// Return a number that can be used to check whether the state has changed. | ||
/// This need not correspond to a specific position, but should be unique for the progress. | ||
fn get_progress(&self) -> usize; | ||
} | ||
|
||
pub trait Writer { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use regex::Regex; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
|
||
pub struct RegexCache { | ||
cache: HashMap<String, Regex>, | ||
} | ||
|
||
impl RegexCache { | ||
// Not public to prevent having more than one instance. | ||
fn new() -> Self { | ||
RegexCache { cache: HashMap::new() } | ||
} | ||
|
||
pub fn make_or_get(&mut self, subpattern: &str) -> &Regex { | ||
if !self.cache.contains_key(subpattern) { | ||
match Regex::new(&format!(r"^ *{}", subpattern)) { | ||
Err(err) => panic!(format!( | ||
"Invalid regular expression '{}' while adding to library; this is a bug:\n{:?}", | ||
subpattern, err | ||
)), | ||
Ok(regex) => { | ||
self.cache.insert(subpattern.to_owned(), regex); | ||
} | ||
} | ||
} | ||
self.cache.get(subpattern).unwrap() | ||
} | ||
} | ||
|
||
thread_local! { | ||
pub static REXCACHE: RefCell<RegexCache> = RefCell::new(RegexCache::new()) | ||
} |
Oops, something went wrong.