Skip to content

Trivial attempt at using the JSON grammer under bun.sh dies on deep recursion #2

Description

@shadowcat-mst

Well, that didn't go to plan.

import { pegex } from 'pegex'

const grammar = `
# A simple grammar for the simple JSON data language.
# See https://github.com/ingydotnet/pegex-json-pm for a Parser implementation   
# that uses this grammar.

%grammar json
%version 0.0.1

json: map | seq

node: map | seq | scalar

map:
  / ~ LCURLY ~ /
  pair* % / ~ COMMA ~ /
  / ~ RCURLY ~ /

pair:
  string
  / ~ COLON ~ /
  node

seq:
  / ~ LSQUARE ~ /
  node* % / ~ COMMA ~ /
  / ~ RSQUARE ~ /

scalar:
  string |
  number |
  boolean |
  null

string: / # XXX Need to code this to spec.
  DOUBLE  # This works for simple cases,
    ((: # but doesn't handle all escaping yet.
      BACK BACK |
      BACK DOUBLE |
      [^ DOUBLE BREAK ]
    )*)
  DOUBLE
/

number: /(
  DASH?
  (: 0 | [1-9] DIGIT* )
  (: DOT DIGIT* )?
  (: [eE] [ DASH PLUS ]? DIGIT+ )?
)/

boolean: true | false

true: /true/

false: /false/

null: /null/
`

const parser = pegex(grammar)

console.log(parser.parse(`[ "foo" ]`))

/*

mallet3=; bun src/json.js 
381 |       return o;
382 |     }
383 |     c = new o.constructor();
384 |     for (k in o) {
385 |       v = o[k];
386 |       c[k] = clone(v);
                   ^
RangeError: Maximum call stack size exceeded.
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)
      at clone (/home/matthewt/.bun/install/cache/pegex@0.1.7@@@1/lib/pegex/pegex/ast.js:386:14)

Bun v1.2.0 (Linux x64)
*/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions