diff --git a/src/syntax_tree/asp.rs b/src/syntax_tree/asp.rs index 68ab850e..04fe964f 100644 --- a/src/syntax_tree/asp.rs +++ b/src/syntax_tree/asp.rs @@ -1,4 +1,12 @@ -use super::Node; +use crate::{ + formatting::asp::default::Format, + parsing::asp::pest::{ + AtomParser, AtomicFormulaParser, BinaryOperatorParser, BodyParser, ComparisonParser, + HeadParser, LiteralParser, PrecomputedTermParser, ProgramParser, RelationParser, + RuleParser, SignParser, TermParser, UnaryOperatorParser, VariableParser, + }, + syntax_tree::{impl_node, Node}, +}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum PrecomputedTerm { @@ -8,19 +16,19 @@ pub enum PrecomputedTerm { Supremum, } -impl Node for PrecomputedTerm {} +impl_node!(PrecomputedTerm, Format, PrecomputedTermParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Variable(pub String); -impl Node for Variable {} +impl_node!(Variable, Format, VariableParser); #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum UnaryOperator { Negative, } -impl Node for UnaryOperator {} +impl_node!(UnaryOperator, Format, UnaryOperatorParser); #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum BinaryOperator { @@ -32,7 +40,7 @@ pub enum BinaryOperator { Interval, } -impl Node for BinaryOperator {} +impl_node!(BinaryOperator, Format, BinaryOperatorParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Term { @@ -49,7 +57,7 @@ pub enum Term { }, } -impl Node for Term {} +impl_node!(Term, Format, TermParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Atom { @@ -57,7 +65,7 @@ pub struct Atom { pub terms: Vec, } -impl Node for Atom {} +impl_node!(Atom, Format, AtomParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Sign { @@ -66,7 +74,7 @@ pub enum Sign { DoubleNegation, } -impl Node for Sign {} +impl_node!(Sign, Format, SignParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Literal { @@ -74,7 +82,7 @@ pub struct Literal { pub atom: Atom, } -impl Node for Literal {} +impl_node!(Literal, Format, LiteralParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Relation { @@ -86,7 +94,7 @@ pub enum Relation { GreaterEqual, } -impl Node for Relation {} +impl_node!(Relation, Format, RelationParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Comparison { @@ -95,7 +103,7 @@ pub struct Comparison { pub rhs: Term, } -impl Node for Comparison {} +impl_node!(Comparison, Format, ComparisonParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum AtomicFormula { @@ -103,7 +111,7 @@ pub enum AtomicFormula { Comparison(Comparison), } -impl Node for AtomicFormula {} +impl_node!(AtomicFormula, Format, AtomicFormulaParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Head { @@ -112,14 +120,14 @@ pub enum Head { Falsity, } -impl Node for Head {} +impl_node!(Head, Format, HeadParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Body { pub formulas: Vec, } -impl Node for Body {} +impl_node!(Body, Format, BodyParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Rule { @@ -127,11 +135,11 @@ pub struct Rule { pub body: Body, } -impl Node for Rule {} +impl_node!(Rule, Format, RuleParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Program { pub rules: Vec, } -impl Node for Program {} +impl_node!(Program, Format, ProgramParser); diff --git a/src/syntax_tree/fol.rs b/src/syntax_tree/fol.rs index 348509b4..f85843b9 100644 --- a/src/syntax_tree/fol.rs +++ b/src/syntax_tree/fol.rs @@ -1,4 +1,13 @@ -use super::Node; +use crate::{ + formatting::fol::default::Format, + parsing::fol::pest::{ + AtomParser, AtomicFormulaParser, BasicIntegerTermParser, BinaryConnectiveParser, + BinaryOperatorParser, ComparisonParser, FormulaParser, GeneralTermParser, GuardParser, + IntegerTermParser, QuantificationParser, QuantifierParser, RelationParser, + UnaryConnectiveParser, UnaryOperatorParser, VariableParser, + }, + syntax_tree::{impl_node, Node}, +}; #[derive(Clone, Debug, Eq, PartialEq)] pub enum BasicIntegerTerm { @@ -8,14 +17,14 @@ pub enum BasicIntegerTerm { IntegerVariable(String), } -impl Node for BasicIntegerTerm {} +impl_node!(BasicIntegerTerm, Format, BasicIntegerTermParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum UnaryOperator { Negative, } -impl Node for UnaryOperator {} +impl_node!(UnaryOperator, Format, UnaryOperatorParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum BinaryOperator { @@ -24,7 +33,7 @@ pub enum BinaryOperator { Multiply, } -impl Node for BinaryOperator {} +impl_node!(BinaryOperator, Format, BinaryOperatorParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum IntegerTerm { @@ -40,7 +49,7 @@ pub enum IntegerTerm { }, } -impl Node for IntegerTerm {} +impl_node!(IntegerTerm, Format, IntegerTermParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum GeneralTerm { @@ -49,7 +58,7 @@ pub enum GeneralTerm { IntegerTerm(IntegerTerm), } -impl Node for GeneralTerm {} +impl_node!(GeneralTerm, Format, GeneralTermParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Atom { @@ -57,7 +66,7 @@ pub struct Atom { pub terms: Vec, } -impl Node for Atom {} +impl_node!(Atom, Format, AtomParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Relation { @@ -69,7 +78,7 @@ pub enum Relation { LessEqual, } -impl Node for Relation {} +impl_node!(Relation, Format, RelationParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Guard { @@ -77,7 +86,7 @@ pub struct Guard { pub term: GeneralTerm, } -impl Node for Guard {} +impl_node!(Guard, Format, GuardParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Comparison { @@ -85,7 +94,7 @@ pub struct Comparison { pub guards: Vec, } -impl Node for Comparison {} +impl_node!(Comparison, Format, ComparisonParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum AtomicFormula { @@ -94,14 +103,14 @@ pub enum AtomicFormula { Comparison(Comparison), } -impl Node for AtomicFormula {} +impl_node!(AtomicFormula, Format, AtomicFormulaParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum UnaryConnective { Negation, } -impl Node for UnaryConnective {} +impl_node!(UnaryConnective, Format, UnaryConnectiveParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Quantifier { @@ -109,7 +118,7 @@ pub enum Quantifier { Exists, } -impl Node for Quantifier {} +impl_node!(Quantifier, Format, QuantifierParser); #[derive(Clone, Debug, Eq, PartialEq)] pub struct Quantification { @@ -117,7 +126,7 @@ pub struct Quantification { pub variables: Vec, } -impl Node for Quantification {} +impl_node!(Quantification, Format, QuantificationParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Sort { @@ -125,7 +134,7 @@ pub enum Sort { General, } -impl Node for Sort {} +// TODO: Should Sort be a Node? #[derive(Clone, Debug, Eq, PartialEq)] pub struct Variable { @@ -133,7 +142,7 @@ pub struct Variable { pub sort: Sort, } -impl Node for Variable {} +impl_node!(Variable, Format, VariableParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum BinaryConnective { @@ -144,7 +153,7 @@ pub enum BinaryConnective { Equivalence, } -impl Node for BinaryConnective {} +impl_node!(BinaryConnective, Format, BinaryConnectiveParser); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Formula { @@ -164,4 +173,4 @@ pub enum Formula { }, } -impl Node for Formula {} +impl_node!(Formula, Format, FormulaParser); diff --git a/src/syntax_tree/mod.rs b/src/syntax_tree/mod.rs index 957d92dd..bf3bfd80 100644 --- a/src/syntax_tree/mod.rs +++ b/src/syntax_tree/mod.rs @@ -1,7 +1,31 @@ -use std::fmt::Debug; +use std::{ + fmt::{Debug, Display}, + str::FromStr, +}; pub mod asp; pub mod fol; -// TODO: Do we want to specify a default parser (-> FromStr) and formatter (-> Display)? -pub trait Node: Clone + Debug + Eq + PartialEq {} +pub trait Node: Clone + Debug + Eq + PartialEq + FromStr + Display {} + +macro_rules! impl_node { + ($node:ty, $format:expr, $parser:ty) => { + impl Node for $node {} + + impl std::fmt::Display for $node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", $format(self)) + } + } + + impl std::str::FromStr for $node { + type Err = <$parser as crate::parsing::Parser>::Error; + + fn from_str(s: &str) -> Result { + <$parser as crate::parsing::Parser>::parse(s) + } + } + }; +} + +pub(crate) use impl_node;