Skip to content

victornava/json-parser

Repository files navigation

JSON Parser

A basic JSON parser written in JavaScript. Just to understand how parsers work.

TODO

  • Elements
  • Empty values
  • Strings
  • Numbers
  • Arrays
  • Objects

Approach

There is function to parse each major element in the grammar. Each parsing function is reponsible for:

  • Detecting a sequence of characters matches an element
  • Producing the correcponding js element
  • Leaving the parser position in the right place

Is this a recursive descent parser?

Running the tests

./test

or

node parser.test.js <test filter>

References

Example tests come from these places.

Grammar (json.org)

json
    element

value
    object
    array
    string
    number
    "true"
    "false"
    "null"

object
    '{' ws '}'
    '{' members '}'

members
    member
    member ',' members

member
    ws string ws ':' element

array
    '[' ws ']'
    '[' elements ']'

elements
    element
    element ',' elements

element
    ws value ws

string
    '"' characters '"'

characters
    ""
    character characters

character
    '0020' . '10FFFF' - '"' - '\'
    '\' escape

escape
    '"'
    '\'
    '/'
    'b'
    'f'
    'n'
    'r'
    't'
    'u' hex hex hex hex

hex
    digit
    'A' . 'F'
    'a' . 'f'

number
    integer fraction exponent

integer
    digit
    onenine digits
    '-' digit
    '-' onenine digits

digits
    digit
    digit digits

digit
    '0'
    onenine

onenine
    '1' . '9'

fraction
    ""
    '.' digits

exponent
    ""
    'E' sign digits
    'e' sign digits

sign
    ""
    '+'
    '-'

ws
    ""
    '0020' ws
    '000A' ws
    '000D' ws
    '0009' ws

About

Basic JSON parser in JavaScript.

Resources

Stars

Watchers

Forks