WIP name analysis.
This commit is contained in:
parent
583136711a
commit
e578250ee6
@ -1,25 +1,62 @@
|
|||||||
use std::collections::HashMap;
|
use crate::ast::ast_node::{AstNode, AstNodeRef};
|
||||||
use crate::ast::ast_node::AstNodeRef;
|
use crate::ast::node::{CompilationUnit, Identifier, UseStatement, UseStatementSuffix};
|
||||||
use crate::ast::node::{CompilationUnit, Identifier};
|
|
||||||
use crate::ast::walk::walk_depth_first;
|
|
||||||
use crate::diagnostic::DmDiagnostic;
|
use crate::diagnostic::DmDiagnostic;
|
||||||
|
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||||
|
use crate::name_analysis::symbol::use_symbol::StarUseStatementSymbol;
|
||||||
|
use crate::name_analysis::symbol::UseStatementSymbol;
|
||||||
use crate::name_analysis::symbol_table::SymbolTable;
|
use crate::name_analysis::symbol_table::SymbolTable;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn gather_identifier(identifier: &Identifier, symbol_table: &SymbolTable, identifier_scope_ids: &mut HashMap<Identifier, usize>) {
|
fn gather_identifier(
|
||||||
|
identifier: &Identifier,
|
||||||
|
symbol_table: &SymbolTable,
|
||||||
|
identifier_scope_ids: &mut HashMap<Identifier, usize>,
|
||||||
|
) {
|
||||||
identifier_scope_ids.insert(identifier.clone(), symbol_table.current_scope_id());
|
identifier_scope_ids.insert(identifier.clone(), symbol_table.current_scope_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gather_compilation_unit(
|
fn gather_use_statement(
|
||||||
compilation_unit: &mut CompilationUnit,
|
use_statement: &UseStatement,
|
||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
identifier_scope_ids: &mut HashMap<Identifier, usize>,
|
|
||||||
diagnostics: &mut Vec<DmDiagnostic>,
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
) {
|
) {
|
||||||
walk_depth_first(compilation_unit, &mut |child| match child {
|
let mut fully_qualified_name = String::new();
|
||||||
AstNodeRef::Operator(_) => {}
|
for prefix in use_statement.prefixes() {
|
||||||
AstNodeRef::Identifier(identifier) => {
|
fully_qualified_name.push_str(&format!("{}::", prefix.identifier().name()));
|
||||||
gather_identifier(&identifier, symbol_table, identifier_scope_ids);
|
}
|
||||||
|
match use_statement.suffix() {
|
||||||
|
UseStatementSuffix::Identifier(identifier) => {}
|
||||||
|
UseStatementSuffix::Star => {
|
||||||
|
let symbol_inner = StarUseStatementSymbol::new(
|
||||||
|
&fully_qualified_name,
|
||||||
|
Some(SourceDefinition::from_use_statement(use_statement)),
|
||||||
|
);
|
||||||
|
let symbol = UseStatementSymbol::Star(symbol_inner);
|
||||||
|
symbol_table.insert_use_statement_symbol(symbol);
|
||||||
|
todo!()
|
||||||
}
|
}
|
||||||
|
UseStatementSuffix::UseList(use_list) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gather_node_children(
|
||||||
|
node: &impl AstNode,
|
||||||
|
symbol_table: &mut SymbolTable,
|
||||||
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
|
) {
|
||||||
|
for child in node.children() {
|
||||||
|
gather_node(child, symbol_table, diagnostics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gather_node(
|
||||||
|
node: AstNodeRef,
|
||||||
|
symbol_table: &mut SymbolTable,
|
||||||
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
|
) {
|
||||||
|
match node {
|
||||||
|
AstNodeRef::Operator(_) => {}
|
||||||
|
AstNodeRef::Identifier(_) => {}
|
||||||
AstNodeRef::FullyQualifiedName(_) => {}
|
AstNodeRef::FullyQualifiedName(_) => {}
|
||||||
AstNodeRef::TypeUseList(_) => {}
|
AstNodeRef::TypeUseList(_) => {}
|
||||||
AstNodeRef::IdentifierList(_) => {}
|
AstNodeRef::IdentifierList(_) => {}
|
||||||
@ -37,9 +74,13 @@ pub fn gather_compilation_unit(
|
|||||||
AstNodeRef::Parameters(_) => {}
|
AstNodeRef::Parameters(_) => {}
|
||||||
AstNodeRef::Parameter(_) => {}
|
AstNodeRef::Parameter(_) => {}
|
||||||
AstNodeRef::ReturnType(_) => {}
|
AstNodeRef::ReturnType(_) => {}
|
||||||
AstNodeRef::CompilationUnit(_) => {}
|
AstNodeRef::CompilationUnit(compilation_unit) => {
|
||||||
|
gather_node_children(compilation_unit, symbol_table, diagnostics);
|
||||||
|
}
|
||||||
AstNodeRef::ParentMod(_) => {}
|
AstNodeRef::ParentMod(_) => {}
|
||||||
AstNodeRef::UseStatement(_) => {}
|
AstNodeRef::UseStatement(use_statement) => {
|
||||||
|
gather_use_statement(use_statement, symbol_table, diagnostics);
|
||||||
|
}
|
||||||
AstNodeRef::UseStatementPrefix(_) => {}
|
AstNodeRef::UseStatementPrefix(_) => {}
|
||||||
AstNodeRef::UseStatementSuffix(_) => {}
|
AstNodeRef::UseStatementSuffix(_) => {}
|
||||||
AstNodeRef::UseList(_) => {}
|
AstNodeRef::UseList(_) => {}
|
||||||
@ -111,5 +152,14 @@ pub fn gather_compilation_unit(
|
|||||||
AstNodeRef::DString(_) => {}
|
AstNodeRef::DString(_) => {}
|
||||||
AstNodeRef::DStringExpression(_) => {}
|
AstNodeRef::DStringExpression(_) => {}
|
||||||
AstNodeRef::BacktickString(_) => {}
|
AstNodeRef::BacktickString(_) => {}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gather_compilation_unit(
|
||||||
|
compilation_unit: &mut CompilationUnit,
|
||||||
|
symbol_table: &mut SymbolTable,
|
||||||
|
identifier_scope_ids: &mut HashMap<Identifier, usize>,
|
||||||
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
|
) {
|
||||||
|
gather_node(compilation_unit.as_node_ref(), symbol_table, diagnostics);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,41 +1,13 @@
|
|||||||
|
pub(super) mod source_definition;
|
||||||
|
pub(super) mod use_symbol;
|
||||||
|
|
||||||
use crate::ast::node::Identifier;
|
use crate::ast::node::Identifier;
|
||||||
|
use source_definition::SourceDefinition;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::range::Range;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct SourceDefinition {
|
|
||||||
file_id: usize,
|
|
||||||
range: Range<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SourceDefinition {
|
|
||||||
pub fn from_identifier(identifier: &Identifier) -> Self {
|
|
||||||
SourceDefinition {
|
|
||||||
file_id: identifier.file_id(),
|
|
||||||
range: identifier.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_identifier_rc(identifier: Rc<RefCell<Identifier>>) -> Self {
|
|
||||||
let borrowed = identifier.borrow();
|
|
||||||
SourceDefinition {
|
|
||||||
file_id: borrowed.file_id(),
|
|
||||||
range: borrowed.range(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_id(&self) -> usize {
|
|
||||||
self.file_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn range(&self) -> Range<usize> {
|
|
||||||
self.range.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait SymbolInner {
|
pub trait SymbolInner {
|
||||||
fn declared_name(&self) -> &str;
|
fn declared_name(&self) -> &str;
|
||||||
fn definition(&self) -> Option<SourceDefinition>;
|
fn definition(&self) -> Option<SourceDefinition>;
|
||||||
42
src/name_analysis/symbol/source_definition.rs
Normal file
42
src/name_analysis/symbol/source_definition.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::range::Range;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use crate::ast::node::{Identifier, UseStatement};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct SourceDefinition {
|
||||||
|
file_id: usize,
|
||||||
|
range: Range<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SourceDefinition {
|
||||||
|
pub fn from_identifier(identifier: &Identifier) -> Self {
|
||||||
|
SourceDefinition {
|
||||||
|
file_id: identifier.file_id(),
|
||||||
|
range: identifier.range(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_identifier_rc(identifier: Rc<RefCell<Identifier>>) -> Self {
|
||||||
|
let borrowed = identifier.borrow();
|
||||||
|
SourceDefinition {
|
||||||
|
file_id: borrowed.file_id(),
|
||||||
|
range: borrowed.range(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_use_statement(use_statement: &UseStatement) -> Self {
|
||||||
|
Self {
|
||||||
|
file_id: use_statement.file_id(),
|
||||||
|
range: use_statement.range(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_id(&self) -> usize {
|
||||||
|
self.file_id
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn range(&self) -> Range<usize> {
|
||||||
|
self.range.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/name_analysis/symbol/use_symbol.rs
Normal file
28
src/name_analysis/symbol/use_symbol.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
use crate::name_analysis::symbol::source_definition::SourceDefinition;
|
||||||
|
|
||||||
|
pub enum UseStatementSymbol {
|
||||||
|
Concrete,
|
||||||
|
Star(StarUseStatementSymbol),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StarUseStatementSymbol {
|
||||||
|
base_fqn: String,
|
||||||
|
source_definition: Option<SourceDefinition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StarUseStatementSymbol {
|
||||||
|
pub fn new(base_fqn: &str, source_definition: Option<SourceDefinition>) -> Self {
|
||||||
|
Self {
|
||||||
|
base_fqn: base_fqn.to_string(),
|
||||||
|
source_definition,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn base_fqn(&self) -> &str {
|
||||||
|
&self.base_fqn
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn source_definition(&self) -> Option<&SourceDefinition> {
|
||||||
|
self.source_definition.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user