zclsyntax: parsing of the two unary operators

This commit is contained in:
Martin Atkins 2017-06-01 07:05:22 -07:00
parent 7f27d7b324
commit ab9bab3578
3 changed files with 59 additions and 0 deletions

View File

@ -90,3 +90,27 @@ func (e *BinaryOpExpr) Range() zcl.Range {
func (e *BinaryOpExpr) StartRange() zcl.Range {
return e.LHS.StartRange()
}
type UnaryOpExpr struct {
Op Operation
Val Expression
SrcRange zcl.Range
SymbolRange zcl.Range
}
func (e *UnaryOpExpr) walkChildNodes(w internalWalkFunc) {
e.Val = w(e.Val).(Expression)
}
func (e *UnaryOpExpr) Value(ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
panic("UnaryOpExpr.Value not yet implemented")
}
func (e *UnaryOpExpr) Range() zcl.Range {
return e.SrcRange
}
func (e *UnaryOpExpr) StartRange() zcl.Range {
return e.SymbolRange
}

View File

@ -26,3 +26,7 @@ func (e *LiteralValueExpr) Variables() []zcl.Traversal {
func (e *ScopeTraversalExpr) Variables() []zcl.Traversal {
return Variables(e)
}
func (e *UnaryOpExpr) Variables() []zcl.Traversal {
return Variables(e)
}

View File

@ -485,6 +485,37 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) {
}, nil
}
case TokenMinus:
tok := p.Read() // eat minus token
// Important to use parseExpressionTerm rather than parseExpression
// here, otherwise we can capture a following binary expression into
// our negation.
// e.g. -46+5 should parse as (-46)+5, not -(46+5)
operand, diags := p.parseExpressionTerm()
return &UnaryOpExpr{
Op: OpNegate,
Val: operand,
SrcRange: zcl.RangeBetween(tok.Range, operand.Range()),
SymbolRange: tok.Range,
}, diags
case TokenBang:
tok := p.Read() // eat bang token
// Important to use parseExpressionTerm rather than parseExpression
// here, otherwise we can capture a following binary expression into
// our negation.
operand, diags := p.parseExpressionTerm()
return &UnaryOpExpr{
Op: OpLogicalNot,
Val: operand,
SrcRange: zcl.RangeBetween(tok.Range, operand.Range()),
SymbolRange: tok.Range,
}, diags
default:
var diags zcl.Diagnostics
if !p.recovery {