Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/syntax/node.rs

10.3 KiB, 1 run

created by r1870400018:63568, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// SPDX-License-Identifier: Apache-2.0
2// Original work copyright (c) the Typst project authors (typst-syntax `node.rs`, version 0.15.1).
3// Modified for Austenite: ported to Hematite's types, IR and error handling.
4// U1 owns this file. The public methods below are the contract U2 codes against; keep their names and
5// meanings, add freely.
6
7use crate::syntax::{
8 FileId,
9 SyntaxKind,
10 Span,
11};
12
13use std::sync::Arc;
14
15/// A node of the concrete syntax tree: a leaf holding source text, an inner node holding children, or an
16/// error holding the text it could not read and why. Cloning is a reference-count bump.
17///
18/// A node may also carry warnings (an empty `*strong*`, an ambiguous raw language tag), which never
19/// change its kind or text.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21pub struct SyntaxNode {
22 kind: SyntaxKind,
23 span: Span,
24 repr: Repr,
25 warns: Option<Arc<Vec<SyntaxWarning>>>,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq, Hash)]
29enum Repr {
30 Empty, // a leaf with no text, constructible in a `const`
31 Leaf(Arc<str>),
32 Inner(Arc<Inner>),
33 Error(Arc<SyntaxError>),
34}
35
36#[derive(Clone, Debug, PartialEq, Eq, Hash)]
37struct Inner {
38 children: Vec<SyntaxNode>,
39 len: usize, // byte length of the covered source
40 erroneous: bool, // some descendant is an error
41 warned: bool, // some descendant carries a warning
42}
43
44impl Inner {
45 fn new(children: Vec<SyntaxNode>) -> Self {
46 let mut len = 0;
47 let mut erroneous = false;
48 let mut warned = false;
49 for c in &children {
50 len += c.len();
51 erroneous |= c.erroneous();
52 warned |= c.has_warnings();
53 }
54 Self { children, len, erroneous, warned }
55 }
56}
57
58/// What an `Error` node records: the source text it swallowed, the message and any hints.
59#[derive(Clone, Debug, PartialEq, Eq, Hash)]
60pub struct SyntaxError {
61 pub text: String,
62 pub message: String,
63 pub hints: Vec<String>,
64}
65
66/// A warning attached to a node. `range` narrows it to a byte range relative to the node's start.
67#[derive(Clone, Debug, PartialEq, Eq, Hash)]
68pub struct SyntaxWarning {
69 pub message: String,
70 pub hints: Vec<String>,
71 pub range: Option<(u32, u32)>,
72 pub span_hints: Vec<(u32, u32, String)>, // hints pinned to a relative byte range
73}
74
75impl SyntaxNode {
76 pub fn leaf(kind: SyntaxKind, text: &str, span: Span) -> Self {
77 let repr = if text.is_empty() { Repr::Empty } else { Repr::Leaf(Arc::from(text)) };
78 Self { kind, span, repr, warns: None }
79 }
80
81 /// An inner node; its span covers its children's.
82 pub fn inner(kind: SyntaxKind, children: Vec<SyntaxNode>) -> Self {
83 let mut span = Span::detached();
84 for c in &children {
85 span = span.join(c.span);
86 }
87 Self { kind, span, repr: Repr::Inner(Arc::new(Inner::new(children))), warns: None }
88 }
89
90 pub fn error(text: &str, message: &str, span: Span) -> Self {
91 Self {
92 kind: SyntaxKind::Error,
93 span,
94 repr: Repr::Error(Arc::new(SyntaxError {
95 text: text.to_string(),
96 message: message.to_string(),
97 hints: Vec::new(),
98 })),
99 warns: None,
100 }
101 }
102
103 /// An empty, detached leaf of the given kind, which a typed view falls back to when the tree lacks
104 /// the child it expects.
105 pub const fn placeholder(kind: SyntaxKind) -> Self {
106 Self { kind, span: Span::detached(), repr: Repr::Empty, warns: None }
107 }
108
109 pub fn kind(&self) -> SyntaxKind { self.kind }
110 pub fn span(&self) -> Span { self.span }
111 pub fn file(&self) -> FileId { self.span.file }
112
113 /// The text of a leaf or an error node, empty for an inner node.
114 pub fn text(&self) -> &str {
115 match &self.repr {
116 Repr::Leaf(t) => t,
117 Repr::Error(e) => &e.text,
118 Repr::Inner(_) => "",
119 Repr::Empty => "",
120 }
121 }
122
123 /// The byte length of the source the node covers.
124 pub fn len(&self) -> usize {
125 match &self.repr {
126 Repr::Leaf(t) => t.len(),
127 Repr::Error(e) => e.text.len(),
128 Repr::Inner(i) => i.len,
129 Repr::Empty => 0,
130 }
131 }
132
133 pub fn is_empty(&self) -> bool { self.len() == 0 }
134
135 /// The children of an inner node, empty for a leaf or an error.
136 pub fn children(&self) -> &[SyntaxNode] {
137 match &self.repr {
138 Repr::Inner(i) => &i.children,
139 _ => &[],
140 }
141 }
142
143 pub fn is_leaf(&self) -> bool { matches!(self.repr, Repr::Leaf(_) | Repr::Empty) }
144
145 pub fn is_inner(&self) -> bool { matches!(self.repr, Repr::Inner(_)) }
146
147 /// The first child of the given kind.
148 pub fn child(&self, kind: SyntaxKind) -> Option<&SyntaxNode> {
149 self.children().iter().find(|c| c.kind == kind)
150 }
151
152 /// The children that are not trivia, in order.
153 pub fn significant(&self) -> impl Iterator<Item = &SyntaxNode> {
154 self.children().iter().filter(|c| !c.kind.is_trivia())
155 }
156
157 pub fn error_info(&self) -> Option<&SyntaxError> {
158 match &self.repr {
159 Repr::Error(e) => Some(e),
160 _ => None,
161 }
162 }
163
164 /// Does this node or any descendant hold a syntax error?
165 pub fn erroneous(&self) -> bool {
166 match &self.repr {
167 Repr::Error(_) => true,
168 Repr::Inner(i) => i.erroneous,
169 _ => false,
170 }
171 }
172
173 /// Does this node or any descendant carry a warning?
174 pub fn has_warnings(&self) -> bool {
175 self.warns.is_some() || match &self.repr {
176 Repr::Inner(i) => i.warned,
177 _ => false,
178 }
179 }
180
181 /// Every error node beneath and including this one, in source order.
182 pub fn errors(&self) -> Vec<(Span, SyntaxError)> {
183 let mut out = Vec::new();
184 self.collect_errors(&mut out);
185 out
186 }
187
188 fn collect_errors(&self, out: &mut Vec<(Span, SyntaxError)>) {
189 match &self.repr {
190 Repr::Error(e) => out.push((self.span, (**e).clone())),
191 Repr::Inner(i) => if i.erroneous {
192 for c in &i.children {
193 c.collect_errors(out);
194 }
195 },
196 _ => (),
197 }
198 }
199
200 /// Every warning beneath and including this one, in source order, each with the span it targets.
201 pub fn warnings(&self) -> Vec<(Span, SyntaxWarning)> {
202 let mut out = Vec::new();
203 self.collect_warnings(&mut out);
204 out
205 }
206
207 fn collect_warnings(&self, out: &mut Vec<(Span, SyntaxWarning)>) {
208 if let Some(ws) = &self.warns {
209 for w in ws.iter() {
210 let span = match w.range {
211 Some((a, b)) if !self.span.is_detached() =>
212 Span::new(self.span.file, self.span.start + a, self.span.start + b),
213 _ => self.span,
214 };
215 out.push((span, w.clone()));
216 }
217 }
218 if let Repr::Inner(i) = &self.repr {
219 if i.warned {
220 for c in &i.children {
221 c.collect_warnings(out);
222 }
223 }
224 }
225 }
226
227 /// The source text the node covers, rebuilt from its leaves -- equal to the source slice, since the
228 /// tree is lossless.
229 pub fn full_text(&self) -> String {
230 let mut s = String::with_capacity(self.len());
231 self.push_text(&mut s);
232 s
233 }
234
235 fn push_text(&self, s: &mut String) {
236 match &self.repr {
237 Repr::Leaf(t) => s.push_str(t),
238 Repr::Error(e) => s.push_str(&e.text),
239 Repr::Inner(i) => for n in i.children.iter() { n.push_text(s); },
240 Repr::Empty => (),
241 }
242 }
243
244 /// Does the node equal another, ignoring spans?
245 pub fn spanless_eq(&self, other: &Self) -> bool {
246 if self.kind != other.kind || self.warns != other.warns {
247 return false;
248 }
249 match (&self.repr, &other.repr) {
250 (Repr::Inner(a), Repr::Inner(b)) => a.children.len() == b.children.len()
251 && a.children.iter().zip(b.children.iter()).all(|(x, y)| x.spanless_eq(y)),
252 (Repr::Error(a), Repr::Error(b)) => a == b,
253 _ => self.text() == other.text() && self.is_leaf() == other.is_leaf(),
254 }
255 }
256}
257
258// Mutation, for the parser only.
259impl SyntaxNode {
260 /// Re-kinds a leaf or inner node. An error node keeps its kind.
261 pub(crate) fn convert_to_kind(&mut self, kind: SyntaxKind) {
262 if !self.kind.is_error() && !kind.is_error() {
263 self.kind = kind;
264 }
265 }
266
267 /// Turns the node into an error covering the same text, unless it already is one.
268 pub(crate) fn convert_to_error(&mut self, message: &str) {
269 if !self.kind.is_error() {
270 let text = self.full_text();
271 *self = SyntaxNode::error(&text, message, self.span);
272 }
273 }
274
275 /// Turns the node into an "expected `thing`, found ..." error.
276 pub(crate) fn expected(&mut self, thing: &str) {
277 let kind = self.kind;
278 self.convert_to_error(&format!("expected {}, found {}", thing, kind.name()));
279 if kind.is_keyword() && matches!(thing, "identifier" | "pattern") {
280 let text = self.text().to_string();
281 self.hint(&format!(
282 "keyword `{}` is not allowed as an identifier; try `{}_` instead", text, text));
283 }
284 }
285
286 /// Turns the node into an "unexpected ..." error.
287 pub(crate) fn unexpected(&mut self) {
288 let name = self.kind.name();
289 self.convert_to_error(&format!("unexpected {}", name));
290 }
291
292 /// Adds a hint to the node's error, or to its latest warning when it is not an error.
293 pub(crate) fn hint(&mut self, hint: &str) {
294 if let Repr::Error(e) = &mut self.repr {
295 Arc::make_mut(e).hints.push(hint.to_string());
296 return;
297 }
298 if let Some(ws) = &mut self.warns {
299 if let Some(w) = Arc::make_mut(ws).last_mut() {
300 w.hints.push(hint.to_string());
301 }
302 }
303 }
304
305 pub(crate) fn warn(&mut self, message: &str) {
306 self.push_warning(SyntaxWarning {
307 message: message.to_string(),
308 hints: Vec::new(),
309 range: None,
310 span_hints: Vec::new(),
311 });
312 }
313
314 /// A warning narrowed to `start..end`, byte offsets relative to the node's start.
315 pub(crate) fn warn_at(&mut self, start: usize, end: usize, message: &str) {
316 let range = Some((start as u32, end.min(self.len()) as u32));
317 self.push_warning(SyntaxWarning { message: message.to_string(), hints: Vec::new(), range, span_hints: Vec::new() });
318 }
319
320 /// A hint on the latest warning, pinned to `start..end` relative to the node's start.
321 pub(crate) fn hint_at(&mut self, start: usize, end: usize, hint: &str) {
322 let range = (start as u32, end.min(self.len()) as u32);
323 if let Some(ws) = &mut self.warns {
324 if let Some(w) = Arc::make_mut(ws).last_mut() {
325 w.span_hints.push((range.0, range.1, hint.to_string()));
326 }
327 }
328 }
329
330 fn push_warning(&mut self, w: SyntaxWarning) {
331 match &mut self.warns {
332 Some(ws) => Arc::make_mut(ws).push(w),
333 None => self.warns = Some(Arc::new(vec![w])),
334 }
335 }
336
337 pub(crate) fn children_mut(&mut self) -> &mut [SyntaxNode] {
338 match &mut self.repr {
339 Repr::Inner(i) => &mut Arc::make_mut(i).children,
340 _ => &mut [],
341 }
342 }
343
344 /// Assigns every node in the tree its byte span in `file`, starting at `start`, and returns the end.
345 pub(crate) fn numberise(&mut self, file: FileId, start: u32) -> u32 {
346 let end = match &mut self.repr {
347 Repr::Inner(i) => {
348 let mut at = start;
349 for c in Arc::make_mut(i).children.iter_mut() {
350 at = c.numberise(file, at);
351 }
352 at
353 },
354 _ => start + self.len() as u32,
355 };
356 self.span = Span::new(file, start, end);
357 end
358 }
359}