only_syntax/syntax_snapshot.rs
1use only_diagnostic::Diagnostic;
2
3use crate::ast_view::DocumentNode;
4use crate::parse::parse_tokens;
5use crate::{LexToken, ParseResult, ParseResultExt, SyntaxNode, lex};
6
7/// Immutable syntax snapshot shared by semantic analysis and editor hosts.
8///
9/// Args:
10/// None.
11///
12/// Returns:
13/// Token stream and parsed CST for a single in-memory source snapshot.
14#[derive(Debug, Clone)]
15pub struct SyntaxSnapshot {
16 pub tokens: Vec<LexToken>,
17 pub parse: ParseResult,
18}
19
20impl SyntaxSnapshot {
21 /// Returns the typed document CST root.
22 ///
23 /// Args:
24 /// None.
25 ///
26 /// Returns:
27 /// Typed document wrapper for the snapshot root.
28 pub fn document(&self) -> DocumentNode {
29 self.parse.document()
30 }
31
32 /// Returns the CST root node for this snapshot.
33 ///
34 /// Args:
35 /// None.
36 ///
37 /// Returns:
38 /// Parsed rowan root node.
39 pub fn root(&self) -> &SyntaxNode {
40 &self.parse.root
41 }
42
43 /// Returns collected syntax diagnostics.
44 ///
45 /// Args:
46 /// None.
47 ///
48 /// Returns:
49 /// Borrowed parse diagnostics for this snapshot.
50 pub fn diagnostics(&self) -> &[Diagnostic] {
51 self.parse.diagnostics()
52 }
53}
54
55/// Lexes and parses source text into an immutable syntax snapshot.
56///
57/// Args:
58/// source: Raw Onlyfile source text.
59///
60/// Returns:
61/// Snapshot containing tokens, CST and syntax diagnostics.
62pub fn snapshot(source: &str) -> SyntaxSnapshot {
63 let tokens = lex(source);
64 let parse = parse_tokens(&tokens);
65
66 SyntaxSnapshot { tokens, parse }
67}