Various gathering and scope table.
This commit is contained in:
parent
b47dea9136
commit
6b206605c1
File diff suppressed because it is too large
Load Diff
@ -19,19 +19,23 @@ The resolve phase has one main responsibility: resolve all references based on t
|
||||
`scope_id` property.
|
||||
*/
|
||||
|
||||
use crate::ast::node::{CompilationUnit, VariableUse};
|
||||
// use crate::name_analysis::resolve::resolve_compilation_unit;
|
||||
use crate::ast::ast_node::AstNode;
|
||||
use crate::ast::node::CompilationUnit;
|
||||
use crate::diagnostic::DmDiagnostic;
|
||||
use crate::name_analysis::gather::gather_compilation_unit;
|
||||
// use crate::name_analysis::resolve::resolve_compilation_unit;
|
||||
use crate::name_analysis::symbol_table::SymbolTable;
|
||||
use codespan_reporting::files::Files;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use crate::name_analysis::scope_table::ScopeTable;
|
||||
|
||||
pub(self) mod fqn_context;
|
||||
mod gather;
|
||||
// mod resolve;
|
||||
pub mod symbol;
|
||||
pub mod symbol_table;
|
||||
mod scope_table;
|
||||
|
||||
pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
|
||||
compilation_units: &[Box<CompilationUnit>],
|
||||
@ -39,7 +43,7 @@ pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
|
||||
symbol_table: &mut SymbolTable,
|
||||
) -> Vec<DmDiagnostic> {
|
||||
let mut diagnostics = vec![];
|
||||
let mut scope_ids: HashMap<&VariableUse, usize> = HashMap::new();
|
||||
let mut scope_table = ScopeTable::new();
|
||||
|
||||
// gather symbols
|
||||
for compilation_unit in compilation_units {
|
||||
@ -48,7 +52,7 @@ pub fn analyze_names<'a, F: Files<'a, FileId = usize, Name = String>>(
|
||||
compilation_unit,
|
||||
&file_name,
|
||||
symbol_table,
|
||||
&mut scope_ids,
|
||||
&mut scope_table,
|
||||
&mut diagnostics,
|
||||
);
|
||||
}
|
||||
|
||||
32
src/name_analysis/scope_table/mod.rs
Normal file
32
src/name_analysis/scope_table/mod.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use crate::ast::node::{FullyQualifiedName, VariableUse};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct ScopeTable<'a> {
|
||||
variable_use_scopes: HashMap<&'a VariableUse, usize>,
|
||||
fqn_scopes: HashMap<&'a FullyQualifiedName, usize>,
|
||||
}
|
||||
|
||||
impl<'a> ScopeTable<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
variable_use_scopes: HashMap::new(),
|
||||
fqn_scopes: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_variable_use_scope(&mut self, variable_use: &'a VariableUse, scope_id: usize) {
|
||||
self.variable_use_scopes.insert(variable_use, scope_id);
|
||||
}
|
||||
|
||||
pub fn insert_fqn_scope(&mut self, fqn: &'a FullyQualifiedName, scope_id: usize) {
|
||||
self.fqn_scopes.insert(fqn, scope_id);
|
||||
}
|
||||
|
||||
pub fn variable_use_scope(&self, variable_use: &'a VariableUse) -> Option<usize> {
|
||||
self.variable_use_scopes.get(&variable_use).copied()
|
||||
}
|
||||
|
||||
pub fn fqn_scope(&self, fqn: &'a FullyQualifiedName) -> Option<usize> {
|
||||
self.fqn_scopes.get(&fqn).copied()
|
||||
}
|
||||
}
|
||||
@ -50,7 +50,6 @@ Identifier:
|
||||
- range:
|
||||
kind: range
|
||||
derive:
|
||||
- Clone
|
||||
- PartialEq
|
||||
- Eq
|
||||
- Hash
|
||||
@ -60,6 +59,10 @@ FullyQualifiedName:
|
||||
- identifiers:
|
||||
vec:
|
||||
rule: Identifier
|
||||
derive:
|
||||
- PartialEq
|
||||
- Eq
|
||||
- Hash
|
||||
|
||||
# Lists
|
||||
TypeUseList:
|
||||
@ -74,7 +77,7 @@ IdentifierList:
|
||||
- identifiers:
|
||||
vec:
|
||||
rule: Identifier
|
||||
ParenthesesOptionalTypeUseList:
|
||||
ParenthesesTypeUseList:
|
||||
struct:
|
||||
children:
|
||||
- type_use_list:
|
||||
@ -90,19 +93,29 @@ TypeUse:
|
||||
- TupleTypeUse
|
||||
- FunctionTypeUse
|
||||
PrimitiveType:
|
||||
leaf_enum:
|
||||
tree_enum:
|
||||
rules:
|
||||
- Byte
|
||||
- Short
|
||||
- Char
|
||||
- Int
|
||||
- Long
|
||||
- Double
|
||||
- Bool
|
||||
- String
|
||||
- Byte:
|
||||
child: false
|
||||
- Short:
|
||||
child: false
|
||||
- Char:
|
||||
child: false
|
||||
- Int:
|
||||
child: false
|
||||
- Long:
|
||||
child: false
|
||||
- Double:
|
||||
child: false
|
||||
- Bool:
|
||||
child: false
|
||||
- String:
|
||||
child: false
|
||||
- TypedArray
|
||||
- Any
|
||||
- Void
|
||||
- Any:
|
||||
child: false
|
||||
- Void:
|
||||
child: false
|
||||
TypedArray:
|
||||
struct:
|
||||
children:
|
||||
@ -160,7 +173,7 @@ GenericParameters:
|
||||
TupleArguments:
|
||||
struct:
|
||||
children:
|
||||
- parentheses_optional_type_use_list
|
||||
- parentheses_type_use_list
|
||||
|
||||
# Implements List
|
||||
ImplementsList:
|
||||
@ -656,7 +669,7 @@ IfClause:
|
||||
skip:
|
||||
rule: Then
|
||||
- statements:
|
||||
skip:
|
||||
vec:
|
||||
rule: Statement
|
||||
IfElseIf:
|
||||
struct:
|
||||
@ -704,7 +717,7 @@ ForStatement:
|
||||
- do_kw:
|
||||
skip:
|
||||
rule: Do
|
||||
- statement:
|
||||
- statements:
|
||||
vec:
|
||||
rule: Statement
|
||||
- end_kw:
|
||||
@ -731,7 +744,6 @@ VariableUse:
|
||||
children:
|
||||
- identifier
|
||||
derive:
|
||||
- Clone
|
||||
- PartialEq
|
||||
- Eq
|
||||
- Hash
|
||||
|
||||
@ -199,7 +199,7 @@ IdentifierList = {
|
||||
~ ( "," ~ Identifier )*
|
||||
}
|
||||
|
||||
ParenthesesOptionalTypeUseList = {
|
||||
ParenthesesTypeUseList = {
|
||||
"("
|
||||
~ TypeUseList?
|
||||
~ ")"
|
||||
@ -272,7 +272,7 @@ GenericParameters = {
|
||||
// Tuple Arguments
|
||||
|
||||
TupleArguments = {
|
||||
ParenthesesOptionalTypeUseList
|
||||
ParenthesesTypeUseList
|
||||
}
|
||||
|
||||
// Implements list
|
||||
|
||||
Loading…
Reference in New Issue
Block a user