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
//! Labelled statements (ECMAScript §14.13).
//!
//! `LabelledStatement : LabelIdentifier : LabelledItem`, and a `LabelIdentifier` is an ordinary
//! `Identifier` — so telling one from an expression statement takes two tokens, the second of
//! which is a `:`. That is the second and last place this parser needs to look ahead; `let` was
//! the first, and both are cases where the same first token begins two productions.
//!
//! # What a label may be attached to
//!
//! `LabelledItem : Statement | FunctionDeclaration`. A `Statement`, so `a: var b;` is fine and
//! `a: let b;` is not — the same asymmetry every body position has, for the same reason. The
//! `FunctionDeclaration` alternative is §14.13.1's, and it is a Syntax Error "unless that source
//! text is non-strict code and the host is a web browser or otherwise supports Labelled Function
//! Declarations". DR-0008's amendment keeps the first half of that condition and drops the second,
//! so it is taken in sloppy code — and only where §14.6.1 and its five siblings allow, which is a
//! rule about *position* and is [`super::LabelledFunction`].
//!
//! # Whether the label means anything is not decided here
//!
//! Every rule about labels — that one may not repeat inside another, that a `break` must name one
//! that exists, that a `continue` must name one on a loop — is a syntax-directed operation over
//! the finished tree (§8.3), and lives in [`crate::static_semantics`]. This file only reads them.
//!
//! That split is what let the parser stop counting. It used to carry an `iteration_depth` and a
//! `switch_depth` so that an unlabelled `break` could be refused where it stood, and those are
//! gone: §14.8.1 and §14.9.1 are questions about the tree, the tree can be asked, and a parser
//! that keeps its own answer to a question the tree can settle has two things to keep right.
use super::{ParseError, Parser};
use crate::ast::{Label, LabelledStatement, Stmt, StmtKind};
use crate::lexer::{Goal, TokenKind, identifier_value};
impl Parser<'_> {
/// Whether a `LabelledStatement` begins here.
///
/// An identifier and then a `:`. The identifier may be written with escapes — unlike `let`,
/// nothing here is a terminal, so `a: ;` labels `a` exactly as `a: ;` does.
pub(super) fn at_labelled_statement(&self) -> Result<bool, ParseError> {
if !self.is_identifier_token(self.current.kind) {
return Ok(false);
}
Ok(self.peek(Goal::Div)?.kind == TokenKind::Colon)
}
/// `LabelledStatement : LabelIdentifier : LabelledItem` (§14.13).
///
/// `function_allowed` is whether this label stands where §B.3.2's `LabelledItem :
/// FunctionDeclaration` is reachable — see [`super::LabelledFunction`]. It is not the same
/// question as strictness and cannot be folded into it: `while (x) a: function f() {}` is
/// sloppy and still refused, because §14.7.3.1 asks about the *position* rather than the mode.
pub(super) fn parse_labelled_statement(
&mut self,
function_allowed: super::LabelledFunction,
) -> Result<Stmt, ParseError> {
let token = self.advance(Goal::Div)?;
let name =
identifier_value(self.source, token.span).ok_or_else(|| self.value_missing(token))?;
// `LabelIdentifier : Identifier`, so §13.1.1 applies here as it does to a reference.
self.check_strict_name(&name, token.span, false)?;
self.eat(TokenKind::Colon, Goal::RegExp, "`:`")?;
self.enter()?;
// §14.13's other alternative, which §B.3.2 makes reachable: `LabelledItem :
// FunctionDeclaration`. Unlike §B.3.4's `if` clause the declaration is **not** wrapped in a
// block, and that is §8.2.12 — `TopLevelVarDeclaredNames` is handed a `LabelledStatement`
// rather than the ordinary operation, so a labelled function at a body's top level is
// var-scoped exactly as an unlabelled one is, and only one inside a block is B.3.3's.
let allowed = function_allowed == super::LabelledFunction::Allowed;
let body = if allowed && self.at_annex_b_function()? {
self.parse_function_declaration(false)
} else {
// `LabelledItem : Statement`, so a declaration may not be labelled — `a: let b;` has no
// derivation, for the reason `if (x) let b;` does not. The permission is passed down
// rather than dropped: `a: b: function f() {}` is a label on a label, and §14.13 gives
// the inner one whatever position the outer one stood in.
self.parse_statement(function_allowed)
};
self.leave();
let body = body?;
Ok(Stmt {
span: token.span.to(body.span),
kind: StmtKind::Labelled(Box::new(LabelledStatement {
label: Label {
name: name.into_owned().into_boxed_str(),
span: token.span,
},
body,
})),
})
}
}
#[cfg(test)]
mod tests {
use crate::parser::test_support::*;
use crate::parser::{ParseErrorKind, parse_script};
use crate::span::Span;
#[test]
fn an_identifier_and_a_colon_begin_a_labelled_statement() {
assert_eq!(statements("a: ;"), ["(label a <empty>)"]);
assert_eq!(statements("a: b;"), ["(label a b)"]);
assert_eq!(statements("a: { b; }"), ["(label a {b})"]);
assert_eq!(statements("a: b: ;"), ["(label a (label b <empty>))"]);
assert_eq!(
statements("a: while (1) ;"),
["(label a (while 1 <empty>))"]
);
assert_eq!(statements("a: if (x) ;"), ["(label a (if x <empty>))"]);
// The label is a `LabelIdentifier`, which is an `Identifier` — so the contextual keywords
// are all available, and only a reserved word is not.
assert_eq!(statements("let: ;"), ["(label let <empty>)"]);
assert_eq!(statements("of: ;"), ["(label of <empty>)"]);
assert_eq!(statements("async: ;"), ["(label async <empty>)"]);
assert!(parse_script("if: ;").is_err());
assert!(parse_script("this: ;").is_err());
// `yield` and `await` belong in the list above and are there now. §13.1 gives
// `LabelIdentifier` the alternatives `[~Yield] yield` and `[~Await] await`, and both
// parameters are off in the sloppy script code this parser handles.
assert_eq!(statements("yield: ;"), ["(label yield <empty>)"]);
assert_eq!(statements("await: ;"), ["(label await <empty>)"]);
assert_eq!(statements("var yield = 1;"), ["(var yield=1)"]);
// Nothing here is a terminal, so an escaped spelling is the same label — unlike `let`,
// where §5.1.5.1 makes the escape decide whether it is a keyword at all.
assert_eq!(statements(r"a: ;"), ["(label a <empty>)"]);
// …and a lone identifier is still an expression statement, the `:` being the whole test.
assert_eq!(statements("a;"), ["a"]);
assert_eq!(statements("a ? b : c;"), ["(? a b c)"]);
let script = parse_script("ab: ;").expect("this parses");
assert_eq!(script.body[0].span, Span::new(0, 5));
}
#[test]
fn yield_and_await_are_ordinary_names_where_no_production_reserves_them() {
// §13.1's conditional alternatives. `Identifier : IdentifierName but not ReservedWord`
// would refuse both — `yield` and `await` are reserved words in §12.7.2 — and all three
// identifier productions add them back, two of them gated on grammar parameters that
// nothing here can turn on. See [`super::is_identifier_token`].
assert_eq!(statements("yield;"), ["yield"]);
assert_eq!(statements("await;"), ["await"]);
assert_eq!(statements("yield = 1;"), ["(= yield 1)"]);
assert_eq!(statements("typeof yield;"), ["(typeof yield)"]);
assert_eq!(statements("yield in a;"), ["(in yield a)"]);
assert_eq!(statements("f(yield, await);"), ["(call f [yield await])"]);
// …as a `BindingIdentifier`, which takes them unconditionally in the grammar and leaves
// the refusing to §13.1.1's early errors — none of which can fire here.
assert_eq!(statements("var yield = 1;"), ["(var yield=1)"]);
assert_eq!(statements("let await = 1;"), ["(let await=1)"]);
assert_eq!(statements("const yield = 1;"), ["(const yield=1)"]);
assert_eq!(
statements("try {} catch (yield) {}"),
["(try {} (catch yield {}))"]
);
assert_eq!(statements("let [yield] = a;"), ["(let [yield]=a)"]);
assert_eq!(
statements("for (var await of a);"),
["(for-of (var await) a <empty>)"]
);
// …as a `LabelIdentifier`.
assert_eq!(statements("yield: ;"), ["(label yield <empty>)"]);
assert_eq!(
statements("await: while (1) continue await;"),
["(label await (while 1 (continue await)))"]
);
// …and in the two places a name is not an `Identifier` at all, which never needed them:
// a property key is an `IdentifierName`, so every reserved word was always allowed.
assert_eq!(statements("a.yield;"), ["(. a yield)"]);
assert_eq!(statements("a.await;"), ["(. a await)"]);
assert_eq!(shape("({yield: 1})"), "{(yield 1)}");
assert_eq!(shape("({if: 1})"), "{(if 1)}");
// Shorthand is an `IdentifierReference`, so it takes the two and no other reserved word.
assert_eq!(shape("({yield})"), "{yield}");
assert_eq!(shape("({await})"), "{await}");
assert!(parse_script("({if});").is_err());
// Every other reserved word is still refused everywhere a name is wanted, which is what
// makes this about those two rather than about reserved words in general.
for source in ["var if = 1;", "new;", "if: ;", "class: ;", "typeof var;"] {
assert!(parse_script(source).is_err(), "{source:?}");
}
// An escaped spelling was always an ordinary identifier, being no terminal at all — so
// these were legal before this slice and are unchanged by it.
assert_eq!(statements(r"var \u0079ield = 1;"), ["(var yield=1)"]);
}
#[test]
fn a_labelled_item_is_a_statement_so_var_may_stand_there_and_let_may_not() {
// `LabelledItem : Statement | FunctionDeclaration`, and `Statement` has no `Declaration`
// alternative — the same rule every body position has.
assert_eq!(statements("a: var b;"), ["(label a (var b))"]);
assert!(parse_script("a: let b;").is_err());
assert!(parse_script("a: const b = 1;").is_err());
// …and inside a block, which is a StatementList, both are fine again.
assert_eq!(statements("a: { let b; }"), ["(label a {(let b)})"]);
// `LabelledItem : FunctionDeclaration` is the other alternative, and §14.13.1 makes it a
// Syntax Error "unless that source text is non-strict code and the host is a web browser
// or otherwise supports Labelled Function Declarations". DR-0008's amendment drops the
// second half of that condition and keeps the first, so this is taken in sloppy code —
// and only where §14.6.1's `IsLabelledFunction` rule allows, which is
// [`super::function::tests::annex_b_takes_a_function_where_a_statement_stands_and_only_where_it_says`].
assert_eq!(statements("a: function f() {}"), ["(label a (fn f [] {}))"]);
assert!(parse_script("'use strict'; a: function f() {}").is_err());
}
#[test]
fn break_and_continue_take_a_label_on_the_same_line_and_not_on_the_next() {
assert_eq!(statements("a: { break a; }"), ["(label a {(break a)})"]);
assert_eq!(
statements("a: while (1) continue a;"),
["(label a (while 1 (continue a)))"]
);
assert_eq!(statements("while (1) break;"), ["(while 1 break)"]);
// §12.10 rule 3: `break [no LineTerminator here] LabelIdentifier`. A name on the next line
// is the next statement, which is what makes the restriction matter — and here it turns a
// labelled break into an unlabelled one rather than into an error.
assert_eq!(
statements("while (1) { break\na; }"),
["(while 1 {break a})"],
"two statements: an unlabelled break, then `a`"
);
assert_eq!(
statements("a: while (1) { continue\na; }"),
["(label a (while 1 {continue a}))"]
);
// …and on one line it is the label it looks like.
assert_eq!(
statements("a: while (1) { continue a; }"),
["(label a (while 1 {(continue a)}))"]
);
// The label is a StringValue, so an escape does not change which one it names.
assert_eq!(statements(r"a: { break a; }"), ["(label a {(break a)})"]);
// A reserved word is no `LabelIdentifier`, so it simply ends the statement.
assert!(parse_script("a: { break if; }").is_err());
}
#[test]
fn every_rule_about_what_a_label_means_is_reported_from_the_tree() {
// The parser reads labels; `crate::static_semantics::labels` decides what they mean. All
// five rules arrive here as one kind of failure each — see that module for the argument.
assert_eq!(script_error("a: a: ;").kind, ParseErrorKind::DuplicateLabel);
assert_eq!(
script_error("a: { a: ; }").kind,
ParseErrorKind::DuplicateLabel
);
assert_eq!(
script_error("break a;").kind,
ParseErrorKind::UndefinedBreakTarget
);
assert_eq!(
script_error("a: { continue a; }").kind,
ParseErrorKind::ContinueOutsideLoop
);
assert_eq!(
script_error("while (1) { a: continue a; }").kind,
ParseErrorKind::UndefinedContinueTarget
);
assert_eq!(
script_error("break;").kind,
ParseErrorKind::BreakOutsideLoop
);
assert_eq!(
script_error("continue;").kind,
ParseErrorKind::ContinueOutsideLoop
);
// The caret goes on the label, or on the jump when there is none.
assert_eq!(script_error("a: ; break zz;").span, Span::new(11, 13));
assert_eq!(script_error("a: a: ;").span, Span::new(3, 4));
// …and the legal shapes still parse, which is what makes the above about meaning rather
// than about syntax.
assert!(parse_script("a: ; a: ;").is_ok());
assert!(parse_script("a: while (1) break a;").is_ok());
assert!(parse_script("a: switch (x) { case 1: break a; }").is_ok());
}
#[test]
fn no_label_however_odd_can_panic() {
let cases = [
"a:".to_string(),
"a: b:".to_string(),
"break a".to_string(),
"continue a".to_string(),
"a: ".repeat(1000),
"a: b: c: d: e: ;".to_string(),
format!(
"{} ;",
(0..500).map(|i| format!("l{i}: ")).collect::<String>()
),
];
for source in &cases {
let _ = parse_script(source);
}
// Labels nest, so they are bounded by the cap rather than by memory.
assert_eq!(
script_error(&"a: ".repeat(1000)).kind,
ParseErrorKind::TooDeeplyNested
);
// …while five hundred distinct ones in a chain is a chain, and the walk handles it.
let many: String = (0..500).map(|i| format!("l{i}: ")).collect::<String>() + ";";
assert!(parse_script(&many).is_err(), "still past the nesting cap");
}
}