-
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.
Add a collection context and symbol collection step.
- Loading branch information
Showing
11 changed files
with
294 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::{ | ||
language::{parsed::Declaration, Visibility}, | ||
semantic_analysis::Namespace, | ||
}; | ||
use sway_error::handler::{ErrorEmitted, Handler}; | ||
use sway_types::{span::Span, Ident}; | ||
|
||
#[derive(Clone)] | ||
/// Contextual state tracked and accumulated throughout symbol collecting. | ||
pub struct SymbolCollectionContext { | ||
/// The namespace context accumulated throughout symbol collecting. | ||
pub(crate) namespace: Namespace, | ||
} | ||
|
||
impl SymbolCollectionContext { | ||
/// Initialize a context at the top-level of a module with its namespace. | ||
pub fn new(namespace: Namespace) -> Self { | ||
Self { namespace } | ||
} | ||
|
||
/// Scope the `CollectionContext` with a new lexical scope. | ||
pub fn scoped<T>( | ||
mut self, | ||
with_scoped_ctx: impl FnOnce(SymbolCollectionContext) -> Result<T, ErrorEmitted>, | ||
) -> Result<T, ErrorEmitted> { | ||
self.namespace.module_mut().push_new_lexical_scope(); | ||
let ret = with_scoped_ctx(self.clone()); | ||
self.namespace.module_mut().pop_lexical_scope(); | ||
ret | ||
} | ||
|
||
/// Enter the submodule with the given name and produce a collection context ready for | ||
/// collecting its content. | ||
/// | ||
/// Returns the result of the given `with_submod_ctx` function. | ||
pub fn enter_submodule<T>( | ||
&mut self, | ||
mod_name: Ident, | ||
visibility: Visibility, | ||
module_span: Span, | ||
with_submod_ctx: impl FnOnce(&mut SymbolCollectionContext) -> T, | ||
) -> T { | ||
self.namespace | ||
.push_new_submodule(mod_name, visibility, module_span); | ||
//let Self { namespace, .. } = self; | ||
//let mut submod_ns = namespace.enter_submodule(mod_name, visibility, module_span); | ||
let ret = with_submod_ctx(self); | ||
self.namespace.pop_submodule(); | ||
ret | ||
} | ||
|
||
/// Short-hand for calling [Items::insert_parsed_symbol]. | ||
pub(crate) fn insert_parsed_symbol( | ||
&mut self, | ||
_handler: &Handler, | ||
name: Ident, | ||
item: Declaration, | ||
) -> Result<(), ErrorEmitted> { | ||
self.namespace | ||
.module_mut() | ||
.current_items_mut() | ||
.insert_parsed_symbol(name, item) | ||
} | ||
} |
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
Oops, something went wrong.