github.com/mithrandie/csvq@v1.18.1/lib/json/path_parser.go (about) 1 // Code generated by goyacc -p jp -o lib/json/path_parser.go -v lib/json/path_parser.output lib/json/path_parser.y. DO NOT EDIT. 2 3 //line lib/json/path_parser.y:2 4 package json 5 6 import __yyfmt__ "fmt" 7 8 //line lib/json/path_parser.y:2 9 10 //line lib/json/path_parser.y:5 11 type jpSymType struct { 12 yys int 13 expression PathExpression 14 member ObjectPath 15 token PathToken 16 } 17 18 const OBJECT_PATH = 57346 19 20 var jpToknames = [...]string{ 21 "$end", 22 "error", 23 "$unk", 24 "OBJECT_PATH", 25 "'.'", 26 } 27 28 var jpStatenames = [...]string{} 29 30 const jpEofCode = 1 31 const jpErrCode = 2 32 const jpInitialStackSize = 16 33 34 //line lib/json/path_parser.y:40 35 36 func ParsePath(src string) (PathExpression, error) { 37 l := new(PathLexer) 38 l.Init(src) 39 jpParse(l) 40 return l.path, l.err 41 } 42 43 //line yacctab:1 44 var jpExca = [...]int{ 45 -1, 1, 46 1, -1, 47 -2, 0, 48 } 49 50 const jpPrivate = 57344 51 52 const jpLast = 7 53 54 var jpAct = [...]int{ 55 4, 2, 3, 1, 0, 0, 5, 56 } 57 58 var jpPact = [...]int{ 59 -2, -1000, -1000, -5, -2, -1000, 60 } 61 62 var jpPgo = [...]int{ 63 0, 3, 1, 64 } 65 66 var jpR1 = [...]int{ 67 0, 1, 1, 2, 2, 68 } 69 70 var jpR2 = [...]int{ 71 0, 0, 1, 1, 3, 72 } 73 74 var jpChk = [...]int{ 75 -1000, -1, -2, 4, 5, -2, 76 } 77 78 var jpDef = [...]int{ 79 1, -2, 2, 3, 0, 4, 80 } 81 82 var jpTok1 = [...]int{ 83 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 84 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 85 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 86 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 87 3, 3, 3, 3, 3, 3, 5, 88 } 89 90 var jpTok2 = [...]int{ 91 2, 3, 4, 92 } 93 94 var jpTok3 = [...]int{ 95 0, 96 } 97 98 var jpErrorMessages = [...]struct { 99 state int 100 token int 101 msg string 102 }{} 103 104 //line yaccpar:1 105 106 /* parser for yacc output */ 107 108 var ( 109 jpDebug = 0 110 jpErrorVerbose = false 111 ) 112 113 type jpLexer interface { 114 Lex(lval *jpSymType) int 115 Error(s string) 116 } 117 118 type jpParser interface { 119 Parse(jpLexer) int 120 Lookahead() int 121 } 122 123 type jpParserImpl struct { 124 lval jpSymType 125 stack [jpInitialStackSize]jpSymType 126 char int 127 } 128 129 func (p *jpParserImpl) Lookahead() int { 130 return p.char 131 } 132 133 func jpNewParser() jpParser { 134 return &jpParserImpl{} 135 } 136 137 const jpFlag = -1000 138 139 func jpTokname(c int) string { 140 if c >= 1 && c-1 < len(jpToknames) { 141 if jpToknames[c-1] != "" { 142 return jpToknames[c-1] 143 } 144 } 145 return __yyfmt__.Sprintf("tok-%v", c) 146 } 147 148 func jpStatname(s int) string { 149 if s >= 0 && s < len(jpStatenames) { 150 if jpStatenames[s] != "" { 151 return jpStatenames[s] 152 } 153 } 154 return __yyfmt__.Sprintf("state-%v", s) 155 } 156 157 func jpErrorMessage(state, lookAhead int) string { 158 const TOKSTART = 4 159 160 if !jpErrorVerbose { 161 return "syntax error" 162 } 163 164 for _, e := range jpErrorMessages { 165 if e.state == state && e.token == lookAhead { 166 return "syntax error: " + e.msg 167 } 168 } 169 170 res := "syntax error: unexpected " + jpTokname(lookAhead) 171 172 // To match Bison, suggest at most four expected tokens. 173 expected := make([]int, 0, 4) 174 175 // Look for shiftable tokens. 176 base := jpPact[state] 177 for tok := TOKSTART; tok-1 < len(jpToknames); tok++ { 178 if n := base + tok; n >= 0 && n < jpLast && jpChk[jpAct[n]] == tok { 179 if len(expected) == cap(expected) { 180 return res 181 } 182 expected = append(expected, tok) 183 } 184 } 185 186 if jpDef[state] == -2 { 187 i := 0 188 for jpExca[i] != -1 || jpExca[i+1] != state { 189 i += 2 190 } 191 192 // Look for tokens that we accept or reduce. 193 for i += 2; jpExca[i] >= 0; i += 2 { 194 tok := jpExca[i] 195 if tok < TOKSTART || jpExca[i+1] == 0 { 196 continue 197 } 198 if len(expected) == cap(expected) { 199 return res 200 } 201 expected = append(expected, tok) 202 } 203 204 // If the default action is to accept or reduce, give up. 205 if jpExca[i+1] != 0 { 206 return res 207 } 208 } 209 210 for i, tok := range expected { 211 if i == 0 { 212 res += ", expecting " 213 } else { 214 res += " or " 215 } 216 res += jpTokname(tok) 217 } 218 return res 219 } 220 221 func jplex1(lex jpLexer, lval *jpSymType) (char, token int) { 222 token = 0 223 char = lex.Lex(lval) 224 if char <= 0 { 225 token = jpTok1[0] 226 goto out 227 } 228 if char < len(jpTok1) { 229 token = jpTok1[char] 230 goto out 231 } 232 if char >= jpPrivate { 233 if char < jpPrivate+len(jpTok2) { 234 token = jpTok2[char-jpPrivate] 235 goto out 236 } 237 } 238 for i := 0; i < len(jpTok3); i += 2 { 239 token = jpTok3[i+0] 240 if token == char { 241 token = jpTok3[i+1] 242 goto out 243 } 244 } 245 246 out: 247 if token == 0 { 248 token = jpTok2[1] /* unknown char */ 249 } 250 if jpDebug >= 3 { 251 __yyfmt__.Printf("lex %s(%d)\n", jpTokname(token), uint(char)) 252 } 253 return char, token 254 } 255 256 func jpParse(jplex jpLexer) int { 257 return jpNewParser().Parse(jplex) 258 } 259 260 func (jprcvr *jpParserImpl) Parse(jplex jpLexer) int { 261 var jpn int 262 var jpVAL jpSymType 263 var jpDollar []jpSymType 264 _ = jpDollar // silence set and not used 265 jpS := jprcvr.stack[:] 266 267 Nerrs := 0 /* number of errors */ 268 Errflag := 0 /* error recovery flag */ 269 jpstate := 0 270 jprcvr.char = -1 271 jptoken := -1 // jprcvr.char translated into internal numbering 272 defer func() { 273 // Make sure we report no lookahead when not parsing. 274 jpstate = -1 275 jprcvr.char = -1 276 jptoken = -1 277 }() 278 jpp := -1 279 goto jpstack 280 281 ret0: 282 return 0 283 284 ret1: 285 return 1 286 287 jpstack: 288 /* put a state and value onto the stack */ 289 if jpDebug >= 4 { 290 __yyfmt__.Printf("char %v in %v\n", jpTokname(jptoken), jpStatname(jpstate)) 291 } 292 293 jpp++ 294 if jpp >= len(jpS) { 295 nyys := make([]jpSymType, len(jpS)*2) 296 copy(nyys, jpS) 297 jpS = nyys 298 } 299 jpS[jpp] = jpVAL 300 jpS[jpp].yys = jpstate 301 302 jpnewstate: 303 jpn = jpPact[jpstate] 304 if jpn <= jpFlag { 305 goto jpdefault /* simple state */ 306 } 307 if jprcvr.char < 0 { 308 jprcvr.char, jptoken = jplex1(jplex, &jprcvr.lval) 309 } 310 jpn += jptoken 311 if jpn < 0 || jpn >= jpLast { 312 goto jpdefault 313 } 314 jpn = jpAct[jpn] 315 if jpChk[jpn] == jptoken { /* valid shift */ 316 jprcvr.char = -1 317 jptoken = -1 318 jpVAL = jprcvr.lval 319 jpstate = jpn 320 if Errflag > 0 { 321 Errflag-- 322 } 323 goto jpstack 324 } 325 326 jpdefault: 327 /* default state action */ 328 jpn = jpDef[jpstate] 329 if jpn == -2 { 330 if jprcvr.char < 0 { 331 jprcvr.char, jptoken = jplex1(jplex, &jprcvr.lval) 332 } 333 334 /* look through exception table */ 335 xi := 0 336 for { 337 if jpExca[xi+0] == -1 && jpExca[xi+1] == jpstate { 338 break 339 } 340 xi += 2 341 } 342 for xi += 2; ; xi += 2 { 343 jpn = jpExca[xi+0] 344 if jpn < 0 || jpn == jptoken { 345 break 346 } 347 } 348 jpn = jpExca[xi+1] 349 if jpn < 0 { 350 goto ret0 351 } 352 } 353 if jpn == 0 { 354 /* error ... attempt to resume parsing */ 355 switch Errflag { 356 case 0: /* brand new error */ 357 jplex.Error(jpErrorMessage(jpstate, jptoken)) 358 Nerrs++ 359 if jpDebug >= 1 { 360 __yyfmt__.Printf("%s", jpStatname(jpstate)) 361 __yyfmt__.Printf(" saw %s\n", jpTokname(jptoken)) 362 } 363 fallthrough 364 365 case 1, 2: /* incompletely recovered error ... try again */ 366 Errflag = 3 367 368 /* find a state where "error" is a legal shift action */ 369 for jpp >= 0 { 370 jpn = jpPact[jpS[jpp].yys] + jpErrCode 371 if jpn >= 0 && jpn < jpLast { 372 jpstate = jpAct[jpn] /* simulate a shift of "error" */ 373 if jpChk[jpstate] == jpErrCode { 374 goto jpstack 375 } 376 } 377 378 /* the current p has no shift on "error", pop stack */ 379 if jpDebug >= 2 { 380 __yyfmt__.Printf("error recovery pops state %d\n", jpS[jpp].yys) 381 } 382 jpp-- 383 } 384 /* there is no state on the stack with an error shift ... abort */ 385 goto ret1 386 387 case 3: /* no shift yet; clobber input char */ 388 if jpDebug >= 2 { 389 __yyfmt__.Printf("error recovery discards %s\n", jpTokname(jptoken)) 390 } 391 if jptoken == jpEofCode { 392 goto ret1 393 } 394 jprcvr.char = -1 395 jptoken = -1 396 goto jpnewstate /* try again in the same state */ 397 } 398 } 399 400 /* reduction by production jpn */ 401 if jpDebug >= 2 { 402 __yyfmt__.Printf("reduce %v in:\n\t%v\n", jpn, jpStatname(jpstate)) 403 } 404 405 jpnt := jpn 406 jppt := jpp 407 _ = jppt // guard against "declared and not used" 408 409 jpp -= jpR2[jpn] 410 // jpp is now the index of $0. Perform the default action. Iff the 411 // reduced production is ε, $1 is possibly out of range. 412 if jpp+1 >= len(jpS) { 413 nyys := make([]jpSymType, len(jpS)*2) 414 copy(nyys, jpS) 415 jpS = nyys 416 } 417 jpVAL = jpS[jpp+1] 418 419 /* consult goto table to find next state */ 420 jpn = jpR1[jpn] 421 jpg := jpPgo[jpn] 422 jpj := jpg + jpS[jpp].yys + 1 423 424 if jpj >= jpLast { 425 jpstate = jpAct[jpg] 426 } else { 427 jpstate = jpAct[jpj] 428 if jpChk[jpstate] != -jpn { 429 jpstate = jpAct[jpg] 430 } 431 } 432 // dummy call; replaced with literal code 433 switch jpnt { 434 435 case 1: 436 jpDollar = jpS[jppt-0 : jppt+1] 437 //line lib/json/path_parser.y:20 438 { 439 jpVAL.expression = ObjectPath{} 440 jplex.(*PathLexer).path = jpVAL.expression 441 } 442 case 2: 443 jpDollar = jpS[jppt-1 : jppt+1] 444 //line lib/json/path_parser.y:25 445 { 446 jpVAL.expression = jpDollar[1].member 447 jplex.(*PathLexer).path = jpVAL.expression 448 } 449 case 3: 450 jpDollar = jpS[jppt-1 : jppt+1] 451 //line lib/json/path_parser.y:32 452 { 453 jpVAL.member = ObjectPath{Name: jpDollar[1].token.Literal} 454 } 455 case 4: 456 jpDollar = jpS[jppt-3 : jppt+1] 457 //line lib/json/path_parser.y:36 458 { 459 jpVAL.member = ObjectPath{Name: jpDollar[1].token.Literal, Child: jpDollar[3].member} 460 } 461 } 462 goto jpstack /* stack new state and value */ 463 }