Skip to content

Instantly share code, notes, and snippets.

@jordanbtucker
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanbtucker/ddfcc0f049883bd3786d to your computer and use it in GitHub Desktop.
Save jordanbtucker/ddfcc0f049883bd3786d to your computer and use it in GitHub Desktop.
JSON5 Lexical and Syntactic Grammar

The JSON5 Grammar

JSON5.stringify produces a String that conforms to the following JSON5 grammar. JSON5.parse accepts a String that conforms to the JSON5 grammar.

The JSON5 Lexical Grammar

JSON5 is similar to ECMAScript source text in that it consists of a sequence of characters conforming to the rules of SourceCharacter. The JSON5 Lexical Grammar defines the tokens that make up a JSON5 text similar to the manner that the ECMAScript lexical grammar defines the tokens of an ECMAScript source test. The JSON5 lexical grammar shares some productions with the ECMAScript lexical grammar. All nonterminal symbols of the grammar that do not begin with the characters "JSON5" are defined by productions of the ECMAScript lexical grammar.

Syntax

JSON5WhiteSpace ::

<TAB>

<CR>

<LF>

<SP>

JSON5Comment ::

Comment

JSON5Identifier ::

JSON5IdentifierStart

JSON5Identifier JSON5IdentifierPart

JSON5IdentifierStart ::

JSON5Letter

$

_

JSON5IdentifierPart ::

JSON5IdentifierStart

DecimalDigits

JSON5Letter

A through Z or a through z

JSON5NullLiteral ::

NullLiteral

JSON5BooleanLiteral ::

BooleanLiteral

JSON5Number ::

StrNumericLiteral

JSON5String ::

StringLiteral

The JSON5 Syntactic Grammar

The JSON5 Syntactic Grammar defines a valid JSON5 text in terms of tokens defined by the JSON5 lexical grammar. The goal symbol of the grammar is JSON5Text.

Syntax

JSON5Text :

JSON5Value

JSON5Value :

JSON5NullLiteral

JSON5BooleanLiteral

JSON5Object

JSON5Array

JSON5String

JSON5Number

JSON5Object :

{ }

{ JSON5MemberList ,opt }

JSON5MemberList :

JSON5Member

JSON5MemberList , JSON5Member

JSON5Member :

JSON5MemberName : JSON5Value

JSON5MemberName :

JSON5Identifier

JSON5String

JSON5Array :

[ ]

[ JSON5ElementList ,opt ]

JSON5ElementList :

JSON5Value

JSON5ElementList , JSON5Value

The JSON5 Grammar
JSON5.stringify produces a String that conforms to the following JSON5 grammar.
JSON5.parse accepts a String that conforms to the JSON5 grammar.
The JSON5 Lexical Grammar
JSON5 is similar to ECMAScript source text in that it consists of a sequence of
characters conforming to the rules of SourceCharacter. The JSON5 Lexical Grammar
defines the tokens that make up a JSON5 text similar to the manner that the
ECMAScript lexical grammar defines the tokens of an ECMAScript source test. The
JSON5 lexical grammar shares some productions with the ECMAScript lexical
grammar. All nonterminal symbols of the grammar that do not begin with the
characters “JSON5” are defined by productions of the ECMAScript lexical grammar.
Syntax
JSON5WhiteSpace ::
<TAB>
<CR>
<LF>
<SP>
JSON5Comment ::
Comment
JSON5Identifier ::
JSON5IdentifierStart
JSON5Identifier JSON5IdentifierPart
JSON5IdentifierStart ::
JSON5Letter
$
_
JSON5IdentifierPart ::
JSON5IdentifierStart
DecimalDigits
JSON5Letter
A through Z or a through z
JSON5NullLiteral ::
NullLiteral
JSON5BooleanLiteral ::
BooleanLiteral
JSON5Number ::
StrNumericLiteral
JSON5String ::
StringLiteral
The JSON5 Syntactic Grammar
The JSON5 Syntactic Grammar defines a valid JSON5 text in terms of tokens
defined by the JSON5 lexical grammar. The goal symbol of the grammar is
JSON5Text.
Syntax
JSON5Text :
JSON5Value
JSON5Value :
JSON5NullLiteral
JSON5BooleanLiteral
JSON5Object
JSON5Array
JSON5String
JSON5Number
JSON5Object :
{ }
{ JSON5MemberList , opt }
JSON5MemberName :
JSON5Identifier
JSON5String
JSON5MemberList :
JSON5Member
JSON5MemberList , JSON5Member
JSON5Member :
JSON5MemberName : JSON5Value
JSON5Array :
[ ]
[ JSON5ElementList , opt ]
JSON5ElementList :
JSON5Value
JSON5ElementList , JSON5Value

JSON5 Grammar

This Gist describes the lexical and syntactic grammar of JSON5.

As of the latest update of this Gist, the canonical implementation of JSON5 (hereby referred to as "JSON5") does not conform to this grammar. Here are the known discrepancies:

  • JSON5 recognizes as whitespace all characters with a Unicode code point value of 0x20 or less.
  • JSON5 does not support the following escape sequences in strings:
    • \v
    • \ followed by a carriage return (\u000D)
    • \ followed by a carriage return (\u000D) and a line feed (\u000A)
  • JSON5 terminates a SingleLineComment at the first line feed (\u000A) but not at any other character defined by LineTerminator.

Work is being done to rectify these discrepancies.

The author of JSON5 has expressed that he would like to include support for Unicode characters and Unicode escape sequences in object member names. When that is implemented, the productions JSON5IdentifierStart, JSON5IdentifierPart, and JSON5Letter should be removed and the following production should be changed:

JSON5Identifier ::

IdentifierName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment