[parser] Add BoolParser and hook it up to existing parsers

Closes #13.
This commit is contained in:
Eryn Wells 2018-08-26 17:42:52 -07:00
parent 3a7fe94b32
commit 4fab810d1f
5 changed files with 48 additions and 6 deletions

View file

@ -0,0 +1,33 @@
/* parser/src/parsers/bool.rs
* Eryn Wells <eryn@erynwells.me>
*/
use sibillexer::{Lex, Token};
use sibiltypes::{Bool, Obj};
use parsers::{NodeParser, NodeParseResult};
#[derive(Debug)] pub struct BoolParser;
impl NodeParser for BoolParser {
fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() {
Token::Bool(value) => {
NodeParseResult::Complete { obj: Obj::new(Bool::from(value)) }
}
_ => {
let msg = format!("Expected bool, found {:?}", lex);
NodeParseResult::error(msg)
}
}
}
fn none(&mut self) -> NodeParseResult {
let msg = format!("Expected bool, found EOF");
NodeParseResult::error(msg)
}
fn subparser_completed(&mut self, obj: Obj) -> NodeParseResult {
let msg = format!("Unexpected subparser result: {}", obj);
NodeParseResult::error(msg)
}
}

View file

@ -5,6 +5,7 @@
use sibillexer::{Lex, Token};
use sibiltypes::{Obj, Pair};
use parsers::{NodeParser, NodeParseResult};
use parsers::bool::BoolParser;
use parsers::sym::SymParser;
#[derive(Debug)]
@ -23,6 +24,10 @@ impl ListParser {
impl NodeParser for ListParser {
fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() {
Token::Bool(_) => {
let parser = BoolParser{};
NodeParseResult::Push { next: Box::new(parser) }
}
Token::LeftParen => {
match self.list {
None => {

View file

@ -2,6 +2,7 @@
* Eryn Wells <eryn@erynwells.me>
*/
mod bool;
mod list;
mod program;
mod sym;

View file

@ -5,6 +5,7 @@
use sibillexer::{Lex, Token};
use sibiltypes::Obj;
use parsers::{NodeParser, NodeParseResult};
use parsers::bool::BoolParser;
use parsers::list::ListParser;
use parsers::sym::SymParser;
@ -20,10 +21,13 @@ impl ProgramParser {
impl NodeParser for ProgramParser {
fn parse(&mut self, lex: &Lex) -> NodeParseResult {
match lex.token() {
Token::Bool(_) => {
let next = Box::new(BoolParser{});
NodeParseResult::Push { next }
},
Token::LeftParen => {
let parser = ListParser::new();
let parser = Box::new(parser);
NodeParseResult::Push { next: parser }
let next = Box::new(ListParser::new());
NodeParseResult::Push { next }
},
Token::RightParen => {
let msg = format!("Expected symbol found {:?}", lex);

View file

@ -6,8 +6,7 @@ use sibillexer::{Lex, Token};
use sibiltypes::{Obj, Sym};
use parsers::{NodeParser, NodeParseResult};
#[derive(Debug)]
pub struct SymParser;
#[derive(Debug)] pub struct SymParser;
impl NodeParser for SymParser {
fn parse(&mut self, lex: &Lex) -> NodeParseResult {
@ -31,7 +30,7 @@ impl NodeParser for SymParser {
}
fn subparser_completed(&mut self, obj: Obj) -> NodeParseResult {
let msg = format!("Unexpected parser result: {}", obj);
let msg = format!("Unexpected subparser result: {}", obj);
NodeParseResult::error(msg)
}
}