Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWE lexer #52 #57

Merged
merged 50 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6d657ef
Add a fake io reader to test lexing #56
mverleg May 15, 2018
90bfe6b
Add a regex cache (again; lost my work) #56
mverleg May 16, 2018
c6ec834
Make the regex cache work #56
mverleg May 18, 2018
97f7f30
Trying to let the lexer deal with indentation somewhat elegantly #56
mverleg May 21, 2018
f33692b
Add a Queue type (which just wraps deque) #so
mverleg May 22, 2018
3728f4e
Add block start/end token and lexing #56
mverleg May 22, 2018
ea5a4cd
Add identifier and keyword lexing #56
mverleg May 22, 2018
03b152a
Progress on sublexers and new tokens, fighting borrow rules #56
mverleg May 22, 2018
219463b
Trying to make delegated lexer compile but not yet #56
mverleg May 25, 2018
ed26686
Partially rewrite the lexer delegation to use Rc #52
mverleg May 29, 2018
d3426ec
Trying to solve everything for Rc mode, but still lifetime problems #52
mverleg May 29, 2018
7aeb9ff
I really liked the idea of enum approach but very much fed up with fi…
mverleg May 30, 2018
12bee40
Compiles but still overlapping borrow at runtime #52
mverleg May 30, 2018
58d9e67
Update test settings to match Rust upgrade #so
mverleg May 31, 2018
0be80eb
With sacrifices to design and brevity, it now works! #52
mverleg May 31, 2018
d3a555b
Add operator lexing #52
mverleg Jun 1, 2018
a60d144
Add string slicing by character #52
mverleg Jun 1, 2018
cab34ba
Fix and add tests for negative slices #52
mverleg Jun 1, 2018
f945ff2
Reserve a few more keywords #so
mverleg Jun 2, 2018
73bcf82
More lexing possibilities #52
mverleg Jun 4, 2018
04ab16a
Lexer infrastructure for testing #52
mverleg Jun 5, 2018
7e32cbe
More lexer infrastructure for testing #52
mverleg Jun 5, 2018
8d6a86f
Make StringReader works #52
mverleg Jun 6, 2018
65a195a
More progress on lexing tests, stuck on tab #52
mverleg Jun 6, 2018
cb67235
Example unit test, not all functionality implemented #52
mverleg Jun 7, 2018
19d69c9
Try to make generators work (but they dont yet) #52
mverleg Jun 8, 2018
5683e29
Fixed the MWE for generator #52
mverleg Jun 9, 2018
3c85472
-m
mverleg Jun 12, 2018
e5ce31c
Restructured lexing using generator, now just borrow/type problem #52
mverleg Jun 12, 2018
f95c892
Progress on rewriting lexer #52
mverleg Jun 14, 2018
ce4dc62
Circular reference problem with generator and container #52
mverleg Jun 17, 2018
e66e934
Deprecate two lexer implementations #52
mverleg Jun 17, 2018
71e6518
Some utils for lexing #52
mverleg Jun 17, 2018
e661070
Implement the combi-lexer #52
mverleg Jun 17, 2018
491e214
Start restructuring code lexer #52
mverleg Jun 17, 2018
10650d5
More code lexing functionality reactivated #52
mverleg Jun 17, 2018
4346a4f
Fairly complex regular expressions for ints and floats #52
mverleg Jun 17, 2018
8b4a125
Start infrastructure for parsing numbers in strings #52
mverleg Jun 17, 2018
28bc91a
Some progress on lexing literals #52
mverleg Jun 17, 2018
fd12412
Parsing of base10 integers implemented #52
mverleg Jun 17, 2018
b61d6f2
Parsing floats implemented #52
mverleg Jun 17, 2018
946f417
Expand lexing and improvements to string utils #52
mverleg Jun 17, 2018
ba3acda
Lexer has an infinite loop somewhere #52
mverleg Jun 17, 2018
0195c7a
Fix the infinite loop, lexing back to prev state #52
mverleg Jun 18, 2018
089c17e
Resolved technical errors, only lexing problems remain #52
mverleg Jun 19, 2018
1d0c8d5
Fix various problems incl stack/queue bug #52
mverleg Jun 19, 2018
f79c75b
MWE of the lexer works! #52
mverleg Jun 20, 2018
cdda4d4
Solve most compiler warnings #52
mverleg Jun 20, 2018
4b82d75
Merge branch 'dev' into lexer
mverleg Jun 20, 2018
6dd62f7
Resolve merge problems #52
mverleg Jun 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ before_script:
sudo: false
cache: cargo
script:
- cargo +nightly fmt --all -- --check
- cargo test --all
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ These instructions were tested on Ubuntu 18.4 (using Bash). It should also work
cargo test --all
cargo run --bin mango-cli

or to build a fast, release-mode native binary:

.. code:: bash

RUSTFLAGS="-C target-cpu=native" cargo build --release

* To deploy the web version in release mode, run the script `dev/build_web.sh` (or view it for the steps needed). It uses Python's SimpleHTTPServer, if you don't have that, you can still find the deployable code in `target/deploy`.

* You're now ready to make changes! If you want to help, you're very welcome! Have a glance at CONTRIBUTING.rst_ if you have a minute.
Expand Down
2 changes: 1 addition & 1 deletion dev/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ set -o pipefail
util_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")/utils"

# Check that the formatting is correct
PYTHONPATH="$util_dir":$PYTHONPATH python3 "$util_dir/run_on_staged.py" 'cargo +nightly fmt --verbose --all -- --write-mode=diff' 'cargo test --all'
PYTHONPATH="$util_dir":$PYTHONPATH python3 "$util_dir/run_on_staged.py" 'cargo +nightly fmt --all -- --check' 'cargo test --all'
2 changes: 1 addition & 1 deletion dev/hooks/utils/run_on_staged.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def do_cmds(cmds):
run(cmd, allow_stderr=True, log=True)
except Exception as err:
stderr.write(str(err))
stderr.write('FAILED, cancelling commit\n')
stderr.write('\nFAILED, cancelling commit\n')
return 1
return 0

Expand Down
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
49 changes: 0 additions & 49 deletions dev/playground/src/hashin.rs → dev/playground/src/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl<H: 'static + Hasher> AnyHasher for H {
}
}

// TODO: but now I want this not for everything
impl<T: 'static + Hash> MyTrait for T {
fn as_any(&self) -> &Any {
self as &Any
Expand All @@ -41,9 +40,6 @@ impl<T: 'static + Hash> MyTrait for T {
}
}

//impl MyTrait for A {}
//impl MyTrait for B {}

impl Hash for MyTrait {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.my_hash(hasher)
Expand All @@ -57,48 +53,3 @@ fn main() {
let x: &MyTrait = &A(1);
x.hash(&mut hasher);
}


//trait PreS: Debug {}
//
//trait HasherAsAny {
// fn as_any(&self) -> &Any;
//}
//
//trait PostS {
// fn as_any(&self) -> &Any;
//
// fn _hash<H: Hasher>(&self, hasher: H);
//}
//
//impl<T: 'static + Hasher> HasherAsAny for T {
// fn as_any(&self) -> &Any {
// self as &Any
// }
//}
//
//impl<T: 'static + PreS> PostS for T {
// fn as_any(&self) -> &Any {
// self as &Any
// }
//
// fn _hash<H: Hasher>(&self, hasher: H) {
// self.as_any().downcast_ref::<T>().hash(hasher)
// }
//}
//
//impl PreS for A {}
//
//impl PreS for B {}
//
//impl Hash for PostS {
// fn hash(&self, hasher: &mut HasherAsAny) {
// self._hash(hasher.as_any().downcast_ref::<T>())
// }
//}
//
//fn main() {
// let x: &PostS = &A(1);
// let m = HashMap::new();
// m.insert(x, 0);
//}
5 changes: 1 addition & 4 deletions rustfmt.toml
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
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(nll)]
//#![feature(generators, generator_trait)]
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate core;
extern crate wasm_bindgen;
Expand All @@ -10,6 +12,7 @@ extern crate derive_new;

pub mod mango {
// Utilities
pub mod io;
pub mod jit;
pub mod ui;
pub mod util;
Expand Down
6 changes: 1 addition & 5 deletions src/mango/ast_full/node/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ impl AssignmentAST {

impl ToText for AssignmentAST {
fn to_text(&self) -> String {
return format!(
"{0:} = ({1:})",
self.assignee.to_text(),
self.value.to_text()
);
return format!("{0:} = ({1:})", self.assignee.to_text(), self.value.to_text());
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/mango/ast_full/node/unary_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ impl UnaryOperationAST {

impl ToText for UnaryOperationAST {
fn to_text(&self) -> String {
return format!(
"({0:} {1:})",
self.operator.to_text(),
self.subject.to_text()
);
return format!("({0:} {1:})", self.operator.to_text(), self.subject.to_text());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/mango/ast_full/terminal/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub struct StringLiteralAST {

impl FloatLiteralAST {
pub fn new(value: f64) -> Self {
FloatLiteralAST {
value: f64eq::new(value),
}
FloatLiteralAST { value: f64eq::new(value) }
}
}

Expand Down
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 stringreader;
pub use self::stringreader::*;
61 changes: 61 additions & 0 deletions src/mango/io/fortest/stringreader.rs
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)
5 changes: 5 additions & 0 deletions src/mango/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod typ;

pub mod fortest;

pub mod util;
28 changes: 28 additions & 0 deletions src/mango/io/typ.rs
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
}
33 changes: 33 additions & 0 deletions src/mango/io/util.rs
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())
}
Loading