parseCascade parses the cascade directive. Two formats: 1. @cascade 2. @cascade(pred1, pred2, ...)
(it *lex.ItemIterator, gq *GraphQuery)
| 2297 | // 1. @cascade |
| 2298 | // 2. @cascade(pred1, pred2, ...) |
| 2299 | func parseCascade(it *lex.ItemIterator, gq *GraphQuery) error { |
| 2300 | item := it.Item() |
| 2301 | items, err := it.Peek(1) |
| 2302 | if err != nil { |
| 2303 | return item.Errorf("Unable to peek lexer after cascade") |
| 2304 | } |
| 2305 | |
| 2306 | // check if it is without any args: |
| 2307 | // 1. @cascade { |
| 2308 | // 2. @cascade } |
| 2309 | // 3. @cascade @ |
| 2310 | // 4. @cascade\n someOtherPred |
| 2311 | if items[0].Typ == itemLeftCurl || items[0].Typ == itemRightCurl || items[0]. |
| 2312 | Typ == itemAt || items[0].Typ == itemName { |
| 2313 | // __all__ implies @cascade i.e. implies values for all the children are mandatory. |
| 2314 | gq.Cascade = append(gq.Cascade, "__all__") |
| 2315 | return nil |
| 2316 | } |
| 2317 | |
| 2318 | count := 0 |
| 2319 | expectArg := true |
| 2320 | it.Next() |
| 2321 | item = it.Item() |
| 2322 | if item.Typ != itemLeftRound { |
| 2323 | return item.Errorf("Expected a left round after cascade, got: %s", item.String()) |
| 2324 | } |
| 2325 | |
| 2326 | loop: |
| 2327 | for it.Next() { |
| 2328 | item := it.Item() |
| 2329 | switch item.Typ { |
| 2330 | case itemRightRound: |
| 2331 | break loop |
| 2332 | case itemComma: |
| 2333 | if expectArg { |
| 2334 | return item.Errorf("Expected a predicate but got comma") |
| 2335 | } |
| 2336 | expectArg = true |
| 2337 | case itemName: |
| 2338 | if !expectArg { |
| 2339 | return item.Errorf("Expected a comma or right round but got: %v", item.Val) |
| 2340 | } |
| 2341 | gq.Cascade = append(gq.Cascade, collectName(it, item.Val)) |
| 2342 | count++ |
| 2343 | expectArg = false |
| 2344 | default: |
| 2345 | return item.Errorf("Unexpected item while parsing: %v", item.Val) |
| 2346 | } |
| 2347 | } |
| 2348 | if expectArg { |
| 2349 | // use the initial item to report error line and column numbers |
| 2350 | return item.Errorf("Unnecessary comma in cascade()") |
| 2351 | } |
| 2352 | if count == 0 { |
| 2353 | return item.Errorf("At least one predicate required in parameterized cascade()") |
| 2354 | } |
| 2355 | return nil |
| 2356 | } |