hcl/parse.y

145 lines
1.6 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
}
2014-08-01 01:44:21 +00:00
%type <list> list
%type <listitem> listitem
%type <obj> block object objectlist
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 01:44:21 +00:00
%token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING
%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{
Elem: $1.Elem,
}
2014-07-31 20:56:51 +00:00
}
2014-08-01 01:44:21 +00:00
objectlist:
object
2014-07-31 20:56:51 +00:00
{
2014-07-31 21:50:53 +00:00
$$ = $1
}
2014-08-01 01:44:21 +00:00
| object objectlist
{
$$ = $1
for k, v := range $2.Elem {
if _, ok := $$.Elem[k]; ok {
$$.Elem[k] = append($$.Elem[k], v...)
} else {
$$.Elem[k] = v
}
}
}
object:
IDENTIFIER EQUAL NUMBER
2014-07-31 21:50:53 +00:00
{
2014-08-01 01:44:21 +00:00
$$ = ObjectNode{
Elem: map[string][]Node{
$1: []Node{
ValueNode{
Type: ValueTypeInt,
Value: $3,
},
},
},
}
2014-07-31 21:50:53 +00:00
}
| IDENTIFIER EQUAL STRING
{
2014-08-01 01:44:21 +00:00
$$ = ObjectNode{
Elem: map[string][]Node{
$1: []Node{
ValueNode{
Type: ValueTypeString,
Value: $3,
},
},
},
}
}
| IDENTIFIER EQUAL LEFTBRACKET list RIGHTBRACKET
{
$$ = ObjectNode{
Elem: map[string][]Node{
$1: $4,
},
}
2014-07-31 21:50:53 +00:00
}
| block
{
$$ = $1
}
block:
2014-08-01 01:44:21 +00:00
blockId LEFTBRACE objectlist RIGHTBRACE
2014-07-31 21:50:53 +00:00
{
2014-08-01 01:44:21 +00:00
$$ = ObjectNode{
Elem: map[string][]Node{
$1: []Node{$3},
},
}
2014-07-31 21:50:53 +00:00
}
| blockId block
{
2014-08-01 01:44:21 +00:00
$$ = ObjectNode{
Elem: map[string][]Node{
$1: []Node{$2},
},
}
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:
object
{
$$ = $1
}
| NUMBER
{
$$ = $1
}
| STRING
{
$$ = $1
}
2014-07-31 20:56:51 +00:00
%%