Skip to main content

r_dcf_syntax/
parser.rs

1//! Infallible lossless parser.
2
3use std::{fmt, ops::Range, sync::Arc};
4
5use rowan::{GreenNode, GreenNodeBuilder, Language};
6
7use crate::{
8    ast::{Record, Root},
9    syntax::{DcfLanguage, SyntaxKind, SyntaxNode},
10    value::is_valid_field_name,
11};
12
13/// A byte span in the UTF-8 source text.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
15pub struct SourceSpan {
16    /// Inclusive byte offset.
17    pub start: usize,
18    /// Exclusive byte offset.
19    pub end: usize,
20}
21
22impl SourceSpan {
23    /// Creates a span from byte offsets.
24    pub const fn new(start: usize, end: usize) -> Self {
25        Self { start, end }
26    }
27
28    /// Returns whether the span contains no bytes.
29    pub const fn is_empty(self) -> bool {
30        self.start == self.end
31    }
32
33    /// Returns the number of bytes in the span.
34    pub const fn len(self) -> usize {
35        self.end - self.start
36    }
37
38    pub(crate) fn from_text_range(range: rowan::TextRange) -> Self {
39        Self::new(
40            u32::from(range.start()) as usize,
41            u32::from(range.end()) as usize,
42        )
43    }
44}
45
46impl From<SourceSpan> for Range<usize> {
47    fn from(span: SourceSpan) -> Self {
48        span.start..span.end
49    }
50}
51
52/// Stable category assigned to a parse diagnostic.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
54pub enum DiagnosticKind {
55    /// A non-indented line has no colon.
56    MalformedField,
57    /// A continuation line has no preceding field in its record.
58    OrphanContinuation,
59    /// Text before the first colon is not a valid field name.
60    InvalidFieldName,
61}
62
63/// A parser diagnostic with a stable category and byte span.
64#[derive(Debug, Clone, PartialEq, Eq, Hash)]
65pub struct Diagnostic {
66    kind: DiagnosticKind,
67    span: SourceSpan,
68}
69
70impl Diagnostic {
71    /// Returns the stable diagnostic category.
72    pub const fn kind(&self) -> DiagnosticKind {
73        self.kind
74    }
75
76    /// Returns the offending source span.
77    pub const fn span(&self) -> SourceSpan {
78        self.span
79    }
80
81    /// Returns a stable human-readable message.
82    pub const fn message(&self) -> &'static str {
83        match self.kind {
84            DiagnosticKind::MalformedField => "field line is missing a colon",
85            DiagnosticKind::OrphanContinuation => "continuation line has no preceding field",
86            DiagnosticKind::InvalidFieldName => "invalid field name",
87        }
88    }
89}
90
91impl fmt::Display for Diagnostic {
92    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(
94            formatter,
95            "{} at {}..{}",
96            self.message(),
97            self.span.start,
98            self.span.end
99        )
100    }
101}
102
103/// Persistent parse result owning only a Rowan green tree and shared diagnostics.
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct Parse {
106    green: GreenNode,
107    diagnostics: Arc<[Diagnostic]>,
108}
109
110impl Parse {
111    /// Parses UTF-8 R DCF text without failing.
112    pub fn new(source: &str) -> Self {
113        parse(source)
114    }
115
116    /// Returns the persistent immutable green tree.
117    pub const fn green(&self) -> &GreenNode {
118        &self.green
119    }
120
121    /// Materializes a transient typed syntax root.
122    pub fn syntax(&self) -> SyntaxNode {
123        SyntaxNode::new_root(self.green.clone())
124    }
125
126    /// Materializes the typed document root.
127    pub fn root(&self) -> Root {
128        Root::new(self.syntax())
129    }
130
131    /// Returns all diagnostics in source order.
132    pub fn diagnostics(&self) -> &[Diagnostic] {
133        &self.diagnostics
134    }
135
136    /// Clones the shared diagnostics allocation.
137    pub fn diagnostics_arc(&self) -> Arc<[Diagnostic]> {
138        Arc::clone(&self.diagnostics)
139    }
140
141    /// Iterates over records in source order.
142    pub fn records(&self) -> impl Iterator<Item = Record> {
143        self.root().records().collect::<Vec<_>>().into_iter()
144    }
145}
146
147impl fmt::Display for Parse {
148    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
149        fmt::Display::fmt(&self.syntax(), formatter)
150    }
151}
152
153/// Parses UTF-8 R DCF text into a lossless syntax tree.
154///
155/// # Panics
156///
157/// Panics when `source` exceeds Rowan's 4 GiB tree-size limit.
158pub fn parse(source: &str) -> Parse {
159    assert!(
160        u32::try_from(source.len()).is_ok(),
161        "Rowan input exceeds 4 GiB"
162    );
163    let lines = physical_lines(source);
164    let mut builder = GreenNodeBuilder::new();
165    let mut diagnostics = Vec::new();
166    builder.start_node(DcfLanguage::kind_to_raw(SyntaxKind::Root));
167    let mut record_open = false;
168    let mut field_open = false;
169
170    for line in lines {
171        let body = &source[line.start..line.body_end];
172        if body.bytes().all(|byte| matches!(byte, b' ' | b'\t' | 0x0c)) {
173            close_field(&mut builder, &mut field_open);
174            close_record(&mut builder, &mut record_open);
175            token(&mut builder, SyntaxKind::Whitespace, body);
176            token(
177                &mut builder,
178                SyntaxKind::LineEnding,
179                &source[line.body_end..line.end],
180            );
181            continue;
182        }
183
184        if !record_open {
185            builder.start_node(DcfLanguage::kind_to_raw(SyntaxKind::Record));
186            record_open = true;
187        }
188
189        if body.starts_with([' ', '\t']) {
190            if field_open {
191                emit_continuation(&mut builder, source, line);
192            } else {
193                emit_error(&mut builder, source, line);
194                diagnostics.push(Diagnostic {
195                    kind: DiagnosticKind::OrphanContinuation,
196                    span: SourceSpan::new(line.start, line.body_end),
197                });
198            }
199            continue;
200        }
201
202        close_field(&mut builder, &mut field_open);
203        if let Some(colon) = body.find(':') {
204            let name = &body[..colon];
205            if is_valid_field_name(name) {
206                builder.start_node(DcfLanguage::kind_to_raw(SyntaxKind::Field));
207                field_open = true;
208                token(&mut builder, SyntaxKind::Name, name);
209                token(&mut builder, SyntaxKind::Colon, ":");
210                token(&mut builder, SyntaxKind::Value, &body[colon + 1..]);
211                token(
212                    &mut builder,
213                    SyntaxKind::LineEnding,
214                    &source[line.body_end..line.end],
215                );
216            } else {
217                emit_error(&mut builder, source, line);
218                diagnostics.push(Diagnostic {
219                    kind: DiagnosticKind::InvalidFieldName,
220                    span: SourceSpan::new(line.start, line.start + colon),
221                });
222            }
223        } else {
224            emit_error(&mut builder, source, line);
225            diagnostics.push(Diagnostic {
226                kind: DiagnosticKind::MalformedField,
227                span: SourceSpan::new(line.start, line.body_end),
228            });
229        }
230    }
231
232    close_field(&mut builder, &mut field_open);
233    close_record(&mut builder, &mut record_open);
234    builder.finish_node();
235    Parse {
236        green: builder.finish(),
237        diagnostics: diagnostics.into(),
238    }
239}
240
241#[derive(Clone, Copy)]
242struct PhysicalLine {
243    start: usize,
244    body_end: usize,
245    end: usize,
246}
247
248fn physical_lines(source: &str) -> Vec<PhysicalLine> {
249    let bytes = source.as_bytes();
250    let mut lines = Vec::new();
251    let mut start = 0;
252    let mut cursor = 0;
253    while cursor < bytes.len() {
254        if matches!(bytes[cursor], b'\r' | b'\n') {
255            let body_end = cursor;
256            cursor += 1;
257            if bytes[body_end] == b'\r' && bytes.get(cursor) == Some(&b'\n') {
258                cursor += 1;
259            }
260            lines.push(PhysicalLine {
261                start,
262                body_end,
263                end: cursor,
264            });
265            start = cursor;
266        } else {
267            cursor += 1;
268        }
269    }
270    if start < source.len() {
271        lines.push(PhysicalLine {
272            start,
273            body_end: source.len(),
274            end: source.len(),
275        });
276    }
277    lines
278}
279
280fn emit_continuation(builder: &mut GreenNodeBuilder<'_>, source: &str, line: PhysicalLine) {
281    let body = &source[line.start..line.body_end];
282    let indent_len = body
283        .bytes()
284        .take_while(|byte| matches!(byte, b' ' | b'\t'))
285        .count();
286    token(builder, SyntaxKind::Indent, &body[..indent_len]);
287    let content = &body[indent_len..];
288    token(
289        builder,
290        if content == "." {
291            SyntaxKind::Dot
292        } else {
293            SyntaxKind::Value
294        },
295        content,
296    );
297    token(
298        builder,
299        SyntaxKind::LineEnding,
300        &source[line.body_end..line.end],
301    );
302}
303
304fn emit_error(builder: &mut GreenNodeBuilder<'_>, source: &str, line: PhysicalLine) {
305    builder.start_node(DcfLanguage::kind_to_raw(SyntaxKind::Error));
306    token(
307        builder,
308        SyntaxKind::ErrorToken,
309        &source[line.start..line.body_end],
310    );
311    token(
312        builder,
313        SyntaxKind::LineEnding,
314        &source[line.body_end..line.end],
315    );
316    builder.finish_node();
317}
318
319fn token(builder: &mut GreenNodeBuilder<'_>, kind: SyntaxKind, text: &str) {
320    if !text.is_empty() {
321        builder.token(DcfLanguage::kind_to_raw(kind), text);
322    }
323}
324
325fn close_field(builder: &mut GreenNodeBuilder<'_>, open: &mut bool) {
326    if *open {
327        builder.finish_node();
328        *open = false;
329    }
330}
331
332fn close_record(builder: &mut GreenNodeBuilder<'_>, open: &mut bool) {
333    if *open {
334        builder.finish_node();
335        *open = false;
336    }
337}