Skip to content

Commit

Permalink
Declare a default Format and Parser for each Node
Browse files Browse the repository at this point in the history
  • Loading branch information
teiesti committed Sep 15, 2023
1 parent df03a0c commit ba3dd4e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 37 deletions.
40 changes: 24 additions & 16 deletions src/syntax_tree/asp.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -49,15 +57,15 @@ pub enum Term {
},
}

impl Node for Term {}
impl_node!(Term, Format, TermParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Atom {
pub predicate: String,
pub terms: Vec<Term>,
}

impl Node for Atom {}
impl_node!(Atom, Format, AtomParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Sign {
Expand All @@ -66,15 +74,15 @@ pub enum Sign {
DoubleNegation,
}

impl Node for Sign {}
impl_node!(Sign, Format, SignParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Literal {
pub sign: Sign,
pub atom: Atom,
}

impl Node for Literal {}
impl_node!(Literal, Format, LiteralParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Relation {
Expand All @@ -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 {
Expand All @@ -95,15 +103,15 @@ pub struct Comparison {
pub rhs: Term,
}

impl Node for Comparison {}
impl_node!(Comparison, Format, ComparisonParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AtomicFormula {
Literal(Literal),
Comparison(Comparison),
}

impl Node for AtomicFormula {}
impl_node!(AtomicFormula, Format, AtomicFormulaParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Head {
Expand All @@ -112,26 +120,26 @@ pub enum Head {
Falsity,
}

impl Node for Head {}
impl_node!(Head, Format, HeadParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Body {
pub formulas: Vec<AtomicFormula>,
}

impl Node for Body {}
impl_node!(Body, Format, BodyParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rule {
pub head: Head,
pub body: Body,
}

impl Node for Rule {}
impl_node!(Rule, Format, RuleParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Program {
pub rules: Vec<Rule>,
}

impl Node for Program {}
impl_node!(Program, Format, ProgramParser);
45 changes: 27 additions & 18 deletions src/syntax_tree/fol.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -40,7 +49,7 @@ pub enum IntegerTerm {
},
}

impl Node for IntegerTerm {}
impl_node!(IntegerTerm, Format, IntegerTermParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum GeneralTerm {
Expand All @@ -49,15 +58,15 @@ pub enum GeneralTerm {
IntegerTerm(IntegerTerm),
}

impl Node for GeneralTerm {}
impl_node!(GeneralTerm, Format, GeneralTermParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Atom {
pub predicate: String,
pub terms: Vec<GeneralTerm>,
}

impl Node for Atom {}
impl_node!(Atom, Format, AtomParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Relation {
Expand All @@ -69,23 +78,23 @@ pub enum Relation {
LessEqual,
}

impl Node for Relation {}
impl_node!(Relation, Format, RelationParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Guard {
pub relation: Relation,
pub term: GeneralTerm,
}

impl Node for Guard {}
impl_node!(Guard, Format, GuardParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Comparison {
pub term: GeneralTerm,
pub guards: Vec<Guard>,
}

impl Node for Comparison {}
impl_node!(Comparison, Format, ComparisonParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AtomicFormula {
Expand All @@ -94,46 +103,46 @@ 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 {
Forall,
Exists,
}

impl Node for Quantifier {}
impl_node!(Quantifier, Format, QuantifierParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Quantification {
pub quantifier: Quantifier,
pub variables: Vec<Variable>,
}

impl Node for Quantification {}
impl_node!(Quantification, Format, QuantificationParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Sort {
Integer,
General,
}

impl Node for Sort {}
// TODO: Should Sort be a Node?

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Variable {
pub name: String,
pub sort: Sort,
}

impl Node for Variable {}
impl_node!(Variable, Format, VariableParser);

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BinaryConnective {
Expand All @@ -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 {
Expand All @@ -164,4 +173,4 @@ pub enum Formula {
},
}

impl Node for Formula {}
impl_node!(Formula, Format, FormulaParser);
30 changes: 27 additions & 3 deletions src/syntax_tree/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
<$parser as crate::parsing::Parser>::parse(s)
}
}
};
}

pub(crate) use impl_node;

0 comments on commit ba3dd4e

Please sign in to comment.