|
| 1 | +/// Loosely based on TypeScript's binder |
| 2 | +/// see: typescript-go/internal/binder/binder.go |
| 3 | +use la_arena::Arena; |
| 4 | +use squawk_syntax::{SyntaxNodePtr, ast, ast::AstNode}; |
| 5 | + |
| 6 | +use crate::scope::{Scope, ScopeId}; |
| 7 | +use crate::symbols::{Name, Schema, Symbol, SymbolKind}; |
| 8 | + |
| 9 | +pub(crate) struct Binder { |
| 10 | + pub(crate) scopes: Arena<Scope>, |
| 11 | + pub(crate) symbols: Arena<Symbol>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Binder { |
| 15 | + fn new() -> Self { |
| 16 | + let mut scopes = Arena::new(); |
| 17 | + let _root_scope = scopes.alloc(Scope::with_parent(None)); |
| 18 | + Binder { |
| 19 | + scopes, |
| 20 | + symbols: Arena::new(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub(crate) fn root_scope(&self) -> ScopeId { |
| 25 | + self.scopes |
| 26 | + .iter() |
| 27 | + .next() |
| 28 | + .map(|(id, _)| id) |
| 29 | + .expect("root scope must exist") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub(crate) fn bind(file: &ast::SourceFile) -> Binder { |
| 34 | + let mut binder = Binder::new(); |
| 35 | + |
| 36 | + bind_file(&mut binder, file); |
| 37 | + |
| 38 | + binder |
| 39 | +} |
| 40 | + |
| 41 | +fn bind_file(b: &mut Binder, file: &ast::SourceFile) { |
| 42 | + for stmt in file.stmts() { |
| 43 | + bind_stmt(b, stmt); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) { |
| 48 | + if let ast::Stmt::CreateTable(create_table) = stmt { |
| 49 | + bind_create_table(b, create_table) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn bind_create_table(b: &mut Binder, create_table: ast::CreateTable) { |
| 54 | + let Some(path) = create_table.path() else { |
| 55 | + return; |
| 56 | + }; |
| 57 | + let Some(table_name) = item_name(&path) else { |
| 58 | + return; |
| 59 | + }; |
| 60 | + let name_ptr = path_to_ptr(&path); |
| 61 | + let schema = schema_name(&path); |
| 62 | + |
| 63 | + let table_id = b.symbols.alloc(Symbol { |
| 64 | + kind: SymbolKind::Table, |
| 65 | + ptr: name_ptr, |
| 66 | + schema, |
| 67 | + }); |
| 68 | + |
| 69 | + let root = b.root_scope(); |
| 70 | + b.scopes[root].insert(table_name, table_id); |
| 71 | +} |
| 72 | + |
| 73 | +fn item_name(path: &ast::Path) -> Option<Name> { |
| 74 | + let segment = path.segment()?; |
| 75 | + |
| 76 | + if let Some(name) = segment.name() { |
| 77 | + return Some(Name::new(name.syntax().text().to_string())); |
| 78 | + } |
| 79 | + if let Some(name) = segment.name_ref() { |
| 80 | + return Some(Name::new(name.syntax().text().to_string())); |
| 81 | + } |
| 82 | + |
| 83 | + None |
| 84 | +} |
| 85 | + |
| 86 | +fn path_to_ptr(path: &ast::Path) -> SyntaxNodePtr { |
| 87 | + if let Some(segment) = path.segment() { |
| 88 | + if let Some(name) = segment.name() { |
| 89 | + return SyntaxNodePtr::new(name.syntax()); |
| 90 | + } |
| 91 | + if let Some(name_ref) = segment.name_ref() { |
| 92 | + return SyntaxNodePtr::new(name_ref.syntax()); |
| 93 | + } |
| 94 | + } |
| 95 | + SyntaxNodePtr::new(path.syntax()) |
| 96 | +} |
| 97 | + |
| 98 | +fn schema_name(path: &ast::Path) -> Schema { |
| 99 | + let Some(qualifier) = path.qualifier() else { |
| 100 | + return Schema::Public; |
| 101 | + }; |
| 102 | + let Some(segment) = qualifier.segment() else { |
| 103 | + return Schema::Public; |
| 104 | + }; |
| 105 | + |
| 106 | + let schema_name = if let Some(name) = segment.name() { |
| 107 | + Name::new(name.syntax().text().to_string()) |
| 108 | + } else if let Some(name_ref) = segment.name_ref() { |
| 109 | + Name::new(name_ref.syntax().text().to_string()) |
| 110 | + } else { |
| 111 | + return Schema::Public; |
| 112 | + }; |
| 113 | + |
| 114 | + Schema::from_name(schema_name) |
| 115 | +} |
0 commit comments