File: Json.g4

package info (click to toggle)
sphinx-a4doc 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: python: 9,560; makefile: 15
file content (122 lines) | stat: -rw-r--r-- 2,697 bytes parent folder | download | duplicates (2)
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
/**
 * JSON (JavaScript Object Notation) is a lightweight data-interchange format.
 */
grammar Json;

/**
 * On top level, JSON consists of a single value. That value can be either
 * a complex structure (such as an `object` or an `array`) or a primitive
 * type (a `STRING` in double quotes, a `NUMBER`,
 * or ``true`` or ``false`` or ``null``).
 */
value
    : object
    | array
    | STRING
    | NUMBER
    | TRUE
    | FALSE
    | NULL
    ;

/**
 * Object is a collection of name/value pairs. In various languages,
 * this is realized as an object, record, struct, dictionary,
 * hash table, keyed list, or associative array.
 */
object
    : '(' (STRING ':' value (',' STRING ':' value)*)? ')'
    ;

/**
 * Array is an ordered list of values. In most languages, this is realized as
 * vector, list, array or sequence.
 */
array
    : '[' (value (',' value)*)? ']'
    ;

/**
 * A number is very much like a C or Java number,
 * except that the octal and hexadecimal formats are not used.
 */
//@ doc:name number
NUMBER
    : '-'? ('0' | [1-9] [0-9]*) ('.' [0-9]+)? EXPONENT?
    ;

//@ doc:inline
//@ doc:importance 0
fragment EXPONENT
    : ('e' | 'E')? ('+' | '-')? [0-9]+
    ;

/**
 * A string is a sequence of zero or more Unicode characters,
 * wrapped in double quotes, using backslash escapes.
 * A character is represented as a single character string.
 * A string is very much like a C or Java string.
 *
 * .. railroad-diagram::
 *
 *    - terminal: '"'
 *    - zero_or_more:
 *      - choice:
 *        - terminal: 'Any unicode character except " and \'
 *        - sequence:
 *          - terminal: '\'
 *          - choice:
 *              - [terminal: '"', comment: quotation mark]
 *              - [terminal: '\', comment: reverse solidus]
 *              - [terminal: '/', comment: solidus]
 *              - [terminal: 'b', comment: backspace]
 *              - [terminal: 'f', comment: formfeed]
 *              - [terminal: 'n', comment: newline]
 *              - [terminal: 'r', comment: carriage return]
 *              - [terminal: 't', comment: horizontal tab]
 *              - [terminal: 'u', terminal: 4 hexdecimal digits]
 *    - terminal: '"'
 */
//@ doc:name string
//@ doc:no-diagram
STRING
   : '"' (ESC | SAFECODEPOINT)* '"'
   ;

//@ doc:nodoc
fragment ESC
   : '\\' (["\\/bfnrt] | UNICODE)
   ;

//@ doc:nodoc
fragment UNICODE
   : 'u' HEX HEX HEX HEX
   ;

//@ doc:nodoc
fragment HEX
   : [0-9a-fA-F]
   ;

//@ doc: nodoc
fragment SAFECODEPOINT
   : ~ ["\\\u0000-\u001F]
   ;

//@ doc:nodoc
//@ doc:name true
TRUE
    : 'true'
    ;

//@ doc:nodoc
//@ doc:name false
FALSE
    : 'false'
    ;

//@ doc:nodoc
//@ doc:name null
NULL
    : 'null'
    ;