terraform/config/lang/lang.y

135 lines
2.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-12 09:28:47 +01:00
token *parserToken
2015-01-11 21:38:45 +01:00
}
2015-01-12 09:28:47 +01:00
%token <str> PROGRAM_BRACKET_LEFT PROGRAM_BRACKET_RIGHT
%token <str> PROGRAM_STRING_START PROGRAM_STRING_END
2015-01-11 22:03:37 +01:00
%token <str> PAREN_LEFT PAREN_RIGHT COMMA
2015-01-11 21:38:45 +01:00
2015-01-12 17:53:27 +01:00
%token <token> IDENTIFIER INTEGER FLOAT STRING
2015-01-12 09:28:47 +01:00
%type <node> expr interpolation literal literalModeTop literalModeValue
2015-01-11 22:03:37 +01:00
%type <nodeList> args
2015-01-11 21:38:45 +01:00
%%
top:
{
parserResult = &ast.LiteralNode{
Value: "",
Type: ast.TypeString,
Posx: ast.Pos{Column: 1, Line: 1},
}
}
| literalModeTop
2015-01-11 21:38:45 +01:00
{
parserResult = $1
}
literalModeTop:
literalModeValue
2015-01-11 22:03:37 +01:00
{
$$ = $1
2015-01-11 22:03:37 +01:00
}
| literalModeTop literalModeValue
2015-01-11 21:38:45 +01:00
{
var result []ast.Node
if c, ok := $1.(*ast.Concat); ok {
result = append(c.Exprs, $2)
} else {
result = []ast.Node{$1, $2}
}
$$ = &ast.Concat{
Exprs: result,
2015-01-12 09:28:47 +01:00
Posx: result[0].Pos(),
2015-01-11 21:38:45 +01:00
}
}
literalModeValue:
literal
{
$$ = $1
}
| interpolation
{
$$ = $1
}
2015-01-11 21:38:45 +01:00
interpolation:
PROGRAM_BRACKET_LEFT expr PROGRAM_BRACKET_RIGHT
{
$$ = $2
}
expr:
literalModeTop
2015-01-11 22:03:37 +01:00
{
$$ = $1
}
2015-01-12 17:53:27 +01:00
| INTEGER
{
$$ = &ast.LiteralNode{
Value: $1.Value.(int),
Type: ast.TypeInt,
Posx: $1.Pos,
}
}
| FLOAT
{
$$ = &ast.LiteralNode{
Value: $1.Value.(float64),
Type: ast.TypeFloat,
Posx: $1.Pos,
}
}
2015-01-11 22:03:37 +01:00
| IDENTIFIER
2015-01-11 21:38:45 +01:00
{
2015-01-12 09:28:47 +01:00
$$ = &ast.VariableAccess{Name: $1.Value.(string), Posx: $1.Pos}
2015-01-11 21:38:45 +01:00
}
2015-01-11 22:03:37 +01:00
| IDENTIFIER PAREN_LEFT args PAREN_RIGHT
2015-01-11 21:38:45 +01:00
{
2015-01-12 09:28:47 +01:00
$$ = &ast.Call{Func: $1.Value.(string), Args: $3, Posx: $1.Pos}
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
{
2015-01-12 09:28:47 +01:00
$$ = &ast.LiteralNode{
Value: $1.Value.(string),
Type: ast.TypeString,
Posx: $1.Pos,
}
2015-01-11 21:38:45 +01:00
}
%%