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)]
#[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 symbols: &'static [SymbolRule],
pub identifiers: &'static [&'static str],
pub imports: ImportGrammar,
pub highlights: &'static [HighlightRule],
}
impl Grammar {
pub const EMPTY: Self = Self {
symbols: &[],
identifiers: &[],
imports: ImportGrammar::EMPTY,
highlights: &[],
};
}
pub trait Language: Send + Sync {
fn id(&self) -> LanguageId;
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
}
}