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
use crate::lexer;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[repr(u16)]
pub enum SyntaxKind {
    /// Identifier or keyword
    IDENT_KW = 1,
    /// defined preprocessor keyword
    DEFINED = 2,
    /// Digit sequence
    DIGITS = 3,
    // Single-char tokens
    /// .
    PERIOD = 4,
    /// +
    PLUS = 5,
    /// -
    DASH = 6,
    /// /
    SLASH = 7,
    /// *
    ASTERISK = 8,
    /// %
    PERCENT = 9,
    /// <
    LANGLE = 10,
    /// >
    RANGLE = 11,
    /// [
    LBRACKET = 12,
    /// ]
    RBRACKET = 13,
    /// (
    LPAREN = 14,
    /// )
    RPAREN = 15,
    /// {
    LBRACE = 16,
    /// }
    RBRACE = 17,
    /// ^
    CARET = 18,
    /// |
    BAR = 19,
    /// &
    AMPERSAND = 20,
    /// ~
    TILDE = 21,
    /// =
    EQUAL = 22,
    /// !
    BANG = 23,
    /// :
    COLON = 24,
    /// ;
    SEMICOLON = 25,
    /// ,
    COMMA = 26,
    /// ?
    QUESTION = 27,
    /// #
    HASH = 28,
    // Other
    /// "string"
    QUOTE_STRING = 29,
    /// <string>
    ANGLE_STRING = 30,
    /// \
    BACKSLASH = 31,
    /// Whitespace
    WS = 32,
    /// Newline
    NEWLINE = 33,
    /// Comment (single-line or multi-line)
    COMMENT = 34,
    // Replaced by WS in this step
    // LINECONT = 35,
    /// Invalid token
    ERROR = 36,
    // Multi-char tokens
    /// <<
    LEFT_OP = 37,
    /// >>
    RIGHT_OP = 38,
    /// ++
    INC_OP = 39,
    /// --
    DEC_OP = 40,
    /// <=
    LE_OP = 41,
    /// >=
    GE_OP = 42,
    /// ==
    EQ_OP = 43,
    /// !=
    NE_OP = 44,
    /// &&
    AND_OP = 45,
    /// ||
    OR_OP = 46,
    /// ^^
    XOR_OP = 47,
    /// *=
    MUL_ASSIGN = 48,
    /// /=
    DIV_ASSIGN = 49,
    /// +=
    ADD_ASSIGN = 50,
    /// %=
    MOD_ASSIGN = 51,
    /// <<=
    LEFT_ASSIGN = 52,
    /// >>=
    RIGHT_ASSIGN = 53,
    /// &=
    AND_ASSIGN = 54,
    /// ^=
    XOR_ASSIGN = 55,
    /// |=
    OR_ASSIGN = 56,
    /// -=
    SUB_ASSIGN = 57,
    /// ##
    PP_CONCAT_OP = 58,
    // Extra types
    /// Concatenation expression
    PP_CONCAT,
    /// #
    PP_EMPTY,
    /// #include
    PP_INCLUDE,
    /// #include path
    PP_INCLUDE_PATH,
    /// #define
    PP_DEFINE,
    PP_DEFINE_ARGS,
    PP_DEFINE_ARG,
    PP_DEFINE_BODY,
    /// #undef
    PP_UNDEF,
    PP_IDENT,
    /// #if
    PP_IF,
    PP_IF_EXPR,
    /// #ifdef
    PP_IFDEF,
    /// #ifndef
    PP_IFNDEF,
    /// #else
    PP_ELSE,
    /// #elif
    PP_ELIF,
    /// #endif
    PP_ENDIF,
    /// #error
    PP_ERROR,
    /// #error body
    PP_ERROR_BODY,
    /// #pragma
    PP_PRAGMA,
    /// #pragma body
    PP_PRAGMA_BODY,
    /// #extension
    PP_EXTENSION,
    /// #version
    PP_VERSION,
    PP_VERSION_NUMBER,
    PP_VERSION_PROFILE,
    /// #line
    PP_LINE,
    /// #line body
    PP_LINE_BODY,
    /// AST root
    ROOT,
    _LAST,
}

impl SyntaxKind {
    pub fn is_whitespace(&self) -> bool {
        matches!(self, Self::COMMENT | Self::WS | Self::NEWLINE)
    }

    pub fn is_trivia(&self) -> bool {
        matches!(self, Self::COMMENT | Self::WS)
    }

    pub fn is_newline(&self) -> bool {
        matches!(self, Self::NEWLINE)
    }

    pub fn paste(lhs: Self, rhs: Self) -> Self {
        use SyntaxKind::*;

        match lhs {
            IDENT_KW => match rhs {
                DEFINED | IDENT_KW | DIGITS => IDENT_KW,
                _ => ERROR,
            },
            DIGITS => match rhs {
                IDENT_KW | DIGITS | PERIOD | DASH | PLUS => DIGITS,
                _ => ERROR,
            },
            LEFT_OP => match rhs {
                EQUAL => LEFT_ASSIGN,
                _ => ERROR,
            },
            RIGHT_OP => match rhs {
                EQUAL => RIGHT_ASSIGN,
                _ => ERROR,
            },
            PERIOD => match rhs {
                DIGITS => DIGITS,
                _ => ERROR,
            },
            EQUAL => match rhs {
                EQUAL => EQ_OP,
                _ => ERROR,
            },
            BANG => match rhs {
                EQUAL => NE_OP,
                _ => ERROR,
            },
            DASH => match rhs {
                DASH => DEC_OP,
                EQUAL => SUB_ASSIGN,
                _ => ERROR,
            },
            PLUS => match rhs {
                PLUS => INC_OP,
                EQUAL => ADD_ASSIGN,
                _ => ERROR,
            },
            ASTERISK => match rhs {
                EQUAL => MUL_ASSIGN,
                _ => ERROR,
            },
            SLASH => match rhs {
                EQUAL => DIV_ASSIGN,
                _ => ERROR,
            },
            PERCENT => match rhs {
                EQUAL => MOD_ASSIGN,
                _ => ERROR,
            },
            LANGLE => match rhs {
                LANGLE => LEFT_OP,
                EQUAL => LE_OP,
                _ => ERROR,
            },
            RANGLE => match rhs {
                RANGLE => RIGHT_OP,
                EQUAL => GE_OP,
                _ => ERROR,
            },
            BAR => match rhs {
                BAR => OR_OP,
                EQUAL => OR_ASSIGN,
                _ => ERROR,
            },
            CARET => match rhs {
                CARET => XOR_OP,
                EQUAL => XOR_ASSIGN,
                _ => ERROR,
            },
            AMPERSAND => match rhs {
                AMPERSAND => AND_OP,
                EQUAL => AND_ASSIGN,
                _ => ERROR,
            },
            HASH => match rhs {
                HASH => PP_CONCAT_OP,
                _ => ERROR,
            },
            _ => ERROR,
        }
    }
}

impl From<lexer::Token> for SyntaxKind {
    fn from(s: lexer::Token) -> Self {
        use SyntaxKind::*;

        // A stray line continuation should just be part of whitespace
        if s == lexer::Token::LINECONT {
            return WS;
        }

        // SAFETY: Aside from LINECONT, SyntaxKind is a superset of lexer::Token
        unsafe { std::mem::transmute(std::mem::transmute::<lexer::Token, u16>(s)) }
    }
}

impl From<SyntaxKind> for rowan::SyntaxKind {
    fn from(kind: SyntaxKind) -> Self {
        Self(kind as u16)
    }
}