parsec-js 0.3.1

JavaScript for Parsec: a parser for ECMAScript 2025 scripts and modules to a syntax tree with a span on every node, ESTree JSON out, and the lexer under it.
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway

//! Data from scripts: the state a page writes into a `<script>` as a
//! literal, read as JSON without running anything.
//!
//! [`literal`] turns an expression made only of literals into a [`Json`]
//! value, and refuses anything else with the span that stopped it.
//! [`page_state`] finds the shapes pages put their state in
//! (`window.__NUXT__ = …`, `var ytInitialData = …`,
//! `JSON.parse("…")`, Next.js's `self.__next_f.push(…)`) and reads each
//! one's value. [`json`] parses JSON text strictly, as `JSON.parse` does,
//! for state that's in a script as JSON, like Next.js's `__NEXT_DATA__`.
//!
//! Nothing is evaluated. No property is read, no function called and no
//! name looked up, with one exception: the arguments of a Nuxt function
//! that only returns a literal are put in place of its parameters.
//!
//! ```
//! use parsec_js::data::{Json, page_state};
//!
//! let src = r#"window.__STATE__ = {user: {name: "Ada", admin: !1}, cart: [1, , 3]};"#;
//! let found = page_state(src).unwrap();
//! assert_eq!(found[0].name, "window.__STATE__");
//! let value = found[0].value.as_ref().unwrap();
//! assert_eq!(
//!     value.to_json_string(),
//!     r#"{"user":{"name":"Ada","admin":false},"cart":[1,null,3]}"#
//! );
//! ```

mod json;
mod page;

use std::collections::HashMap;
use std::fmt;

use parsec_base::Span;

use crate::ast::{Ast, LiteralKind, Node, NodeId, PropertyKind, UnaryOp};
use crate::text;

pub use json::json;
pub use page::{Found, next_flight, page_state, page_state_in, script_type};

/// A JSON value. An object keeps its keys in the order they first
/// appear, each once.
#[derive(Clone, Debug, PartialEq)]
pub enum Json {
    Null,
    Bool(bool),
    /// Never NaN; it may be infinite, from `1e400`, which JSON writes
    /// as `null` as `JSON.stringify` does.
    Number(f64),
    String(String),
    Array(Vec<Json>),
    Object(Vec<(String, Json)>),
}

impl Json {
    /// The value as JSON text, the way `JSON.stringify` writes it: no
    /// spaces, numbers as JavaScript prints them, `-0` as `0`, and an
    /// infinite number as `null`.
    pub fn to_json_string(&self) -> String {
        let mut out = String::new();
        self.write(&mut out);
        out
    }

    fn write(&self, out: &mut String) {
        match self {
            Json::Null => out.push_str("null"),
            Json::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
            Json::Number(x) => text::write_js_number(out, *x),
            Json::String(s) => text::write_json_str(out, s),
            Json::Array(items) => {
                out.push('[');
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    item.write(out);
                }
                out.push(']');
            }
            Json::Object(entries) => {
                out.push('{');
                for (i, (key, value)) in entries.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    text::write_json_str(out, key);
                    out.push(':');
                    value.write(out);
                }
                out.push('}');
            }
        }
    }

    /// How much copying this value costs: a unit a value, plus its
    /// strings' bytes.
    fn weight(&self) -> usize {
        match self {
            Json::String(s) => 1 + s.len(),
            Json::Array(items) => 1 + items.iter().map(Json::weight).sum::<usize>(),
            Json::Object(entries) => {
                1 + entries
                    .iter()
                    .map(|(k, v)| k.len() + v.weight())
                    .sum::<usize>()
            }
            _ => 1,
        }
    }
}

/// Why a value isn't data, and where it stopped being.
///
/// `reason` is one of:
///
/// | Reason | What it was |
/// |---|---|
/// | `not-a-literal` | a name, a call, an operator, a function: anything that would have to run |
/// | `substitution` | a template's `${…}`; the span is the expression in it |
/// | `bigint` | a BigInt, which JSON has no way to write |
/// | `regexp` | a regular expression literal |
/// | `computed-key` | `{[k]: v}` |
/// | `method` | a method, getter or setter |
/// | `spread` | `...x` in an array, an object or arguments |
/// | `proto` | `__proto__: v`, which sets the prototype rather than a key |
/// | `not-a-number` | `-`, `+` or `!` on something other than a number (or, for `!`, a boolean) |
/// | `undefined` | the whole value is `undefined`, which JSON can't hold |
/// | `lone-surrogate` | a string with half a surrogate pair, which a Rust string can't hold |
/// | `parameters` | a Nuxt function's parameter that isn't a plain name |
/// | `function-body` | a Nuxt function that does more than return a value |
/// | `too-large` | substituting arguments would copy far more than the source holds |
/// | `json-syntax` | JSON text that `JSON.parse` would reject |
/// | `too-deep` | JSON nested deeper than [`DEFAULT_MAX_DEPTH`](crate::DEFAULT_MAX_DEPTH) |
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Refused {
    pub span: Span,
    pub reason: &'static str,
}

impl fmt::Display for Refused {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} at {}..{}",
            self.reason, self.span.start, self.span.end
        )
    }
}

impl std::error::Error for Refused {}

fn refuse<T>(span: Span, reason: &'static str) -> Result<T, Refused> {
    Err(Refused { span, reason })
}

/// The value of `expr`, an expression in `ast` parsed from `src`, if it's
/// made only of literals:
///
/// - objects with plain keys (names, strings, numbers), arrays, strings,
///   numbers, `true`, `false`, `null`, and templates without
///   substitutions;
/// - `-` and `+` on a number, `!` on a number or boolean (minifiers write
///   `!0` for `true` and `!1` for `false`), and parentheses;
/// - `undefined` and `void` on a literal, which, as in `JSON.stringify`,
///   leave a key out of an object and are `null` in an array; a hole in
///   an array is `null` too.
///
/// A key given twice keeps its first place and its last value, as in
/// JavaScript. Keys stay in source order; JavaScript would put the ones
/// that are array indices first. Anything else is refused: see
/// [`Refused`].
///
/// ```
/// use parsec_js::{Node, parse_script};
/// use parsec_js::data::literal;
///
/// let src = "x = {a: [1, -2.5, `t`], b: void 0, 'c': !0}";
/// let ast = parse_script(src).unwrap();
/// let Node::Program { body, .. } = *ast.node(ast.root()) else { unreachable!() };
/// let Node::Expression { expression, .. } = *ast.node(ast.get(body)[0]) else { unreachable!() };
/// let Node::Assign { right, .. } = *ast.node(expression) else { unreachable!() };
/// let value = literal(&ast, src, right).unwrap();
/// assert_eq!(value.to_json_string(), r#"{"a":[1,-2.5,"t"],"c":true}"#);
/// ```
pub fn literal(ast: &Ast, src: &str, expr: NodeId) -> Result<Json, Refused> {
    Reader::new(ast, src).whole(expr)
}

/// Reads literals out of a tree, with the parameters of a Nuxt function
/// bound to their arguments' values.
struct Reader<'a> {
    ast: &'a Ast,
    src: &'a str,
    /// A parameter's value, `None` for `undefined`.
    env: HashMap<String, Option<Json>>,
    /// What substituting may still copy, by [`Json::weight`].
    budget: usize,
}

impl<'a> Reader<'a> {
    fn new(ast: &'a Ast, src: &'a str) -> Reader<'a> {
        Reader {
            ast,
            src,
            env: HashMap::new(),
            budget: 0,
        }
    }

    /// The value of `id`, which may not be `undefined`.
    fn whole(&mut self, id: NodeId) -> Result<Json, Refused> {
        match self.value(id)? {
            Some(v) => Ok(v),
            None => refuse(self.ast.span(id), "undefined"),
        }
    }

    fn string(&self, span: Span, units: &[u16]) -> Result<String, Refused> {
        String::from_utf16(units).or_else(|_| refuse(span, "lone-surrogate"))
    }

    /// A string literal's value, decoded straight to a Rust string unless
    /// it has a surrogate.
    fn string_literal(&self, span: Span) -> Result<String, Refused> {
        let body = Span::new(span.start + 1, span.end - 1).text(self.src);
        match text::string_value_str(body) {
            Some(s) => Ok(s),
            None => self.string(span, &text::string_value(body)),
        }
    }

    /// The value of `id`, `None` for `undefined`.
    fn value(&mut self, id: NodeId) -> Result<Option<Json>, Refused> {
        let span = self.ast.span(id);
        let v = match *self.ast.node(id) {
            Node::Paren { expression } => return self.value(expression),
            Node::Literal(kind) => match kind {
                LiteralKind::String => Json::String(self.string_literal(span)?),
                LiteralKind::Number => Json::Number(text::number_value(span.text(self.src))),
                LiteralKind::Boolean => Json::Bool(span.text(self.src) == "true"),
                LiteralKind::Null => Json::Null,
                LiteralKind::BigInt => return refuse(span, "bigint"),
                LiteralKind::RegExp => return refuse(span, "regexp"),
            },
            Node::Template {
                quasis,
                expressions,
            } => {
                if let Some(&first) = self.ast.get(expressions).first() {
                    return refuse(self.ast.span(first), "substitution");
                }
                let quasi = self.ast.get(quasis)[0];
                let body = self.ast.span(quasi).text(self.src);
                // A template turns CR and CRLF into LF, so only one with
                // neither those nor an escape is its own text.
                if !body.contains(['\\', '\r']) {
                    Json::String(body.to_owned())
                } else {
                    // An untagged template's escapes are all valid, or it
                    // wouldn't have parsed.
                    let units = text::template_cooked(body).unwrap_or_default();
                    Json::String(self.string(span, &units)?)
                }
            }
            Node::Identifier => {
                let name = text::ident_name(span.text(self.src));
                if let Some(v) = self.env.get(name.as_ref()) {
                    let weight = v.as_ref().map_or(1, Json::weight);
                    if weight > self.budget {
                        return refuse(span, "too-large");
                    }
                    self.budget -= weight;
                    return Ok(v.clone());
                }
                if name == "undefined" {
                    return Ok(None);
                }
                return refuse(span, "not-a-literal");
            }
            Node::Unary { operator, argument } => {
                let arg = self.value(argument)?;
                return match (operator, arg) {
                    (UnaryOp::Void, _) => Ok(None),
                    (UnaryOp::Minus, Some(Json::Number(x))) => Ok(Some(Json::Number(-x))),
                    (UnaryOp::Plus, Some(Json::Number(x))) => Ok(Some(Json::Number(x))),
                    (UnaryOp::Not, Some(Json::Number(x))) => {
                        Ok(Some(Json::Bool(x == 0.0 || x.is_nan())))
                    }
                    (UnaryOp::Not, Some(Json::Bool(b))) => Ok(Some(Json::Bool(!b))),
                    (UnaryOp::Minus | UnaryOp::Plus | UnaryOp::Not, _) => {
                        refuse(self.ast.span(argument), "not-a-number")
                    }
                    _ => refuse(span, "not-a-literal"),
                };
            }
            Node::Array { elements, .. } => {
                let mut items = Vec::with_capacity(elements.len());
                for &e in self.ast.get(elements) {
                    match *self.ast.node(e) {
                        Node::Elision => items.push(Json::Null),
                        Node::Spread { .. } => return refuse(self.ast.span(e), "spread"),
                        _ => items.push(self.value(e)?.unwrap_or(Json::Null)),
                    }
                }
                Json::Array(items)
            }
            Node::Object { properties, .. } => {
                let mut object = Object::with_capacity(properties.len());
                for &p in self.ast.get(properties) {
                    let Node::Property {
                        key,
                        value,
                        kind,
                        method,
                        shorthand,
                        computed,
                    } = *self.ast.node(p)
                    else {
                        return refuse(self.ast.span(p), "spread");
                    };
                    if method || kind != PropertyKind::Init {
                        return refuse(self.ast.span(p), "method");
                    }
                    if computed {
                        return refuse(self.ast.span(key), "computed-key");
                    }
                    let name = self.key(key)?;
                    if name == "__proto__" && !shorthand {
                        return refuse(self.ast.span(key), "proto");
                    }
                    let v = self.value(value)?;
                    object.set(name, v);
                }
                object.finish()
            }
            _ => return refuse(span, "not-a-literal"),
        };
        Ok(Some(v))
    }

    /// A property key that isn't computed: a name, a string or a number.
    fn key(&self, key: NodeId) -> Result<String, Refused> {
        let span = self.ast.span(key);
        let raw = span.text(self.src);
        match *self.ast.node(key) {
            Node::Literal(LiteralKind::String) => self.string_literal(span),
            Node::Literal(LiteralKind::Number) => {
                let mut s = String::new();
                text::write_js_number(&mut s, text::number_value(raw));
                Ok(s)
            }
            Node::Literal(LiteralKind::BigInt) => refuse(span, "bigint"),
            _ => Ok(text::ident_name(raw).into_owned()),
        }
    }
}

/// An object being built: a key given again keeps its place and takes
/// the new value, and a key whose value is `undefined` is left out.
struct Object {
    entries: Vec<(String, Option<Json>)>,
    /// Each key's place, once there are more than [`Object::SCAN`] of
    /// them; below that a scan is cheaper than hashing.
    index: HashMap<String, usize>,
}

impl Object {
    const SCAN: usize = 16;

    fn with_capacity(n: usize) -> Object {
        Object {
            entries: Vec::with_capacity(n),
            index: HashMap::new(),
        }
    }

    fn set(&mut self, key: String, value: Option<Json>) {
        let at = if self.entries.len() <= Object::SCAN {
            self.entries.iter().position(|(k, _)| *k == key)
        } else {
            if self.index.is_empty() {
                self.index = (self.entries.iter().enumerate())
                    .map(|(i, (k, _))| (k.clone(), i))
                    .collect();
            }
            self.index.get(&key).copied()
        };
        match at {
            Some(i) => self.entries[i].1 = value,
            None => {
                if !self.index.is_empty() {
                    self.index.insert(key.clone(), self.entries.len());
                }
                self.entries.push((key, value));
            }
        }
    }

    fn finish(self) -> Json {
        Json::Object(
            self.entries
                .into_iter()
                .filter_map(|(k, v)| Some((k, v?)))
                .collect(),
        )
    }
}