only-syntax 0.3.1

Syntax parsing and CST construction for the only task language.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use only_diagnostic::{Diagnostic, DiagnosticCode, DiagnosticPhase, DiagnosticSeverity};
use rowan::SyntaxNodeChildren;
use text_size::{TextRange, TextSize};
use winnow::Parser;
use winnow::combinator::alt;
use winnow::error::{ContextError, ErrMode, ModalResult};
use winnow::token::any;

use crate::ast_view::DocumentNode;
use crate::builder::ParseTreeBuilder;
use crate::cst::SyntaxNode;
use crate::cursor::TokenCursor;
use crate::recover::{
    advance, consume_line, starts_indented_namespace_boundary, starts_indented_namespace_member,
    starts_top_level_item,
};
use crate::trivia::{is_trivia, line_contains_kind, line_has_non_trivia};
use crate::{LexToken, SyntaxKind, lex};

#[derive(Debug, Clone)]
pub struct ParseResult {
    pub root: SyntaxNode,
    diagnostics: Vec<Diagnostic>,
}

impl ParseResult {
    /// Returns the typed document CST root.
    ///
    /// Args:
    /// None.
    ///
    /// Returns:
    /// Typed document wrapper for the parse root.
    pub fn document(&self) -> DocumentNode {
        DocumentNode::cast(self.root.clone()).expect("parse root must always be a document node")
    }
}

/// Extension helpers for parse results used by hosts and tests.
pub trait ParseResultExt {
    /// Returns root CST children for top-level inspection.
    fn root_children(&self) -> SyntaxNodeChildren<crate::cst::OnlyLanguage>;

    /// Returns collected parse diagnostics.
    fn diagnostics(&self) -> &[Diagnostic];
}

impl ParseResultExt for ParseResult {
    fn root_children(&self) -> SyntaxNodeChildren<crate::cst::OnlyLanguage> {
        self.root.children()
    }

    fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }
}

/// Parses Onlyfile text into a shallow CST with line-level recovery.
///
/// Args:
/// source: Raw Onlyfile source text.
///
/// Returns:
/// Parse result containing CST root and collected diagnostics.
pub fn parse(source: &str) -> ParseResult {
    let tokens = lex(source);
    parse_tokens(&tokens)
}

pub(crate) fn parse_tokens(tokens: &[LexToken]) -> ParseResult {
    let mut builder = ParseTreeBuilder::new();
    let mut diagnostics = Vec::new();
    let kinds = tokens.iter().map(|token| token.kind).collect::<Vec<_>>();
    let mut cursor = TokenCursor::new(tokens, &kinds);
    let mut in_braced_namespace = false;

    loop {
        let trivia = cursor.skip_trivia();
        builder.push_tokens(trivia);

        let Some(token) = cursor.current() else {
            break;
        };
        if token.kind == SyntaxKind::Eof {
            break;
        }

        let mut input = cursor.remaining();
        let (item, consumed) =
            (|input: &mut &[SyntaxKind]| parse_top_level_item(input, in_braced_namespace))
                .with_taken()
                .parse_next(&mut input)
                .expect("top-level parser should always consume a non-EOF item");
        let token_slice = cursor.consume(consumed.len());

        match item {
            ParsedTopLevelItem::Directive { malformed } => {
                if malformed {
                    diagnostics.push(parse_error(
                        "parse.malformed-directive",
                        "invalid directive",
                        token.range,
                    ));
                    builder.push_node(SyntaxKind::Error, token_slice);
                    continue;
                }
                builder.push_node(SyntaxKind::Directive, token_slice);
            }
            ParsedTopLevelItem::DocComment => {
                builder.push_node(SyntaxKind::DocComment, token_slice);
            }
            ParsedTopLevelItem::Namespace {
                malformed,
                is_close,
                has_open_brace,
            } => {
                if malformed {
                    diagnostics.push(parse_error(
                        "parse.malformed-namespace-header",
                        "invalid namespace",
                        token.range,
                    ));
                    builder.push_node(SyntaxKind::Error, token_slice);
                    continue;
                }
                builder.push_node(SyntaxKind::NamespaceBlock, token_slice);
                in_braced_namespace = has_open_brace && !is_close;
            }
            ParsedTopLevelItem::Task {
                saw_colon,
                malformed,
            } => {
                if !saw_colon || malformed {
                    diagnostics.push(parse_error(
                        "parse.malformed-task-header",
                        "invalid task header",
                        token.range,
                    ));
                    builder.push_node(SyntaxKind::Error, token_slice);
                    continue;
                }
                builder.push_task(token_slice);
            }
            ParsedTopLevelItem::Unexpected => {
                diagnostics.push(parse_error(
                    "parse.unexpected-token",
                    "unexpected text",
                    token.range,
                ));
                builder.push_node(SyntaxKind::Error, token_slice);
            }
        }
    }

    ParseResult {
        root: builder.finish(),
        diagnostics,
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParsedTopLevelItem {
    Directive {
        malformed: bool,
    },
    DocComment,
    Namespace {
        malformed: bool,
        is_close: bool,
        has_open_brace: bool,
    },
    Task {
        saw_colon: bool,
        malformed: bool,
    },
    Unexpected,
}

fn parse_top_level_item(
    input: &mut &[SyntaxKind],
    in_braced_namespace: bool,
) -> ModalResult<ParsedTopLevelItem> {
    alt((
        parse_directive_item,
        parse_doc_comment_item,
        parse_namespace_item,
        |input: &mut &[SyntaxKind]| parse_task_item(input, in_braced_namespace),
        parse_unexpected_item,
    ))
    .parse_next(input)
}

fn parse_directive_item(input: &mut &[SyntaxKind]) -> ModalResult<ParsedTopLevelItem> {
    token_kind(input, SyntaxKind::Bang)?;
    let malformed = !line_has_non_trivia(input) || line_contains_kind(input, SyntaxKind::Comment);
    consume_line(input);
    Ok(ParsedTopLevelItem::Directive { malformed })
}

fn parse_doc_comment_item(input: &mut &[SyntaxKind]) -> ModalResult<ParsedTopLevelItem> {
    token_kind(input, SyntaxKind::Percent)?;
    consume_line(input);
    Ok(ParsedTopLevelItem::DocComment)
}

fn parse_namespace_item(input: &mut &[SyntaxKind]) -> ModalResult<ParsedTopLevelItem> {
    if input.first() == Some(&SyntaxKind::RBrace) {
        advance(input);
        let malformed =
            line_has_non_trivia(input) || line_contains_kind(input, SyntaxKind::Comment);
        consume_line(input);
        return Ok(ParsedTopLevelItem::Namespace {
            malformed,
            is_close: true,
            has_open_brace: false,
        });
    }

    token_kind(input, SyntaxKind::LBracket)?;
    let has_open_brace = line_contains_kind(input, SyntaxKind::LBrace);
    let malformed = namespace_open_is_malformed(input);
    consume_line(input);
    Ok(ParsedTopLevelItem::Namespace {
        malformed,
        is_close: false,
        has_open_brace,
    })
}

fn namespace_open_is_malformed(input: &[SyntaxKind]) -> bool {
    let line = input
        .iter()
        .copied()
        .take_while(|kind| !matches!(kind, SyntaxKind::Newline | SyntaxKind::Eof))
        .collect::<Vec<_>>();
    let mut index = 0;

    while line.get(index) == Some(&SyntaxKind::Whitespace) {
        index += 1;
    }
    if line.get(index) == Some(&SyntaxKind::Ident) {
        index += 1;
    }
    while line.get(index) == Some(&SyntaxKind::Whitespace) {
        index += 1;
    }
    if line.get(index) != Some(&SyntaxKind::RBracket) {
        return true;
    }
    index += 1;
    while line.get(index) == Some(&SyntaxKind::Whitespace) {
        index += 1;
    }
    if index == line.len() {
        return false;
    }
    if line.get(index) != Some(&SyntaxKind::LBrace) {
        return true;
    }
    index += 1;
    while line.get(index) == Some(&SyntaxKind::Whitespace) {
        index += 1;
    }
    index != line.len()
}

fn parse_task_item(
    input: &mut &[SyntaxKind],
    in_braced_namespace: bool,
) -> ModalResult<ParsedTopLevelItem> {
    token_kind(input, SyntaxKind::Ident)?;
    let mut saw_colon = false;
    let mut header_complete = false;
    let mut line_start = false;
    let mut malformed = false;
    let mut expect_guard_at = false;
    let mut phase = TaskHeaderPhase::BeforeTail;
    let mut saw_parameter_list = false;
    let mut continuation_header = false;
    let mut expect_clause_start = false;
    let mut continuation_indent = false;
    let mut expect_param_indent = false;

    while let Some(kind) = input.first().copied() {
        if header_complete
            && line_start
            && (starts_top_level_item(kind)
                || starts_indented_namespace_boundary(input)
                || (in_braced_namespace && starts_indented_namespace_member(input)))
        {
            break;
        }

        if saw_colon
            && !header_complete
            && !matches!(kind, SyntaxKind::Whitespace | SyntaxKind::Newline)
        {
            malformed = true;
        }

        if !header_complete {
            if expect_param_indent {
                match kind {
                    SyntaxKind::Indent => expect_param_indent = false,
                    SyntaxKind::RParen => expect_param_indent = false,
                    _ => {
                        malformed = true;
                        break;
                    }
                }
            }

            if kind == SyntaxKind::Comment {
                malformed = true;
            }

            if continuation_header && expect_clause_start {
                match kind {
                    SyntaxKind::Indent | SyntaxKind::Whitespace => {
                        continuation_indent = true;
                    }
                    SyntaxKind::Question
                    | SyntaxKind::Amp
                    | SyntaxKind::ShellKw
                    | SyntaxKind::ShellFallbackKw => {
                        malformed |= !continuation_indent;
                        expect_clause_start = false;
                    }
                    SyntaxKind::Colon => {
                        malformed |= continuation_indent;
                        expect_clause_start = false;
                    }
                    SyntaxKind::Newline => malformed = true,
                    _ => {
                        malformed = true;
                        expect_clause_start = false;
                    }
                }
            }

            match &mut phase {
                TaskHeaderPhase::BeforeTail => match kind {
                    SyntaxKind::LParen => {
                        saw_parameter_list = true;
                        phase = TaskHeaderPhase::Params { depth: 1 };
                    }
                    SyntaxKind::Question => {
                        phase = TaskHeaderPhase::Guard { depth: 0 };
                        expect_guard_at = true;
                    }
                    SyntaxKind::Amp => {
                        phase = TaskHeaderPhase::Dependencies {
                            group_depth: 0,
                            saw_group: false,
                        };
                    }
                    SyntaxKind::Whitespace | SyntaxKind::Indent => {}
                    SyntaxKind::At if expect_guard_at => {
                        expect_guard_at = false;
                    }
                    _ => {
                        if expect_guard_at {
                            malformed = true;
                            expect_guard_at = false;
                        }
                    }
                },
                TaskHeaderPhase::Params { depth } => match kind {
                    SyntaxKind::LParen => *depth += 1,
                    SyntaxKind::RParen => {
                        if *depth == 0 {
                            malformed = true;
                        } else {
                            *depth -= 1;
                            if *depth == 0 {
                                phase = TaskHeaderPhase::BeforeTail;
                            }
                        }
                    }
                    _ => {}
                },
                TaskHeaderPhase::Guard { depth } => match kind {
                    SyntaxKind::LParen => *depth += 1,
                    SyntaxKind::RParen => {
                        if *depth > 0 {
                            *depth -= 1;
                        }
                        if *depth == 0 {
                            phase = TaskHeaderPhase::BeforeTail;
                        }
                    }
                    SyntaxKind::At if expect_guard_at => {
                        expect_guard_at = false;
                    }
                    SyntaxKind::Whitespace | SyntaxKind::Indent => {}
                    _ => {
                        if expect_guard_at {
                            malformed = true;
                            expect_guard_at = false;
                        }
                    }
                },
                TaskHeaderPhase::Dependencies {
                    group_depth,
                    saw_group,
                } => match kind {
                    SyntaxKind::LParen => {
                        if *group_depth > 0 {
                            malformed = true;
                        }
                        *group_depth += 1;
                        *saw_group = true;
                    }
                    SyntaxKind::RParen => {
                        if *group_depth == 0 {
                            malformed = true;
                        } else {
                            *group_depth -= 1;
                        }
                    }
                    SyntaxKind::Question | SyntaxKind::At => malformed = true,
                    SyntaxKind::ShellKw | SyntaxKind::ShellFallbackKw if *group_depth == 0 => {
                        phase = TaskHeaderPhase::Shell;
                    }
                    SyntaxKind::Unknown if kind == SyntaxKind::Unknown => {}
                    _ => {}
                },
                TaskHeaderPhase::Shell => {}
            }
        }

        if kind == SyntaxKind::Colon && phase.is_balanced() {
            saw_colon = true;
        }
        advance(input);

        if kind == SyntaxKind::Eof {
            break;
        }

        if kind == SyntaxKind::Newline && !saw_colon {
            if matches!(phase, TaskHeaderPhase::Params { depth } if depth > 0) {
                expect_param_indent = true;
                line_start = true;
                continue;
            }
            if saw_parameter_list && phase.is_balanced() && !expect_guard_at {
                continuation_header = true;
                expect_clause_start = true;
                continuation_indent = false;
                line_start = true;
                continue;
            }
            malformed |= !phase.is_balanced() || expect_guard_at;
            break;
        }

        if kind == SyntaxKind::Newline && saw_colon {
            malformed |= !phase.is_balanced() || expect_guard_at;
            header_complete = true;
        }

        line_start = kind == SyntaxKind::Newline;
    }

    Ok(ParsedTopLevelItem::Task {
        saw_colon,
        malformed,
    })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TaskHeaderPhase {
    BeforeTail,
    Params { depth: usize },
    Guard { depth: usize },
    Dependencies { group_depth: usize, saw_group: bool },
    Shell,
}

impl TaskHeaderPhase {
    fn is_balanced(self) -> bool {
        match self {
            TaskHeaderPhase::BeforeTail | TaskHeaderPhase::Shell => true,
            TaskHeaderPhase::Params { depth } | TaskHeaderPhase::Guard { depth } => depth == 0,
            TaskHeaderPhase::Dependencies { group_depth, .. } => group_depth == 0,
        }
    }
}

fn parse_unexpected_item(input: &mut &[SyntaxKind]) -> ModalResult<ParsedTopLevelItem> {
    any::<_, ErrMode<ContextError>>
        .verify(|kind: &SyntaxKind| !is_trivia(*kind) && *kind != SyntaxKind::Eof)
        .value(ParsedTopLevelItem::Unexpected)
        .parse_next(input)
}

fn token_kind(input: &mut &[SyntaxKind], kind: SyntaxKind) -> ModalResult<SyntaxKind> {
    any::<_, ErrMode<ContextError>>
        .verify(move |candidate: &SyntaxKind| *candidate == kind)
        .parse_next(input)
}

fn parse_error(code: &str, message: &str, range: TextRange) -> Diagnostic {
    Diagnostic::new(
        DiagnosticSeverity::Error,
        DiagnosticCode::new(code),
        message,
        DiagnosticPhase::Parse,
        normalize_range(range),
    )
}

fn normalize_range(range: TextRange) -> TextRange {
    if range.is_empty() {
        TextRange::new(range.start(), range.start() + TextSize::from(1))
    } else {
        range
    }
}