zclsyntax: parsing of boolean/null literals and root variable references

This commit is contained in:
Martin Atkins 2017-06-01 06:56:45 -07:00
parent 76d29e4031
commit 7f27d7b324
2 changed files with 56 additions and 0 deletions

View File

@ -36,6 +36,30 @@ func TestExpressionParseAndValue(t *testing.T) {
cty.NumberIntVal(1),
1, // Unbalanced parentheses
},
{
`true`,
nil,
cty.True,
0,
},
{
`false`,
nil,
cty.False,
0,
},
{
`null`,
nil,
cty.NullVal(cty.DynamicPseudoType),
0,
},
{
`true true`,
nil,
cty.True,
1, // extra characters after expression
},
}
for _, test := range tests {

View File

@ -453,6 +453,38 @@ func (p *parser) parseExpressionTerm() (Expression, zcl.Diagnostics) {
SrcRange: tok.Range,
}, nil
case TokenIdent:
tok := p.Read() // eat identifier token
name := string(tok.Bytes)
switch name {
case "true":
return &LiteralValueExpr{
Val: cty.True,
SrcRange: tok.Range,
}, nil
case "false":
return &LiteralValueExpr{
Val: cty.False,
SrcRange: tok.Range,
}, nil
case "null":
return &LiteralValueExpr{
Val: cty.NullVal(cty.DynamicPseudoType),
SrcRange: tok.Range,
}, nil
default:
return &ScopeTraversalExpr{
Traversal: zcl.Traversal{
zcl.TraverseRoot{
Name: name,
SrcRange: tok.Range,
},
},
SrcRange: tok.Range,
}, nil
}
default:
var diags zcl.Diagnostics
if !p.recovery {