Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Precedence trait containing all the precedence formatting #11

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 27 additions & 37 deletions src/formatting/asp/default.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use {
crate::syntax_tree::{
asp::{
Atom, AtomicFormula, BinaryOperator, Body, Comparison, Constant, Head, Literal,
Program, Relation, Rule, Sign, Term, UnaryOperator, Variable,
crate::{
formatting::{Associativity, Precedence},
syntax_tree::{
asp::{
Atom, AtomicFormula, BinaryOperator, Body, Comparison, Constant, Head, Literal,
Program, Relation, Rule, Sign, Term, UnaryOperator, Variable,
},
Node,
},
Node,
},
std::fmt::{self, Display, Formatter},
};
Expand Down Expand Up @@ -52,7 +55,7 @@ impl Display for Format<'_, BinaryOperator> {
}
}

impl Format<'_, Term> {
impl Precedence for Format<'_, Term> {
fn precedence(&self) -> usize {
match self.0 {
Term::Constant(Constant::Integer(1..)) => 1,
Expand All @@ -76,44 +79,31 @@ impl Format<'_, Term> {
} => 4,
}
}

fn associativity(&self) -> Associativity {
Associativity::Left
}

fn fmt_operator(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
Term::UnaryOperation { op, .. } => write!(f, "{}", Format(op)),
Term::BinaryOperation { op, .. } => match op {
BinaryOperator::Interval => write!(f, "{}", Format(op)),
_ => write!(f, " {} ", Format(op)),
},
Term::Constant(_) | Term::Variable(_) => unreachable!(),
}
}
}

impl Display for Format<'_, Term> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
Term::Constant(c) => Format(c).fmt(f),
Term::Variable(v) => Format(v).fmt(f),
Term::UnaryOperation { op, arg } => {
let op = Format(op);
let arg = Format(arg.as_ref());

op.fmt(f)?;
if self.precedence() < arg.precedence() {
write!(f, "({arg})")
} else {
write!(f, "{arg}")
}
}
Term::BinaryOperation { op, lhs, rhs } => {
let op = Format(op);
let lhs = Format(lhs.as_ref());
let rhs = Format(rhs.as_ref());

if self.precedence() < lhs.precedence() {
write!(f, "({lhs})")
} else {
write!(f, "{lhs}")
}?;
if *op.0 == BinaryOperator::Interval {
write!(f, "{op}")
} else {
write!(f, " {op} ")
}?;
if self.precedence() <= rhs.precedence() {
write!(f, "({rhs})")
} else {
write!(f, "{rhs}")
}
Term::UnaryOperation { arg, .. } => self.fmt_unary(Format(arg.as_ref()), f),
Term::BinaryOperation { lhs, rhs, .. } => {
self.fmt_binary(Format(lhs.as_ref()), Format(rhs.as_ref()), f)
}
}
}
Expand Down
143 changes: 51 additions & 92 deletions src/formatting/fol/default.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use {
crate::syntax_tree::{
fol::{
Atom, AtomicFormula, BasicIntegerTerm, BinaryConnective, BinaryOperator, Comparison,
Formula, GeneralTerm, Guard, IntegerTerm, Quantification, Quantifier, Relation, Sort,
UnaryConnective, UnaryOperator, Variable,
crate::{
formatting::{Associativity, Precedence},
syntax_tree::{
fol::{
Atom, AtomicFormula, BasicIntegerTerm, BinaryConnective, BinaryOperator,
Comparison, Formula, GeneralTerm, Guard, IntegerTerm, Quantification, Quantifier,
Relation, Sort, UnaryConnective, UnaryOperator, Variable,
},
Node,
},
Node,
},
std::fmt::{self, Display, Formatter},
};
Expand Down Expand Up @@ -41,7 +44,9 @@ impl Display for Format<'_, BinaryOperator> {
}
}

impl Format<'_, IntegerTerm> {
impl Format<'_, IntegerTerm> {}

impl Precedence for Format<'_, IntegerTerm> {
fn precedence(&self) -> usize {
match self.0 {
IntegerTerm::BasicIntegerTerm(BasicIntegerTerm::Numeral(1..)) => 1,
Expand All @@ -60,39 +65,27 @@ impl Format<'_, IntegerTerm> {
} => 3,
}
}

fn associativity(&self) -> Associativity {
Associativity::Left
}

fn fmt_operator(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
IntegerTerm::UnaryOperation { op, .. } => write!(f, "{}", Format(op)),
IntegerTerm::BinaryOperation { op, .. } => write!(f, " {} ", Format(op)),
IntegerTerm::BasicIntegerTerm(_) => unreachable!(),
}
}
}

impl Display for Format<'_, IntegerTerm> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
IntegerTerm::BasicIntegerTerm(t) => Format(t).fmt(f),
IntegerTerm::UnaryOperation { op, arg } => {
let op = Format(op);
let arg = Format(arg.as_ref());

write!(f, "{op}")?;
if self.precedence() < arg.precedence() {
write!(f, "({arg})")
} else {
write!(f, "{arg}")
}
}
IntegerTerm::BinaryOperation { op, lhs, rhs } => {
let op = Format(op);
let lhs = Format(lhs.as_ref());
let rhs = Format(rhs.as_ref());

if self.precedence() < lhs.precedence() {
write!(f, "({lhs})")
} else {
write!(f, "{lhs}")
}?;
write!(f, " {op} ")?;
if self.precedence() <= rhs.precedence() {
write!(f, "({rhs})")
} else {
write!(f, "{rhs}")
}
IntegerTerm::UnaryOperation { arg, .. } => self.fmt_unary(Format(arg.as_ref()), f),
IntegerTerm::BinaryOperation { lhs, rhs, .. } => {
self.fmt_binary(Format(lhs.as_ref()), Format(rhs.as_ref()), f)
}
}
}
Expand Down Expand Up @@ -232,7 +225,7 @@ impl Display for Format<'_, BinaryConnective> {
}
}

impl Format<'_, Formula> {
impl Precedence for Format<'_, Formula> {
fn precedence(&self) -> usize {
match self.0 {
Formula::AtomicFormula(_) => 0,
Expand All @@ -255,20 +248,33 @@ impl Format<'_, Formula> {
}
}

fn is_left_associative(&self) -> bool {
fn associativity(&self) -> Associativity {
match self.0 {
Formula::BinaryFormula {
Formula::UnaryFormula { .. }
| Formula::QuantifiedFormula { .. }
| Formula::BinaryFormula {
connective:
BinaryConnective::Conjunction
| BinaryConnective::Disjunction
| BinaryConnective::ReverseImplication,
..
} => true,
} => Associativity::Left,
Formula::BinaryFormula {
connective: BinaryConnective::Equivalence | BinaryConnective::Implication,
..
} => false,
_ => unreachable!(), // TODO
} => Associativity::Right,
Formula::AtomicFormula(_) => unreachable!(),
}
}

fn fmt_operator(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
Formula::UnaryFormula { connective, .. } => write!(f, "{} ", Format(connective)),
Formula::QuantifiedFormula { quantification, .. } => {
write!(f, "{} ", Format(quantification))
}
Formula::BinaryFormula { connective, .. } => write!(f, " {} ", Format(connective)),
Formula::AtomicFormula(_) => unreachable!(),
}
}
}
Expand All @@ -277,59 +283,12 @@ impl Display for Format<'_, Formula> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
Formula::AtomicFormula(a) => Format(a).fmt(f),
Formula::UnaryFormula {
connective,
formula,
} => {
let connective = Format(connective);
let formula = Format(formula.as_ref());

write!(f, "{connective} ")?;
if self.precedence() < formula.precedence() {
write!(f, "({formula})")
} else {
write!(f, "{formula}")
}
}
Formula::QuantifiedFormula {
quantification,
formula,
} => {
let connective = Format(quantification);
let formula = Format(formula.as_ref());

write!(f, "{connective} ")?;
if self.precedence() < formula.precedence() {
write!(f, "({formula})")
} else {
write!(f, "{formula}")
}
Formula::UnaryFormula { formula, .. } => self.fmt_unary(Format(formula.as_ref()), f),
Formula::QuantifiedFormula { formula, .. } => {
self.fmt_unary(Format(formula.as_ref()), f)
}
Formula::BinaryFormula {
connective,
lhs,
rhs,
} => {
let op = Format(connective);
let lhs = Format(lhs.as_ref());
let rhs = Format(rhs.as_ref());

if self.precedence() < lhs.precedence()
|| self.precedence() == lhs.precedence() && !lhs.is_left_associative()
{
write!(f, "({lhs})")
} else {
write!(f, "{lhs}")
}?;

write!(f, " {op} ")?;
if self.precedence() < rhs.precedence()
|| self.precedence() == rhs.precedence() && self.is_left_associative()
{
write!(f, "({rhs})")
} else {
write!(f, "{rhs}")
}
Formula::BinaryFormula { lhs, rhs, .. } => {
self.fmt_binary(Format(lhs.as_ref()), Format(rhs.as_ref()), f)
}
}
}
Expand Down
Loading
Loading