Merge branch 'chars-lexer'
This commit is contained in:
commit
b6a9b8a855
22 changed files with 579 additions and 1253 deletions
|
@ -4,4 +4,3 @@ version = "0.1.0"
|
||||||
authors = ["Eryn Wells <eryn@erynwells.me>"]
|
authors = ["Eryn Wells <eryn@erynwells.me>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
sibiltypes = { path = "../types" }
|
|
||||||
|
|
|
@ -1,108 +0,0 @@
|
||||||
/* char.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::marker::Sized;
|
|
||||||
use charset;
|
|
||||||
|
|
||||||
pub trait FromChar {
|
|
||||||
fn from_char(c: char) -> Option<Self> where Self: Sized;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Lexable {
|
|
||||||
fn is_character_leader(&self) -> bool;
|
|
||||||
fn is_dot(&self) -> bool;
|
|
||||||
fn is_hash(&self) -> bool;
|
|
||||||
fn is_quote(&self) -> bool;
|
|
||||||
fn is_left_paren(&self) -> bool;
|
|
||||||
fn is_right_paren(&self) -> bool;
|
|
||||||
fn is_string_quote(&self) -> bool;
|
|
||||||
fn is_string_escape_leader(&self) -> bool;
|
|
||||||
fn is_string_escaped(&self) -> bool;
|
|
||||||
fn is_newline(&self) -> bool;
|
|
||||||
fn is_eof(&self) -> bool;
|
|
||||||
|
|
||||||
fn is_identifier_initial(&self) -> bool;
|
|
||||||
fn is_identifier_subsequent(&self) -> bool;
|
|
||||||
fn is_identifier_delimiter(&self) -> bool;
|
|
||||||
|
|
||||||
fn is_boolean_true(&self) -> bool;
|
|
||||||
fn is_boolean_false(&self) -> bool;
|
|
||||||
|
|
||||||
fn is_comment_initial(&self) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Lexable for char {
|
|
||||||
fn is_left_paren(&self) -> bool {
|
|
||||||
*self == '('
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_right_paren(&self) -> bool {
|
|
||||||
*self == ')'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_character_leader(&self) -> bool {
|
|
||||||
*self == '\\'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_dot(&self) -> bool {
|
|
||||||
*self == '.'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_hash(&self) -> bool {
|
|
||||||
*self == '#'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_quote(&self) -> bool {
|
|
||||||
*self == '\''
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_string_quote(&self) -> bool {
|
|
||||||
*self == '"'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_string_escape_leader(&self) -> bool {
|
|
||||||
*self == '\\'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_string_escaped(&self) -> bool {
|
|
||||||
*self == '"' || *self == '\\'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_boolean_true(&self) -> bool {
|
|
||||||
*self == 't'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_boolean_false(&self) -> bool {
|
|
||||||
*self == 'f'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_newline(&self) -> bool {
|
|
||||||
*self == '\n'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_eof(&self) -> bool {
|
|
||||||
*self == '\0'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_comment_initial(&self) -> bool {
|
|
||||||
*self == ';'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_identifier_initial(&self) -> bool {
|
|
||||||
charset::identifier_initials().contains(&self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_identifier_subsequent(&self) -> bool {
|
|
||||||
charset::identifier_subsequents().contains(&self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_identifier_delimiter(&self) -> bool {
|
|
||||||
self.is_whitespace()
|
|
||||||
|| self.is_comment_initial()
|
|
||||||
|| self.is_left_paren()
|
|
||||||
|| self.is_right_paren()
|
|
||||||
|| self.is_string_quote()
|
|
||||||
|| self.is_eof()
|
|
||||||
}
|
|
||||||
}
|
|
53
lexer/src/chars.rs
Normal file
53
lexer/src/chars.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/* lexer/src/chars.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub trait Lexable {
|
||||||
|
fn is_left_paren(&self) -> bool;
|
||||||
|
fn is_right_paren(&self) -> bool;
|
||||||
|
fn is_identifier_initial(&self) -> bool;
|
||||||
|
fn is_identifier_subsequent(&self) -> bool;
|
||||||
|
fn is_identifier_delimiter(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Lexable for char {
|
||||||
|
fn is_left_paren(&self) -> bool {
|
||||||
|
*self == '('
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_right_paren(&self) -> bool {
|
||||||
|
*self == ')'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_identifier_initial(&self) -> bool {
|
||||||
|
self.is_alphabetic() || self.is_special_initial()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_identifier_subsequent(&self) -> bool {
|
||||||
|
self.is_identifier_initial() || self.is_numeric() || self.is_special_subsequent()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_identifier_delimiter(&self) -> bool {
|
||||||
|
self.is_whitespace() || self.is_left_paren() || self.is_right_paren()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait LexableSpecial {
|
||||||
|
fn is_special_initial(&self) -> bool;
|
||||||
|
fn is_special_subsequent(&self) -> bool;
|
||||||
|
fn is_explicit_sign(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LexableSpecial for char {
|
||||||
|
fn is_special_initial(&self) -> bool {
|
||||||
|
"!$%&*/:<=>?~_^".contains(*self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_special_subsequent(&self) -> bool {
|
||||||
|
self.is_explicit_sign() || ".@".contains(*self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_explicit_sign(&self) -> bool {
|
||||||
|
*self == '+' || *self == '-'
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,43 +0,0 @@
|
||||||
/* charset.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::iter::FromIterator;
|
|
||||||
|
|
||||||
pub type CharSet = HashSet<char>;
|
|
||||||
|
|
||||||
// TODO: Use std::sync::Once for these sets?
|
|
||||||
// https://doc.rust-lang.org/beta/std/sync/struct.Once.html
|
|
||||||
|
|
||||||
fn ascii_letters() -> CharSet {
|
|
||||||
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars();
|
|
||||||
CharSet::from_iter(letters)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ascii_digits() -> CharSet {
|
|
||||||
let digits = "1234567890".chars();
|
|
||||||
CharSet::from_iter(digits)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A set of all characters allowed to start Scheme identifiers.
|
|
||||||
pub fn identifier_initials() -> CharSet {
|
|
||||||
let letters = ascii_letters();
|
|
||||||
let extras = CharSet::from_iter("!$%&*/:<=>?~_^".chars());
|
|
||||||
let mut initials = CharSet::new();
|
|
||||||
initials.extend(letters.iter());
|
|
||||||
initials.extend(extras.iter());
|
|
||||||
initials
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A set of all characters allowed to follow an identifier initial.
|
|
||||||
pub fn identifier_subsequents() -> CharSet {
|
|
||||||
let initials = identifier_initials();
|
|
||||||
let digits = ascii_digits();
|
|
||||||
let extras = CharSet::from_iter(".+-".chars());
|
|
||||||
let mut subsequents = CharSet::new();
|
|
||||||
subsequents.extend(initials.iter());
|
|
||||||
subsequents.extend(digits.iter());
|
|
||||||
subsequents.extend(extras.iter());
|
|
||||||
subsequents
|
|
||||||
}
|
|
16
lexer/src/error.rs
Normal file
16
lexer/src/error.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
/* lexer/src/error.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub struct Error {
|
||||||
|
message: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
pub fn new(msg: String) -> Error {
|
||||||
|
Error {
|
||||||
|
message: msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,526 +0,0 @@
|
||||||
/* lexer.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
use sibiltypes::Object;
|
|
||||||
use sibiltypes::number::Exact;
|
|
||||||
|
|
||||||
use char::{FromChar, Lexable};
|
|
||||||
use number::{NumberBuilder, Radix, Sign};
|
|
||||||
use str::{CharAt, RelativeIndexable};
|
|
||||||
use token::{Lex, Token};
|
|
||||||
|
|
||||||
type StateResult = Result<Option<Token>, String>;
|
|
||||||
|
|
||||||
trait HasResult {
|
|
||||||
fn has_token(&self) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum State {
|
|
||||||
Char,
|
|
||||||
NamedChar(HashSet<&'static str>, String),
|
|
||||||
Comment,
|
|
||||||
Initial,
|
|
||||||
Id,
|
|
||||||
Dot,
|
|
||||||
Hash,
|
|
||||||
Number,
|
|
||||||
NumberExact,
|
|
||||||
NumberDecimal,
|
|
||||||
NumberRadix,
|
|
||||||
NumberSign,
|
|
||||||
Sign,
|
|
||||||
String,
|
|
||||||
StringEscape,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Lexer {
|
|
||||||
input: String,
|
|
||||||
begin: usize,
|
|
||||||
forward: usize,
|
|
||||||
line: usize,
|
|
||||||
line_offset: usize,
|
|
||||||
state: State,
|
|
||||||
number_builder: NumberBuilder,
|
|
||||||
string_value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Lexer {
|
|
||||||
pub fn new(input: &str) -> Lexer {
|
|
||||||
Lexer {
|
|
||||||
input: String::from(input),
|
|
||||||
begin: 0,
|
|
||||||
forward: 0,
|
|
||||||
line: 1,
|
|
||||||
line_offset: 1,
|
|
||||||
state: State::Initial,
|
|
||||||
number_builder: NumberBuilder::new(),
|
|
||||||
string_value: String::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Lexer {
|
|
||||||
fn begin_lexing(&mut self) {
|
|
||||||
self.forward = self.begin;
|
|
||||||
self.state = State::Initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Advance the forward pointer to the next character.
|
|
||||||
fn advance(&mut self) {
|
|
||||||
self.forward = self.input.index_after(self.forward);
|
|
||||||
self.line_offset += 1;
|
|
||||||
println!("> forward={}", self.forward);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Retract the forward pointer to the previous character.
|
|
||||||
fn retract(&mut self) {
|
|
||||||
self.forward = self.input.index_before(self.forward);
|
|
||||||
self.line_offset -= 1;
|
|
||||||
println!("< forward={}", self.forward);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Advance the begin pointer to prepare for the next iteration.
|
|
||||||
fn advance_begin(&mut self) {
|
|
||||||
self.begin = self.input.index_after(self.forward);
|
|
||||||
self.forward = self.begin;
|
|
||||||
println!("> begin={}, forward={}", self.begin, self.forward);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update lexer state when it encounters a newline.
|
|
||||||
fn handle_newline(&mut self) {
|
|
||||||
self.line += 1;
|
|
||||||
self.line_offset = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the substring between the two input indexes. This is the value to give to a new Token instance.
|
|
||||||
fn value(&self) -> String {
|
|
||||||
self.input[self.begin .. self.forward].to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error_string(&self, message: String) -> String {
|
|
||||||
format!("{}:{}: {}", self.line, self.line_offset, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn token_result(&self, token: Token) -> StateResult {
|
|
||||||
Ok(Some(token))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generic_error(&self, c: char) -> StateResult {
|
|
||||||
Err(self.error_string(format!("Invalid token character: {}", c)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Lexer {
|
|
||||||
/// Handle self.state == State::Initial
|
|
||||||
fn state_initial(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_left_paren() {
|
|
||||||
return self.token_result(Token::LeftParen);
|
|
||||||
}
|
|
||||||
else if c.is_right_paren() {
|
|
||||||
return self.token_result(Token::RightParen);
|
|
||||||
}
|
|
||||||
else if c.is_dot() {
|
|
||||||
self.state = State::Dot;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_hash() {
|
|
||||||
self.state = State::Hash;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_quote() {
|
|
||||||
return self.token_result(Token::Quote);
|
|
||||||
}
|
|
||||||
else if c.is_string_quote() {
|
|
||||||
self.string_value = String::from("");
|
|
||||||
self.state = State::String;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
else if let Some(sign) = Sign::from_char(c) {
|
|
||||||
self.number_builder = NumberBuilder::new();
|
|
||||||
self.number_builder.sign(sign);
|
|
||||||
self.state = State::Sign;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_identifier_initial() {
|
|
||||||
self.state = State::Id;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
else if c.is_digit(10) {
|
|
||||||
self.number_builder = NumberBuilder::new();
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.state = State::Number;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
else if c.is_whitespace() {
|
|
||||||
if c.is_newline() {
|
|
||||||
self.handle_newline();
|
|
||||||
}
|
|
||||||
self.advance_begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
else if c.is_comment_initial() {
|
|
||||||
self.state = State::Comment;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::Id
|
|
||||||
fn state_identifier(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_identifier_subsequent() {
|
|
||||||
// Stay in Id state.
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_identifier_delimiter() {
|
|
||||||
let value = self.value();
|
|
||||||
self.retract();
|
|
||||||
return self.token_result(Token::Id(value));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::Char
|
|
||||||
fn state_char(&mut self, c: char) -> StateResult {
|
|
||||||
self.advance();
|
|
||||||
let lower_c = c.to_lowercase().collect::<String>();
|
|
||||||
let mut candidates: HashSet<&str> = HashSet::new();
|
|
||||||
for c in names::set().iter() {
|
|
||||||
if c.starts_with(&lower_c) {
|
|
||||||
candidates.insert(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if candidates.len() > 0 {
|
|
||||||
self.state = State::NamedChar(candidates, lower_c);
|
|
||||||
} else {
|
|
||||||
return self.token_result(Token::Character(Object::Char(c)));
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::NamedChar
|
|
||||||
fn state_named_char(&mut self, c: char) -> StateResult {
|
|
||||||
let (candidates, mut progress) = match self.state {
|
|
||||||
State::NamedChar(ref candidates, ref progress) => (candidates.clone(), progress.clone()),
|
|
||||||
_ => panic!("Called state_named_char without being in NamedChar state")
|
|
||||||
};
|
|
||||||
|
|
||||||
if c.is_identifier_delimiter() || c.is_eof() {
|
|
||||||
if progress.len() == 1 {
|
|
||||||
self.retract();
|
|
||||||
let token_char = Object::Char(progress.chars().next().unwrap());
|
|
||||||
return self.token_result(Token::Character(token_char));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
progress.push(c);
|
|
||||||
|
|
||||||
let candidates: HashSet<&str> = {
|
|
||||||
let filtered = candidates.iter().filter(|c| c.starts_with(&progress)).map(|c| *c);
|
|
||||||
filtered.collect()
|
|
||||||
};
|
|
||||||
|
|
||||||
if candidates.len() == 1 {
|
|
||||||
let candidate = *candidates.iter().next().unwrap();
|
|
||||||
if candidate == &progress {
|
|
||||||
let token_char = Object::from_char_named(&progress);
|
|
||||||
self.token_result(Token::Character(token_char))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
self.state = State::NamedChar(candidates, progress);
|
|
||||||
self.advance();
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if candidates.len() > 1 {
|
|
||||||
self.state = State::NamedChar(candidates, progress);
|
|
||||||
self.advance();
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
self.generic_error(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::Dot
|
|
||||||
fn state_dot(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_identifier_delimiter() {
|
|
||||||
self.retract();
|
|
||||||
return self.token_result(Token::Dot);
|
|
||||||
}
|
|
||||||
else if c.is_digit(10) {
|
|
||||||
self.number_builder = NumberBuilder::new();
|
|
||||||
self.number_builder.extend_decimal_value(c);
|
|
||||||
self.state = State::NumberDecimal;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::Hash
|
|
||||||
fn state_hash(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_boolean_true() || c.is_boolean_false() {
|
|
||||||
self.advance();
|
|
||||||
let token_bool = Object::Bool(c.is_boolean_true());
|
|
||||||
return self.token_result(Token::Boolean(token_bool));
|
|
||||||
}
|
|
||||||
else if c.is_left_paren() {
|
|
||||||
self.advance();
|
|
||||||
return self.token_result(Token::LeftVectorParen);
|
|
||||||
}
|
|
||||||
else if c.is_character_leader() {
|
|
||||||
self.state = State::Char;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if let Some(radix) = Radix::from_char(c) {
|
|
||||||
self.number_builder.radix(radix);
|
|
||||||
self.state = State::NumberRadix;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if let Some(exactness) = Exact::from_char(c) {
|
|
||||||
self.number_builder.exact(exactness);
|
|
||||||
self.state = State::NumberExact;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle self.state == State::Number
|
|
||||||
fn state_number(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_digit(self.number_builder.radix_value()) {
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_dot() {
|
|
||||||
self.state = State::NumberDecimal;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_identifier_delimiter() {
|
|
||||||
self.retract();
|
|
||||||
return self.token_result(Token::Number(self.number_builder.resolve()));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_number_exactness(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_hash() {
|
|
||||||
self.state = State::Hash;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if let Some(sign) = Sign::from_char(c) {
|
|
||||||
self.number_builder.sign(sign);
|
|
||||||
self.state = State::NumberSign;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_digit(self.number_builder.radix_value()) {
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.state = State::Number;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_number_decimal(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_digit(Radix::Dec.value()) {
|
|
||||||
self.number_builder.extend_decimal_value(c);
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_identifier_delimiter() {
|
|
||||||
self.retract();
|
|
||||||
return self.token_result(Token::Number(self.number_builder.resolve()));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_number_radix(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_digit(self.number_builder.radix_value()) {
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.state = State::Number;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_dot() {
|
|
||||||
self.state = State::NumberDecimal;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_hash() {
|
|
||||||
self.state = State::Hash;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if let Some(sign) = Sign::from_char(c) {
|
|
||||||
self.number_builder.sign(sign);
|
|
||||||
self.state = State::NumberSign;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_number_sign(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_digit(self.number_builder.radix_value()) {
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.state = State::Number;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_dot() {
|
|
||||||
self.state = State::NumberDecimal;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_sign(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_digit(Radix::Dec.value()) {
|
|
||||||
self.number_builder.extend_value(c);
|
|
||||||
self.state = State::Number;
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
else if c.is_identifier_delimiter() {
|
|
||||||
let value = self.value();
|
|
||||||
self.retract();
|
|
||||||
return self.token_result(Token::Id(value));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return self.generic_error(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_string(&mut self, c: char) -> StateResult {
|
|
||||||
self.advance();
|
|
||||||
if c.is_string_quote() {
|
|
||||||
return self.token_result(Token::String(self.string_value.clone()));
|
|
||||||
}
|
|
||||||
else if c.is_string_escape_leader() {
|
|
||||||
self.state = State::StringEscape;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
self.string_value.push(c);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_string_escape(&mut self, c: char) -> StateResult {
|
|
||||||
let char_to_push = match c {
|
|
||||||
'0' => '\0',
|
|
||||||
'n' => '\n',
|
|
||||||
't' => '\t',
|
|
||||||
'"' => '"',
|
|
||||||
'\\' => '\\',
|
|
||||||
_ => return Err(self.error_string(format!("Invalid string escape character: {}", c))),
|
|
||||||
};
|
|
||||||
self.string_value.push(char_to_push);
|
|
||||||
self.state = State::String;
|
|
||||||
self.advance();
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn state_comment(&mut self, c: char) -> StateResult {
|
|
||||||
if c.is_newline() {
|
|
||||||
self.handle_newline();
|
|
||||||
return self.token_result(Token::Comment(self.value()));
|
|
||||||
}
|
|
||||||
else if c.is_eof() {
|
|
||||||
return self.token_result(Token::Comment(self.value()));
|
|
||||||
}
|
|
||||||
self.advance();
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for Lexer {
|
|
||||||
type Item = Lex;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Lex> {
|
|
||||||
self.begin_lexing();
|
|
||||||
if self.begin == self.input.len() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let mut token: Option<Token> = None;
|
|
||||||
println!("Lexing '{}'", &self.input[self.begin ..]);
|
|
||||||
while token.is_none() {
|
|
||||||
let c = match self.input.char_at(self.forward) {
|
|
||||||
Some(c) => c,
|
|
||||||
None => '\0',
|
|
||||||
};
|
|
||||||
println!("state={:?} c='{}'", self.state, c);
|
|
||||||
let previous_forward = self.forward;
|
|
||||||
let result = match self.state {
|
|
||||||
State::Char=> self.state_char(c),
|
|
||||||
State::NamedChar(_, _) => self.state_named_char(c),
|
|
||||||
State::Comment => self.state_comment(c),
|
|
||||||
State::Dot => self.state_dot(c),
|
|
||||||
State::Hash => self.state_hash(c),
|
|
||||||
State::Id => self.state_identifier(c),
|
|
||||||
State::Initial => self.state_initial(c),
|
|
||||||
State::Number => self.state_number(c),
|
|
||||||
State::NumberDecimal => self.state_number_decimal(c),
|
|
||||||
State::NumberExact => self.state_number_exactness(c),
|
|
||||||
State::NumberRadix => self.state_number_radix(c),
|
|
||||||
State::NumberSign => self.state_number_sign(c),
|
|
||||||
State::Sign => self.state_sign(c),
|
|
||||||
State::String => self.state_string(c),
|
|
||||||
State::StringEscape => self.state_string_escape(c),
|
|
||||||
};
|
|
||||||
debug_assert!(result.has_token() || self.forward != previous_forward, "No lexing progress made!");
|
|
||||||
if result.has_token() {
|
|
||||||
token = result.ok().unwrap();
|
|
||||||
}
|
|
||||||
else if result.is_err() {
|
|
||||||
assert!(false, "{}", result.err().unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.advance_begin();
|
|
||||||
match token {
|
|
||||||
Some(t) => Some(Lex::new(t, self.line, self.line_offset)),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasResult for StateResult {
|
|
||||||
fn has_token(&self) -> bool {
|
|
||||||
match *self {
|
|
||||||
Ok(ref token) => match *token {
|
|
||||||
Some(_) => true,
|
|
||||||
None => false,
|
|
||||||
},
|
|
||||||
Err(_) => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
265
lexer/src/lib.rs
265
lexer/src/lib.rs
|
@ -1,183 +1,124 @@
|
||||||
extern crate sibiltypes;
|
/* lexer/src/lib.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
mod char;
|
use std::iter::Peekable;
|
||||||
mod charset;
|
use chars::Lexable;
|
||||||
mod lexer;
|
|
||||||
mod number;
|
mod chars;
|
||||||
mod str;
|
mod error;
|
||||||
mod token;
|
mod token;
|
||||||
|
|
||||||
pub use lexer::Lexer;
|
pub use error::Error;
|
||||||
pub use token::Token;
|
pub use token::{Lex, Token};
|
||||||
|
|
||||||
pub fn lex(input: &str) -> Lexer {
|
pub type Result = std::result::Result<Lex, Error>;
|
||||||
Lexer::new(&input)
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
enum Resume { Here, AtNext }
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
enum IterationResult {
|
||||||
|
Finish,
|
||||||
|
Continue,
|
||||||
|
Emit(Token, Resume),
|
||||||
|
Error(Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
pub struct Lexer<T> where T: Iterator<Item=char> {
|
||||||
mod tests {
|
input: Peekable<T>,
|
||||||
use sibiltypes::{Bool, Char, Number};
|
line: usize,
|
||||||
use std::iter::Iterator;
|
offset: usize,
|
||||||
use super::lex;
|
}
|
||||||
use lexer::Lexer;
|
|
||||||
use token::Token;
|
|
||||||
|
|
||||||
#[test]
|
impl<T> Lexer<T> where T: Iterator<Item=char> {
|
||||||
fn finds_parens() {
|
pub fn new(input: T) -> Lexer<T> {
|
||||||
check_single_token("(", Token::LeftParen);
|
Lexer {
|
||||||
check_single_token(")", Token::RightParen);
|
input: input.peekable(),
|
||||||
check_single_token("#(", Token::LeftVectorParen);
|
line: 0,
|
||||||
|
offset: 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn emit(&self, token: Token, resume: Resume) -> IterationResult {
|
||||||
fn finds_characters() {
|
IterationResult::Emit(token, resume)
|
||||||
check_single_token("#\\a", Token::Character(Char('a')));
|
|
||||||
check_single_token("#\\n", Token::Character(Char('n')));
|
|
||||||
check_single_token("#\\s", Token::Character(Char('s')));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn fail(&self, msg: String) -> IterationResult {
|
||||||
fn finds_named_characters() {
|
IterationResult::Error(Error::new(msg))
|
||||||
check_single_token("#\\newline", Token::Character(Char('\n')));
|
|
||||||
check_single_token("#\\null", Token::Character(Char('\0')));
|
|
||||||
check_single_token("#\\space", Token::Character(Char(' ')));
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
impl<T> Lexer<T> where T: Iterator<Item=char> {
|
||||||
fn finds_dots() {
|
fn handle_whitespace(&mut self, c: char) {
|
||||||
check_single_token(".", Token::Dot);
|
if c == '\n' {
|
||||||
|
self.line += 1;
|
||||||
let mut lexer = Lexer::new("abc . abc");
|
self.offset = 0;
|
||||||
assert_next_token(&mut lexer, &Token::Id(String::from("abc")));
|
}
|
||||||
assert_next_token(&mut lexer, &Token::Dot);
|
else {
|
||||||
assert_next_token(&mut lexer, &Token::Id(String::from("abc")));
|
self.offset += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
impl<T> Iterator for Lexer<T> where T: Iterator<Item=char> {
|
||||||
fn finds_identifiers() {
|
type Item = Result;
|
||||||
let tok = |s: &str| { check_single_token(s, Token::Id(String::from(s))); };
|
|
||||||
tok("abc");
|
|
||||||
tok("number?");
|
|
||||||
tok("+");
|
|
||||||
tok("-");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
fn finds_booleans() {
|
let mut buffer = String::new();
|
||||||
check_single_token("#t", Token::Boolean(Bool(true)));
|
loop {
|
||||||
check_single_token("#f", Token::Boolean(Bool(false)));
|
let peek = self.input.peek().map(char::clone);
|
||||||
}
|
let result = if buffer.is_empty() {
|
||||||
|
match peek {
|
||||||
#[test]
|
Some(c) if c.is_left_paren() => {
|
||||||
fn finds_comments() {
|
buffer.push(c);
|
||||||
let s = "; a comment";
|
self.emit(Token::LeftParen, Resume::AtNext)
|
||||||
check_single_token(s, Token::Comment(String::from(s)));
|
},
|
||||||
}
|
Some(c) if c.is_right_paren() => {
|
||||||
|
buffer.push(c);
|
||||||
#[test]
|
self.emit(Token::RightParen, Resume::AtNext)
|
||||||
fn finds_escaped_characters_in_strings() {
|
},
|
||||||
check_single_token("\"\\\\\"", Token::String(String::from("\\")));
|
Some(c) if c.is_whitespace() => {
|
||||||
check_single_token("\"\\\"\"", Token::String(String::from("\"")));
|
self.handle_whitespace(c);
|
||||||
check_single_token("\"\\n\"", Token::String(String::from("\n")));
|
IterationResult::Continue
|
||||||
}
|
},
|
||||||
|
Some(c) if c.is_identifier_initial() => {
|
||||||
#[test]
|
buffer.push(c);
|
||||||
fn finds_numbers() {
|
IterationResult::Continue
|
||||||
check_single_token("34", Token::Number(Number::from_int(34, true)));
|
},
|
||||||
check_single_token(".34", Token::Number(Number::from_float(0.34, false)));
|
Some(c) => self.fail(format!("Invalid character: {}", c)),
|
||||||
check_single_token("0.34", Token::Number(Number::from_float(0.34, false)));
|
// We found EOF and there's no pending string, so just finish.
|
||||||
}
|
None => IterationResult::Finish,
|
||||||
|
}
|
||||||
#[test]
|
|
||||||
fn finds_rational_numbers() {
|
|
||||||
check_single_token("3/2", Token::Number(Number::from_quotient(3, 2, true)));
|
|
||||||
check_single_token("-3/2", Token::Number(Number::from_quotient(-3, 2, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_negative_numbers() {
|
|
||||||
check_single_token("-3", Token::Number(Number::from_int(-3, true)));
|
|
||||||
check_single_token("-0", Token::Number(Number::from_int(-0, true)));
|
|
||||||
check_single_token("-0.56", Token::Number(Number::from_float(-0.56, false)));
|
|
||||||
check_single_token("-3.14159", Token::Number(Number::from_float(-3.14159, false)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_bin_numbers() {
|
|
||||||
check_single_token("#b0", Token::Number(Number::from_int(0b0, true)));
|
|
||||||
check_single_token("#b01011", Token::Number(Number::from_int(0b01011, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_dec_numbers() {
|
|
||||||
check_single_token("34", Token::Number(Number::from_int(34, true)));
|
|
||||||
check_single_token("#d89", Token::Number(Number::from_int(89, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_oct_numbers() {
|
|
||||||
check_single_token("#o45", Token::Number(Number::from_int(0o45, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_exact_numbers() {
|
|
||||||
check_single_token("#e45", Token::Number(Number::from_int(45, true)));
|
|
||||||
check_single_token("#e-45", Token::Number(Number::from_int(-45, true)));
|
|
||||||
check_single_token("#e4.5", Token::Number(Number::from_float(4.5, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_hex_numbers() {
|
|
||||||
check_single_token("#h4A65", Token::Number(Number::from_int(0x4A65, true)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_quote() {
|
|
||||||
check_single_token("'", Token::Quote);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn finds_strings() {
|
|
||||||
check_single_token("\"\"", Token::String(String::from("")));
|
|
||||||
check_single_token("\"abc\"", Token::String(String::from("abc")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn lexes_simple_expression() {
|
|
||||||
check_tokens("(+ 3.4 6.8)", vec![
|
|
||||||
Token::LeftParen,
|
|
||||||
Token::Id(String::from("+")),
|
|
||||||
Token::Number(Number::from_float(3.4, false)),
|
|
||||||
Token::Number(Number::from_float(6.8, false)),
|
|
||||||
Token::RightParen]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn lexes_quoted_identifier() {
|
|
||||||
check_tokens("'abc", vec![Token::Quote, Token::Id(String::from("abc"))]);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_single_token(input: &str, expected: Token) {
|
|
||||||
let mut lexer = Lexer::new(input);
|
|
||||||
assert_next_token(&mut lexer, &expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_tokens(input: &str, expected: Vec<Token>) {
|
|
||||||
let lexer = lex(input);
|
|
||||||
let mut expected_iter = expected.iter();
|
|
||||||
for lex in lexer {
|
|
||||||
if let Some(expected_token) = expected_iter.next() {
|
|
||||||
assert_eq!(lex.token, *expected_token);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
assert!(false, "Found a token we didn't expect: {:?}", lex.token);
|
match peek {
|
||||||
}
|
Some(c) if c.is_identifier_subsequent() => {
|
||||||
|
buffer.push(c);
|
||||||
|
IterationResult::Continue
|
||||||
|
}
|
||||||
|
Some(c) if c.is_identifier_delimiter() =>
|
||||||
|
self.emit(Token::Id, Resume::Here),
|
||||||
|
Some(c) => self.fail(format!("Invalid character: {}", c)),
|
||||||
|
// Found EOF. Emit what we have and finish.
|
||||||
|
// Note: the Resume argument doesn't matter in this case since the input
|
||||||
|
// iterator will always be None from here on.
|
||||||
|
None => self.emit(Token::Id, Resume::Here),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
IterationResult::Finish => break,
|
||||||
|
IterationResult::Continue => self.input.next(),
|
||||||
|
IterationResult::Emit(token, resume) => {
|
||||||
|
if resume == Resume::AtNext {
|
||||||
|
self.input.next();
|
||||||
|
}
|
||||||
|
let lex = Lex::new(token, &buffer, self.line, self.offset);
|
||||||
|
return Some(Ok(lex))
|
||||||
|
},
|
||||||
|
IterationResult::Error(err) => return Some(Err(err)),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// TODO: Check that all expected tokens are consumed.
|
None
|
||||||
}
|
|
||||||
|
|
||||||
fn assert_next_token(lexer: &mut Lexer, expected: &Token) {
|
|
||||||
let lex = lexer.next().unwrap();
|
|
||||||
assert_eq!(lex.token, *expected);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
lexer/src/main.rs
Normal file
14
lexer/src/main.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/* lexer/src/main.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern crate sibillexer;
|
||||||
|
|
||||||
|
use sibillexer::Lexer;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let lexer = Lexer::new("(ab (cd) ef)".chars());
|
||||||
|
for tok in lexer {
|
||||||
|
println!("found {:?}", tok.unwrap());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,176 +0,0 @@
|
||||||
/* number.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
use sibiltypes::Object;
|
|
||||||
use sibiltypes::number::{Number, Exact};
|
|
||||||
use char::FromChar;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Radix { Bin, Oct, Dec, Hex }
|
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
|
||||||
pub enum Sign { Pos, Neg }
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NumberBuilder {
|
|
||||||
exact: Exact,
|
|
||||||
radix: Radix,
|
|
||||||
sign: Sign,
|
|
||||||
value: f64,
|
|
||||||
point: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NumberBuilder {
|
|
||||||
pub fn new() -> NumberBuilder {
|
|
||||||
NumberBuilder {
|
|
||||||
exact: Exact::Yes,
|
|
||||||
radix: Radix::Dec,
|
|
||||||
sign: Sign::Pos,
|
|
||||||
value: 0.0,
|
|
||||||
point: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exact<'a>(&'a mut self, ex: Exact) -> &'a mut NumberBuilder {
|
|
||||||
self.exact = ex;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn radix<'a>(&'a mut self, r: Radix) -> &'a mut NumberBuilder {
|
|
||||||
self.radix = r;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sign<'a>(&'a mut self, s: Sign) -> &'a mut NumberBuilder {
|
|
||||||
self.sign = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extend_value<'a>(&'a mut self, digit: char) -> &'a mut Self {
|
|
||||||
if let Some(place) = NumberBuilder::place_value(digit) {
|
|
||||||
self.value = self.radix.float_value() * self.value + place;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// TODO: Indicate an error.
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn extend_decimal_value<'a>(&'a mut self, digit: char) -> &'a mut Self {
|
|
||||||
self.extend_value(digit);
|
|
||||||
self.point += 1;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resolve(&self) -> Number {
|
|
||||||
// TODO: Convert fields to Number type.
|
|
||||||
let value = if self.point > 0 { self.value / 10u32.pow(self.point) as f64 } else { self.value };
|
|
||||||
let value = if self.sign == Sign::Neg { value * -1.0 } else { value };
|
|
||||||
// TODO: Use an integer if we can.
|
|
||||||
Number::from_float(value, self.exact)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn radix_value(&self) -> u32 {
|
|
||||||
self.radix.value()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn place_value(digit: char) -> Option<f64> {
|
|
||||||
match digit {
|
|
||||||
'0' ... '9' => Some((digit as u32 - '0' as u32) as f64),
|
|
||||||
'a' ... 'f' => Some((digit as u32 - 'a' as u32 + 10) as f64),
|
|
||||||
'A' ... 'F' => Some((digit as u32 - 'A' as u32 + 10) as f64),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Radix {
|
|
||||||
pub fn value(&self) -> u32 {
|
|
||||||
match *self {
|
|
||||||
Radix::Bin => 2,
|
|
||||||
Radix::Oct => 8,
|
|
||||||
Radix::Dec => 10,
|
|
||||||
Radix::Hex => 16,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn float_value(&self) -> f64 {
|
|
||||||
self.value() as f64
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromChar for Radix {
|
|
||||||
fn from_char(c: char) -> Option<Radix> {
|
|
||||||
match c {
|
|
||||||
'b' => Some(Radix::Bin),
|
|
||||||
'o' => Some(Radix::Oct),
|
|
||||||
'd' => Some(Radix::Dec),
|
|
||||||
'h' => Some(Radix::Hex),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromChar for Sign {
|
|
||||||
fn from_char(c: char) -> Option<Sign> {
|
|
||||||
match c {
|
|
||||||
'+' => Some(Sign::Pos),
|
|
||||||
'-' => Some(Sign::Neg),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromChar for Exact {
|
|
||||||
fn from_char(c: char) -> Option<Exact> {
|
|
||||||
match c {
|
|
||||||
'i' => Some(Exact::No),
|
|
||||||
'e' => Some(Exact::Yes),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use sibiltypes::Number;
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn builds_integers() {
|
|
||||||
let mut b = NumberBuilder::new();
|
|
||||||
b.extend_value('3');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(3, true));
|
|
||||||
b.extend_value('4');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(34, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn builds_negative_integers() {
|
|
||||||
let num = NumberBuilder::new().sign(Sign::Neg).extend_value('3').resolve();
|
|
||||||
assert_eq!(num, Number::from_int(-3, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn builds_pointy_numbers() {
|
|
||||||
let mut b = NumberBuilder::new();
|
|
||||||
b.extend_value('5');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(5, true));
|
|
||||||
b.extend_decimal_value('3');
|
|
||||||
assert_eq!(b.resolve(), Number::from_float(5.3, false));
|
|
||||||
b.extend_decimal_value('4');
|
|
||||||
assert_eq!(b.resolve(), Number::from_float(5.34, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn builds_hex() {
|
|
||||||
let mut b = NumberBuilder::new();
|
|
||||||
b.radix(Radix::Hex).extend_value('4');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(0x4, true));
|
|
||||||
b.extend_value('A');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(0x4A, true));
|
|
||||||
b.extend_value('6');
|
|
||||||
assert_eq!(b.resolve(), Number::from_int(0x4A6, true));
|
|
||||||
}
|
|
||||||
}
|
|
103
lexer/src/str.rs
103
lexer/src/str.rs
|
@ -1,103 +0,0 @@
|
||||||
/* str.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
pub trait RelativeIndexable {
|
|
||||||
/// Get the index of the character boundary preceding the given index. The index does not need to be on a character
|
|
||||||
/// boundary.
|
|
||||||
fn index_before(&self, usize) -> usize;
|
|
||||||
|
|
||||||
/// Get the index of the character boundary following the given index. The index does not need to be on a character
|
|
||||||
/// boundary.
|
|
||||||
fn index_after(&self, usize) -> usize;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait CharAt {
|
|
||||||
/// Get the character at the given byte index. This index must be at a character boundary as defined by
|
|
||||||
/// `is_char_boundary()`.
|
|
||||||
fn char_at(&self, usize) -> Option<char>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RelativeIndexable for str {
|
|
||||||
fn index_before(&self, index: usize) -> usize {
|
|
||||||
if index == 0 {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
let mut index = index;
|
|
||||||
if index > self.len() {
|
|
||||||
index = self.len();
|
|
||||||
}
|
|
||||||
loop {
|
|
||||||
index -= 1;
|
|
||||||
if self.is_char_boundary(index) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index_after(&self, index: usize) -> usize {
|
|
||||||
if index >= self.len() {
|
|
||||||
return self.len();
|
|
||||||
}
|
|
||||||
let mut index = index;
|
|
||||||
loop {
|
|
||||||
index += 1;
|
|
||||||
if self.is_char_boundary(index) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CharAt for str {
|
|
||||||
fn char_at(&self, index: usize) -> Option<char> {
|
|
||||||
if !self.is_char_boundary(index) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let end = self.index_after(index);
|
|
||||||
let char_str = &self[index .. end];
|
|
||||||
char_str.chars().nth(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn index_before_is_well_behaved_for_ascii() {
|
|
||||||
let s = "abc";
|
|
||||||
|
|
||||||
// Sanity
|
|
||||||
assert_eq!(s.index_before(0), 0);
|
|
||||||
assert_eq!(s.index_before(2), 1);
|
|
||||||
|
|
||||||
// An index beyond the string bounds returns the index of the last character in the string.
|
|
||||||
{
|
|
||||||
let idx = s.index_before(4);
|
|
||||||
assert_eq!(idx, 2);
|
|
||||||
assert!(s.is_char_boundary(idx));
|
|
||||||
let last_char = &s[idx ..];
|
|
||||||
assert_eq!(last_char.len(), 1);
|
|
||||||
assert_eq!(last_char.chars().nth(0), Some('c'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn index_after_is_well_behaved_for_ascii() {
|
|
||||||
let s = "abc";
|
|
||||||
|
|
||||||
// Sanity
|
|
||||||
assert_eq!(s.index_after(0), 1);
|
|
||||||
assert_eq!(s.index_after(2), 3);
|
|
||||||
|
|
||||||
// An index beyond the string bounds returns the length of the string
|
|
||||||
{
|
|
||||||
let idx = s.index_after(4);
|
|
||||||
assert_eq!(idx, s.len());
|
|
||||||
assert!(s.is_char_boundary(idx));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +1,28 @@
|
||||||
/* token.rs
|
/* lexer/src/token.rs
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use sibiltypes::Object;
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
pub enum Token {
|
|
||||||
Boolean(Object),
|
|
||||||
Character(Object),
|
|
||||||
Comment(Object),
|
|
||||||
Dot,
|
|
||||||
Id(Object),
|
|
||||||
LeftParen,
|
|
||||||
LeftVectorParen,
|
|
||||||
Number(Object),
|
|
||||||
Quote,
|
|
||||||
RightParen,
|
|
||||||
String(Object),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Lex is a Token extracted from a specific position in an input string. It contains useful
|
|
||||||
/// information about the token's place in that input.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Lex {
|
pub struct Lex {
|
||||||
token: Token,
|
token: Token,
|
||||||
|
value: String,
|
||||||
line: usize,
|
line: usize,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
|
pub enum Token { LeftParen, RightParen, Id, }
|
||||||
|
|
||||||
impl Lex {
|
impl Lex {
|
||||||
pub fn new(token: Token, line: usize, offset: usize) -> Lex {
|
pub fn new(token: Token, value: &str, line: usize, offset: usize) -> Lex {
|
||||||
Lex { token: token, line: line, offset: offset }
|
Lex {
|
||||||
|
token: token,
|
||||||
|
value: String::from(value),
|
||||||
|
line: line,
|
||||||
|
offset: offset,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn token(&self) -> &Token { &self.token }
|
pub fn token(&self) -> Token { self.token }
|
||||||
pub fn line(&self) -> usize { self.line }
|
pub fn value(&self) -> &str { self.value.as_str() }
|
||||||
pub fn offset(&self) -> usize { self.offset }
|
|
||||||
}
|
}
|
||||||
|
|
33
lexer/tests/single_tokens.rs
Normal file
33
lexer/tests/single_tokens.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/* lexer/tests/single_token.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! Tests that single tokens are matches by the lexer.
|
||||||
|
|
||||||
|
extern crate sibillexer;
|
||||||
|
|
||||||
|
use sibillexer::{Lexer, Lex, Token};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lexer_finds_left_paren() {
|
||||||
|
let expected_lex = Lex::new(Token::LeftParen, "(", 0, 0);
|
||||||
|
let mut lex = Lexer::new("(".chars());
|
||||||
|
assert_eq!(lex.next(), Some(Ok(expected_lex)));
|
||||||
|
assert_eq!(lex.next(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lexer_finds_right_paren() {
|
||||||
|
let expected_lex = Lex::new(Token::RightParen, ")", 0, 0);
|
||||||
|
let mut lex = Lexer::new(")".chars());
|
||||||
|
assert_eq!(lex.next(), Some(Ok(expected_lex)));
|
||||||
|
assert_eq!(lex.next(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lexer_finds_id() {
|
||||||
|
let expected_lex = Lex::new(Token::Id, "abc", 0, 0);
|
||||||
|
let mut lex = Lexer::new("abc".chars());
|
||||||
|
assert_eq!(lex.next(), Some(Ok(expected_lex)));
|
||||||
|
assert_eq!(lex.next(), None);
|
||||||
|
}
|
|
@ -5,14 +5,50 @@
|
||||||
extern crate sibillexer;
|
extern crate sibillexer;
|
||||||
extern crate sibiltypes;
|
extern crate sibiltypes;
|
||||||
|
|
||||||
mod program;
|
mod node_parser;
|
||||||
|
|
||||||
use sibillexer::Lexer;
|
use std::iter::Peekable;
|
||||||
|
use sibillexer::Result as LexerResult;
|
||||||
use sibiltypes::Object;
|
use sibiltypes::Object;
|
||||||
|
use node_parser::{NodeParser, IdParser, ListParser};
|
||||||
|
|
||||||
struct ParseError { }
|
/// The output of calling `parse()` on a Parser is one of these Result objects.
|
||||||
|
pub type Result = std::result::Result<Object, ParseError>;
|
||||||
|
|
||||||
type Result = std::result::Result<Object, ParseError>;
|
#[derive(Debug)]
|
||||||
|
pub struct ParseError;
|
||||||
|
|
||||||
|
pub struct Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
|
input: Peekable<T>,
|
||||||
|
parsers: Vec<Box<NodeParser>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
|
pub fn new(input: T) -> Parser<T> {
|
||||||
|
Parser {
|
||||||
|
input: input.peekable(),
|
||||||
|
parsers: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Iterator for Parser<T> where T: Iterator<Item=LexerResult> {
|
||||||
|
type Item = Result;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
loop {
|
||||||
|
if let Some(lex) = self.input.next() {
|
||||||
|
if let Ok(lex) = lex {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq!(self.parsers.len(), 0);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
17
parser/src/main.rs
Normal file
17
parser/src/main.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/* parser/src/main.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern crate sibillexer;
|
||||||
|
extern crate sibilparser;
|
||||||
|
|
||||||
|
use sibillexer::Lexer;
|
||||||
|
use sibilparser::Parser;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let lexer = Lexer::new("(ab)".chars());
|
||||||
|
let parser = Parser::new(lexer);
|
||||||
|
for thing in parser {
|
||||||
|
println!("{:?}", thing);
|
||||||
|
}
|
||||||
|
}
|
91
parser/src/node_parser.rs
Normal file
91
parser/src/node_parser.rs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/* node_parser.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use sibillexer::{Lex, Token};
|
||||||
|
use sibiltypes::{Object, ObjectPtr};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum NodeParseResult {
|
||||||
|
/// Continue parsing with this NodeParser. The passed in Lex was consumed.
|
||||||
|
Continue,
|
||||||
|
/// This NodeParser has completed its work and has produced the given Object
|
||||||
|
/// as a result.
|
||||||
|
Complete { obj: ObjectPtr },
|
||||||
|
/// Push a new NodeParser onto the parsing stack and let that parser proceed
|
||||||
|
/// with the current Lex.
|
||||||
|
Push { next: Box<NodeParser> },
|
||||||
|
/// There was an error parsing with the current Lex.
|
||||||
|
Error { msg: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `NodeParser` is responsible for parsing one particular thing in the Scheme
|
||||||
|
/// parse tree. Roughly, there should be one `XParser` for each variant of the
|
||||||
|
/// `sibiltypes::Object` enum. As the top-level `Parser` object progresses
|
||||||
|
/// through the stream of tokens, new NodeParsers are created to handle the
|
||||||
|
/// nodes it encounters.
|
||||||
|
pub trait NodeParser: Debug {
|
||||||
|
fn parse(&mut self, lex: Lex) -> NodeParseResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ProgramParser {
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgramParser {
|
||||||
|
pub fn new() -> ProgramParser {
|
||||||
|
ProgramParser { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeParser for ProgramParser {
|
||||||
|
fn parse(&mut self, lex: Lex) -> NodeParseResult {
|
||||||
|
NodeParseResult::Error { msg: "womp".to_string() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct IdParser {
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IdParser {
|
||||||
|
pub fn new() -> IdParser {
|
||||||
|
IdParser { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeParser for IdParser {
|
||||||
|
fn parse(&mut self, lex: Lex) -> NodeParseResult {
|
||||||
|
match lex.token() {
|
||||||
|
Token::Id => {
|
||||||
|
let value = String::from(lex.value());
|
||||||
|
let obj = ObjectPtr::new(Object::Symbol(value));
|
||||||
|
NodeParseResult::Complete { obj: obj }
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let msg = String::from(format!("Invalid token: {:?}", lex));
|
||||||
|
NodeParseResult::Error { msg: msg }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ListParser {
|
||||||
|
list: ObjectPtr
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListParser {
|
||||||
|
pub fn new() -> ListParser {
|
||||||
|
ListParser {
|
||||||
|
list: ObjectPtr::Null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NodeParser for ListParser {
|
||||||
|
fn parse(&mut self, lex: Lex) -> NodeParseResult {
|
||||||
|
NodeParseResult::Error { msg: "womp".to_string() }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +0,0 @@
|
||||||
/* parser/src/program.rs
|
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
|
||||||
*/
|
|
||||||
|
|
||||||
use sibillexer::Lexer;
|
|
||||||
use super::Result;
|
|
||||||
use super::ParseError;
|
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
pub mod number;
|
|
||||||
pub mod char;
|
|
||||||
|
|
||||||
mod bool;
|
|
||||||
mod object;
|
mod object;
|
||||||
mod predicates;
|
mod pair;
|
||||||
|
mod sym;
|
||||||
|
|
||||||
pub use object::Object;
|
pub use object::Obj;
|
||||||
pub use object::ObjectPtr;
|
pub use pair::Pair;
|
||||||
pub use predicates::*;
|
pub use sym::Sym;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -90,13 +90,14 @@ impl fmt::Display for Number {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::Exact;
|
||||||
use super::Number;
|
use super::Number;
|
||||||
use super::real::Real;
|
use super::real::Real;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn exact_numbers_are_exact() {
|
fn exact_numbers_are_exact() {
|
||||||
assert!(Number::from_int(3, true).is_exact());
|
assert!(Number::from_int(3, Exact::Yes).is_exact());
|
||||||
assert!(!Number::from_int(3, false).is_exact());
|
assert!(!Number::from_int(3, Exact::No).is_exact());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
|
|
||||||
//! # Objects
|
//! # Objects
|
||||||
//!
|
//!
|
||||||
//! All scheme types are represented by the `Object` enum defined in this
|
//! All Scheme types implement the `Object` trait defined in this module. Most
|
||||||
//! module. Most references to objects are going to be through an `ObjectPtr`.
|
//! references to objects are going to be through an `ObjectPtr`.
|
||||||
//!
|
//!
|
||||||
//! ## Type Predicates
|
//! ## Type Predicates
|
||||||
//!
|
//!
|
||||||
|
@ -13,105 +13,133 @@
|
||||||
//! available types in Scheme. These predicates are implemented as `is_*`
|
//! available types in Scheme. These predicates are implemented as `is_*`
|
||||||
//! methods in a bunch of `Is*` traits defined below.
|
//! methods in a bunch of `Is*` traits defined below.
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ops::Deref;
|
use super::*;
|
||||||
use number::Number;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
pub enum Obj {
|
||||||
pub enum ObjectPtr {
|
|
||||||
/// Absence of a value. A null pointer.
|
|
||||||
Null,
|
Null,
|
||||||
/// A pointer to an object.
|
Ptr(Box<Object>)
|
||||||
Ptr(Box<Object>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
pub trait Object:
|
||||||
pub enum Object {
|
fmt::Display
|
||||||
Bool(bool),
|
{
|
||||||
ByteVector(Vec<u8>),
|
fn as_any(&self) -> &Any;
|
||||||
Char(char),
|
fn as_pair(&self) -> Option<&Pair>;
|
||||||
Number(Number),
|
fn as_sym(&self) -> Option<&Sym>;
|
||||||
Pair(ObjectPtr, ObjectPtr),
|
|
||||||
//Procedure/*( TODO: Something )*/,
|
|
||||||
//Record/*( TODO: Something )*/,
|
|
||||||
String(String),
|
|
||||||
Symbol(String),
|
|
||||||
Vector(Vec<ObjectPtr>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectPtr {
|
impl Obj {
|
||||||
pub fn new(obj: Object) -> ObjectPtr { ObjectPtr::Ptr(Box::new(obj)) }
|
pub fn unbox_as<T: 'static + Object>(&self) -> Option<&T> {
|
||||||
}
|
match self {
|
||||||
|
Obj::Null => None,
|
||||||
impl fmt::Display for ObjectPtr {
|
Obj::Ptr(obj) => obj.as_any().downcast_ref::<T>()
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match *self {
|
|
||||||
ObjectPtr::Null => write!(f, "()"),
|
|
||||||
ObjectPtr::Ptr(ref bx) => write!(f, "{}", bx.deref()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Object {
|
impl fmt::Display for Obj {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match self {
|
||||||
Object::Bool(ref v) => {
|
Obj::Null => write!(f, "null"),
|
||||||
let out = if *v { "#t" } else { "#f" };
|
Obj::Ptr(obj) => write!(f, "{}", obj)
|
||||||
write!(f, "{}", out)
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::ByteVector(ref vec) => {
|
|
||||||
// TODO: Actually write the vector values.
|
|
||||||
write!(f, "#u8(").and_then(|_| write!(f, ")"))
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::Char(ref c) => {
|
|
||||||
// TODO: This is not correct for all cases. See section 6.6 of the spec.
|
|
||||||
write!(f, "#\\{}", c)
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::Number(ref n) => {
|
|
||||||
// TODO: Implement Display for Number
|
|
||||||
write!(f, "{}", n)
|
|
||||||
}
|
|
||||||
|
|
||||||
Object::Pair(ref car, ref cdr) => {
|
|
||||||
write!(f, "(").and_then(|_| Object::fmt_pair(car, cdr, f))
|
|
||||||
.and_then(|_| write!(f, ")"))
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::String(ref st) => {
|
|
||||||
write!(f, "\"{}\"", st)
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::Symbol(ref sym) => {
|
|
||||||
write!(f, "{}", sym)
|
|
||||||
},
|
|
||||||
|
|
||||||
Object::Vector(ref vec) => {
|
|
||||||
// TODO: Actually write the vector values.
|
|
||||||
vec.iter().enumerate().fold(write!(f, "#("), |acc, (i, obj)| {
|
|
||||||
let space = if i == (vec.len() - 1) { " " } else { "" };
|
|
||||||
acc.and(write!(f, "{}{}", obj, space))
|
|
||||||
}).and(write!(f, ")"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Object {
|
//#[derive(Debug, PartialEq)]
|
||||||
fn fmt_pair(car: &ObjectPtr, cdr: &ObjectPtr, f: &mut fmt::Formatter) -> fmt::Result {
|
//pub enum Object {
|
||||||
write!(f, "{}", car).and_then(|r| match cdr {
|
// ByteVector(Vec<u8>),
|
||||||
&ObjectPtr::Null => Ok(r), // Don't write anything.
|
// Char(char),
|
||||||
&ObjectPtr::Ptr(ref ptr) => match ptr.deref() {
|
// Number(Number),
|
||||||
&Object::Pair(ref next_car, ref next_cdr) => {
|
// Pair(ObjectPtr, ObjectPtr),
|
||||||
write!(f, " ").and_then(|_| Object::fmt_pair(next_car, next_cdr, f))
|
// //Procedure/*( TODO: Something )*/,
|
||||||
},
|
// //Record/*( TODO: Something )*/,
|
||||||
_ => write!(f, " . {}", ptr)
|
// String(String),
|
||||||
}
|
// Symbol(String),
|
||||||
})
|
// Vector(Vec<ObjectPtr>),
|
||||||
}
|
//}
|
||||||
}
|
//
|
||||||
|
//impl ObjectPtr {
|
||||||
|
// pub fn new(obj: Object) -> ObjectPtr {
|
||||||
|
// ObjectPtr::Ptr(Box::new(obj))
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub fn new_pair() -> ObjectPtr {
|
||||||
|
// ObjectPtr::new(Object::Pair(ObjectPtr::Null, ObjectPtr::Null))
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//impl fmt::Display for ObjectPtr {
|
||||||
|
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// match *self {
|
||||||
|
// ObjectPtr::Null => write!(f, "()"),
|
||||||
|
// ObjectPtr::Ptr(ref bx) => write!(f, "{}", bx.deref()),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//impl fmt::Display for Object {
|
||||||
|
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// match *self {
|
||||||
|
// Object::Bool(ref v) => {
|
||||||
|
// let out = if *v { "#t" } else { "#f" };
|
||||||
|
// write!(f, "{}", out)
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::ByteVector(ref vec) => {
|
||||||
|
// // TODO: Actually write the vector values.
|
||||||
|
// write!(f, "#u8(").and_then(|_| write!(f, ")"))
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::Char(ref c) => {
|
||||||
|
// // TODO: This is not correct for all cases. See section 6.6 of the spec.
|
||||||
|
// write!(f, "#\\{}", c)
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::Number(ref n) => {
|
||||||
|
// // TODO: Implement Display for Number
|
||||||
|
// write!(f, "{}", n)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Object::Pair(ref car, ref cdr) => {
|
||||||
|
// write!(f, "(").and_then(|_| Object::fmt_pair(car, cdr, f))
|
||||||
|
// .and_then(|_| write!(f, ")"))
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::String(ref st) => {
|
||||||
|
// write!(f, "\"{}\"", st)
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::Symbol(ref sym) => {
|
||||||
|
// write!(f, "{}", sym)
|
||||||
|
// },
|
||||||
|
//
|
||||||
|
// Object::Vector(ref vec) => {
|
||||||
|
// // TODO: Actually write the vector values.
|
||||||
|
// vec.iter().enumerate().fold(write!(f, "#("), |acc, (i, obj)| {
|
||||||
|
// let space = if i == (vec.len() - 1) { " " } else { "" };
|
||||||
|
// acc.and(write!(f, "{}{}", obj, space))
|
||||||
|
// }).and(write!(f, ")"))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//impl Object {
|
||||||
|
// fn fmt_pair(car: &ObjectPtr, cdr: &ObjectPtr, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
// write!(f, "{}", car).and_then(|r| match cdr {
|
||||||
|
// &ObjectPtr::Null => Ok(r), // Don't write anything.
|
||||||
|
// &ObjectPtr::Ptr(ref ptr) => match ptr.deref() {
|
||||||
|
// &Object::Pair(ref next_car, ref next_cdr) => {
|
||||||
|
// write!(f, " ").and_then(|_| Object::fmt_pair(next_car, next_cdr, f))
|
||||||
|
// },
|
||||||
|
// _ => write!(f, " . {}", ptr)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
41
types/src/pair.rs
Normal file
41
types/src/pair.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/* types/src/pair.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
use std::fmt;
|
||||||
|
use super::*;
|
||||||
|
use object::Object;
|
||||||
|
|
||||||
|
pub struct Pair {
|
||||||
|
car: Obj,
|
||||||
|
cdr: Obj
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pair {
|
||||||
|
fn fmt_pair(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let r = write!(f, "{}", self.car);
|
||||||
|
r.and_then(|r| match self.cdr {
|
||||||
|
Obj::Null => Ok(r), // Don't write anything.
|
||||||
|
Obj::Ptr(ref next) => {
|
||||||
|
match next.as_pair() {
|
||||||
|
Some(next_pair) => write!(f, " ").and_then(|_| next_pair.fmt_pair(f)),
|
||||||
|
None => write!(f, " . {}", next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Object for Pair {
|
||||||
|
fn as_any(&self) -> &Any { self }
|
||||||
|
fn as_pair(&self) -> Option<&Pair> { Some(self) }
|
||||||
|
fn as_sym(&self) -> Option<&Sym> { None }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Pair {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "(").and_then(|_| self.fmt_pair(f))
|
||||||
|
.and_then(|_| write!(f, ")"))
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,10 @@
|
||||||
/* types/src/predicates.rs
|
/* types/src/preds.rs
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//! This module defines several important predicates for determing what kind of
|
||||||
|
//! a thing this Object is.
|
||||||
|
|
||||||
pub trait IsNull {
|
pub trait IsNull {
|
||||||
/// Is this thing null?
|
/// Is this thing null?
|
||||||
fn is_null(&self) -> bool { false }
|
fn is_null(&self) -> bool { false }
|
||||||
|
@ -20,12 +23,21 @@ pub trait IsChar {
|
||||||
pub trait IsNumber {
|
pub trait IsNumber {
|
||||||
/// Is this thing a number?
|
/// Is this thing a number?
|
||||||
fn is_number(&self) -> bool { self.is_complex() || self.is_real() || self.is_rational() || self.is_integer() }
|
fn is_number(&self) -> bool { self.is_complex() || self.is_real() || self.is_rational() || self.is_integer() }
|
||||||
|
|
||||||
/// Should return `true` if this Value is a complex number type.
|
/// Should return `true` if this Value is a complex number type.
|
||||||
fn is_complex(&self) -> bool { self.is_real() }
|
fn is_complex(&self) -> bool { self.is_real() }
|
||||||
|
|
||||||
/// Should return `true` if this Value is a real number type.
|
/// Should return `true` if this Value is a real number type.
|
||||||
fn is_real(&self) -> bool { self.is_rational() }
|
fn is_real(&self) -> bool { self.is_rational() }
|
||||||
|
|
||||||
/// Should return `true` if this Value is a rational number type.
|
/// Should return `true` if this Value is a rational number type.
|
||||||
fn is_rational(&self) -> bool { self.is_integer() }
|
fn is_rational(&self) -> bool { self.is_integer() }
|
||||||
|
|
||||||
/// Should return `true` if this Value is a integer number type.
|
/// Should return `true` if this Value is a integer number type.
|
||||||
fn is_integer(&self) -> bool { false }
|
fn is_integer(&self) -> bool { false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait IsPair {
|
||||||
|
/// Should return `true` if this Value is a pair.
|
||||||
|
fn is_pair(&self) -> bool { false }
|
||||||
|
}
|
22
types/src/sym.rs
Normal file
22
types/src/sym.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/* types/src/symbol.rs
|
||||||
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
use std::fmt;
|
||||||
|
use object::Object;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub struct Sym(String);
|
||||||
|
|
||||||
|
impl Object for Sym {
|
||||||
|
fn as_any(&self) -> &Any { self }
|
||||||
|
fn as_pair(&self) -> Option<&Pair> { None }
|
||||||
|
fn as_sym(&self) -> Option<&Sym> { Some(self) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Sym {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue