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#[serde(transparent)]
31pub struct LanguageId(Cow<'static, str>);
32
33impl LanguageId {
34 pub const fn new(id: &'static str) -> Self {
35 Self(Cow::Borrowed(id))
36 }
37
38 pub fn as_str(&self) -> &str {
39 &self.0
40 }
41}
42
43impl From<String> for LanguageId {
44 fn from(id: String) -> Self {
45 Self(Cow::Owned(id))
46 }
47}
48
49impl From<&str> for LanguageId {
50 fn from(id: &str) -> Self {
51 Self(Cow::Owned(id.to_owned()))
52 }
53}
54
55impl fmt::Display for LanguageId {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 f.write_str(&self.0)
58 }
59}
60
61impl fmt::Debug for LanguageId {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(f, "LanguageId({})", self.0)
64 }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub struct Grammar {
72 pub symbols: &'static [SymbolRule],
73 pub identifiers: &'static [&'static str],
74 pub imports: ImportGrammar,
75 pub highlights: &'static [HighlightRule],
76}
77
78impl Grammar {
79 pub const EMPTY: Self = Self {
80 symbols: &[],
81 identifiers: &[],
82 imports: ImportGrammar::EMPTY,
83 highlights: &[],
84 };
85}
86
87pub trait Language: Send + Sync {
88 fn id(&self) -> LanguageId;
89
90 fn extensions(&self) -> &'static [&'static str];
92
93 fn semantics(&self) -> &'static Semantics;
95
96 fn paths(&self) -> PathSyntax;
99
100 fn glob_marker(&self) -> Option<&'static str> {
104 None
105 }
106
107 fn facts(&self, source: &str) -> Result<Facts, SearchError>;
109
110 fn accepts(&self, query: &Query) -> Result<(), SearchError> {
114 let _ = query;
115 Ok(())
116 }
117
118 fn find(&self, source: &str, query: &Query) -> Result<Vec<RawMatch>, SearchError>;
120
121 fn symbols(&self, source: &str) -> Result<Vec<Symbol>, SearchError> {
123 let _ = source;
124 Ok(Vec::new())
125 }
126
127 fn references(&self, source: &str, name: &str) -> Result<Vec<RawMatch>, SearchError> {
129 let _ = (source, name);
130 Ok(Vec::new())
131 }
132
133 fn imports(&self, source: &str) -> Result<Vec<ImportRef>, SearchError> {
135 let _ = source;
136 Ok(Vec::new())
137 }
138
139 fn highlights(&self, source: &str) -> Result<Vec<Highlight>, SearchError> {
141 let _ = source;
142 Ok(Vec::new())
143 }
144
145 fn layout(&self) -> Option<&dyn Layout> {
147 None
148 }
149
150 fn surgery(&self) -> Option<&dyn Surgery> {
152 None
153 }
154}