-
-
Notifications
You must be signed in to change notification settings - Fork 621
Expand file tree
/
Copy pathmod.rs
More file actions
350 lines (316 loc) · 10.8 KB
/
mod.rs
File metadata and controls
350 lines (316 loc) · 10.8 KB
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
//! This module contains all literal expressions, which represents the primitive values in ECMAScript.
//!
//! More information:
//! - [ECMAScript reference][spec]
//! - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-primary-expression-literals
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals
mod array;
mod object;
mod template;
pub use array::ArrayLiteral;
use core::ops::ControlFlow;
pub use object::{ObjectLiteral, ObjectMethodDefinition, PropertyDefinition};
pub use template::{TemplateElement, TemplateLiteral};
use crate::{
LinearSpan, LinearSpanIgnoreEq, Span, Spanned,
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, Sym, ToInternedString};
use num_bigint::BigInt;
use super::Expression;
/// Literals represent values in ECMAScript.
///
/// These are fixed values **not variables** that you literally provide in your script.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-primary-expression-literals
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, PartialEq)]
pub struct Literal {
kind: LiteralKind,
span: Span,
linear_span: LinearSpanIgnoreEq,
}
impl Literal {
/// Create a new [`Literal`].
#[inline]
#[must_use]
pub fn new<T: Into<LiteralKind>>(kind: T, span: Span) -> Self {
Self {
kind: kind.into(),
span,
linear_span: LinearSpanIgnoreEq(LinearSpan::default()),
}
}
/// Create a new [`Literal`] with a [`LinearSpan`] for source text tracking.
#[inline]
#[must_use]
pub fn with_linear_span<T: Into<LiteralKind>>(
kind: T,
span: Span,
linear_span: LinearSpan,
) -> Self {
Self {
kind: kind.into(),
span,
linear_span: LinearSpanIgnoreEq(linear_span),
}
}
/// Get reference to the [`LiteralKind`] of [`Literal`].
#[inline]
#[must_use]
pub const fn kind(&self) -> &LiteralKind {
&self.kind
}
/// Get mutable reference to the [`LiteralKind`] of [`Literal`].
#[inline]
#[must_use]
pub const fn kind_mut(&mut self) -> &mut LiteralKind {
&mut self.kind
}
/// Get the [`LinearSpan`] of this literal in the source text.
#[inline]
#[must_use]
pub const fn linear_span(&self) -> LinearSpan {
self.linear_span.0
}
/// Get position of the node.
#[inline]
#[must_use]
pub const fn as_string(&self) -> Option<Sym> {
if let LiteralKind::String(sym) = self.kind() {
return Some(*sym);
}
None
}
/// Check if [`Literal`] is a [`LiteralKind::Undefined`].
#[inline]
#[must_use]
pub const fn is_undefined(&self) -> bool {
matches!(self.kind(), LiteralKind::Undefined)
}
}
impl Spanned for Literal {
#[inline]
fn span(&self) -> Span {
self.span
}
}
impl From<Literal> for Expression {
#[inline]
fn from(lit: Literal) -> Self {
Self::Literal(lit)
}
}
impl ToInternedString for Literal {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
self.kind().to_interned_string(interner)
}
}
impl VisitWith for Literal {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
if let LiteralKind::String(sym) = &self.kind {
visitor.visit_sym(sym)
} else {
ControlFlow::Continue(())
}
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
if let LiteralKind::String(sym) = &mut self.kind {
visitor.visit_sym_mut(sym)
} else {
ControlFlow::Continue(())
}
}
}
/// Literals represent values in ECMAScript.
///
/// These are fixed values **not variables** that you literally provide in your script.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-primary-expression-literals
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub enum LiteralKind {
/// A string literal is zero or more characters enclosed in double (`"`) or single (`'`) quotation marks.
///
/// A string must be delimited by quotation marks of the same type (that is, either both single quotation marks, or both double quotation marks).
/// You can call any of the String object's methods on a string literal value.
/// ECMAScript automatically converts the string literal to a temporary String object,
/// calls the method, then discards the temporary String object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-string-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#String_literals
String(Sym),
/// A floating-point number literal.
///
/// The exponent part is an "`e`" or "`E`" followed by an integer, which can be signed (preceded by "`+`" or "`-`").
/// A floating-point literal must have at least one digit, and either a decimal point or "`e`" (or "`E`").
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-number-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Floating-point_literals
Num(f64),
/// Integer types can be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-number-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Numeric_literals
Int(i32),
/// `BigInt` provides a way to represent whole numbers larger than the largest number ECMAScript
/// can reliably represent with the `Number` primitive.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-bigint-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Numeric_literals
BigInt(Box<BigInt>),
/// The Boolean type has two literal values: `true` and `false`.
///
/// The Boolean object is a wrapper around the primitive Boolean data type.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-boolean-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Boolean_literals
Bool(bool),
/// In JavaScript, `null` is marked as one of the primitive values, cause it's behaviour is seemingly primitive.
///
/// In computer science, a null value represents a reference that points,
/// generally intentionally, to a nonexistent or invalid object or address.
/// The meaning of a null reference varies among language implementations.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-null-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Glossary/null
Null,
/// This represents the JavaScript `undefined` value, it does not reference the `undefined` global variable,
/// it will directly evaluate to `undefined`.
///
/// NOTE: This is used for optimizations.
Undefined,
}
/// Represents a numeric value.
#[derive(Debug, Clone, Copy)]
pub enum Number {
/// An integer.
Int(i32),
/// A floating point number.
Num(f64),
}
impl LiteralKind {
/// Returns [`Number::Int`] for [`LiteralKind::Int`], [`Number::Num`] for [`LiteralKind::Num`], and [`None`] otherwise.
#[must_use]
pub fn as_number(&self) -> Option<Number> {
Some(match self {
Self::Int(int) => Number::Int(*int),
Self::Num(num) => Number::Num(*num),
_ => return None,
})
}
}
/// Manual implementation, because `Undefined` is never constructed during parsing.
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for LiteralKind {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let c = <u8 as arbitrary::Arbitrary<'a>>::arbitrary(u)? % 6;
match c {
0 => Ok(Self::String(<Sym as arbitrary::Arbitrary>::arbitrary(u)?)),
1 => Ok(Self::Num(<f64 as arbitrary::Arbitrary>::arbitrary(u)?)),
2 => Ok(Self::Int(<i32 as arbitrary::Arbitrary>::arbitrary(u)?)),
3 => Ok(Self::BigInt(Box::new(
<BigInt as arbitrary::Arbitrary>::arbitrary(u)?,
))),
4 => Ok(Self::Bool(<bool as arbitrary::Arbitrary>::arbitrary(u)?)),
5 => Ok(Self::Null),
_ => unreachable!(),
}
}
}
impl From<Sym> for LiteralKind {
#[inline]
fn from(string: Sym) -> Self {
Self::String(string)
}
}
impl From<f64> for LiteralKind {
#[inline]
fn from(num: f64) -> Self {
Self::Num(num)
}
}
impl From<i32> for LiteralKind {
#[inline]
fn from(i: i32) -> Self {
Self::Int(i)
}
}
impl From<BigInt> for LiteralKind {
#[inline]
fn from(i: BigInt) -> Self {
Self::BigInt(Box::new(i))
}
}
impl From<Box<BigInt>> for LiteralKind {
#[inline]
fn from(i: Box<BigInt>) -> Self {
Self::BigInt(i)
}
}
impl From<bool> for LiteralKind {
#[inline]
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl ToInternedString for LiteralKind {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
match *self {
Self::String(st) => {
format!("\"{}\"", interner.resolve_expect(st))
}
Self::Num(num) => num.to_string(),
Self::Int(num) => num.to_string(),
Self::BigInt(ref num) => format!("{num}n"),
Self::Bool(v) => v.to_string(),
Self::Null => "null".to_owned(),
Self::Undefined => "undefined".to_owned(),
}
}
}