| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "github.com/tomwright/dasel/v3/selector/ast" |
| 5 | "github.com/tomwright/dasel/v3/selector/lexer" |
| 6 | ) |
| 7 | |
| 8 | // parseFollowingSymbols deals with the expressions following symbols/variables, e.g. |
| 9 | // $this[0][1]['name'] |
| 10 | // foo['bar']['baz'][1] |
| 11 | func parseFollowingSymbol(p *Parser, prev ast.Expr) (ast.Expr, error) { |
| 12 | res := ast.Expressions{prev} |
| 13 | |
| 14 | for p.hasToken() { |
| 15 | if p.current().IsKind(lexer.Spread) { |
| 16 | p.advanceN(1) |
| 17 | res = append(res, ast.SpreadExpr{}) |
| 18 | continue |
| 19 | } |
| 20 | |
| 21 | // String based indexes |
| 22 | if p.current().IsKind(lexer.OpenBracket) { |
| 23 | |
| 24 | if p.peekN(1).IsKind(lexer.Spread) && p.peekN(2).IsKind(lexer.CloseBracket) { |
| 25 | p.advanceN(3) |
| 26 | res = append(res, ast.SpreadExpr{}) |
| 27 | continue |
| 28 | } |
| 29 | |
| 30 | if p.peekN(1).IsKind(lexer.Star) && p.peekN(2).IsKind(lexer.CloseBracket) { |
| 31 | p.advanceN(3) |
| 32 | res = append(res, ast.SpreadExpr{}) |
| 33 | continue |
| 34 | } |
| 35 | |
| 36 | e, err := parseIndexSquareBrackets(p, false) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | switch ex := e.(type) { |
| 41 | case ast.RangeExpr: |
| 42 | res = append(res, ex) |
| 43 | case ast.IndexExpr: |
| 44 | // Convert this to a property expr. This property executor deals |
| 45 | // with maps + arrays. |
| 46 | res = append(res, ast.PropertyExpr{ |
| 47 | Property: ex.Index, |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | //e, err := p.parseExpressionsFromTo(lexer.OpenBracket, lexer.CloseBracket, nil, true, bpDefault) |
| 52 | //if err != nil { |
| 53 | // return nil, err |
| 54 | //} |
| 55 | //res = append(res, ast.PropertyExpr{ |
| 56 | // Property: e, |
| 57 | //}) |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | break |
| 62 | } |
| 63 | |
| 64 | return ast.ChainExprs(res...), nil |
| 65 | } |
| 66 | |
| 67 | func parseSymbol(p *Parser, withFollowing bool, allowFunc bool) (ast.Expr, error) { |
| 68 | token := p.current() |
| 69 | |
| 70 | next := p.peek() |
| 71 | |
| 72 | // Handle functions |
| 73 | if next.IsKind(lexer.OpenParen) && allowFunc { |
| 74 | return parseFunc(p) |
| 75 | } |
| 76 | |
| 77 | prop := ast.PropertyExpr{ |
| 78 | Property: ast.StringExpr{Value: token.Value}, |
| 79 | } |
| 80 | |
| 81 | p.advance() |
| 82 | |
| 83 | if withFollowing { |
| 84 | res, err := parseFollowingSymbol(p, prop) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return res, nil |
| 89 | } |
| 90 | |
| 91 | return prop, nil |
| 92 | } |