From e1f2c6b88b12e380153dbbe2c3fc78a1b0c25630 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Dec 2016 16:58:02 -0500 Subject: [PATCH] Basic parsing infrastructure .. doesn't actually do anything --- src/parser/mod.rs | 37 ++++++++++++++++++++++++++++++++++ src/parser/nodes/constant.rs | 19 ++++++++++------- src/parser/nodes/expression.rs | 5 +++++ src/parser/nodes/mod.rs | 13 +++--------- src/parser/nodes/program.rs | 10 ++++++++- 5 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/parser/nodes/expression.rs diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 31c5a54..e768349 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2,4 +2,41 @@ * Eryn Wells */ +pub use self::nodes::Program; + mod nodes; + +use lexer::Lexer; + +pub struct Parser { + lexer: Lexer, +} + +impl Parser { + pub fn new(lexer: Lexer) -> Parser { + Parser { lexer: lexer } + } + + pub fn parse(&self) -> nodes::Program { + self.parse_program() + } +} + +impl Parser { + fn parse_program(&self) -> nodes::Program { + let program = Program::new(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use lexer::Lexer; + + #[test] + fn parses_empty_input() { + let parser = Parser::new(Lexer::new("")); + let parsed = parser.parse(); + assert_eq!(parsed, Program::new()); + } +} diff --git a/src/parser/nodes/constant.rs b/src/parser/nodes/constant.rs index cd40049..473a562 100644 --- a/src/parser/nodes/constant.rs +++ b/src/parser/nodes/constant.rs @@ -5,20 +5,25 @@ use std::fmt; use types::{Boolean, Number}; use super::TreeDebug; +use super::expression::Expression; pub trait ConstantValue {} impl ConstantValue for Boolean {} impl ConstantValue for Number {} -#[derive(Debug)] -pub struct Constant { +pub struct Constant<'a, V: ConstantValue> { + parent: Option<&'a Expression>, value: V } -impl TreeDebug for Constant {} - -impl Constant { - pub fn new(value: V) -> Constant { - Constant { value: value } +impl<'a, V: ConstantValue> Constant<'a, V> { + pub fn new(value: V) -> Constant<'a, V> { + Constant { parent: None, value: value } + } +} + +impl<'a, V: ConstantValue + fmt::Debug> fmt::Debug for Constant<'a, V> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Constant {{ {:?} }}", self.value) } } diff --git a/src/parser/nodes/expression.rs b/src/parser/nodes/expression.rs new file mode 100644 index 0000000..979c004 --- /dev/null +++ b/src/parser/nodes/expression.rs @@ -0,0 +1,5 @@ +/* parser/nodes/expression.rs + * Eryn Wells + */ + +pub trait Expression {} diff --git a/src/parser/nodes/mod.rs b/src/parser/nodes/mod.rs index 83aeedc..2c943a8 100644 --- a/src/parser/nodes/mod.rs +++ b/src/parser/nodes/mod.rs @@ -5,20 +5,13 @@ use std::fmt; pub use self::constant::Constant; +pub use self::program::Program; mod constant; +mod expression; mod program; -use self::constant::ConstantValue; - -trait Expression {} -impl fmt::Debug for Expression { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Expression") - } -} -impl TreeDebug for Expression {} -impl Expression for Constant {} +use self::expression::Expression; /// Conveniently print out a node in the tree trait TreeDebug: fmt::Debug { diff --git a/src/parser/nodes/program.rs b/src/parser/nodes/program.rs index cdf9ed6..3cf71eb 100644 --- a/src/parser/nodes/program.rs +++ b/src/parser/nodes/program.rs @@ -5,12 +5,19 @@ use std::fmt; use super::TreeDebug; -use super::Expression; +use super::expression::Expression; pub struct Program { forms: Vec>, } +impl Program { + pub fn new() -> Program { + Program { forms: Vec::new() } + } +} + +/* impl fmt::Debug for Program { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.tree_fmt(f, 0) @@ -30,3 +37,4 @@ impl TreeDebug for Program { result } } +*/