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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use std::fmt;
use super::Span;
/// Binary operators that can be part of an assignment.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum BinOp {
Plus,
Minus,
Star,
Slash,
Percent,
}
/// Delimiters
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum Delim {
/// Round parenthesis (i.e., `(` or `)`).
Paren,
/// Square bracket (i.e., `[` or `]`).
Bracket,
/// Double square brackets (i.e., `[[` or `]]`).
DoubleBracket,
/// Double angle brackets (i.e., `<<` or `>>`).
DoubleAngleBracket,
/// Curly brace (i.e., `{` or `}`).
Brace,
/// Double quote (i.e., `"`).
DoubleQuote,
/// Backtick (i.e., `` ` ``).
Backtick,
}
/// Class of keywords.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum KeywordClass {
/// Strict keywords that are currently used.
Used,
/// Strict keywords that are reserved for forward compatibility.
Reserved,
/// Special header keys. Weak keywords that are only special in headers.
HeaderKey,
/// Pragma names. Weak keywords that are only special in pragmas.
Pragma,
/// Language feature names. Weak keywords that are only special in pragmas.
FeatureName,
}
macro_rules! decl_keyword {
{
pub enum Keyword {
$(
inline $class:ident {
$(
$(#[$attr:meta])*
$keyword:ident = $value:ident $( | $alt_value:ident )*,
)*
}
)*
$(
$(#[$enum_class_attr:meta])*
pub enum $enum_class:ident {
$(
$(#[$enum_attr:meta])*
$enum_keyword:ident = $enum_value:ident $( | $enum_alt_value:ident )*,
)*
}
)*
}
} => {
/// Language keywords.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum Keyword {
$(
$(
$(#[$attr])*
$keyword,
)*
)*
$(
$enum_class($enum_class),
)*
}
$(
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
$(#[$enum_class_attr])*
pub enum $enum_class {
$(
$(#[$enum_attr])*
$enum_keyword,
)*
}
)*
impl Keyword {
pub fn identify(s: &str) -> Option<Self> {
$(
$(
if (stringify!($value)) == s {
return Some(Self::$keyword);
}
$(
if (stringify!($alt_value)) == s {
return Some(Self::$keyword);
}
)*
)*
)*
$(
$(
if (stringify!($enum_value)) == s {
return Some(Self::$enum_class($enum_class::$enum_keyword));
}
$(
if (stringify!($enum_alt_value)) == s {
return Some(Self::$enum_class($enum_class::$enum_keyword));
}
)*
)*
)*
return None;
}
pub fn into_canonical_str(self) -> &'static str {
match self {
$(
$(
Self::$keyword => stringify!($value),
)*
)*
$(
$(
Self::$enum_class($enum_class::$enum_keyword) => stringify!($enum_value),
)*
)*
}
}
pub fn class(self) -> KeywordClass {
match self {
$(
$(
Self::$keyword => KeywordClass::$class,
)*
)*
$(
$(
Self::$enum_class($enum_class::$enum_keyword) => KeywordClass::$enum_class,
)*
)*
}
}
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.into_canonical_str())
}
}
};
}
decl_keyword! {
pub enum Keyword {
inline Used {
/// `set`.
Set = set,
/// `call`.
Call = call,
/// Boolean literal `true`.
True = true,
/// Boolean literal `false`.
False = false,
/// Null literal `null`.
Null = null,
/// `if`.
If = if,
/// `elseif`.
ElseIf = elseif,
/// `else`.
Else = else,
/// `endif`.
EndIf = endif,
/// `to` operator.
To = to,
/// `is` operator.
Is = is,
/// `eq` operator.
Eq = eq,
/// `neq` operator.
Neq = neq,
/// `lt` operator.
Lt = lt,
/// `lte` operator.
Lte = lte,
/// `gt` operator.
Gt = gt,
/// `gte` operator.
Gte = gte,
/// `and` operator.
And = and,
/// `or` operator.
Or = or,
/// `xor` operator.
Xor = xor,
/// `not` operator.
Not = not,
/// `return`
Return = return,
}
inline Reserved {
/// `for`
For = for,
/// `loop`
Loop = loop,
/// `while`
While = while,
/// `do`
Do = do,
/// `next`
Done = done,
/// `continue`
Continue = continue,
/// `break`
Break = break,
/// `in` operator.
In = in,
}
pub enum HeaderKey {
/// The `title` header key. This is only parsed as a keyword when in header mode.
Title = title,
/// The `tags` header key. This is only parsed as a keyword when in header mode.
Tags = tags,
}
/// Pragma names. Weak keywords that are only special in pragmas.
pub enum Pragma {
/// `feature`
Feature = feature,
/// `disable_feature`
DisableFeature = disable_feature,
/// `private`
Private = private,
/// `sub`
Sub = sub,
}
/// Language feature names. Weak keywords that are only special in pragmas.
pub enum FeatureName {
/// `unicode_identifiers`
UnicodeIdentifiers = unicode_identifiers,
/// `scoped_nodes`
ScopedNodes = scoped_nodes,
/// `scoped_variables`
ScopedVariables = scoped_variables,
/// `subroutine`
Subroutine = subroutine,
/// `string_interpolation`
StringInterpolation = string_interpolation,
/// `extended_escape`
ExtendedEscape = extended_escape,
}
}
}
macro_rules! decl_escape_char {
{
pub enum EscapeChar {
$(#[$invalid_attr:meta])*
Invalid,
$(
$(#[$attr:meta])*
$name:ident = $chr:expr,
)*
}
} => {
/// Escape characters
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum EscapeChar {
$(#[$invalid_attr])*
Invalid,
$(
$(#[$attr])*
$name,
)*
}
impl EscapeChar {
pub fn identify(s: char) -> Self {
match s {
$(
$chr => Self::$name,
)*
_ => Self::Invalid,
}
}
/// Returns the escaped character if valid.
pub fn chr(self) -> Option<char> {
match self {
$(
Self::$name => Some($chr),
)*
Self::Invalid => None,
}
}
}
};
}
decl_escape_char! {
pub enum EscapeChar {
/// Invalid escape
Invalid,
/// Backslash
Backslash = '\\',
/// Double quote
DoubleQuote = '"',
/// Left bracket
LeftBracket = '[',
/// Left angle bracket
LeftAngleBracket = '<',
/// Backtick (`string_interpolation`)
Backtick = '`',
/// Left brace (`string_interpolation`)
Brace = '{',
/// Newline (`extended_escape`)
Newline = 'n',
/// Tab (`extended_escape`)
Tab = 't',
/// Hashtag (`extended_escape`)
Hashtag = '#',
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum PragmaStyle {
/// `//#`
Outer,
/// `//#!`
Inner,
}
/// Token kind.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Kind {
/* Binary operators */
/* Assignment operators */
Eq,
BinOpEq(BinOp),
/* Logical operators */
/// `||`
OrOr,
/// `&&`
AndAnd,
/// `^`
Xor,
/* Comparison operators */
/// `==`
EqEq,
/// `!=`
Neq,
/// `>`
Gt,
/// `<`
Lt,
/// `>=`
Gte,
/// `<=`
Lte,
/* Binary operators that can be part of assignment */
BinOp(BinOp),
/* Unary operators */
/// `!`
Not,
/// `~`
Tilde,
/* Structural symbols */
/// Line break
LineBreak,
/// End-of-file
Eof,
/// `//#` or `//#!`
Pragma(PragmaStyle),
/// `,`
Comma,
/// `.`
Period,
/// `->`
Arrow,
/// `|`
Pipe,
/// Hash symbol `#`
Hash,
/// `---`
TripleDash,
/// `===`
TripleEq,
/// Header key-value separator `:`
Colon,
/// Opening delimiter
OpenDelim(Delim),
/// Closing delimiter
CloseDelim(Delim),
/// Indent token for shortcut options
Indent,
/// Un-indent token for shortcut options
UnIndent,
/* Identifiers */
/// Keyword
Keyword(Keyword),
/// Identifier
Ident,
/// `$` sigil.
Dollar,
/// `@` sigil.
At,
/// `@@` sigil.
AtAt,
/* Literals and text */
/// Decimal number.
Number,
/// Plain text (dialogue or inside string literals).
Text,
/// Single escaped character within text
EscapeChar(EscapeChar),
/// Byte escape
EscapeByte,
/// Unicode escape
EscapeUnicode,
/* Junk */
/// Whitespace
Whitespace,
/// Non-pragma comment
Comment,
/// Characters that are not understood
Unknown,
}
/// A locally spanned token.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Token {
pub kind: Kind,
pub span: Span,
}
impl Token {
pub fn new(kind: Kind, span: Span) -> Self {
Token { kind, span }
}
pub fn with_kind(mut self, kind: Kind) -> Self {
self.kind = kind;
self
}
pub fn with_span(mut self, span: Span) -> Self {
self.span = span;
self
}
}
impl fmt::Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use BinOp as B;
let show_as = match self {
B::Plus => "+",
B::Minus => "-",
B::Star => "*",
B::Slash => "/",
B::Percent => "%",
};
f.write_str(show_as)
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Kind as K;
match self {
K::Eq => write!(f, "="),
K::BinOpEq(bin_op) => write!(f, "{}=", bin_op),
K::OrOr => write!(f, "||"),
K::AndAnd => write!(f, "&&"),
K::Xor => write!(f, "^"),
K::EqEq => write!(f, "=="),
K::Neq => write!(f, "!="),
K::Gt => write!(f, ">"),
K::Lt => write!(f, "<"),
K::Gte => write!(f, ">="),
K::Lte => write!(f, "<="),
K::BinOp(bin_op) => write!(f, "{}", bin_op),
K::Not => write!(f, "!"),
K::Tilde => write!(f, "~"),
K::LineBreak => write!(f, "line break"),
K::Eof => write!(f, "end of file"),
K::Pragma(PragmaStyle::Inner) => write!(f, "//#!"),
K::Pragma(PragmaStyle::Outer) => write!(f, "//#"),
K::Comma => write!(f, "','"),
K::Period => write!(f, "'.'"),
K::Arrow => write!(f, "->"),
K::Pipe => write!(f, "|"),
K::Hash => write!(f, "#"),
K::TripleDash => write!(f, "---"),
K::TripleEq => write!(f, "==="),
K::Colon => write!(f, ":"),
K::OpenDelim(Delim::DoubleQuote) => write!(f, "\""),
K::CloseDelim(Delim::DoubleQuote) => write!(f, "\""),
K::OpenDelim(Delim::Backtick) => write!(f, "`"),
K::CloseDelim(Delim::Backtick) => write!(f, "`"),
K::OpenDelim(Delim::DoubleBracket) => write!(f, "[["),
K::CloseDelim(Delim::DoubleBracket) => write!(f, "]]"),
K::OpenDelim(Delim::DoubleAngleBracket) => write!(f, "<<"),
K::CloseDelim(Delim::DoubleAngleBracket) => write!(f, ">>"),
K::OpenDelim(Delim::Bracket) => write!(f, "["),
K::CloseDelim(Delim::Bracket) => write!(f, "]"),
K::OpenDelim(Delim::Paren) => write!(f, "("),
K::CloseDelim(Delim::Paren) => write!(f, ")"),
K::OpenDelim(Delim::Brace) => write!(f, "{{"),
K::CloseDelim(Delim::Brace) => write!(f, "}}"),
K::Indent => write!(f, "indent"),
K::UnIndent => write!(f, "unindent"),
K::Keyword(kw) => write!(f, "{}", kw),
K::Ident => write!(f, "identifier"),
K::Dollar => write!(f, "$"),
K::At => write!(f, "@"),
K::AtAt => write!(f, "@@"),
K::Number => write!(f, "decimal number"),
K::Text => write!(f, "text"),
K::EscapeChar(_) | K::EscapeByte | K::EscapeUnicode => write!(f, "escape sequence"),
K::Whitespace => write!(f, "whitespace"),
K::Comment => write!(f, "comment"),
K::Unknown => write!(f, "unparsable characters"),
}
}
}