diff --git a/fastn-resolved/src/lib.rs b/fastn-resolved/src/lib.rs index 419462a5e..3b1fb7ffa 100644 --- a/fastn-resolved/src/lib.rs +++ b/fastn-resolved/src/lib.rs @@ -34,16 +34,10 @@ pub type Map = std::collections::BTreeMap; #[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] pub enum Definition { - SymbolAlias { + /// every module is a "thing" and can be referred to etc., so we need to keep track of them + Module { package: String, - module: String, - name: String, - line_number: usize, - }, - ModuleAlias { - package: String, - module: String, - line_number: usize, + module: Option, }, Record(fastn_resolved::Record), OrType(fastn_resolved::OrType), @@ -74,9 +68,14 @@ impl Definition { fastn_resolved::Definition::Function(f) => f.name.to_string(), fastn_resolved::Definition::WebComponent(w) => w.name.to_string(), fastn_resolved::Definition::Export { to, .. } => to.to_string(), - // TODO: check if the following two are valid - Definition::SymbolAlias { name, .. } => name.clone(), - Definition::ModuleAlias { .. } => todo!(), + Definition::Module { + package, + module: None, + } => package.clone(), + Definition::Module { + package, + module: Some(module), + } => format!("{}/{}", package, module), } } @@ -90,8 +89,9 @@ impl Definition { Definition::OrTypeWithVariant { variant, .. } => variant.line_number(), Definition::WebComponent(w) => w.line_number, Definition::Export { line_number, .. } => *line_number, - Definition::SymbolAlias { line_number, .. } => *line_number, - Definition::ModuleAlias { line_number, .. } => *line_number, + // module is not defined on any given line, unless it is defined using the future + // module proposal, till now we return 0 + Definition::Module { .. } => 0, } } diff --git a/ftd/src/interpreter/tdoc.rs b/ftd/src/interpreter/tdoc.rs index 7ee5a4c58..c9715eff1 100644 --- a/ftd/src/interpreter/tdoc.rs +++ b/ftd/src/interpreter/tdoc.rs @@ -631,8 +631,7 @@ impl<'a> TDoc<'a> { ), ftd::interpreter::Thing::Function(f) => (f.return_kind, false), ftd::interpreter::Thing::Export { .. } => unreachable!(), - ftd::interpreter::Thing::ModuleAlias { .. } - | ftd::interpreter::Thing::SymbolAlias { .. } => todo!(), + ftd::interpreter::Thing::Module { .. } => todo!(), }; ( diff --git a/v0.5/fastn-compiler/src/compiler.rs b/v0.5/fastn-compiler/src/compiler.rs index b1ba38de5..8655a4924 100644 --- a/v0.5/fastn-compiler/src/compiler.rs +++ b/v0.5/fastn-compiler/src/compiler.rs @@ -16,7 +16,7 @@ impl Compiler { symbols: Box, source: &str, package: &str, - module: &str, + module: Option<&str>, // auto_import_scope: fastn_unresolved::SFId, mut arena: fastn_unresolved::Arena, ) -> Self { @@ -224,7 +224,7 @@ pub async fn compile( symbols: Box, source: &str, package: &str, - module: &str, + module: Option<&str>, // auto_import_scope: fastn_unresolved::SFId, arena: fastn_unresolved::Arena, ) -> Result { diff --git a/v0.5/fastn-unresolved/src/debug.rs b/v0.5/fastn-unresolved/src/debug.rs index f0d42426f..bb64673c4 100644 --- a/v0.5/fastn-unresolved/src/debug.rs +++ b/v0.5/fastn-unresolved/src/debug.rs @@ -112,6 +112,7 @@ impl fastn_unresolve crate::UR::UnResolved(u) => u.idebug(arena), crate::UR::NotFound => unimplemented!(), crate::UR::Invalid(_) => unimplemented!(), + crate::UR::InvalidN(_) => unimplemented!(), } } } diff --git a/v0.5/fastn-unresolved/src/lib.rs b/v0.5/fastn-unresolved/src/lib.rs index 86d854324..8cf9d7f72 100644 --- a/v0.5/fastn-unresolved/src/lib.rs +++ b/v0.5/fastn-unresolved/src/lib.rs @@ -13,6 +13,7 @@ mod utils; #[cfg(test)] pub(crate) use debug::JIDebug; pub use parser::parse; +use std::num::NonZeroU16; pub type URD = fastn_unresolved::UR; pub type URCI = fastn_unresolved::UR< @@ -34,9 +35,9 @@ pub struct Symbol { /// this store the /# of the symbol interned: string_interner::DefaultSymbol, // u32 /// length of the part of the symbol - package_len: u16, + package_len: NonZeroU16, /// length of the part of the symbol - module_len: u16, + module_len: Option, } #[derive(Debug, Clone, Hash, PartialEq, Eq)] @@ -45,7 +46,7 @@ pub struct Module { /// this store the /# of the symbol interned: string_interner::DefaultSymbol, // u32 /// length of the part of the symbol - package_len: u16, + package_len: NonZeroU16, } #[derive(Debug, Clone)] diff --git a/v0.5/fastn-unresolved/src/parser/import.rs b/v0.5/fastn-unresolved/src/parser/import.rs index 7644ecf27..6abfd5e41 100644 --- a/v0.5/fastn-unresolved/src/parser/import.rs +++ b/v0.5/fastn-unresolved/src/parser/import.rs @@ -68,8 +68,8 @@ fn parse_import( Some(Import { module: fastn_unresolved::Module::new( - caption.inner_str(module).str(), caption.inner_str(package).str(), + Some(caption.inner_str(module).str()), arena, ), alias: alias.map(|v| fastn_section::Identifier { diff --git a/v0.5/fastn-unresolved/src/parser/mod.rs b/v0.5/fastn-unresolved/src/parser/mod.rs index 7a72b849b..b3e0a3e15 100644 --- a/v0.5/fastn-unresolved/src/parser/mod.rs +++ b/v0.5/fastn-unresolved/src/parser/mod.rs @@ -67,7 +67,7 @@ where println!("--------- testing -----------\n{source}\n--------- source ------------"); let mut arena = fastn_unresolved::Arena::default(); - let module = fastn_unresolved::Module::new("main", "", &mut arena); + let module = fastn_unresolved::Module::new("main", None, &mut arena); let (mut document, sections) = fastn_unresolved::Document::new( module, diff --git a/v0.5/fastn-unresolved/src/resolver/symbol.rs b/v0.5/fastn-unresolved/src/resolver/symbol.rs index 58ce9f6ff..a27783016 100644 --- a/v0.5/fastn-unresolved/src/resolver/symbol.rs +++ b/v0.5/fastn-unresolved/src/resolver/symbol.rs @@ -36,7 +36,7 @@ pub fn resolve( // if it is not in unresolved-state, or if it is missing in definitions, we add "bar#x" // to output.stuck_on. // if it is in error state, or not found state, we resolve ourselves as them. - fastn_unresolved::Symbol::new(package.str(), module.str(), name.str(), arena) + fastn_unresolved::Symbol::new(package.str(), Some(module.str()), name.str(), arena) } fastn_section::IdentifierReference::Imported { module, @@ -50,7 +50,7 @@ pub fn resolve( // module from the module alias. // we combine the target module with the name, "x" to get the target symbol. match definitions.get(target_symbol.str(arena)) { - Some(fastn_unresolved::UR::Resolved(fastn_resolved::Definition::ModuleAlias { + Some(fastn_unresolved::UR::Resolved(fastn_resolved::Definition::Module { package, module, .. @@ -58,7 +58,7 @@ pub fn resolve( // what we stored in resolved place, no further action is required. *name = fastn_unresolved::UR::Resolved(fastn_unresolved::Symbol::new( package, - module, + module.as_deref(), dotted_name.str(), arena, )); diff --git a/v0.5/fastn-unresolved/src/utils.rs b/v0.5/fastn-unresolved/src/utils.rs index 7bb226025..737f366d2 100644 --- a/v0.5/fastn-unresolved/src/utils.rs +++ b/v0.5/fastn-unresolved/src/utils.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroU16; + impl fastn_unresolved::Document { pub(crate) fn new( module: fastn_unresolved::Module, @@ -47,6 +49,7 @@ impl fastn_unresolved::Definition { fastn_unresolved::UR::Resolved(ref r) => r.str(), fastn_unresolved::UR::NotFound => unreachable!(), fastn_unresolved::UR::Invalid(_) => unreachable!(), + fastn_unresolved::UR::InvalidN(_) => unreachable!(), } } @@ -136,27 +139,25 @@ impl fastn_unresolved::UR { impl fastn_unresolved::Symbol { pub fn new( package: &str, - module: &str, + module: Option<&str>, name: &str, arena: &mut fastn_unresolved::Arena, ) -> fastn_unresolved::Symbol { - let v = if module.is_empty() { - format!("{package}#{name}") - } else { - format!("{package}/{module}#{name}") + let v = match module { + Some(module) => format!("{package}/{module}#{name}"), + None => format!("{package}#{name}"), }; fastn_unresolved::Symbol { - package_len: package.len() as u16, - module_len: module.len() as u16, + package_len: NonZeroU16::new(package.len() as u16).unwrap(), + module_len: module.map(|v| NonZeroU16::new(v.len() as u16).unwrap()), interned: arena.interner.get_or_intern(v), } } pub fn parent(&self, arena: &mut fastn_unresolved::Arena) -> fastn_unresolved::Module { - let v = if self.module_len == 0 { - self.package(arena).to_string() - } else { - format!("{}/{}", self.package(arena), self.module(arena)) + let v = match self.module_len { + None => format!("{}/{}", self.package(arena), self.module(arena).unwrap()), + Some(_) => self.package(arena).to_string(), }; fastn_unresolved::Module { package_len: self.package_len, @@ -173,32 +174,36 @@ impl fastn_unresolved::Symbol { } pub fn package<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str { - &self.str(arena)[..self.package_len as usize] + &self.str(arena)[..self.package_len.get() as usize] } - pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str { - &self.str(arena)[self.package_len as usize + 1 - ..self.package_len as usize + 1 + self.module_len as usize] + pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> Option<&'a str> { + self.module_len.map(|module_len| { + &self.str(arena)[self.package_len.get() as usize + 1 + ..self.package_len.get() as usize + 1 + module_len.get() as usize] + }) } pub fn name<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str { - &self.str(arena)[self.package_len as usize + 1 + self.module_len as usize + 1..] + &self.str(arena)[self.package_len.get() as usize + + 1 + + self.module_len.map(|v| v.get()).unwrap_or_default() as usize + + 1..] } } impl fastn_unresolved::Module { pub fn new( package: &str, - module: &str, + module: Option<&str>, arena: &mut fastn_unresolved::Arena, ) -> fastn_unresolved::Module { - let v = if module.is_empty() { - package.to_string() - } else { - format!("{package}/{module}") + let v = match module { + None => package.to_string(), + Some(module) => format!("{package}/{module}"), }; fastn_unresolved::Module { - package_len: package.len() as u16, + package_len: NonZeroU16::new(package.len() as u16).unwrap(), interned: arena.interner.get_or_intern(v), } } @@ -208,11 +213,11 @@ impl fastn_unresolved::Module { } pub fn package<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str { - &self.str(arena)[..self.package_len as usize] + &self.str(arena)[..self.package_len.get() as usize] } pub fn module<'a>(&self, arena: &'a fastn_unresolved::Arena) -> &'a str { - &self.str(arena)[self.package_len as usize + 1..] + &self.str(arena)[self.package_len.get() as usize + 1..] } pub fn symbol( @@ -220,9 +225,16 @@ impl fastn_unresolved::Module { name: &str, arena: &mut fastn_unresolved::Arena, ) -> fastn_unresolved::Symbol { - let module_len = - arena.interner.resolve(self.interned).unwrap().len() as u16 - self.package_len; - let v = if module_len == 0 { + let module_len = { + let len = arena.interner.resolve(self.interned).unwrap().len() as u16 + - self.package_len.get(); + if len > 0 { + Some(NonZeroU16::new(len).unwrap()) + } else { + None + } + }; + let v = if module_len.is_none() { format!("{}#{name}", self.package(arena)) } else { format!("{}/{}#{name}", self.package(arena), self.module(arena)) diff --git a/v0.5/fastn/src/commands/render.rs b/v0.5/fastn/src/commands/render.rs index 3b615c5f1..1ba446181 100644 --- a/v0.5/fastn/src/commands/render.rs +++ b/v0.5/fastn/src/commands/render.rs @@ -20,7 +20,7 @@ async fn render_document( let source = std::fs::File::open(path) .and_then(std::io::read_to_string) .unwrap(); - let o = fastn_compiler::compile(Box::new(fastn::Symbols {}), &source, "main", "", arena) + let o = fastn_compiler::compile(Box::new(fastn::Symbols {}), &source, "main", None, arena) .await .unwrap(); let h = fastn_runtime::HtmlData::from_cd(o); diff --git a/v0.5/fastn/src/symbols.rs b/v0.5/fastn/src/symbols.rs index d0e987510..9cb17365a 100644 --- a/v0.5/fastn/src/symbols.rs +++ b/v0.5/fastn/src/symbols.rs @@ -58,7 +58,11 @@ fn file_for_symbol( arena: &mut fastn_unresolved::Arena, ) -> (String, fastn_unresolved::Module) { ( - format!("{}/{}.ftd", symbol.package(arena), symbol.module(arena)), + // this code is non sense right now + match symbol.module(arena) { + Some(module) => format!("{}/{}.ftd", symbol.package(arena), module), + None => format!("{}/index.ftd", symbol.package(arena)), + }, symbol.parent(arena), ) }