1mod registry;
10
11use std::borrow::Cow;
12use std::fmt;
13
14use serde::{Deserialize, Serialize};
15
16pub use registry::LanguageRegistry;
17
18use crate::facts::Facts;
19use crate::highlight::{Highlight, HighlightRule};
20use crate::import::{ImportGrammar, ImportRef};
21use crate::paths::PathSyntax;
22use crate::query::Query;
23use crate::resolve::{Layout, Surgery};
24use crate::search::{RawMatch, SearchError};
25use crate::semantics::Semantics;
26use crate::symbol::{Symbol, SymbolRule};
27
28#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
30#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
31#[serde(transparent)]
32pub struct LanguageId(Cow<'static, str>);
33
34impl LanguageId {
35 pub const fn new(id: &'static str) -> Self {
36 Self(Cow::Borrowed(id))
37 }
38
39 pub fn as_str(&self) -> &str {
40 &self.0
41 }
42}
43
44impl From<String> for LanguageId {
45 fn from(id: String) -> Self {
46 Self(Cow::Owned(id))
47 }
48}
49
50impl From<&str> for LanguageId {
51 fn from(id: &str) -> Self {
52 Self(Cow::Owned(id.to_owned()))
53 }
54}
55
56impl fmt::Display for LanguageId {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 f.write_str(&self.0)
59 }
60}
61
62impl fmt::Debug for LanguageId {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "LanguageId({})", self.0)
65 }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct Grammar {
73 pub import_scopes: &'static [&'static str],
74 pub module_scopes: Option<crate::ModuleScopeRule>,
75 pub macro_scopes: Option<crate::MacroScopeRule>,
76 pub binding_markers: &'static [&'static str],
77 pub navigation_values: &'static [&'static str],
78 pub calls: &'static [crate::CallRule],
79 pub callees: &'static [crate::CalleeRule],
80 pub anonymous_callables: &'static [&'static str],
81 pub pattern_constructors: &'static [(&'static str, &'static str)],
83 pub pattern_containers: &'static [&'static str],
84 pub qualified_imports: &'static [crate::QualifiedImportRule],
85 pub bindings: &'static [crate::BindingRule],
86 pub lexical_qualified: &'static [&'static str],
87 pub lexical_boundaries: &'static [&'static str],
88 pub lexical_containers: &'static [&'static str],
89 pub lexical_barriers: &'static [&'static str],
90 pub named_imports: &'static [crate::NamedImportRule],
91 pub named_modules: bool,
92 pub non_named_exports: &'static [&'static str],
94 pub symbols: &'static [SymbolRule],
95 pub signatures: &'static [crate::SignatureRule],
96 pub identifiers: &'static [&'static str],
97 pub imports: ImportGrammar,
98 pub highlights: &'static [HighlightRule],
99 pub navigation_types: &'static [&'static str],
101 pub navigation_barriers: &'static [&'static str],
103 pub navigation_bindings: &'static [&'static str],
105}
106
107impl Grammar {
108 pub const EMPTY: Self = Self {
109 import_scopes: &[],
110 module_scopes: None,
111 macro_scopes: None,
112 binding_markers: &[],
113 navigation_values: &[],
114 calls: &[],
115 callees: &[],
116 anonymous_callables: &[],
117 pattern_constructors: &[],
118 pattern_containers: &[],
119 qualified_imports: &[],
120 bindings: &[],
121 lexical_qualified: &[],
122 lexical_boundaries: &[],
123 lexical_containers: &[],
124 lexical_barriers: &[],
125 named_imports: &[],
126 named_modules: false,
127 non_named_exports: &[],
128 symbols: &[],
129 signatures: &[],
130 identifiers: &[],
131 imports: ImportGrammar::EMPTY,
132 highlights: &[],
133 navigation_types: &[],
134 navigation_barriers: &[],
135 navigation_bindings: &[],
136 };
137}
138
139pub trait Language: Send + Sync {
140 fn id(&self) -> LanguageId;
141 fn reference_group(&self) -> LanguageId {
144 self.id()
145 }
146
147 fn extensions(&self) -> &'static [&'static str];
149
150 fn semantics(&self) -> &'static Semantics;
152
153 fn paths(&self) -> PathSyntax;
156
157 fn glob_marker(&self) -> Option<&'static str> {
161 None
162 }
163
164 fn facts(&self, source: &str) -> Result<Facts, SearchError>;
166
167 fn accepts(&self, query: &Query) -> Result<(), SearchError> {
171 let _ = query;
172 Ok(())
173 }
174
175 fn find(&self, source: &str, query: &Query) -> Result<Vec<RawMatch>, SearchError>;
177
178 fn symbols(&self, source: &str) -> Result<Vec<Symbol>, SearchError> {
180 let _ = source;
181 Ok(Vec::new())
182 }
183
184 fn references(&self, source: &str, name: &str) -> Result<Vec<RawMatch>, SearchError> {
186 let _ = (source, name);
187 Ok(Vec::new())
188 }
189
190 fn imports(&self, source: &str) -> Result<Vec<ImportRef>, SearchError> {
192 let _ = source;
193 Ok(Vec::new())
194 }
195
196 fn highlights(&self, source: &str) -> Result<Vec<Highlight>, SearchError> {
198 let _ = source;
199 Ok(Vec::new())
200 }
201
202 fn layout(&self) -> Option<&dyn Layout> {
204 None
205 }
206
207 fn surgery(&self) -> Option<&dyn Surgery> {
209 None
210 }
211}