Skip to content

Commit

Permalink
wip: track token end position
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Dec 10, 2021
1 parent 61b8054 commit 5bec910
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
42 changes: 37 additions & 5 deletions pkg/exprparser/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,44 @@ func (impl *interperterImpl) Evaluate(input string) (interface{}, int, error) {

result, err2 := impl.evaluateNode(exprNode)

// todo: this does not work and we need to calculate/track the
// node end position
fmt.Printf("read text: %s offset: %d line %d column %d\n",
exprNode.Token().Value, exprNode.Token().Offset, exprNode.Token().Line, exprNode.Token().Column)
return result, impl.bytesRead(exprNode), err2
}

return result, len(exprNode.Token().Value), err2
func (impl *interperterImpl) bytesRead(exprNode actionlint.ExprNode) int {
switch node := exprNode.(type) {
case *actionlint.VariableNode:
return len(node.Token().Value)
case *actionlint.BoolNode:
return len(node.Token().Value)
case *actionlint.NullNode:
return len(node.Token().Value)
case *actionlint.IntNode:
return len(node.Token().Value)
case *actionlint.FloatNode:
return len(node.Token().Value)
case *actionlint.StringNode:
return len(node.Token().Value)
case *actionlint.IndexAccessNode:
return node.Index.Token().Offset + len(node.Index.Token().Value) + 1
case *actionlint.ObjectDerefNode:
return node.Receiver.Token().Offset + len(node.Property) + 1
case *actionlint.ArrayDerefNode:
return node.Receiver.Token().Offset + 2
case *actionlint.NotOpNode:
return 1
case *actionlint.CompareOpNode:
return node.Right.Token().Offset + len(node.Right.Token().Value)
case *actionlint.LogicalOpNode:
return node.Right.Token().Offset + len(node.Right.Token().Value)
case *actionlint.FuncCallNode:
if len(node.Args) == 0 {
return node.Token().Offset + len(node.Token().Value)
}
lastArg := node.Args[len(node.Args)-1]
return lastArg.Token().Offset + impl.bytesRead(lastArg) + 1
default:
panic(fmt.Sprintf("TODO: unknown node type: %s node: %+v", reflect.TypeOf(exprNode), exprNode))
}
}

func (impl *interperterImpl) evaluateNode(exprNode actionlint.ExprNode) (interface{}, error) {
Expand Down
10 changes: 1 addition & 9 deletions pkg/runner/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ func (ee expressionEvaluator) InterpolateWithStringCheck(in string) (string, boo
state = expressionStartDollar

default:
fmt.Printf("%d output %c\n", i, character)
output += string(character)
}

Expand Down Expand Up @@ -180,14 +179,11 @@ func (ee expressionEvaluator) InterpolateWithStringCheck(in string) (string, boo
if skip == 0 {
result, pos, err := ee.interpreter.Evaluate(in[i:])
if err != nil {
fmt.Printf("Failed to eval: %s\n", err)
return "", false
}

output += ee.toString(result)

fmt.Printf("read %d in '%s'\n", pos, in[i:pos])

if (pos - 1) == 0 {
state = expressionEndBracket1
} else {
Expand All @@ -204,14 +200,12 @@ func (ee expressionEvaluator) InterpolateWithStringCheck(in string) (string, boo
// todo: handle error
switch character {
case '}':
fmt.Printf("first closing bracket\n")
state = expressionEndBracket2
}
case expressionEndBracket2:
// todo: handle error
switch character {
case '}':
fmt.Printf("second closing bracket\n")
state = passThrough
}
}
Expand All @@ -220,12 +214,10 @@ func (ee expressionEvaluator) InterpolateWithStringCheck(in string) (string, boo
if state != passThrough {
switch state {
case expressionStartDollar, expressionStartBracket1, expressionStartBracket2, expressionEndBracket1, expressionEndBracket2:
return "qwerydfkjökyxfd", false
panic("unexpected state while parsing expression")
}
}

fmt.Printf("eval result of '%s' is '%s'\n", in, output)

return output, true
}

Expand Down

0 comments on commit 5bec910

Please sign in to comment.