| 1 |
%{ |
| 2 |
// Copyright (c) 2020-2022, Maxime Soulé |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// This source code is licensed under the BSD-style license found in the |
| 6 |
// LICENSE file in the root directory of this source tree. |
| 7 |
|
| 8 |
package json |
| 9 |
|
| 10 |
type Placeholder struct { |
| 11 |
Num int |
| 12 |
Name string |
| 13 |
} |
| 14 |
|
| 15 |
type Operator struct { |
| 16 |
Name string |
| 17 |
Params []any |
| 18 |
} |
| 19 |
|
| 20 |
type member struct { |
| 21 |
key string |
| 22 |
value any |
| 23 |
} |
| 24 |
|
| 25 |
func finalize(l yyLexer, value any) { |
| 26 |
l.(*json).value = value |
| 27 |
} |
| 28 |
%} |
| 29 |
|
| 30 |
%union { |
| 31 |
object map[string]any |
| 32 |
member member |
| 33 |
array []any |
| 34 |
string string |
| 35 |
value any |
| 36 |
} |
| 37 |
|
| 38 |
%start json |
| 39 |
|
| 40 |
%token <value> TRUE FALSE NULL NUMBER PLACEHOLDER SUB_PARSER |
| 41 |
%token <string> STRING OPERATOR |
| 42 |
|
| 43 |
%type <object> object members |
| 44 |
%type <member> member |
| 45 |
%type <array> array elements op_params |
| 46 |
%type <value> json value operator |
| 47 |
|
| 48 |
%% |
| 49 |
|
| 50 |
json: value |
| 51 |
{ |
| 52 |
$$ = $1 |
| 53 |
finalize(yylex, $$) |
| 54 |
} |
| 55 |
|
| 56 |
value: |
| 57 |
object { $$ = $1 } |
| 58 |
| array { $$ = $1 } |
| 59 |
| operator { $$ = $1 } |
| 60 |
| SUB_PARSER { $$ = $1 } |
| 61 |
| STRING { $$ = $1 } |
| 62 |
| NUMBER |
| 63 |
| TRUE |
| 64 |
| FALSE |
| 65 |
| NULL |
| 66 |
| PLACEHOLDER |
| 67 |
; |
| 68 |
|
| 69 |
object: '{' '}' |
| 70 |
{ |
| 71 |
$$ = map[string]any{} |
| 72 |
} |
| 73 |
| '{' members '}' |
| 74 |
{ |
| 75 |
$$ = $2 |
| 76 |
} |
| 77 |
| '{' members ',' '}' // not JSON spec but useful |
| 78 |
{ |
| 79 |
$$ = $2 |
| 80 |
} |
| 81 |
|
| 82 |
members: member |
| 83 |
{ |
| 84 |
$$ = map[string]any{ |
| 85 |
$1.key: $1.value, |
| 86 |
} |
| 87 |
} |
| 88 |
| members ',' member |
| 89 |
{ |
| 90 |
$1[$3.key] = $3.value |
| 91 |
$$ = $1 |
| 92 |
} |
| 93 |
|
| 94 |
member: STRING ':' value |
| 95 |
{ |
| 96 |
$$ = member{ |
| 97 |
key: $1, |
| 98 |
value: $3, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
array: '[' ']' |
| 103 |
{ |
| 104 |
$$ = []any{} |
| 105 |
} |
| 106 |
| '[' elements ']' |
| 107 |
{ |
| 108 |
$$ = $2 |
| 109 |
} |
| 110 |
| '[' elements ',' ']' // not JSON spec but useful |
| 111 |
{ |
| 112 |
$$ = $2 |
| 113 |
} |
| 114 |
|
| 115 |
elements: value |
| 116 |
{ |
| 117 |
$$ = []any{$1} |
| 118 |
} |
| 119 |
| elements ',' value |
| 120 |
{ |
| 121 |
$$ = append($1, $3) |
| 122 |
} |
| 123 |
|
| 124 |
op_params: '(' ')' |
| 125 |
{ |
| 126 |
$$ = []any{} |
| 127 |
} |
| 128 |
| '(' elements ')' |
| 129 |
{ |
| 130 |
$$ = $2 |
| 131 |
} |
| 132 |
| '(' elements ',' ')' |
| 133 |
{ |
| 134 |
$$ = $2 |
| 135 |
} |
| 136 |
|
| 137 |
operator: |
| 138 |
OPERATOR op_params |
| 139 |
{ |
| 140 |
op := yylex.(*json).newOperator($1, $2) |
| 141 |
if op == nil { |
| 142 |
return 1 |
| 143 |
} |
| 144 |
$$ = op |
| 145 |
} |
| 146 |
| OPERATOR |
| 147 |
{ |
| 148 |
op := yylex.(*json).newOperator($1, nil) |
| 149 |
if op == nil { |
| 150 |
return 1 |
| 151 |
} |
| 152 |
$$ = op |
| 153 |
} |
| 154 |
%% |