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
use crate::literal::Literal;
/// A token produced by the Elm lexer.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub enum Token {
// ── Keywords ─────────────────────────────────────────────────────
/// `module`
Module,
/// `where`
Where,
/// `import`
Import,
/// `as`
As,
/// `exposing`
Exposing,
/// `type`
Type,
/// `alias`
Alias,
/// `port`
Port,
/// `if`
If,
/// `then`
Then,
/// `else`
Else,
/// `case`
Case,
/// `of`
Of,
/// `let`
Let,
/// `in`
In,
/// `infix`
Infix,
// ── Delimiters ───────────────────────────────────────────────────
/// `(`
LeftParen,
/// `)`
RightParen,
/// `[`
LeftBracket,
/// `]`
RightBracket,
/// `{`
LeftBrace,
/// `}`
RightBrace,
/// `,`
Comma,
/// `|`
Pipe,
/// `=`
Equals,
/// `:`
Colon,
/// `.`
Dot,
/// `..`
DotDot,
/// `\`
Backslash,
/// `_`
Underscore,
/// `->`
Arrow,
// ── Operators ────────────────────────────────────────────────────
/// Any operator: `+`, `-`, `*`, `/`, `//`, `^`, `++`, `::`, `<|`, `|>`,
/// `>>`, `<<`, `==`, `/=`, `<`, `>`, `<=`, `>=`, `&&`, `||`, `</>`, etc.
///
/// We store operators as strings rather than individual variants because
/// Elm's operator set is extensible (via `infix` declarations in core
/// packages), and the parser handles precedence/associativity.
Operator(String),
/// `-` when used as prefix negation (contextually disambiguated from
/// the `-` operator).
Minus,
// ── Identifiers ──────────────────────────────────────────────────
/// A lowercase identifier: `foo`, `myFunction`, `x1`
LowerName(String),
/// An uppercase identifier: `Maybe`, `Cmd`, `Html`
UpperName(String),
// ── Literals ─────────────────────────────────────────────────────
/// A literal value (char, string, int, hex, float).
Literal(Literal),
// ── Comments ─────────────────────────────────────────────────────
/// A single-line comment: `-- ...`
LineComment(String),
/// A block comment: `{- ... -}` (may be nested)
BlockComment(String),
/// A documentation comment: `{-| ... -}`
DocComment(String),
// ── Special ──────────────────────────────────────────────────────
/// A GLSL shader block: `[glsl| ... |]`
Glsl(String),
/// A newline. The lexer emits these so the parser can track
/// indentation-sensitive layout.
Newline,
/// End of file.
Eof,
}
// Manual Eq because Token contains Literal which contains f64.
impl Eq for Token {}
impl Token {
/// Look up a keyword from a lowercase identifier string.
/// Returns `None` if the string is not a keyword.
pub fn keyword(s: &str) -> Option<Token> {
match s {
"module" => Some(Token::Module),
"where" => Some(Token::Where),
"import" => Some(Token::Import),
"as" => Some(Token::As),
"exposing" => Some(Token::Exposing),
"type" => Some(Token::Type),
"alias" => Some(Token::Alias),
"port" => Some(Token::Port),
// Note: `effect` is NOT a keyword — it is only contextual in
// `effect module` declarations and is a valid identifier
// everywhere else, just like `left`, `right`, `non`.
"if" => Some(Token::If),
"then" => Some(Token::Then),
"else" => Some(Token::Else),
"case" => Some(Token::Case),
"of" => Some(Token::Of),
"let" => Some(Token::Let),
"in" => Some(Token::In),
"infix" => Some(Token::Infix),
// Note: `left`, `right`, `non` are NOT keywords — they are only
// contextual in `infix` declarations and are valid identifiers
// everywhere else (e.g., `String.left`, `Dict` node fields).
_ => None,
}
}
/// Returns `true` if this token is a comment.
pub fn is_comment(&self) -> bool {
matches!(
self,
Token::LineComment(_) | Token::BlockComment(_) | Token::DocComment(_)
)
}
/// Returns `true` if this token is whitespace or a newline.
pub fn is_whitespace(&self) -> bool {
matches!(self, Token::Newline)
}
}