| 1584 | } |
| 1585 | |
| 1586 | func parseGeoArgs(it *lex.ItemIterator, g *Function) error { |
| 1587 | buf := new(bytes.Buffer) |
| 1588 | if _, err := buf.WriteString("["); err != nil { |
| 1589 | return err |
| 1590 | } |
| 1591 | depth := 1 |
| 1592 | loop: |
| 1593 | for { |
| 1594 | if valid := it.Next(); !valid { |
| 1595 | return it.Errorf("Got EOF while parsing Geo tokens") |
| 1596 | } |
| 1597 | item := it.Item() |
| 1598 | switch item.Typ { |
| 1599 | case itemLeftSquare: |
| 1600 | if _, err := buf.WriteString(item.Val); err != nil { |
| 1601 | return err |
| 1602 | } |
| 1603 | depth++ |
| 1604 | case itemRightSquare: |
| 1605 | if _, err := buf.WriteString(item.Val); err != nil { |
| 1606 | return err |
| 1607 | } |
| 1608 | depth-- |
| 1609 | case itemMathOp, itemComma, itemName: |
| 1610 | // Writing tokens to buffer. |
| 1611 | if _, err := buf.WriteString(item.Val); err != nil { |
| 1612 | return err |
| 1613 | } |
| 1614 | default: |
| 1615 | return item.Errorf("Found invalid item: %s while parsing geo arguments.", |
| 1616 | item.Val) |
| 1617 | } |
| 1618 | |
| 1619 | switch { |
| 1620 | case depth > 4 || depth < 0: |
| 1621 | return item.Errorf("Invalid bracket sequence") |
| 1622 | case depth == 0: |
| 1623 | break loop |
| 1624 | } |
| 1625 | } |
| 1626 | // Lets append the concatenated Geo token to Args. |
| 1627 | // TODO - See if we can directly encode to Geo format. |
| 1628 | g.Args = append(g.Args, Arg{Value: buf.String()}) |
| 1629 | items, err := it.Peek(1) |
| 1630 | if err != nil { |
| 1631 | return it.Errorf("Unexpected EOF while parsing args") |
| 1632 | } |
| 1633 | item := items[0] |
| 1634 | if item.Typ != itemRightRound && item.Typ != itemComma { |
| 1635 | return item.Errorf("Expected right round or comma. Got: %+v", |
| 1636 | items[0]) |
| 1637 | } |
| 1638 | return nil |
| 1639 | } |
| 1640 | |
| 1641 | // parseFuncArgs will try to parse the arguments inside an array ([]). If the values |
| 1642 | // are prefixed with $ they are treated as Dql variables, otherwise they are used as scalar values. |