Skip to main content

ktrs_syntax/
lib.rs

1//! Lossless Kotlin syntax tree. Node and token kinds mirror the Kotlin compiler's PSI element
2//! types one-to-one (see `kinds.tsv`), so trees can be diffed against `DebugUtil.psiToString`.
3
4mod dump;
5mod generated {
6    pub(crate) mod kinds;
7}
8pub mod tree;
9
10use std::rc::Rc;
11
12pub use dump::psi_dump;
13pub use generated::kinds::SyntaxKind;
14pub use text_size::{TextRange, TextSize};
15pub use tree::{ElementId, Tree, TreeBuilder};
16
17impl SyntaxKind {
18    pub fn from_raw(raw: u16) -> SyntaxKind {
19        generated::kinds::ALL[raw as usize]
20    }
21
22    pub fn is_trivia(self) -> bool {
23        matches!(
24            self,
25            SyntaxKind::WHITE_SPACE
26                | SyntaxKind::EOL_COMMENT
27                | SyntaxKind::BLOCK_COMMENT
28                | SyntaxKind::SHEBANG_COMMENT
29                | SyntaxKind::DOC_COMMENT
30        )
31    }
32}
33
34/// A parsed file: the tree plus one message per `ERROR_ELEMENT`, in tree preorder.
35#[derive(Debug, Clone)]
36pub struct Parse {
37    pub tree: Rc<Tree>,
38    pub error_messages: Vec<String>,
39}
40
41impl Parse {
42    pub fn has_errors(&self) -> bool {
43        !self.error_messages.is_empty()
44    }
45}