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

feat: AST pretty-printer #153

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions crates/ast/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ impl UserDefinableOperator {
Self::Ne => Either::Right(BinOpKind::Ne),
}
}

/// Returns the string representation of the operator.
pub const fn to_str(&self) -> &'static str {
either::for_both!(self.to_op(), op => op.to_str())
}
}

/// A contract, abstract contract, interface, or library definition:
Expand Down
18 changes: 18 additions & 0 deletions crates/ast/src/ast/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ pub struct Lit {
pub kind: LitKind,
}

impl fmt::Display for Lit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { kind, symbol, span: _ } = self;
match kind {
LitKind::Str(StrKind::Str, _) => write!(f, "\"{symbol}\""),
LitKind::Str(StrKind::Unicode, _) => write!(f, "unicode\"{symbol}\""),
LitKind::Str(StrKind::Hex, _) => write!(f, "hex\"{symbol}\""),
LitKind::Number(_)
| LitKind::Rational(_)
| LitKind::Err(_)
| LitKind::Address(_)
| LitKind::Bool(_) => {
write!(f, "{symbol}")
}
}
}
}

/// A kind of literal.
#[derive(Clone, Debug)]
pub enum LitKind {
Expand Down
1 change: 1 addition & 0 deletions crates/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ pub use solar_interface as interface;
mod ast;
pub use ast::*;

pub mod pretty;
pub mod token;
pub mod visit;
Loading
Loading