mod registry;
use std::borrow::Cow;
use std::fmt;
use serde::{Deserialize, Serialize};
pub use registry::LanguageRegistry;
use crate::facts::Facts;
use crate::highlight::{Highlight, HighlightRule};
use crate::import::{ImportGrammar, ImportRef};
use crate::paths::PathSyntax;
use crate::query::Query;
use crate::resolve::{Layout, Surgery};
use crate::search::{RawMatch, SearchError};
use crate::semantics::Semantics;
use crate::symbol::{Symbol, SymbolRule};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(transparent)]
pub struct LanguageId(Cow<'static, str>);
impl LanguageId {
pub const fn new(id: &'static str) -> Self {
Self(Cow::Borrowed(id))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for LanguageId {
fn from(id: String) -> Self {
Self(Cow::Owned(id))
}
}
impl From<&str> for LanguageId {
fn from(id: &str) -> Self {
Self(Cow::Owned(id.to_owned()))
}
}
impl fmt::Display for LanguageId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Debug for LanguageId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "LanguageId({})", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Grammar {
pub import_scopes: &'static [&'static str],
pub module_scopes: Option<crate::ModuleScopeRule>,
pub macro_scopes: Option<crate::MacroScopeRule>,
pub binding_markers: &'static [&'static str],
pub navigation_values: &'static [&'static str],
pub calls: &'static [crate::CallRule],
pub callees: &'static [crate::CalleeRule],
pub anonymous_callables: &'static [&'static str],
pub pattern_constructors: &'static [(&'static str, &'static str)],
pub pattern_containers: &'static [&'static str],
pub qualified_imports: &'static [crate::QualifiedImportRule],
pub bindings: &'static [crate::BindingRule],
pub lexical_qualified: &'static [&'static str],
pub lexical_boundaries: &'static [&'static str],
pub lexical_containers: &'static [&'static str],
pub lexical_barriers: &'static [&'static str],
pub named_imports: &'static [crate::NamedImportRule],
pub named_modules: bool,
pub non_named_exports: &'static [&'static str],
pub symbols: &'static [SymbolRule],
pub signatures: &'static [crate::SignatureRule],
pub identifiers: &'static [&'static str],
pub imports: ImportGrammar,
pub highlights: &'static [HighlightRule],
pub navigation_types: &'static [&'static str],
pub navigation_barriers: &'static [&'static str],
pub navigation_bindings: &'static [&'static str],
}
impl Grammar {
pub const EMPTY: Self = Self {
import_scopes: &[],
module_scopes: None,
macro_scopes: None,
binding_markers: &[],
navigation_values: &[],
calls: &[],
callees: &[],
anonymous_callables: &[],
pattern_constructors: &[],
pattern_containers: &[],
qualified_imports: &[],
bindings: &[],
lexical_qualified: &[],
lexical_boundaries: &[],
lexical_containers: &[],
lexical_barriers: &[],
named_imports: &[],
named_modules: false,
non_named_exports: &[],
symbols: &[],
signatures: &[],
identifiers: &[],
imports: ImportGrammar::EMPTY,
highlights: &[],
navigation_types: &[],
navigation_barriers: &[],
navigation_bindings: &[],
};
}
pub trait Language: Send + Sync {
fn id(&self) -> LanguageId;
fn reference_group(&self) -> LanguageId {
self.id()
}
fn extensions(&self) -> &'static [&'static str];
fn semantics(&self) -> &'static Semantics;
fn paths(&self) -> PathSyntax;
fn glob_marker(&self) -> Option<&'static str> {
None
}
fn facts(&self, source: &str) -> Result<Facts, SearchError>;
fn accepts(&self, query: &Query) -> Result<(), SearchError> {
let _ = query;
Ok(())
}
fn find(&self, source: &str, query: &Query) -> Result<Vec<RawMatch>, SearchError>;
fn symbols(&self, source: &str) -> Result<Vec<Symbol>, SearchError> {
let _ = source;
Ok(Vec::new())
}
fn references(&self, source: &str, name: &str) -> Result<Vec<RawMatch>, SearchError> {
let _ = (source, name);
Ok(Vec::new())
}
fn imports(&self, source: &str) -> Result<Vec<ImportRef>, SearchError> {
let _ = source;
Ok(Vec::new())
}
fn highlights(&self, source: &str) -> Result<Vec<Highlight>, SearchError> {
let _ = source;
Ok(Vec::new())
}
fn layout(&self) -> Option<&dyn Layout> {
None
}
fn surgery(&self) -> Option<&dyn Surgery> {
None
}
}