use crate::{ImportBinding, ModulePath, Name, Span, Symbol, SymbolKind};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum BindingNamespace {
Type,
Value,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct LexicalBinding {
pub symbol: Symbol,
pub scope: Span,
pub excluded: Vec<Span>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub uninitialized: Vec<Span>,
pub visible_from: usize,
pub namespace: BindingNamespace,
#[serde(default)]
pub explicit: bool,
}
impl LexicalBinding {
pub fn initialized(&self, span: Span) -> bool {
!self
.uninitialized
.iter()
.any(|region| region.contains(&span))
}
pub fn visible(&self, name: &str, span: Span, namespace: BindingNamespace) -> bool {
self.symbol.name == name
&& self.namespace == namespace
&& self.scope.contains(&span)
&& self.visible_from <= span.start
&& !self.excluded.iter().any(|s| s.contains(&span))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct NamedImport {
pub local: String,
pub imported: String,
pub module: Option<ModulePath>,
pub name_span: Span,
pub alias_span: Span,
pub reexport: bool,
pub type_only: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BindingRule {
pub node: &'static str,
pub name: Option<&'static str>,
pub scopes: &'static [&'static str],
pub kind: SymbolKind,
pub namespace: BindingNamespace,
pub after: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NamedImportRule {
pub name: Option<&'static str>,
pub imported: Option<&'static str>,
pub node: &'static str,
pub statement: &'static str,
pub reexport: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct QualifiedImport {
pub span: Span,
pub binding: String,
pub member: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QualifiedImportRule {
pub node: &'static str,
pub object: &'static str,
pub member: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ModuleDeclaration {
pub name_span: Span,
pub restriction: Option<ModulePath>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ModuleScope {
pub path: Vec<Name>,
pub span: Span,
pub declaration: Option<Span>,
pub declarations: Vec<ModuleDeclaration>,
pub imports: Vec<ImportBinding>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ModuleScopeRule {
pub node: &'static str,
pub name: &'static str,
pub body: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ImportScope {
pub span: Span,
pub imports: Vec<ImportBinding>,
#[serde(default)]
pub aliases: Vec<Span>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ScopeUncertainty {
pub scope: Span,
pub invocation: Span,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MacroScopeRule {
pub node: &'static str,
pub scopes: &'static [&'static str],
pub expression_containers: &'static [&'static str],
pub expression_fields: &'static [(&'static str, &'static str)],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PatternRole {
Identifier,
Constant,
Constructor,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum ConstructorShape {
Unit,
Tuple,
Record,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PatternConstructor {
pub name_span: Span,
pub owner: Option<Span>,
pub shape: ConstructorShape,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PatternReference {
pub span: Span,
pub path: ModulePath,
pub role: PatternRole,
pub shape: Option<ConstructorShape>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PatternBinding {
pub name: String,
pub span: Span,
pub by_ref: bool,
pub mutable: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PatternAlternatives {
pub branches: Vec<Vec<PatternBinding>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PatternScope {
pub span: Span,
pub scope: Span,
pub excluded: Vec<Span>,
pub visible_from: usize,
pub references: Vec<PatternReference>,
pub alternatives: Vec<PatternAlternatives>,
}
impl PatternScope {
pub fn contains(&self, span: Span) -> bool {
self.span.contains(&span)
|| (self.scope.contains(&span)
&& self.visible_from <= span.start
&& !self
.excluded
.iter()
.any(|excluded| excluded.contains(&span)))
}
}
#[cfg(test)]
mod initialization_tests {
use super::*;
#[test]
fn initialization_regions_preserve_binding_ownership() {
let mut binding = LexicalBinding {
symbol: Symbol::plain(SymbolKind::Variable, "x", Span::new(5, 6), Span::new(5, 9)),
scope: Span::new(0, 20),
excluded: vec![],
uninitialized: vec![],
visible_from: 0,
namespace: BindingNamespace::Value,
explicit: true,
};
assert!(binding.initialized(Span::new(2, 3)));
binding.uninitialized.push(Span::new(0, 5));
assert!(binding.visible("x", Span::new(2, 3), BindingNamespace::Value));
assert!(!binding.initialized(Span::new(2, 3)));
assert!(binding.initialized(Span::new(10, 11)));
assert_eq!(
serde_json::from_value::<LexicalBinding>(serde_json::to_value(&binding).unwrap())
.unwrap(),
binding
);
}
}