Skip to content

Commit

Permalink
Make AST serializable (FuelLabs#4193)
Browse files Browse the repository at this point in the history
- Implements `serde::Serialize` for `sway_ast::Module` and such.
- Adds a simple integration test to `sway_parse` which uses `insta` for
RON snapshot testing.

This should allow us to dump AST in more convenient formats and also use
tools like `insta` to do testing on the serialized output.

Note: Deserialization was left for later.
  • Loading branch information
AlicanC authored Mar 10, 2023
1 parent 4c1c1d8 commit 10f2b00
Show file tree
Hide file tree
Showing 43 changed files with 516 additions and 101 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sway-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Sway's parser"

[dependencies]
extension-trait = "1.0.1"
num-bigint = "0.4.3"
num-bigint = { version = "0.4.3", features = ["serde"] }
num-traits = "0.2.14"
serde = { version = "1.0", features = ["derive"] }
sway-types = { version = "0.35.5", path = "../sway-types" }
2 changes: 1 addition & 1 deletion sway-ast/src/assignable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum Assignable {
Var(Ident),
Index {
Expand Down
8 changes: 4 additions & 4 deletions sway-ast/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct Annotated<T> {
pub attribute_list: Vec<AttributeDecl>,
pub value: T,
Expand All @@ -13,7 +13,7 @@ pub struct Annotated<T> {
// #[attribute(value)]
// #[attribute(value0, value1, value2)]

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AttributeDecl {
pub hash_kind: AttributeHashKind,
pub attribute: SquareBrackets<Punctuated<Attribute, CommaToken>>,
Expand Down Expand Up @@ -42,7 +42,7 @@ impl Spanned for AttributeDecl {
/// enum Bar {}
/// #![doc("a Sway enum")]
/// ```
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum AttributeHashKind {
/// Inner specifies that the attribute belongs to
/// the item before it.
Expand All @@ -52,7 +52,7 @@ pub enum AttributeHashKind {
Outer(HashToken),
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct Attribute {
pub name: Ident,
pub args: Option<Parens<Punctuated<Ident, CommaToken>>>,
Expand Down
4 changes: 2 additions & 2 deletions sway-ast/src/brackets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::priv_prelude::*;

macro_rules! define_brackets (
($ty_name:ident) => {
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct $ty_name<T> {
pub inner: T,
pub span: Span,
Expand Down Expand Up @@ -37,7 +37,7 @@ define_brackets!(Braces);
define_brackets!(Parens);
define_brackets!(SquareBrackets);

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AngleBrackets<T> {
pub open_angle_bracket_token: OpenAngleBracketToken,
pub inner: T,
Expand Down
10 changes: 5 additions & 5 deletions sway-ast/src/expr/asm.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AsmBlock {
pub asm_token: AsmToken,
pub registers: Parens<Punctuated<AsmRegisterDeclaration, CommaToken>>,
pub contents: Braces<AsmBlockContents>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AsmRegisterDeclaration {
pub register: Ident,
pub value_opt: Option<(ColonToken, Box<Expr>)>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AsmBlockContents {
pub instructions: Vec<(Instruction, SemicolonToken)>,
pub final_expr_opt: Option<AsmFinalExpr>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AsmFinalExpr {
pub register: Ident,
pub ty_opt: Option<(ColonToken, Ty)>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AsmImmediate {
pub span: Span,
pub parsed: BigUint,
Expand Down
36 changes: 21 additions & 15 deletions sway-ast/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{priv_prelude::*, PathExprSegment};
pub mod asm;
pub mod op_code;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum Expr {
/// A malformed expression.
///
Expand Down Expand Up @@ -256,13 +256,13 @@ impl Spanned for Expr {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ReassignmentOp {
pub variant: ReassignmentOpVariant,
pub span: Span,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum ReassignmentOpVariant {
Equals,
AddEquals,
Expand All @@ -287,26 +287,26 @@ impl ReassignmentOpVariant {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct AbiCastArgs {
pub name: PathType,
pub comma_token: CommaToken,
pub address: Box<Expr>,
}

#[allow(clippy::type_complexity)]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct IfExpr {
pub if_token: IfToken,
pub condition: IfCondition,
pub then_block: Braces<CodeBlockContents>,
pub else_opt: Option<(
ElseToken,
ControlFlow<Braces<CodeBlockContents>, Box<IfExpr>>,
LoopControlFlow<Braces<CodeBlockContents>, Box<IfExpr>>,
)>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum IfCondition {
Expr(Box<Expr>),
Let {
Expand All @@ -317,21 +317,27 @@ pub enum IfCondition {
},
}

#[derive(Clone, Debug, Serialize)]
pub enum LoopControlFlow<B, C = ()> {
Continue(C),
Break(B),
}

impl Spanned for IfExpr {
fn span(&self) -> Span {
let start = self.if_token.span();
let end = match &self.else_opt {
Some((_else_token, tail)) => match tail {
ControlFlow::Break(block) => block.span(),
ControlFlow::Continue(if_expr) => if_expr.span(),
LoopControlFlow::Break(block) => block.span(),
LoopControlFlow::Continue(if_expr) => if_expr.span(),
},
None => self.then_block.span(),
};
Span::join(start, end)
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum ExprTupleDescriptor {
Nil,
Cons {
Expand All @@ -341,7 +347,7 @@ pub enum ExprTupleDescriptor {
},
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum ExprArrayDescriptor {
Sequence(Punctuated<Expr, CommaToken>),
Repeat {
Expand All @@ -351,7 +357,7 @@ pub enum ExprArrayDescriptor {
},
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct MatchBranch {
pub pattern: Pattern,
pub fat_right_arrow_token: FatRightArrowToken,
Expand All @@ -365,7 +371,7 @@ impl Spanned for MatchBranch {
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
pub enum MatchBranchKind {
Block {
block: Braces<CodeBlockContents>,
Expand Down Expand Up @@ -394,13 +400,13 @@ impl Spanned for MatchBranchKind {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct CodeBlockContents {
pub statements: Vec<Statement>,
pub final_expr_opt: Option<Box<Expr>>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ExprStructField {
pub field_name: Ident,
pub expr_opt: Option<(ColonToken, Box<Expr>)>,
Expand Down
4 changes: 2 additions & 2 deletions sway-ast/src/expr/op_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::priv_prelude::*;

macro_rules! define_op_code (
($ty_name:ident, $s:literal) => (
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct $ty_name {
span: Span,
}
Expand Down Expand Up @@ -106,7 +106,7 @@ macro_rules! define_op_codes (
}
)*

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub enum Instruction {
$($op_name {
token: $ty_name,
Expand Down
4 changes: 2 additions & 2 deletions sway-ast/src/generics.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct GenericParams {
pub parameters: AngleBrackets<Punctuated<Ident, CommaToken>>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct GenericArgs {
pub parameters: AngleBrackets<Punctuated<Ty, CommaToken>>,
}
Expand Down
2 changes: 1 addition & 1 deletion sway-ast/src/item/item_abi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{priv_prelude::*, ItemTraitItem};

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ItemAbi {
pub abi_token: AbiToken,
pub name: Ident,
Expand Down
4 changes: 2 additions & 2 deletions sway-ast/src/item/item_configurable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ItemConfigurable {
pub configurable_token: ConfigurableToken,
pub fields: Braces<Punctuated<Annotated<ConfigurableField>, CommaToken>>,
Expand All @@ -12,7 +12,7 @@ impl Spanned for ItemConfigurable {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ConfigurableField {
pub name: Ident,
pub colon_token: ColonToken,
Expand Down
2 changes: 1 addition & 1 deletion sway-ast/src/item/item_const.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct ItemConst {
pub visibility: Option<PubToken>,
pub const_token: ConstToken,
Expand Down
Loading

0 comments on commit 10f2b00

Please sign in to comment.