Skip to main content

r_dcf_syntax/
syntax.rs

1//! Rowan language and syntax kinds.
2
3use rowan::Language;
4
5/// The kinds of nodes and tokens in an R DCF syntax tree.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(u16)]
8pub enum SyntaxKind {
9    /// The document root node.
10    Root,
11    /// A record node.
12    Record,
13    /// A field node.
14    Field,
15    /// A node containing malformed input.
16    Error,
17    /// A field-name token.
18    Name,
19    /// The first colon on a field line.
20    Colon,
21    /// Value text on a physical line.
22    Value,
23    /// Indentation at the start of a continuation line.
24    Indent,
25    /// A dot denoting a logical blank continuation line.
26    Dot,
27    /// Whitespace belonging to a record separator.
28    Whitespace,
29    /// An LF, CRLF, or lone CR line ending.
30    LineEnding,
31    /// Text inside an error node.
32    ErrorToken,
33}
34
35/// Rowan language marker for R DCF trees.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub enum DcfLanguage {}
38
39impl Language for DcfLanguage {
40    type Kind = SyntaxKind;
41
42    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
43        match raw.0 {
44            0 => SyntaxKind::Root,
45            1 => SyntaxKind::Record,
46            2 => SyntaxKind::Field,
47            3 => SyntaxKind::Error,
48            4 => SyntaxKind::Name,
49            5 => SyntaxKind::Colon,
50            6 => SyntaxKind::Value,
51            7 => SyntaxKind::Indent,
52            8 => SyntaxKind::Dot,
53            9 => SyntaxKind::Whitespace,
54            10 => SyntaxKind::LineEnding,
55            11 => SyntaxKind::ErrorToken,
56            _ => panic!("invalid R DCF syntax kind"),
57        }
58    }
59
60    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
61        rowan::SyntaxKind(kind as u16)
62    }
63}
64
65/// A typed Rowan syntax node.
66pub type SyntaxNode = rowan::SyntaxNode<DcfLanguage>;
67/// A typed Rowan syntax token.
68pub type SyntaxToken = rowan::SyntaxToken<DcfLanguage>;
69/// A typed Rowan node-or-token element.
70pub type SyntaxElement = rowan::NodeOrToken<SyntaxNode, SyntaxToken>;