Deeper parsing

This commit is contained in:
Mitchell Hashimoto 2014-07-31 14:50:53 -07:00
parent da4c96fbd5
commit 8e04dbf597
4 changed files with 76 additions and 4 deletions

View File

@ -16,6 +16,14 @@ func TestLex(t *testing.T) {
"comment.hcl",
[]int{IDENTIFIER, EQUAL, STRING, lexEOF},
},
{
"structure_basic.hcl",
[]int{
IDENTIFIER, LEFTBRACE,
IDENTIFIER, EQUAL, NUMBER,
RIGHTBRACE, lexEOF,
},
},
{
"structure.hcl",
[]int{

39
parse.y
View File

@ -11,7 +11,8 @@ package hcl
str string
}
%type <obj> object
%type <obj> block object
%type <str> blockId
%token <num> NUMBER
%token <str> IDENTIFIER EQUAL SEMICOLON STRING
@ -26,9 +27,41 @@ top:
}
object:
IDENTIFIER EQUAL STRING
object SEMICOLON
{
$$ = map[string]interface{}{$1: $3}
$$ = $1
}
| IDENTIFIER EQUAL NUMBER
{
$$ = map[string]interface{}{$1: []interface{}{$3}}
}
| IDENTIFIER EQUAL STRING
{
$$ = map[string]interface{}{$1: []interface{}{$3}}
}
| block
{
$$ = $1
}
block:
blockId LEFTBRACE object RIGHTBRACE
{
$$ = map[string]interface{}{$1: []interface{}{$3}}
}
| blockId block
{
$$ = map[string]interface{}{$1: []interface{}{$2}}
}
blockId:
IDENTIFIER
{
$$ = $1
}
| STRING
{
$$ = $1
}
%%

View File

@ -15,7 +15,35 @@ func TestParse(t *testing.T) {
{
"comment.hcl",
map[string]interface{}{
"foo": "bar",
"foo": []interface{}{"bar"},
},
},
{
"structure_basic.hcl",
map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"value": []interface{}{7},
},
},
},
},
{
"structure.hcl",
map[string]interface{}{
"foo": []interface{}{
map[string]interface{}{
"bar": []interface{}{
map[string]interface{}{
"baz": []interface{}{
map[string]interface{}{
"key": []interface{}{7},
},
},
},
},
},
},
},
},
}

View File

@ -0,0 +1,3 @@
foo {
value = 7
}