[fine] export, dump source map, lookup fix

This commit is contained in:
John Doty 2024-02-15 06:29:56 -08:00
parent a3ae4339cf
commit a21f18da6e
4 changed files with 65 additions and 16 deletions

View file

@ -41,17 +41,10 @@ impl ModuleLoader for StandardModuleLoader {
let result = match std::fs::canonicalize(&p) {
Ok(p) => match p.into_os_string().into_string() {
Ok(s) => s,
Err(_e) => {
eprintln!("ERROR INTO OS STRING: {}", _e.to_string_lossy());
name.clone()
}
Err(_e) => name.clone(),
},
Err(_e) => {
eprintln!("ERROR CANONICAL {}: {_e}", p.display());
name.clone()
}
Err(_e) => name.clone(),
};
eprintln!("**** {source} {name} => {result}");
result
}

View file

@ -167,6 +167,8 @@ pub enum TreeKind {
WildcardPattern,
Import,
Export,
ExportList,
}
pub struct Tree {
@ -576,6 +578,7 @@ fn file(p: &mut CParser) {
match p.peek() {
TokenKind::Import => import(p),
TokenKind::Class => class(p),
TokenKind::Export => export(p),
TokenKind::RightBrace => {
// An error parsing mismatched braces can leave me at an
// un-balanced right brace, which unfortunately will not be
@ -636,6 +639,36 @@ fn import(p: &mut CParser) {
p.end(m, TreeKind::Import);
}
fn export(p: &mut CParser) {
let m = p.start();
p.expect_start(TokenKind::Export);
match p.peek() {
TokenKind::Fun => function(p),
TokenKind::Let => statement_let(p),
TokenKind::Identifier => export_list(p),
TokenKind::Semicolon => (),
_ => p.error("expected something to export"),
}
p.end(m, TreeKind::Export);
}
fn export_list(p: &mut CParser) {
let m = p.start();
p.expect_start(TokenKind::Identifier);
while p.eat(TokenKind::Comma) {
p.expect(
TokenKind::Identifier,
"expected an identifier to export in this export list",
);
}
p.expect(TokenKind::Semicolon, "expected a ; to end the export list");
p.end(m, TreeKind::ExportList);
}
fn class(p: &mut CParser) {
let m = p.start();

View file

@ -163,7 +163,7 @@ pub enum Type {
Alternate(Box<[Type]>),
// A module of some kind. What module?
Module(ImportRecord),
Module(Rc<str>, ImportRecord),
}
impl Type {
@ -258,7 +258,7 @@ impl fmt::Display for Type {
}
Ok(())
}
Module(name) => write!(f, "module {}", name.name),
Module(name, _) => write!(f, "module {}", name),
}
}
}
@ -2245,9 +2245,9 @@ impl Semantics {
self.internal_compiler_error(None, "import map not initialized");
};
let name = tok.as_str(&self.source);
match import_map.get(name) {
Some(import) => Some(Type::Module(import.clone())),
let name = string_constant_to_string(tok.as_str(&self.source));
match import_map.get(&name) {
Some(import) => Some(Type::Module(name.into(), import.clone())),
None => {
self.report_error_tree(tree, format!("unable to resolve module import {name}"));
Some(Type::Error)
@ -2405,6 +2405,21 @@ impl Semantics {
}
}
{
match self.import_map.get() {
Some(m) => {
eprintln!("Import map:");
for (k, b) in m.iter() {
eprintln!(" {k} => {} ({})", b.name, b.module_id);
}
eprintln!();
}
None => {
eprintln!("The import map is not set.\n")
}
}
}
if let Some(tr) = tr {
eprintln!("This is about the tree: {:?}", &self.syntax_tree[tr]);
eprintln!("The logical parent chain of the tree was:\n");
@ -2529,6 +2544,9 @@ pub fn check(s: &Semantics) {
TreeKind::Import => {
let _ = s.type_of(t);
}
TreeKind::Export => {}
TreeKind::ExportList => {}
}
}
}

View file

@ -42,6 +42,7 @@ pub enum TokenKind {
Await,
Class,
Else,
Export,
False,
For,
From,
@ -294,6 +295,9 @@ impl<'a> Tokens<'a> {
if ident == "else" {
return TokenKind::Else;
}
if ident == "export" {
return TokenKind::Export;
}
}
'f' => {
if ident == "false" {
@ -639,12 +643,13 @@ mod tests {
test_tokens!(
more_more_keywords,
"in is match _ as",
"in is match _ as export",
(0, In, "in"),
(3, Is, "is"),
(6, Match, "match"),
(12, Underscore, "_"),
(14, As, "as")
(14, As, "as"),
(17, Export, "export")
);
test_tokens!(