symbolique 0.1.0

Symbol table pipeline for language servers — parse, link, merge, and resolve symbols across files, built on the laburnum LSP framework.
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Linked-level symbol partitions.
//!
//! Stage 2 of the symbolique pipeline. Linking adds import bindings to a
//! file's symbols:
//!
//! - **LinkedSymbols**: Index-only partition with `SymbolEntry` index entries
//!   pointing to shapes in the shared `Symbols` partition
//!
//! Imports are file-scoped: each file has its own set of imports.

use {
  super::records::SymbolEntry,
  crate::core::{Ident, SymbolPath, SymbolSortKey, Value},
  laburnum::{
    Ident as LaburnumIdent,
    database::{Partition, PartitionKey},
  },
  std::{hash::Hash, marker::PhantomData},
};

/// Linked-level symbol index partition (index-only).
///
/// Stores only index entries that reference shapes in the shared `Symbols`
/// partition. Each entry contains a span and a handle to the symbol shape.
#[derive(Hash)]
pub struct LinkedSymbols<V, I, P>(PhantomData<(V, I, P)>);

impl<V, I, P> Default for LinkedSymbols<V, I, P> {
  fn default() -> Self {
    Self(PhantomData)
  }
}

impl<V, I, P> Clone for LinkedSymbols<V, I, P> {
  fn clone(&self) -> Self {
    *self
  }
}

impl<V, I, P> Copy for LinkedSymbols<V, I, P> {}

impl<V, I, P> std::fmt::Debug for LinkedSymbols<V, I, P> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("LinkedSymbols").finish()
  }
}

impl<V, I, P> PartitionKey for LinkedSymbols<V, I, P>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
{
  const KEY: LaburnumIdent = LaburnumIdent::new("symbolique::linked_symbols");
}

impl<V, I, P> Partition for LinkedSymbols<V, I, P>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
{
  type Record = (); // Index-only
  type IndexEntry = SymbolEntry<V, I, P>;
  type SortKey = SymbolSortKey;
}

/// Linked-level symbol index partition.
#[derive(Hash)]
pub struct LinkedSymbolIndex<V, I, P>(PhantomData<(V, I, P)>);

impl<V, I, P> Default for LinkedSymbolIndex<V, I, P> {
  fn default() -> Self {
    Self(PhantomData)
  }
}

impl<V, I, P> Clone for LinkedSymbolIndex<V, I, P> {
  fn clone(&self) -> Self {
    *self
  }
}

impl<V, I, P> Copy for LinkedSymbolIndex<V, I, P> {}

impl<V, I, P> std::fmt::Debug for LinkedSymbolIndex<V, I, P> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("LinkedSymbolIndex").finish()
  }
}

impl<V, I, P> PartitionKey for LinkedSymbolIndex<V, I, P>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
{
  const KEY: LaburnumIdent =
    LaburnumIdent::new("symbolique::linked_symbol_index");
}

impl<V, I, P> Partition for LinkedSymbolIndex<V, I, P>
where
  V: Value<I>,
  I: Ident,
  P: SymbolPath,
{
  type Record = (); // Index-only
  type IndexEntry = SymbolEntry<V, I, P>;
  type SortKey = SymbolSortKey;
}

#[cfg(test)]
mod tests {
  use super::*;

  #[derive(Debug, Clone, Hash, PartialEq, Eq)]
  struct TestPath(String);

  impl crate::core::SymbolPath for TestPath {
    fn sort_key(&self) -> String {
      self.0.clone()
    }
  }

  #[test]
  fn linked_symbols_partition_key() {
    use crate::core::{DefaultValue, StringIdent};

    let key = LinkedSymbols::<DefaultValue, StringIdent, TestPath>::KEY;
    let key2 = LinkedSymbolIndex::<DefaultValue, StringIdent, TestPath>::KEY;
    assert_ne!(key, key2);
  }

  #[test]
  fn linked_symbols_is_index_only() {
    use crate::core::{DefaultValue, StringIdent};

    fn assert_record_is_unit<P: Partition<Record = ()>>() {}

    assert_record_is_unit::<LinkedSymbols<DefaultValue, StringIdent, TestPath>>(
    );
  }
}