terraform/config/lang/lang.y

84 lines
1.3 KiB
Plaintext
Raw Normal View History

2015-01-11 21:38:45 +01:00
// This is the yacc input for creating the parser for interpolation
// expressions in Go. To build it, just run `go generate` on this
// package, as the lexer has the go generate pragma within it.
%{
package lang
import (
"github.com/hashicorp/terraform/config/lang/ast"
)
%}
%union {
2015-01-11 22:03:37 +01:00
node ast.Node
nodeList []ast.Node
str string
2015-01-11 21:38:45 +01:00
}
%token <str> STRING IDENTIFIER PROGRAM_BRACKET_LEFT PROGRAM_BRACKET_RIGHT
2015-01-11 22:03:37 +01:00
%token <str> PAREN_LEFT PAREN_RIGHT COMMA
2015-01-11 21:38:45 +01:00
%type <node> expr interpolation literal
2015-01-11 22:03:37 +01:00
%type <nodeList> args
2015-01-11 21:38:45 +01:00
%%
top:
literal
{
parserResult = $1
}
2015-01-11 22:03:37 +01:00
| interpolation
{
parserResult = $1
}
2015-01-11 21:38:45 +01:00
| literal interpolation
{
parserResult = &ast.Concat{
Exprs: []ast.Node{$1, $2},
}
}
interpolation:
PROGRAM_BRACKET_LEFT expr PROGRAM_BRACKET_RIGHT
{
$$ = $2
}
expr:
2015-01-11 22:03:37 +01:00
literal
{
$$ = $1
}
| IDENTIFIER
2015-01-11 21:38:45 +01:00
{
$$ = &ast.VariableAccess{Name: $1}
}
2015-01-11 22:03:37 +01:00
| IDENTIFIER PAREN_LEFT args PAREN_RIGHT
2015-01-11 21:38:45 +01:00
{
2015-01-11 22:03:37 +01:00
$$ = &ast.Call{Func: $1, Args: $3}
2015-01-11 21:38:45 +01:00
}
2015-01-11 22:03:37 +01:00
args:
{
$$ = nil
}
| args COMMA expr
{
$$ = append($1, $3)
}
| expr
{
$$ = append($$, $1)
}
2015-01-11 21:38:45 +01:00
literal:
STRING
{
$$ = &ast.LiteralNode{Value: $1, Type: ast.TypeString}
}
%%