Skip to content

Commit

Permalink
Promote Sort to a Node and use its new parser to solve a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
teiesti committed Aug 22, 2024
1 parent c830877 commit 9d308ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
19 changes: 11 additions & 8 deletions src/formatting/fol/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,7 @@ impl Display for Format<'_, Quantifier> {

impl Display for Format<'_, FunctionConstant> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let name = &self.0.name;
let sort = &self.0.sort;

match sort {
Sort::General => write!(f, "{name}$g"),
Sort::Integer => write!(f, "{name}$i"),
Sort::Symbol => write!(f, "{name}$s"),
}
write!(f, "{}${}", self.0.name, Format(&self.0.sort))
}
}

Expand All @@ -219,6 +212,16 @@ impl Display for Format<'_, Variable> {
}
}

impl Display for Format<'_, Sort> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.0 {
Sort::General => write!(f, "g"),
Sort::Integer => write!(f, "i"),
Sort::Symbol => write!(f, "s"),
}
}
}

impl Display for Format<'_, Quantification> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let quantifier = &self.0.quantifier;
Expand Down
1 change: 1 addition & 0 deletions src/parsing/fol/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ keyword = _{ primitive | binary_connective | unary_connective | quantifier }
supremum = { "#sup" }

sort = { general_sort | integer_sort | symbolic_sort }
sort_eoi = _{ sort ~ EOI }
general_sort = @{ "g" ~ "eneral"? }
integer_sort = @{ "i" ~ "nteger"? }
symbolic_sort = @{ "s" ~ "ymbol"? }
Expand Down
49 changes: 39 additions & 10 deletions src/parsing/fol/pest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ impl PestParser for QuantificationParser {
}
}

pub struct SortParser;

impl PestParser for SortParser {
type Node = Sort;

type InternalParser = internal::Parser;
type Rule = internal::Rule;
const RULE: internal::Rule = internal::Rule::sort_eoi;

fn translate_pair(pair: pest::iterators::Pair<'_, Self::Rule>) -> Self::Node {
match pair.as_rule() {
internal::Rule::sort => Self::translate_pairs(pair.into_inner()),
internal::Rule::general_sort => Sort::General,
internal::Rule::symbolic_sort => Sort::Symbol,
internal::Rule::integer_sort => Sort::Integer,
_ => Self::report_unexpected_pair(pair),
}
}
}

pub struct UnaryConnectiveParser;

impl PestParser for UnaryConnectiveParser {
Expand Down Expand Up @@ -729,15 +749,10 @@ impl PestParser for PlaceholderDeclarationParser {
.as_str()
.into();

let sort = match pairs.next() {
Some(pair) => match pair.as_rule() {
internal::Rule::general_sort => Sort::General,
internal::Rule::symbolic_sort => Sort::Symbol,
internal::Rule::integer_sort => Sort::Integer,
_ => Self::report_unexpected_pair(pair),
},
None => Sort::General,
};
let sort = pairs
.next()
.map(SortParser::translate_pair)
.unwrap_or_else(|| Sort::General);

if let Some(pair) = pairs.next() {
Self::report_unexpected_pair(pair)
Expand Down Expand Up @@ -805,7 +820,7 @@ mod tests {
AnnotatedFormulaParser, AtomParser, AtomicFormulaParser, BinaryConnectiveParser,
BinaryOperatorParser, ComparisonParser, FormulaParser, GeneralTermParser, GuardParser,
IntegerTermParser, PredicateParser, QuantificationParser, QuantifierParser,
RelationParser, SpecificationParser, SymbolicTermParser, TheoryParser,
RelationParser, SortParser, SpecificationParser, SymbolicTermParser, TheoryParser,
UnaryConnectiveParser, UnaryOperatorParser, UserGuideParser, VariableParser,
},
crate::{
Expand Down Expand Up @@ -1229,6 +1244,20 @@ mod tests {
.should_reject(["p and b"]);
}

#[test]
fn parse_sort() {
SortParser
.should_parse_into([
("i", Sort::Integer),
("integer", Sort::Integer),
("s", Sort::Symbol),
("symbol", Sort::Symbol),
("g", Sort::General),
("general", Sort::General),
])
.should_reject(["int", "sym", "gen"]);
}

#[test]
fn parse_unary_connective() {
UnaryConnectiveParser
Expand Down
8 changes: 4 additions & 4 deletions src/syntax_tree/fol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use {
BinaryOperatorParser, ComparisonParser, DirectionParser, FormulaParser,
FunctionConstantParser, GeneralTermParser, GuardParser, IntegerTermParser,
PlaceholderDeclarationParser, PredicateParser, QuantificationParser, QuantifierParser,
RelationParser, RoleParser, SpecificationParser, SymbolicTermParser, TheoryParser,
UnaryConnectiveParser, UnaryOperatorParser, UserGuideEntryParser, UserGuideParser,
VariableParser,
RelationParser, RoleParser, SortParser, SpecificationParser, SymbolicTermParser,
TheoryParser, UnaryConnectiveParser, UnaryOperatorParser, UserGuideEntryParser,
UserGuideParser, VariableParser,
},
syntax_tree::{impl_node, Node},
verifying::problem,
Expand Down Expand Up @@ -502,7 +502,7 @@ pub enum Sort {
Symbol,
}

// TODO: Should Sort be a Node?
impl_node!(Sort, Format, SortParser);

#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct FunctionConstant {
Expand Down

0 comments on commit 9d308ec

Please sign in to comment.