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
use either::Either;
use swc_atoms::js_word;
use super::*;
use crate::token::Keyword;
impl<I: Tokens> Parser<I> {
pub(super) fn parse_maybe_private_name(&mut self) -> PResult<Either<PrivateName, Ident>> {
let is_private = is!(self, '#');
if is_private {
self.parse_private_name().map(Either::Left)
} else {
self.parse_ident_name().map(Either::Right)
}
}
pub(super) fn parse_private_name(&mut self) -> PResult<PrivateName> {
let start = cur_pos!(self);
assert_and_bump!(self, '#');
let hash_end = self.input.prev_span().hi;
if self.input.cur_pos() - hash_end != BytePos(0) {
syntax_error!(
self,
span!(self, start),
SyntaxError::SpaceBetweenHashAndIdent
);
}
let id = self.parse_ident_name()?;
Ok(PrivateName {
span: span!(self, start),
id,
})
}
pub(super) fn parse_ident_ref(&mut self) -> PResult<Ident> {
let ctx = self.ctx();
self.parse_ident(!ctx.in_generator, !ctx.in_async)
}
pub(super) fn parse_label_ident(&mut self) -> PResult<Ident> {
let ctx = self.ctx();
self.parse_ident(!ctx.in_generator, !ctx.in_async)
}
pub(super) fn parse_ident_name(&mut self) -> PResult<Ident> {
let in_type = self.ctx().in_type;
let start = cur_pos!(self);
let w = match cur!(self, true) {
Ok(&Word(..)) => match bump!(self) {
Word(w) => w.into(),
_ => unreachable!(),
},
Ok(&Token::JSXName { .. }) if in_type => match bump!(self) {
Token::JSXName { name } => name,
_ => unreachable!(),
},
_ => syntax_error!(self, SyntaxError::ExpectedIdent),
};
Ok(Ident::new(w, span!(self, start)))
}
pub(super) fn parse_module_export_name(&mut self) -> PResult<ModuleExportName> {
let module_export_name = match cur!(self, false) {
Ok(&Token::Str { .. }) => match self.parse_lit()? {
Lit::Str(str_lit) => ModuleExportName::Str(str_lit),
_ => unreachable!(),
},
Ok(&Word(..)) => ModuleExportName::Ident(self.parse_ident_name()?),
_ => {
unexpected!(self, "identifier or string");
}
};
Ok(module_export_name)
}
pub(super) fn parse_ident(&mut self, incl_yield: bool, incl_await: bool) -> PResult<Ident> {
trace_cur!(self, parse_ident);
let start = cur_pos!(self);
let word = self.parse_with(|p| {
let w = match cur!(p, true) {
Ok(&Word(..)) => match bump!(p) {
Word(w) => w,
_ => unreachable!(),
},
_ => syntax_error!(p, SyntaxError::ExpectedIdent),
};
match w {
Word::Ident(ref name @ js_word!("enum")) => {
p.emit_err(
p.input.prev_span(),
SyntaxError::InvalidIdentInStrict(name.clone()),
);
}
Word::Keyword(name @ Keyword::Yield) | Word::Keyword(name @ Keyword::Let) => {
p.emit_strict_mode_err(
p.input.prev_span(),
SyntaxError::InvalidIdentInStrict(name.into_js_word()),
);
}
Word::Ident(ref name @ js_word!("static"))
| Word::Ident(ref name @ js_word!("implements"))
| Word::Ident(ref name @ js_word!("interface"))
| Word::Ident(ref name @ js_word!("package"))
| Word::Ident(ref name @ js_word!("private"))
| Word::Ident(ref name @ js_word!("protected"))
| Word::Ident(ref name @ js_word!("public")) => {
p.emit_strict_mode_err(
p.input.prev_span(),
SyntaxError::InvalidIdentInStrict(name.clone()),
);
}
_ => {}
}
match w {
Word::Keyword(Keyword::Await) if p.ctx().in_declare => Ok(js_word!("await")),
Word::Keyword(Keyword::Await) if p.ctx().module | p.ctx().in_async => {
syntax_error!(p, p.input.prev_span(), SyntaxError::InvalidIdentInAsync)
}
Word::Keyword(Keyword::This) if p.input.syntax().typescript() => {
Ok(js_word!("this"))
}
Word::Keyword(Keyword::Let) => Ok(js_word!("let")),
Word::Ident(ident) => {
if ident == js_word!("arguments") && p.ctx().in_class_field {
p.emit_err(p.input.prev_span(), SyntaxError::ArgumentsInClassField)
}
Ok(ident)
}
Word::Keyword(Keyword::Yield) if incl_yield => Ok(js_word!("yield")),
Word::Keyword(Keyword::Await) if incl_await => Ok(js_word!("await")),
Word::Keyword(..) | Word::Null | Word::True | Word::False => {
syntax_error!(p, p.input.prev_span(), SyntaxError::ExpectedIdent)
}
}
})?;
Ok(Ident::new(word, span!(self, start)))
}
}