Move fqsn code into separate module
This commit is contained in:
parent
383eb7bb62
commit
eb6a7e95a9
59
schala-lang/language/src/symbol_table/fqsn.rs
Normal file
59
schala-lang/language/src/symbol_table/fqsn.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
use std::{fmt, rc::Rc};
|
||||||
|
|
||||||
|
/// Fully-qualified symbol name
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
||||||
|
pub struct Fqsn {
|
||||||
|
//TODO Fqsn's need to be cheaply cloneable
|
||||||
|
pub scopes: Vec<ScopeSegment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Fqsn {
|
||||||
|
pub fn from_scope_stack(scopes: &[ScopeSegment], new_name: Rc<String>) -> Self {
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for s in scopes {
|
||||||
|
v.push(s.clone());
|
||||||
|
}
|
||||||
|
v.push(ScopeSegment::Name(new_name));
|
||||||
|
Fqsn { scopes: v }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn from_strs(strs: &[&str]) -> Fqsn {
|
||||||
|
let mut scopes = vec![];
|
||||||
|
for s in strs {
|
||||||
|
scopes.push(ScopeSegment::Name(Rc::new(s.to_string())));
|
||||||
|
}
|
||||||
|
Fqsn { scopes }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_elem(&self) -> Rc<String> {
|
||||||
|
let ScopeSegment::Name(name) = self.scopes.last().unwrap();
|
||||||
|
name.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Fqsn {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let delim = "::";
|
||||||
|
let Fqsn { scopes } = self;
|
||||||
|
write!(f, "FQSN<{}", scopes[0])?;
|
||||||
|
for item in scopes[1..].iter() {
|
||||||
|
write!(f, "{}{}", delim, item)?;
|
||||||
|
}
|
||||||
|
write!(f, ">")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO eventually this should use ItemId's to avoid String-cloning
|
||||||
|
/// One segment within a scope.
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
||||||
|
pub enum ScopeSegment {
|
||||||
|
Name(Rc<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ScopeSegment {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let ScopeSegment::Name(name) = self;
|
||||||
|
write!(f, "{}", name)
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,8 @@ use crate::{
|
|||||||
type_inference::{self, PendingType, TypeBuilder, TypeContext, TypeId, VariantBuilder},
|
type_inference::{self, PendingType, TypeBuilder, TypeContext, TypeId, VariantBuilder},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod fqsn;
|
||||||
|
pub use fqsn::{Fqsn, ScopeSegment};
|
||||||
mod resolver;
|
mod resolver;
|
||||||
mod symbol_trie;
|
mod symbol_trie;
|
||||||
use symbol_trie::SymbolTrie;
|
use symbol_trie::SymbolTrie;
|
||||||
@ -27,64 +29,6 @@ use crate::identifier::{define_id_kind, Id, IdStore};
|
|||||||
define_id_kind!(DefItem);
|
define_id_kind!(DefItem);
|
||||||
pub type DefId = Id<DefItem>;
|
pub type DefId = Id<DefItem>;
|
||||||
|
|
||||||
/// Fully-qualified symbol name
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
|
||||||
pub struct Fqsn {
|
|
||||||
//TODO Fqsn's need to be cheaply cloneable
|
|
||||||
scopes: Vec<ScopeSegment>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Fqsn {
|
|
||||||
fn from_scope_stack(scopes: &[ScopeSegment], new_name: Rc<String>) -> Self {
|
|
||||||
let mut v = Vec::new();
|
|
||||||
for s in scopes {
|
|
||||||
v.push(s.clone());
|
|
||||||
}
|
|
||||||
v.push(ScopeSegment::Name(new_name));
|
|
||||||
Fqsn { scopes: v }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn from_strs(strs: &[&str]) -> Fqsn {
|
|
||||||
let mut scopes = vec![];
|
|
||||||
for s in strs {
|
|
||||||
scopes.push(ScopeSegment::Name(Rc::new(s.to_string())));
|
|
||||||
}
|
|
||||||
Fqsn { scopes }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn last_elem(&self) -> Rc<String> {
|
|
||||||
let ScopeSegment::Name(name) = self.scopes.last().unwrap();
|
|
||||||
name.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Fqsn {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let delim = "::";
|
|
||||||
let Fqsn { scopes } = self;
|
|
||||||
write!(f, "FQSN<{}", scopes[0])?;
|
|
||||||
for item in scopes[1..].iter() {
|
|
||||||
write!(f, "{}{}", delim, item)?;
|
|
||||||
}
|
|
||||||
write!(f, ">")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO eventually this should use ItemId's to avoid String-cloning
|
|
||||||
/// One segment within a scope.
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
|
||||||
enum ScopeSegment {
|
|
||||||
Name(Rc<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ScopeSegment {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let ScopeSegment::Name(name) = self;
|
|
||||||
write!(f, "{}", name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum SymbolError {
|
pub enum SymbolError {
|
||||||
|
Loading…
Reference in New Issue
Block a user