-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
309 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
"github.com/tomwright/dasel/v3/selector/ast" | ||
) | ||
|
||
func conditionalExprExecutor(e ast.ConditionalExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
cond, err := ExecuteAST(e.Cond, data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error evaluating condition: %w", err) | ||
} | ||
|
||
condBool, err := cond.BoolValue() | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting condition to boolean: %w", err) | ||
} | ||
|
||
if condBool { | ||
return ExecuteAST(e.Then, data) | ||
} | ||
|
||
if e.Else != nil { | ||
return ExecuteAST(e.Else, data) | ||
} | ||
|
||
return model.NewNullValue(), nil | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,9 @@ const ( | |
LessThanOrEqual | ||
Exclamation | ||
Null | ||
If | ||
Else | ||
ElseIf | ||
) | ||
|
||
type Tokens []Token | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package parser | ||
|
||
import ( | ||
"github.com/tomwright/dasel/v3/selector/ast" | ||
"github.com/tomwright/dasel/v3/selector/lexer" | ||
) | ||
|
||
func parseIfBody(p *Parser) (ast.Expr, error) { | ||
return p.parseExpressionsFromTo(lexer.OpenCurly, lexer.CloseCurly, []lexer.TokenKind{}, true, bpDefault) | ||
} | ||
|
||
func parseIfCondition(p *Parser) (ast.Expr, error) { | ||
return p.parseExpressionsFromTo(lexer.OpenParen, lexer.CloseParen, []lexer.TokenKind{}, true, bpDefault) | ||
} | ||
|
||
func parseIf(p *Parser) (ast.Expr, error) { | ||
p.pushScope(scopeIf) | ||
defer p.popScope() | ||
|
||
if err := p.expect(lexer.If); err != nil { | ||
return nil, err | ||
} | ||
p.advance() | ||
|
||
var exprs []*ast.ConditionalExpr | ||
|
||
process := func(parseCond bool) error { | ||
var err error | ||
var cond ast.Expr | ||
if parseCond { | ||
cond, err = parseIfCondition(p) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
body, err := parseIfBody(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
exprs = append(exprs, &ast.ConditionalExpr{ | ||
Cond: cond, | ||
Then: body, | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
if err := process(true); err != nil { | ||
return nil, err | ||
} | ||
|
||
for p.current().IsKind(lexer.ElseIf) { | ||
p.advance() | ||
|
||
if err := process(true); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if p.current().IsKind(lexer.Else) { | ||
p.advance() | ||
|
||
body, err := parseIfBody(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
exprs[len(exprs)-1].Else = body | ||
} | ||
|
||
for i := len(exprs) - 1; i >= 0; i-- { | ||
if i > 0 { | ||
exprs[i-1].Else = *exprs[i] | ||
} | ||
} | ||
|
||
return *exprs[0], nil | ||
} | ||
|
||
func (p *Parser) parseExpressionsFromTo( | ||
from lexer.TokenKind, | ||
to lexer.TokenKind, | ||
splitOn []lexer.TokenKind, | ||
requireExpressions bool, | ||
bp bindingPower, | ||
) (ast.Expr, error) { | ||
if err := p.expect(from); err != nil { | ||
return nil, err | ||
} | ||
p.advance() | ||
|
||
t, err := p.parseExpressions( | ||
[]lexer.TokenKind{to}, | ||
splitOn, | ||
requireExpressions, | ||
bp, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return t, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.