hcl/parse.y

147 lines
1.7 KiB
Plaintext
Raw Normal View History

2014-07-31 20:56:51 +00:00
// This is the yacc input for creating the parser for HCL.
%{
package hcl
%}
%union {
2014-08-01 01:44:21 +00:00
list []Node
listitem Node
2014-07-31 20:56:51 +00:00
num int
2014-08-01 01:44:21 +00:00
obj ObjectNode
2014-07-31 20:56:51 +00:00
str string
}
%type <list> list objectlist
%type <listitem> listitem objectitem
%type <obj> block object
2014-07-31 21:50:53 +00:00
%type <str> blockId
2014-07-31 20:56:51 +00:00
%token <num> NUMBER
2014-08-01 18:18:47 +00:00
%token <str> COLON COMMA IDENTIFIER EQUAL NEWLINE STRING
2014-08-01 01:44:21 +00:00
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET
2014-07-31 20:56:51 +00:00
%%
top:
2014-08-01 01:44:21 +00:00
objectlist
2014-07-31 20:56:51 +00:00
{
2014-08-01 01:44:21 +00:00
hclResult = &ObjectNode{
Key: "",
Elem: $1,
2014-08-01 01:44:21 +00:00
}
2014-07-31 20:56:51 +00:00
}
2014-08-01 01:44:21 +00:00
objectlist:
objectitem
2014-07-31 20:56:51 +00:00
{
$$ = []Node{$1}
2014-07-31 21:50:53 +00:00
}
| objectitem objectlist
2014-08-01 01:44:21 +00:00
{
$$ = append($2, $1)
2014-08-01 01:44:21 +00:00
}
object:
LEFTBRACE objectlist RIGHTBRACE
{
$$ = ObjectNode{Elem: $2}
}
| LEFTBRACE RIGHTBRACE
{
$$ = ObjectNode{}
}
objectitem:
2014-08-01 01:44:21 +00:00
IDENTIFIER EQUAL NUMBER
2014-07-31 21:50:53 +00:00
{
$$ = AssignmentNode{
Key: $1,
Value: LiteralNode{
Type: ValueTypeInt,
Value: $3,
2014-08-01 01:44:21 +00:00
},
}
2014-07-31 21:50:53 +00:00
}
| IDENTIFIER EQUAL STRING
{
$$ = AssignmentNode{
Key: $1,
Value: LiteralNode{
Type: ValueTypeString,
Value: $3,
2014-08-01 01:44:21 +00:00
},
}
}
| IDENTIFIER EQUAL object
{
$$ = AssignmentNode{
Key: $1,
Value: $3,
}
}
2014-08-01 01:44:21 +00:00
| IDENTIFIER EQUAL LEFTBRACKET list RIGHTBRACKET
{
$$ = AssignmentNode{
Key: $1,
Value: ListNode{Elem: $4},
2014-08-01 01:44:21 +00:00
}
2014-07-31 21:50:53 +00:00
}
| block
{
$$ = $1
}
block:
blockId object
2014-07-31 21:50:53 +00:00
{
$$ = $2
$$.Key = $1
2014-07-31 21:50:53 +00:00
}
| blockId block
{
2014-08-01 01:44:21 +00:00
$$ = ObjectNode{
Key: $1,
Elem: []Node{$2},
2014-08-01 01:44:21 +00:00
}
2014-07-31 21:50:53 +00:00
}
blockId:
IDENTIFIER
{
$$ = $1
}
| STRING
{
$$ = $1
2014-07-31 20:56:51 +00:00
}
2014-08-01 01:44:21 +00:00
list:
listitem
{
$$ = []Node{$1}
}
| list COMMA listitem
{
$$ = append($1, $3)
}
listitem:
2014-08-01 18:18:47 +00:00
NUMBER
2014-08-01 01:44:21 +00:00
{
$$ = LiteralNode{
Type: ValueTypeInt,
Value: $1,
}
2014-08-01 01:44:21 +00:00
}
| STRING
{
$$ = LiteralNode{
Type: ValueTypeString,
Value: $1,
}
2014-08-01 01:44:21 +00:00
}
2014-07-31 20:56:51 +00:00
%%