forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LSP: Rewrite
LexedProgram
and ParseProgram
traversal to use the P…
…arse trait. (FuelLabs#4220) ## Description This work was started in FuelLabs#3772. I've decided to split that up into 2 PR's, this one implements a new `Parse` trait for the lexed and parsed ast's. The second one coming up with do the same but for the typed ast. The diff is quite big but it's just essentially moving the logic that was there into the trait impl's. Partially addresses FuelLabs#3799
- Loading branch information
1 parent
f1fcec7
commit 0fbdb3b
Showing
10 changed files
with
1,188 additions
and
1,103 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,63 +1,53 @@ | ||
use crate::core::{ | ||
token::{self, AstToken, SymbolKind, Token, TypeDefinition, TypedAstToken}, | ||
token_map::TokenMap, | ||
use crate::{ | ||
core::token::{self, AstToken, SymbolKind, Token, TypeDefinition, TypedAstToken}, | ||
traverse::ParseContext, | ||
}; | ||
use sway_core::language::{ | ||
parsed::{AstNode, AstNodeContent, Declaration}, | ||
ty, | ||
}; | ||
|
||
pub struct Dependency<'a> { | ||
tokens: &'a TokenMap, | ||
} | ||
|
||
impl<'a> Dependency<'a> { | ||
pub fn new(tokens: &'a TokenMap) -> Self { | ||
Self { tokens } | ||
} | ||
|
||
/// Insert Declaration tokens into the TokenMap. | ||
pub fn collect_parsed_declaration(&self, node: &AstNode) { | ||
if let AstNodeContent::Declaration(declaration) = &node.content { | ||
let parsed_token = AstToken::Declaration(declaration.clone()); | ||
/// Insert Declaration tokens into the TokenMap. | ||
pub fn collect_parsed_declaration(node: &AstNode, ctx: &ParseContext) { | ||
if let AstNodeContent::Declaration(declaration) = &node.content { | ||
let parsed_token = AstToken::Declaration(declaration.clone()); | ||
|
||
let (ident, symbol_kind) = match declaration { | ||
Declaration::VariableDeclaration(variable) => { | ||
(variable.name.clone(), SymbolKind::Variable) | ||
} | ||
Declaration::StructDeclaration(decl) => (decl.name.clone(), SymbolKind::Struct), | ||
Declaration::TraitDeclaration(decl) => (decl.name.clone(), SymbolKind::Trait), | ||
Declaration::FunctionDeclaration(decl) => (decl.name.clone(), SymbolKind::Function), | ||
Declaration::ConstantDeclaration(decl) => (decl.name.clone(), SymbolKind::Const), | ||
Declaration::EnumDeclaration(decl) => (decl.name.clone(), SymbolKind::Enum), | ||
_ => return, | ||
}; | ||
let (ident, symbol_kind) = match declaration { | ||
Declaration::VariableDeclaration(variable) => { | ||
(variable.name.clone(), SymbolKind::Variable) | ||
} | ||
Declaration::StructDeclaration(decl) => (decl.name.clone(), SymbolKind::Struct), | ||
Declaration::TraitDeclaration(decl) => (decl.name.clone(), SymbolKind::Trait), | ||
Declaration::FunctionDeclaration(decl) => (decl.name.clone(), SymbolKind::Function), | ||
Declaration::ConstantDeclaration(decl) => (decl.name.clone(), SymbolKind::Const), | ||
Declaration::EnumDeclaration(decl) => (decl.name.clone(), SymbolKind::Enum), | ||
_ => return, | ||
}; | ||
|
||
let key = token::to_ident_key(&ident); | ||
let token = Token::from_parsed(parsed_token, symbol_kind); | ||
self.tokens.insert(key, token); | ||
} | ||
let key = token::to_ident_key(&ident); | ||
let token = Token::from_parsed(parsed_token, symbol_kind); | ||
ctx.tokens.insert(key, token); | ||
} | ||
} | ||
|
||
/// Insert TypedDeclaration tokens into the TokenMap. | ||
pub fn collect_typed_declaration(&self, node: &ty::TyAstNode) { | ||
if let ty::TyAstNodeContent::Declaration(declaration) = &node.content { | ||
let typed_token = TypedAstToken::TypedDeclaration(declaration.clone()); | ||
/// Insert TypedDeclaration tokens into the TokenMap. | ||
pub fn collect_typed_declaration(node: &ty::TyAstNode, ctx: &ParseContext) { | ||
if let ty::TyAstNodeContent::Declaration(declaration) = &node.content { | ||
let typed_token = TypedAstToken::TypedDeclaration(declaration.clone()); | ||
|
||
let ident = match declaration { | ||
ty::TyDeclaration::VariableDeclaration(variable) => variable.name.clone(), | ||
ty::TyDeclaration::StructDeclaration(decl_ref) => decl_ref.name.clone(), | ||
ty::TyDeclaration::EnumDeclaration(decl_ref) => decl_ref.name.clone(), | ||
ty::TyDeclaration::TraitDeclaration { name, .. } | ||
| ty::TyDeclaration::FunctionDeclaration { name, .. } | ||
| ty::TyDeclaration::ConstantDeclaration { name, .. } => name.clone(), | ||
_ => return, | ||
}; | ||
let ident = token::to_ident_key(&ident); | ||
if let Some(mut token) = self.tokens.try_get_mut(&ident).try_unwrap() { | ||
token.typed = Some(typed_token); | ||
token.type_def = Some(TypeDefinition::Ident(ident.0)); | ||
} | ||
let ident = match declaration { | ||
ty::TyDeclaration::VariableDeclaration(variable) => variable.name.clone(), | ||
ty::TyDeclaration::StructDeclaration(decl_ref) => decl_ref.name.clone(), | ||
ty::TyDeclaration::EnumDeclaration(decl_ref) => decl_ref.name.clone(), | ||
ty::TyDeclaration::TraitDeclaration { name, .. } | ||
| ty::TyDeclaration::FunctionDeclaration { name, .. } | ||
| ty::TyDeclaration::ConstantDeclaration { name, .. } => name.clone(), | ||
_ => return, | ||
}; | ||
let ident = token::to_ident_key(&ident); | ||
if let Some(mut token) = ctx.tokens.try_get_mut(&ident).try_unwrap() { | ||
token.typed = Some(typed_token); | ||
token.type_def = Some(TypeDefinition::Ident(ident.0)); | ||
} | ||
} | ||
} |
Oops, something went wrong.