Skip to content

Commit

Permalink
Moves out the AST definitions from sway_parse into sway_ast (Fuel…
Browse files Browse the repository at this point in the history
…Labs#2438)

* misc minor improvements to CFA

* split out sway_ast from sway_parse

* cargo clippy
  • Loading branch information
Centril authored Aug 3, 2022
1 parent ccc11c0 commit 09d6787
Show file tree
Hide file tree
Showing 102 changed files with 3,008 additions and 2,395 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"forc-util",
"scripts/examples-checker",
"scripts/mdbook-forc-documenter",
"sway-ast",
"sway-core",
"sway-fmt",
"sway-fmt-v2",
Expand Down
15 changes: 15 additions & 0 deletions sway-ast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "sway-ast"
version = "0.19.1"
authors = ["Fuel Labs <[email protected]>"]
edition = "2021"
homepage = "https://fuel.network/"
license = "Apache-2.0"
repository = "https://github.com/FuelLabs/sway"
description = "Sway's parser"

[dependencies]
extension-trait = "1.0.1"
num-bigint = "0.4.3"
num-traits = "0.2.14"
sway-types = { version = "0.19.1", path = "../sway-types" }
File renamed without changes.
41 changes: 41 additions & 0 deletions sway-ast/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::priv_prelude::*;

#[derive(Clone, Debug)]
pub struct Annotated<T> {
pub attribute_list: Vec<AttributeDecl>,
pub value: T,
}

// Attributes can have any number of arguments:
//
// #[attribute]
// #[attribute()]
// #[attribute(value)]
// #[attribute(value0, value1, value2)]

#[derive(Clone, Debug)]
pub struct AttributeDecl {
pub hash_token: HashToken,
pub attribute: SquareBrackets<Attribute>,
}

impl Spanned for AttributeDecl {
fn span(&self) -> Span {
Span::join(self.hash_token.span(), self.attribute.span())
}
}

#[derive(Clone, Debug)]
pub struct Attribute {
pub name: Ident,
pub args: Option<Parens<Punctuated<Ident, CommaToken>>>,
}

impl Spanned for Attribute {
fn span(&self) -> Span {
self.args
.as_ref()
.map(|args| Span::join(self.name.span(), args.span()))
.unwrap_or_else(|| self.name.span())
}
}
60 changes: 60 additions & 0 deletions sway-ast/src/brackets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::priv_prelude::*;

macro_rules! define_brackets (
($ty_name:ident) => {
#[derive(Clone, Debug)]
pub struct $ty_name<T> {
pub inner: T,
pub span: Span,
}

impl<T> $ty_name<T> {
pub fn new<'a>(inner: T, span: Span) -> $ty_name<T> {
$ty_name {
inner,
span,
}
}

pub fn get(&self) -> &T {
&self.inner
}

pub fn into_inner(self) -> T {
self.inner
}
}

impl<T> Spanned for $ty_name<T> {
fn span(&self) -> Span {
self.span.clone()
}
}
};
);

define_brackets!(Braces);
define_brackets!(Parens);
define_brackets!(SquareBrackets);

#[derive(Clone, Debug)]
pub struct AngleBrackets<T> {
pub open_angle_bracket_token: OpenAngleBracketToken,
pub inner: T,
pub close_angle_bracket_token: CloseAngleBracketToken,
}

impl<T> AngleBrackets<T> {
pub fn into_inner(self) -> T {
self.inner
}
}

impl<T> Spanned for AngleBrackets<T> {
fn span(&self) -> Span {
Span::join(
self.open_angle_bracket_token.span(),
self.close_angle_bracket_token.span(),
)
}
}
27 changes: 27 additions & 0 deletions sway-ast/src/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::priv_prelude::*;

pub struct Dependency {
pub dep_token: DepToken,
pub path: DependencyPath,
pub semicolon_token: SemicolonToken,
}

impl Spanned for Dependency {
fn span(&self) -> Span {
Span::join(self.dep_token.span(), self.semicolon_token.span())
}
}

pub struct DependencyPath {
pub prefix: Ident,
pub suffixes: Vec<(ForwardSlashToken, Ident)>,
}

impl Spanned for DependencyPath {
fn span(&self) -> Span {
match self.suffixes.last() {
Some((_forward_slash_token, suffix)) => Span::join(self.prefix.span(), suffix.span()),
None => self.prefix.span(),
}
}
}
44 changes: 44 additions & 0 deletions sway-ast/src/expr/asm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::priv_prelude::*;

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

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

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

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

#[derive(Clone, Debug)]
pub struct AsmImmediate {
pub span: Span,
pub parsed: BigUint,
}

impl Spanned for AsmImmediate {
fn span(&self) -> Span {
self.span.clone()
}
}

impl Spanned for AsmBlock {
fn span(&self) -> Span {
Span::join(self.asm_token.span(), self.contents.span())
}
}
Loading

0 comments on commit 09d6787

Please sign in to comment.