From f279c52b503e9dacb558d8a46a75b42bc5222ee4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 29 Apr 2019 00:12:36 +0200 Subject: =?UTF-8?q?Simple=20dynamic,=20scoped=20function=20parsing=20?= =?UTF-8?q?=F0=9F=93=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax.rs | 73 ++++++++++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 51 deletions(-) (limited to 'src/syntax.rs') diff --git a/src/syntax.rs b/src/syntax.rs index fdb71d14..dd83b4a3 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -1,8 +1,9 @@ //! Tokenized and syntax tree representations of source code. -use std::fmt::Debug; use std::collections::HashMap; +use std::fmt::{self, Display, Formatter}; use std::ops::Deref; +use crate::func::Function; use crate::utility::StrExt; @@ -38,22 +39,22 @@ pub enum Token<'s> { /// A tree representation of the source. #[derive(Debug, PartialEq)] -pub struct SyntaxTree<'s> { +pub struct SyntaxTree { /// The children. - pub nodes: Vec>, + pub nodes: Vec, } -impl<'s> SyntaxTree<'s> { +impl SyntaxTree { /// Create an empty syntax tree. #[inline] - pub fn new() -> SyntaxTree<'s> { + pub fn new() -> SyntaxTree { SyntaxTree { nodes: vec![] } } } /// A node in the abstract syntax tree. #[derive(Debug, PartialEq)] -pub enum Node<'s> { +pub enum Node { /// Whitespace between other nodes. Space, /// A line feed. @@ -65,18 +66,24 @@ pub enum Node<'s> { /// Indicates that math mode was enabled/disabled. ToggleMath, /// A literal word. - Word(&'s str), + Word(String), /// A function invocation. Func(FuncInvocation), } /// A complete function invocation consisting of header and body. -#[derive(Debug, PartialEq)] +#[derive(Debug)] pub struct FuncInvocation { pub header: FuncHeader, pub body: Box, } +impl PartialEq for FuncInvocation { + fn eq(&self, other: &FuncInvocation) -> bool { + (self.header == other.header) && (&self.body == &other.body) + } +} + /// Contains header information of a function invocation. #[derive(Debug, Clone, PartialEq)] pub struct FuncHeader { @@ -85,53 +92,11 @@ pub struct FuncHeader { pub kwargs: HashMap } -use std::any::Any; - -/// Types that act as functions. -pub trait Function: Debug + FunctionHelper { - /// Parse the function. - fn parse() -> Self where Self: Sized; - - /// Execute the function and optionally yield a return value. - fn typeset(&self, header: &FuncHeader) -> Option; -} - -trait FunctionHelper { - fn help_as_any(&self) -> &dyn Any; - fn help_eq(&self, other: &dyn Function) -> bool; -} - -impl FunctionHelper for T where T: Clone + PartialEq + 'static { - fn help_as_any(&self) -> &dyn Any { - self - } - - fn help_eq(&self, other: &dyn Function) -> bool { - if let Some(other) = other.help_as_any().downcast_ref::() { - self == other - } else { - false - } - } -} - -impl PartialEq for &dyn Function { - fn eq(&self, other: &dyn Function) -> bool { - self.help_eq(other) - } -} - -impl PartialEq> for Box { - fn eq(&self, other: &Box) -> bool { - &*self == &*other - } -} - /// A potentially unevaluated expression. #[derive(Debug, Clone, PartialEq)] pub enum Expression {} -/// A valid identifier. +/// An owned valid identifier. #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct Ident(String); @@ -154,6 +119,12 @@ impl Ident { } } +impl Display for Ident { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(&self.0, f) + } +} + impl Deref for Ident { type Target = str; -- cgit v1.2.3