|
| 1 | +%{ |
| 2 | +package parser |
| 3 | + |
| 4 | +import ( |
| 5 | + "github.com/cockroachdb/cockroach/pkg/sql/scanner" |
| 6 | + "github.com/cockroachdb/cockroach/pkg/util/jsonpath" |
| 7 | +) |
| 8 | + |
| 9 | +%} |
| 10 | + |
| 11 | +%{ |
| 12 | + |
| 13 | +var _ scanner.ScanSymType = &jsonpathSymType{} |
| 14 | + |
| 15 | +func (s *jsonpathSymType) jsonpathScanSymType() {} |
| 16 | + |
| 17 | +// ID implements the scanner.ScanSymType interface. |
| 18 | +func (s *jsonpathSymType) ID() int32 { |
| 19 | + return s.id |
| 20 | +} |
| 21 | + |
| 22 | +// SetID implements the scanner.ScanSymType interface. |
| 23 | +func (s *jsonpathSymType) SetID(id int32) { |
| 24 | + s.id = id |
| 25 | +} |
| 26 | + |
| 27 | +// Pos implements the scanner.ScanSymType interface. |
| 28 | +func (s *jsonpathSymType) Pos() int32 { |
| 29 | + return s.pos |
| 30 | +} |
| 31 | + |
| 32 | +// SetPos implements the scanner.ScanSymType interface. |
| 33 | +func (s *jsonpathSymType) SetPos(pos int32) { |
| 34 | + s.pos = pos |
| 35 | +} |
| 36 | + |
| 37 | +// Str implements the scanner.ScanSymType interface. |
| 38 | +func (s *jsonpathSymType) Str() string { |
| 39 | + return s.str |
| 40 | +} |
| 41 | + |
| 42 | +// SetStr implements the scanner.ScanSymType interface. |
| 43 | +func (s *jsonpathSymType) SetStr(str string) { |
| 44 | + s.str = str |
| 45 | +} |
| 46 | + |
| 47 | +// UnionVal implements the scanner.ScanSymType interface. |
| 48 | +func (s *jsonpathSymType) UnionVal() interface{} { |
| 49 | + return s.union.val |
| 50 | +} |
| 51 | + |
| 52 | +// SetUnionVal implements the scanner.ScanSymType interface. |
| 53 | +func (s *jsonpathSymType) SetUnionVal(val interface{}) { |
| 54 | + s.union.val = val |
| 55 | +} |
| 56 | + |
| 57 | +type jsonpathSymUnion struct { |
| 58 | + val interface{} |
| 59 | +} |
| 60 | + |
| 61 | +func (u *jsonpathSymUnion) expr() jsonpath.Expr { |
| 62 | + return u.val.(jsonpath.Expr) |
| 63 | +} |
| 64 | + |
| 65 | +func (u *jsonpathSymUnion) accessor() jsonpath.Accessor { |
| 66 | + return u.val.(jsonpath.Accessor) |
| 67 | +} |
| 68 | + |
| 69 | +func (u *jsonpathSymUnion) query() jsonpath.Query { |
| 70 | + return u.val.(jsonpath.Query) |
| 71 | +} |
| 72 | + |
| 73 | +func (u *jsonpathSymUnion) root() jsonpath.Root { |
| 74 | + return u.val.(jsonpath.Root) |
| 75 | +} |
| 76 | + |
| 77 | +func (u *jsonpathSymUnion) key() jsonpath.Key { |
| 78 | + return u.val.(jsonpath.Key) |
| 79 | +} |
| 80 | + |
| 81 | +func (u *jsonpathSymUnion) wildcard() jsonpath.Wildcard { |
| 82 | + return u.val.(jsonpath.Wildcard) |
| 83 | +} |
| 84 | + |
| 85 | +func (u *jsonpathSymUnion) bool() bool { |
| 86 | + return u.val.(bool) |
| 87 | +} |
| 88 | + |
| 89 | +%} |
| 90 | + |
| 91 | +%union{ |
| 92 | + id int32 |
| 93 | + pos int32 |
| 94 | + str string |
| 95 | + union jsonpathSymUnion |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Basic non-keyword token types. These are hard-wired into the core lexer. |
| 100 | + * They must be listed first so that their numeric codes do not depend on |
| 101 | + * the set of keywords. Keep this list in sync with backend/parser/gram.y! |
| 102 | + * |
| 103 | + * Some of these are not directly referenced in this file, but they must be |
| 104 | + * here anyway. |
| 105 | + */ |
| 106 | +%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op |
| 107 | +%token <*tree.NumVal> ICONST PARAM |
| 108 | +%token <str> TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER |
| 109 | +%token <str> LESS_EQUALS GREATER_EQUALS NOT_EQUALS |
| 110 | + |
| 111 | +%token <str> ERROR |
| 112 | + |
| 113 | +%token <str> STRICT |
| 114 | +%token <str> LAX |
| 115 | + |
| 116 | +%type <jsonpath.Expr> jsonpath |
| 117 | +%type <jsonpath.Expr> expr_or_predicate |
| 118 | +%type <jsonpath.Expr> expr |
| 119 | +%type <jsonpath.Expr> accessor_expr |
| 120 | +%type <jsonpath.Accessor> accessor_op |
| 121 | +%type <jsonpath.Accessor> path_primary |
| 122 | +%type <jsonpath.Accessor> key |
| 123 | +%type <str> key_name |
| 124 | +%type <jsonpath.Accessor> array_accessor |
| 125 | +%type <str> any_identifier |
| 126 | +%type <str> unreserved_keyword |
| 127 | +%type <bool> mode |
| 128 | + |
| 129 | +%% |
| 130 | + |
| 131 | +jsonpath: |
| 132 | + mode expr_or_predicate |
| 133 | + { |
| 134 | + jp := jsonpath.Jsonpath{Query: $2.query(), Strict: $1.bool()} |
| 135 | + jsonpathlex.(*lexer).SetJsonpath(jp) |
| 136 | + } |
| 137 | +; |
| 138 | + |
| 139 | +mode: |
| 140 | + STRICT |
| 141 | + { |
| 142 | + $$.val = true |
| 143 | + } |
| 144 | +| LAX |
| 145 | + { |
| 146 | + $$.val = false |
| 147 | + } |
| 148 | +| /* empty */ |
| 149 | + { |
| 150 | + $$.val = false |
| 151 | + } |
| 152 | +; |
| 153 | + |
| 154 | +expr_or_predicate: |
| 155 | + expr |
| 156 | + { |
| 157 | + $$.val = $1.query() |
| 158 | + } |
| 159 | +; |
| 160 | + |
| 161 | +expr: |
| 162 | + accessor_expr |
| 163 | + { |
| 164 | + $$.val = $1.query() |
| 165 | + } |
| 166 | +; |
| 167 | + |
| 168 | +accessor_expr: |
| 169 | + path_primary |
| 170 | + { |
| 171 | + $$.val = jsonpath.Query{Accessors: []jsonpath.Accessor{$1.accessor()}} |
| 172 | + } |
| 173 | +| accessor_expr accessor_op |
| 174 | + { |
| 175 | + a := $1.query() |
| 176 | + a.Accessors = append(a.Accessors, $2.accessor()) |
| 177 | + $$.val = a |
| 178 | + } |
| 179 | +; |
| 180 | + |
| 181 | +path_primary: |
| 182 | + '$' |
| 183 | + { |
| 184 | + $$.val = jsonpath.Root{} |
| 185 | + } |
| 186 | +; |
| 187 | + |
| 188 | +accessor_op: |
| 189 | + '.' key |
| 190 | + { |
| 191 | + $$.val = $2.key() |
| 192 | + } |
| 193 | +| array_accessor |
| 194 | + { |
| 195 | + $$.val = $1.wildcard() |
| 196 | + } |
| 197 | +; |
| 198 | + |
| 199 | +key: |
| 200 | + key_name |
| 201 | + { |
| 202 | + $$.val = jsonpath.Key{Key: $1} |
| 203 | + } |
| 204 | +; |
| 205 | + |
| 206 | +key_name: |
| 207 | + any_identifier |
| 208 | + { |
| 209 | + $$ = $1 |
| 210 | + } |
| 211 | +; |
| 212 | + |
| 213 | +array_accessor: |
| 214 | + '[' '*' ']' |
| 215 | + { |
| 216 | + $$.val = jsonpath.Wildcard{} |
| 217 | + } |
| 218 | +; |
| 219 | + |
| 220 | +any_identifier: |
| 221 | + IDENT |
| 222 | +| unreserved_keyword |
| 223 | +; |
| 224 | + |
| 225 | +unreserved_keyword: |
| 226 | + STRICT |
| 227 | +| LAX |
| 228 | +; |
| 229 | + |
| 230 | +%% |
0 commit comments