Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.
Open
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
16 changes: 15 additions & 1 deletion smt2parser/src/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
lexer,
visitors::{
CommandVisitor, ConstantVisitor, KeywordVisitor, QualIdentifierVisitor, SExprVisitor,
Smt2Visitor, SortVisitor, SymbolKind, SymbolVisitor, TermVisitor,
Smt2Visitor, SortVisitor, SymbolKind, SymbolVisitor, TermVisitor, TheoryVisitor,
},
Binary, Decimal, Hexadecimal, Numeral, Position,
};
Expand Down Expand Up @@ -196,6 +196,10 @@ pub enum Command<
},
}

pub struct Theory<Symbol = self::Symbol> {
name: Symbol,
}

/// An implementation of [`Smt2Visitor`] that returns concrete syntax values.
#[derive(Default, Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
pub struct SyntaxBuilder;
Expand Down Expand Up @@ -1045,6 +1049,15 @@ impl Command {
}
}

impl TheoryVisitor<Symbol> for SyntaxBuilder {
type E = Error;
type T = Theory;

fn visit_theory(&mut self, name: Symbol) -> Result<Self::T, Self::E> {
Ok(Theory { name })
}
}

impl Smt2Visitor for SyntaxBuilder {
type Error = Error;
type Constant = Constant;
Expand All @@ -1055,6 +1068,7 @@ impl Smt2Visitor for SyntaxBuilder {
type Symbol = Symbol;
type Term = Term;
type Command = Command;
type Theory = Theory;

fn syntax_error(&mut self, position: crate::Position, s: String) -> Self::Error {
Error::SyntaxError(position, s)
Expand Down
1 change: 1 addition & 0 deletions smt2parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const KEYWORDS: &[(&str, Token)] = {
("set-info", SetInfo),
("set-logic", SetLogic),
("set-option", SetOption),
("theory", Theory),
]
};

Expand Down
4 changes: 3 additions & 1 deletion smt2parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub type Binary = Vec<bool>;
pub use concrete::Error;
/// A position in the input.
pub use lexer::Position;
use parser::ParseResult;

/// Parse the input data and return a stream of interpreted SMT2 commands
pub struct CommandStream<R, T>
Expand Down Expand Up @@ -121,7 +122,8 @@ where
}
if unmatched_paren == 0 {
return match parser.end_of_input() {
Ok((command, _)) => Some(Ok(command)),
Ok((ParseResult::Command(command), _)) => Some(Ok(command)),
Ok((ParseResult::Theory(res), _)) => unimplemented!(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will return an error value here, obviously.

Err(err) => Some(Err(err)),
};
}
Expand Down
25 changes: 22 additions & 3 deletions smt2parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use internal::{Parser, Token};

pomelo! {
%module internal;
%include { use crate::visitors; }
%include { use crate::visitors; use crate::parser::ParseResult; }

%stack_size 0;

Expand All @@ -34,6 +34,10 @@ pomelo! {

%type command T::Command;

%type theory T::Theory;

%type mode ParseResult<T::Command, T::Theory>;

%type term T::Term;
%type terms Vec<T::Term>;

Expand Down Expand Up @@ -89,7 +93,7 @@ pomelo! {
%type sort_dec (T::Symbol, crate::Numeral);
%type sort_decs Vec<(T::Symbol, crate::Numeral)>;

%start_symbol command;
%start_symbol mode;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps input or start instead of mode ?


bound_symbol ::= Symbol(s) { extra.0.visit_bound_symbol(s)? }
fresh_symbol ::= Symbol(s) { extra.0.visit_fresh_symbol(s, crate::visitors::SymbolKind::Unknown)? }
Expand Down Expand Up @@ -355,6 +359,18 @@ pomelo! {
command ::= LeftParen SetLogic bound_symbol(x) RightParen { extra.0.visit_set_logic(x)? }
// ( set-option ⟨attribute⟩ )
command ::= LeftParen SetOption keyword(k) attribute_value(v) RightParen { extra.0.visit_set_option(k, v)? }


// ( theory ⟨symbol⟩ ⟨theory_attributes⟩+ )
theory ::= LeftParen Theory fresh_symbol(s) RightParen { extra.0.visit_theory(s)? }

mode ::= command(c) { ParseResult::Command(c) }
mode ::= theory(td) { ParseResult::Theory(td) }
}

pub enum ParseResult<C, T> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. (Need inline doc comment for public API though :))
In full generality, we could pass an artificial input token to signal what we expect but this is not even needed here since there is no ambiguity.

Command(C),
Theory(T),
}

#[cfg(test)]
Expand All @@ -369,7 +385,10 @@ pub(crate) mod tests {
for token in tokens.into_iter() {
p.parse(token)?;
}
Ok(p.end_of_input()?.0)
match p.end_of_input()?.0 {
ParseResult::Command(c) => Ok(c),
ParseResult::Theory(t) => panic!("Expected command"),
}
}

#[test]
Expand Down
16 changes: 15 additions & 1 deletion smt2parser/src/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
visitors::{
AttributeValue, CommandVisitor, ConstantVisitor, DatatypeDec, FunctionDec, Identifier,
KeywordVisitor, QualIdentifierVisitor, SExprVisitor, Smt2Visitor, SortVisitor, SymbolKind,
SymbolVisitor, TermVisitor,
SymbolVisitor, TermVisitor, TheoryVisitor,
},
Binary, Decimal, Hexadecimal, Numeral, Position,
};
Expand Down Expand Up @@ -720,6 +720,19 @@ where
}
}

impl<R, V> TheoryVisitor<V::Symbol> for R
where
R: Rewriter<V = V>,
V: Smt2Visitor,
{
type T = V::Theory;
type E = R::Error;

fn visit_theory(&mut self, name: V::Symbol) -> Result<Self::T, Self::E> {
self.visit_theory(name)
}
}

impl<R, V> CommandVisitor<V::Term, V::Symbol, V::Sort, V::Keyword, V::Constant, V::SExpr> for R
where
R: Rewriter<V = V>,
Expand Down Expand Up @@ -910,6 +923,7 @@ where
type Symbol = V::Symbol;
type Term = V::Term;
type Command = V::Command;
type Theory = V::Theory;

fn syntax_error(&mut self, pos: Position, s: String) -> Self::Error {
self.visitor().syntax_error(pos, s).into()
Expand Down
12 changes: 11 additions & 1 deletion smt2parser/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
concrete::Error,
visitors::{
CommandVisitor, ConstantVisitor, KeywordVisitor, QualIdentifierVisitor, SExprVisitor,
Smt2Visitor, SortVisitor, SymbolKind, SymbolVisitor, TermVisitor,
Smt2Visitor, SortVisitor, SymbolKind, SymbolVisitor, TermVisitor, TheoryVisitor,
},
Binary, Decimal, Hexadecimal, Numeral, Position,
};
Expand Down Expand Up @@ -553,6 +553,15 @@ impl CommandVisitor<Term, Symbol, Sort, Keyword, Constant, SExpr> for Smt2Counte
}
}

impl TheoryVisitor<Symbol> for Smt2Counters {
type E = Error;
type T = ();

fn visit_theory(&mut self, _name: Symbol) -> Result<(), Self::E> {
Ok(())
}
}

impl Smt2Visitor for Smt2Counters {
type Error = Error;
type Constant = ();
Expand All @@ -563,6 +572,7 @@ impl Smt2Visitor for Smt2Counters {
type Symbol = ();
type Term = Term;
type Command = ();
type Theory = ();

fn syntax_error(&mut self, position: Position, s: String) -> Self::Error {
Error::SyntaxError(position, s)
Expand Down
13 changes: 13 additions & 0 deletions smt2parser/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ pub trait Smt2Visitor:
<Self as Smt2Visitor>::SExpr,
T = <Self as Smt2Visitor>::Command,
E = <Self as Smt2Visitor>::Error,
> + TheoryVisitor<
<Self as Smt2Visitor>::Symbol,
T = <Self as Smt2Visitor>::Theory,
E = <Self as Smt2Visitor>::Error,
>
{
type Error;
Expand All @@ -493,11 +497,20 @@ pub trait Smt2Visitor:
type Symbol;
type Term;
type Command;
type Theory;

fn syntax_error(&mut self, position: crate::Position, s: String) -> Self::Error;
fn parsing_error(&mut self, position: crate::Position, s: String) -> Self::Error;
}

/// A visitor for the entire SMT2 syntax.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix comment

pub trait TheoryVisitor<Symbol> {
type E;
type T;

fn visit_theory(&mut self, name: Symbol) -> Result<Self::T, Self::E>;
}

impl<Symbol> std::fmt::Display for Index<Symbol>
where
Symbol: std::fmt::Display,
Expand Down