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
use crate::{RawDoc, Span};
#[derive(Clone, Debug, PartialEq)]
pub enum TokenKind {
NumberLiteral(f64),
/// No `n` suffix, no leading sign (unary `-` is at the AST level).
/// String because the value may exceed any fixed-width integer.
BigIntLiteral(String),
StringLiteral(String),
BooleanLiteral(bool),
NullLiteral,
/// No `${…}` interpolations; parser collapses to a plain string literal.
TemplateNoSubstitution(String),
/// Cooked text up to the first `${`. Lexer pushes a frame onto
/// `template_frames`; the next depth-0 `}` re-enters template scanning.
TemplateHead(String),
TemplateMiddle(String),
/// Cooked text from the last `}` to the closing backtick.
/// Pops the frame pushed by [`TemplateHead`](Self::TemplateHead).
TemplateTail(String),
/// `source` preserves escape sequences verbatim (the regex engine
/// interprets them, not the lexer). Context disambiguation
/// (literal vs. division) via `is_regex_context`.
RegexLiteral {
source: String,
flags: String,
},
// Identifier (span-only; text via &source[span.start..span.end])
Identifier,
Let,
Const,
Function,
If,
Else,
While,
Do,
For,
Break,
Continue,
Return,
Switch,
Case,
Default,
/// Phase-1 forgiveness: accepted as a leading modifier on top-level
/// declarations and ignored (there is no export consumer yet).
Export,
Void,
Interface,
Enum,
/// Also drives the `"field" in x` narrowing predicate.
In,
Typeof,
Import,
Instanceof,
New,
Try,
Catch,
Finally,
Throw,
Class,
Extends,
Implements,
Super,
This,
Plus,
Minus,
Star,
Slash,
Percent,
Equals,
PlusEquals,
MinusEquals,
StarEquals,
SlashEquals,
/// Lowered by the compound-assignment desugar to `x = x % y`.
PercentEquals,
/// Right-associative, higher precedence than `*`/`/`/`%`.
/// Lex order: `**=` matched before `**` before `*=`.
StarStar,
StarStarEquals,
/// Postfix-only (no prefix `++x`). Lex order: `++` matched before `+=`.
PlusPlus,
MinusMinus,
EqEqEq,
EqEq,
BangEqEq,
BangEq,
LessThan,
GreaterThan,
LessEquals,
GreaterEquals,
Bang,
AmpAmp,
Amp,
AmpEquals,
PipeEquals,
Caret,
CaretEquals,
Tilde,
Pipe,
PipePipe,
Dot,
/// Parser accepts only inside destructuring patterns (`{ … }` / `[ … ]`).
DotDotDot,
Arrow,
Question,
/// Single token so the postfix-chain parser dispatches on one peek.
QuestionDot,
QuestionQuestion,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Comma,
Colon,
Semicolon,
Newline,
Eof,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
/// `/** ... */` doc comment captured as out-of-band trivia;
/// consumed by the parser at declaration tokens.
pub leading_doc: Option<RawDoc>,
}
impl Token {
pub fn new(kind: TokenKind, span: Span) -> Self {
Self {
kind,
span,
leading_doc: None,
}
}
}
#[cfg(test)]
mod tests {
use super::{Token, TokenKind};
use crate::Span;
#[test]
fn construct_literal_keyword_operator() {
let num = Token::new(
TokenKind::NumberLiteral(42.5),
Span::new(crate::FileId(0), 0, 4).unwrap(),
);
assert_eq!(num.kind, TokenKind::NumberLiteral(42.5));
assert_eq!(num.span, Span::new(crate::FileId(0), 0, 4).unwrap());
assert!(num.leading_doc.is_none());
let kw = Token::new(TokenKind::Let, Span::new(crate::FileId(0), 5, 8).unwrap());
assert_eq!(kw.kind, TokenKind::Let);
let op = Token::new(
TokenKind::EqEqEq,
Span::new(crate::FileId(0), 10, 13).unwrap(),
);
assert_eq!(op.kind, TokenKind::EqEqEq);
}
#[test]
fn clone_and_equality() {
let t = Token::new(
TokenKind::StringLiteral("hello".to_string()),
Span::new(crate::FileId(0), 0, 7).unwrap(),
);
assert_eq!(t.clone(), t);
}
}