Skip to content

Commit

Permalink
Some utils for lexing #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 17, 2018
1 parent e66e934 commit 71e6518
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/mango/io/fortest/stringreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl Reader for StringReader {
};
})
}

fn get_progress(&self) -> usize {
self.index
}
}

// TODO: tests (spaces, end)
4 changes: 4 additions & 0 deletions src/mango/io/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub trait Reader: Debug {
///
/// 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 {
Expand Down
3 changes: 3 additions & 0 deletions src/mango/util/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod queue;
pub use self::queue::Queue;

pub mod stack;
pub use self::stack::Stack;
18 changes: 11 additions & 7 deletions src/mango/util/collection/queue.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use std::collections::VecDeque;

/// A one-ended queue.
/// This is just a wrapper around deque so nobody pushes or pops the wrong end.
/// A one-ended queue. See also [Stack].
/// This is just a wrapper around vec so nobody pushes or pops the wrong end.
pub struct Queue<T> {
deque: VecDeque<T>,
items: Vec<T>,
}

impl<T> Queue<T> {
pub fn new() -> Self {
Queue {
deque: VecDeque::with_capacity(16),
items: Vec::with_capacity(16),
}
}

pub fn push(&mut self, value: T) {
self.deque.push_back(value)
self.items.push(value)
}

pub fn pop(&mut self) -> Option<T> {
self.deque.pop_front()
self.items.pop()
}

/// Moves all the elements from a vector into the queue.
pub fn append(&mut self, mut other: Vec<T>) {
self.items.append(&mut other);
}
}
27 changes: 27 additions & 0 deletions src/mango/util/collection/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::collections::VecDeque;

/// A one-ended stack. See also [Queue].
/// This is just a wrapper around deque so nobody pushes or pops the wrong end.
pub struct Stack<T> {
items: VecDeque<T>,
}

impl<T> Stack<T> {
pub fn new() -> Self {
Stack {
items: VecDeque::with_capacity(16),
}
}

pub fn push(&mut self, value: T) {
self.items.push_back(value)
}

pub fn pop(&mut self) -> Option<T> {
self.items.pop_back()
}

pub fn borrow_mut(&mut self) -> Option<&mut T> {
self.items.back_mut()
}
}

0 comments on commit 71e6518

Please sign in to comment.