-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store attributes in the
ParseProgram
declaration types. (#2792)
This PR carries over any attributes that were parsed and stored in `sway_ast::Module` into the `ParseProgram` decl types. My primary motivation for adding this was so the language server could access: 1. the attribute tokens so we can assign correct semantic information to them for correct syntax highlighting 2. allow access to the doc attribute so we can provide in-editor documentation pop-ups for hover requests. See `sway-lsp/src/utils/attributes.rs` mod for the intended API for accessing these tokens. I did notice that in #1488 @ortho mentioned the next step would be to pass the attributes into the AST. Feel free to let me know if you had another idea for how these would be stored. I'm converting the `AttributeDecl` into the `Attribute` type below which basically throws away bracket & punctuation tokens and retains each of the `Ident`s. Then the `AttributesMap` is what is stored in the AST. Similar to how it worked before but now instead of having the map just store the vec of args, it also stores the attribute name. ```rust pub enum AttributeKind { Doc, Storage, } pub struct Attribute { pub name: Ident, pub args: Vec<Ident>, } pub type AttributesMap = HashMap<AttributeKind, Vec<Attribute>>; ```
- Loading branch information
1 parent
d5115ae
commit 4a0b313
Showing
14 changed files
with
169 additions
and
59 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![allow(dead_code)] | ||
use crate::core::token::{AstToken, Token}; | ||
use sway_core::{Attribute, AttributeKind, AttributesMap, Declaration}; | ||
|
||
pub(crate) fn attributes_map(token: &Token) -> Option<AttributesMap> { | ||
match &token.parsed { | ||
AstToken::Declaration(declaration) => match declaration { | ||
Declaration::EnumDeclaration(decl) => Some(decl.attributes.clone()), | ||
Declaration::FunctionDeclaration(decl) => Some(decl.attributes.clone()), | ||
Declaration::StructDeclaration(decl) => Some(decl.attributes.clone()), | ||
Declaration::ConstantDeclaration(decl) => Some(decl.attributes.clone()), | ||
Declaration::StorageDeclaration(decl) => Some(decl.attributes.clone()), | ||
_ => None, | ||
}, | ||
AstToken::StorageField(field) => Some(field.attributes.clone()), | ||
AstToken::StructField(field) => Some(field.attributes.clone()), | ||
AstToken::TraitFn(trait_fn) => Some(trait_fn.attributes.clone()), | ||
AstToken::EnumVariant(variant) => Some(variant.attributes.clone()), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub(crate) fn doc_attributes(token: &Token) -> Option<Vec<Attribute>> { | ||
attributes_map(token).and_then(|mut attributes| attributes.remove(&AttributeKind::Doc)) | ||
} | ||
|
||
pub(crate) fn storage_attributes(token: &Token) -> Option<Vec<Attribute>> { | ||
attributes_map(token).and_then(|mut attributes| attributes.remove(&AttributeKind::Storage)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod attributes; | ||
pub(crate) mod common; | ||
pub mod debug; | ||
pub(crate) mod function; | ||
|