hcl: complex keys can be set using strings

This commit is contained in:
Mitchell Hashimoto 2014-11-12 21:08:08 -08:00
parent 96d5b40433
commit ab2c4c3120
4 changed files with 122 additions and 92 deletions

View File

@ -45,6 +45,8 @@ func TestLex(t *testing.T) {
[]int{ []int{
IDENTIFIER, LEFTBRACE, IDENTIFIER, LEFTBRACE,
IDENTIFIER, EQUAL, NUMBER, IDENTIFIER, EQUAL, NUMBER,
STRING, EQUAL, NUMBER,
STRING, EQUAL, NUMBER,
RIGHTBRACE, lexEOF, RIGHTBRACE, lexEOF,
}, },
}, },

View File

@ -22,7 +22,7 @@ import (
%type <objlist> list listitems objectlist %type <objlist> list listitems objectlist
%type <obj> block number object objectitem %type <obj> block number object objectitem
%type <obj> listitem %type <obj> listitem
%type <str> blockId exp frac %type <str> blockId exp objectkey frac
%token <b> BOOL %token <b> BOOL
%token <num> NUMBER %token <num> NUMBER
@ -69,13 +69,23 @@ object:
} }
} }
objectkey:
IDENTIFIER
{
$$ = $1
}
| STRING
{
$$ = $1
}
objectitem: objectitem:
IDENTIFIER EQUAL number objectkey EQUAL number
{ {
$$ = $3 $$ = $3
$$.Key = $1 $$.Key = $1
} }
| IDENTIFIER EQUAL BOOL | objectkey EQUAL BOOL
{ {
$$ = &Object{ $$ = &Object{
Key: $1, Key: $1,
@ -83,7 +93,7 @@ objectitem:
Value: $3, Value: $3,
} }
} }
| IDENTIFIER EQUAL STRING | objectkey EQUAL STRING
{ {
$$ = &Object{ $$ = &Object{
Key: $1, Key: $1,
@ -91,12 +101,12 @@ objectitem:
Value: $3, Value: $3,
} }
} }
| IDENTIFIER EQUAL object | objectkey EQUAL object
{ {
$3.Key = $1 $3.Key = $1
$$ = $3 $$ = $3
} }
| IDENTIFIER EQUAL list | objectkey EQUAL list
{ {
$$ = &Object{ $$ = &Object{
Key: $1, Key: $1,

View File

@ -1,3 +1,5 @@
foo { foo {
value = 7 value = 7
"value" = 8
"complex::value" = 9
} }

188
hcl/y.go
View File

@ -60,75 +60,81 @@ const hclEofCode = 1
const hclErrCode = 2 const hclErrCode = 2
const hclMaxDepth = 200 const hclMaxDepth = 200
//line parse.y:248 //line parse.y:258
//line yacctab:1 //line yacctab:1
var hclExca = []int{ var hclExca = []int{
-1, 1, -1, 1,
1, -1, 1, -1,
-2, 0, -2, 0,
-1, 6,
9, 7,
-2, 17,
-1, 7,
9, 8,
-2, 18,
} }
const hclNprod = 33 const hclNprod = 35
const hclPrivate = 57344 const hclPrivate = 57344
var hclTokenNames []string var hclTokenNames []string
var hclStates []string var hclStates []string
const hclLast = 61 const hclLast = 63
var hclAct = []int{ var hclAct = []int{
32, 26, 3, 19, 9, 8, 27, 28, 29, 28, 34, 28, 3, 21, 10, 9, 29, 30, 31, 30,
29, 15, 22, 4, 22, 40, 7, 22, 16, 21, 31, 17, 24, 42, 6, 44, 45, 7, 18, 23,
12, 21, 20, 34, 21, 35, 8, 37, 31, 42, 13, 41, 22, 40, 24, 43, 1, 37, 9, 39,
43, 1, 4, 39, 4, 7, 38, 7, 36, 41, 36, 23, 2, 6, 6, 33, 7, 7, 14, 38,
24, 13, 22, 44, 7, 2, 12, 10, 34, 21, 26, 15, 24, 13, 24, 46, 25, 11, 36, 23,
33, 25, 5, 6, 30, 18, 0, 17, 23, 11, 35, 23, 27, 5, 4, 8, 32, 20, 19, 0,
14, 0, 16, 12,
} }
var hclPact = []int{ var hclPact = []int{
5, -1000, 5, -1000, -5, -1000, 33, -1000, -1000, 7, 6, -1000, 6, -1000, -5, -1000, -1000, -1000, 30, -1000,
-1000, -1000, 26, -1000, -1000, -1000, -1000, -1000, -1000, -11, 7, -1000, -1000, 26, -1000, -1000, -1000, -1000, -1000, -1000,
12, 9, -1000, 24, -1000, -9, -1000, 31, 28, 10, -1000, -11, 19, 39, -1000, 25, -1000, -9, -1000, 18,
23, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 16, 8, 9, -1000, -1000, -1000, -1000, -1000, -1000, -1000,
-1000, -1000, 37, -1000, -1000, -1000, -1000, -1000, -1000, 37, -1000, -1000,
} }
var hclPgo = []int{ var hclPgo = []int{
0, 3, 55, 54, 45, 52, 50, 47, 2, 0, 0, 3, 57, 56, 32, 53, 50, 47, 2, 0,
53, 1, 51, 31, 55, 1, 54, 52, 26,
} }
var hclR1 = []int{ var hclR1 = []int{
0, 13, 13, 4, 4, 7, 7, 8, 8, 8, 0, 14, 14, 4, 4, 7, 7, 12, 12, 8,
8, 8, 8, 5, 5, 10, 10, 2, 2, 3, 8, 8, 8, 8, 8, 5, 5, 10, 10, 2,
3, 3, 9, 9, 6, 6, 6, 6, 1, 1, 2, 3, 3, 3, 9, 9, 6, 6, 6, 6,
11, 11, 12, 1, 1, 11, 11, 13,
} }
var hclR2 = []int{ var hclR2 = []int{
0, 0, 1, 1, 2, 3, 2, 3, 3, 3, 0, 0, 1, 1, 2, 3, 2, 1, 1, 3,
3, 3, 1, 2, 2, 1, 1, 3, 2, 1, 3, 3, 3, 3, 1, 2, 2, 1, 1, 3,
3, 2, 1, 1, 1, 2, 2, 3, 2, 1, 2, 1, 3, 2, 1, 1, 1, 2, 2, 3,
2, 2, 2, 2, 1, 2, 2, 2,
} }
var hclChk = []int{ var hclChk = []int{
-1000, -13, -4, -8, 8, -5, -10, 11, -8, 9, -1000, -14, -4, -8, -12, -5, 8, 11, -10, -8,
-7, -5, 13, 8, -6, 4, 11, -7, -2, -1, 9, -7, -5, 13, 8, 11, -6, 4, 11, -7,
15, 12, 5, -4, 14, -12, -11, 17, 18, 19, -2, -1, 15, 12, 5, -4, 14, -13, -11, 17,
-3, 16, -9, -6, 11, -1, 14, -11, 5, 5, 18, 19, -3, 16, -9, -6, 11, -1, 14, -11,
5, 16, 6, 7, -9, 5, 5, 5, 16, 6, 7, -9,
} }
var hclDef = []int{ var hclDef = []int{
1, -2, 2, 3, 15, 12, 0, 16, 4, 0, 1, -2, 2, 3, 0, 14, -2, -2, 0, 4,
13, 14, 0, 15, 7, 8, 9, 10, 11, 24, 0, 15, 16, 0, 17, 18, 9, 10, 11, 12,
0, 0, 29, 0, 6, 25, 26, 0, 0, 0, 13, 26, 0, 0, 31, 0, 6, 27, 28, 0,
0, 18, 19, 22, 23, 28, 5, 27, 32, 30, 0, 0, 0, 20, 21, 24, 25, 30, 5, 29,
31, 17, 0, 21, 20, 34, 32, 33, 19, 0, 23, 22,
} }
var hclTok1 = []int{ var hclTok1 = []int{
@ -408,12 +414,22 @@ hcldefault:
} }
case 7: case 7:
//line parse.y:74 //line parse.y:74
{
hclVAL.str = hclS[hclpt-0].str
}
case 8:
//line parse.y:78
{
hclVAL.str = hclS[hclpt-0].str
}
case 9:
//line parse.y:84
{ {
hclVAL.obj = hclS[hclpt-0].obj hclVAL.obj = hclS[hclpt-0].obj
hclVAL.obj.Key = hclS[hclpt-2].str hclVAL.obj.Key = hclS[hclpt-2].str
} }
case 8: case 10:
//line parse.y:79 //line parse.y:89
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Key: hclS[hclpt-2].str, Key: hclS[hclpt-2].str,
@ -421,8 +437,8 @@ hcldefault:
Value: hclS[hclpt-0].b, Value: hclS[hclpt-0].b,
} }
} }
case 9: case 11:
//line parse.y:87 //line parse.y:97
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Key: hclS[hclpt-2].str, Key: hclS[hclpt-2].str,
@ -430,14 +446,14 @@ hcldefault:
Value: hclS[hclpt-0].str, Value: hclS[hclpt-0].str,
} }
} }
case 10: case 12:
//line parse.y:95 //line parse.y:105
{ {
hclS[hclpt-0].obj.Key = hclS[hclpt-2].str hclS[hclpt-0].obj.Key = hclS[hclpt-2].str
hclVAL.obj = hclS[hclpt-0].obj hclVAL.obj = hclS[hclpt-0].obj
} }
case 11: case 13:
//line parse.y:100 //line parse.y:110
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Key: hclS[hclpt-2].str, Key: hclS[hclpt-2].str,
@ -445,19 +461,19 @@ hcldefault:
Value: hclS[hclpt-0].objlist, Value: hclS[hclpt-0].objlist,
} }
} }
case 12: case 14:
//line parse.y:108 //line parse.y:118
{ {
hclVAL.obj = hclS[hclpt-0].obj hclVAL.obj = hclS[hclpt-0].obj
} }
case 13: case 15:
//line parse.y:114 //line parse.y:124
{ {
hclS[hclpt-0].obj.Key = hclS[hclpt-1].str hclS[hclpt-0].obj.Key = hclS[hclpt-1].str
hclVAL.obj = hclS[hclpt-0].obj hclVAL.obj = hclS[hclpt-0].obj
} }
case 14: case 16:
//line parse.y:119 //line parse.y:129
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Key: hclS[hclpt-1].str, Key: hclS[hclpt-1].str,
@ -465,64 +481,64 @@ hcldefault:
Value: []*Object{hclS[hclpt-0].obj}, Value: []*Object{hclS[hclpt-0].obj},
} }
} }
case 15:
//line parse.y:129
{
hclVAL.str = hclS[hclpt-0].str
}
case 16:
//line parse.y:133
{
hclVAL.str = hclS[hclpt-0].str
}
case 17: case 17:
//line parse.y:139 //line parse.y:139
{ {
hclVAL.objlist = hclS[hclpt-1].objlist hclVAL.str = hclS[hclpt-0].str
} }
case 18: case 18:
//line parse.y:143 //line parse.y:143
{ {
hclVAL.objlist = nil hclVAL.str = hclS[hclpt-0].str
} }
case 19: case 19:
//line parse.y:149 //line parse.y:149
{ {
hclVAL.objlist = []*Object{hclS[hclpt-0].obj} hclVAL.objlist = hclS[hclpt-1].objlist
} }
case 20: case 20:
//line parse.y:153 //line parse.y:153
{ {
hclVAL.objlist = append(hclS[hclpt-2].objlist, hclS[hclpt-0].obj) hclVAL.objlist = nil
} }
case 21: case 21:
//line parse.y:157 //line parse.y:159
{ {
hclVAL.objlist = hclS[hclpt-1].objlist hclVAL.objlist = []*Object{hclS[hclpt-0].obj}
} }
case 22: case 22:
//line parse.y:163 //line parse.y:163
{ {
hclVAL.obj = hclS[hclpt-0].obj hclVAL.objlist = append(hclS[hclpt-2].objlist, hclS[hclpt-0].obj)
} }
case 23: case 23:
//line parse.y:167 //line parse.y:167
{
hclVAL.objlist = hclS[hclpt-1].objlist
}
case 24:
//line parse.y:173
{
hclVAL.obj = hclS[hclpt-0].obj
}
case 25:
//line parse.y:177
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Type: ValueTypeString, Type: ValueTypeString,
Value: hclS[hclpt-0].str, Value: hclS[hclpt-0].str,
} }
} }
case 24: case 26:
//line parse.y:176 //line parse.y:186
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Type: ValueTypeInt, Type: ValueTypeInt,
Value: hclS[hclpt-0].num, Value: hclS[hclpt-0].num,
} }
} }
case 25: case 27:
//line parse.y:183 //line parse.y:193
{ {
fs := fmt.Sprintf("%d.%s", hclS[hclpt-1].num, hclS[hclpt-0].str) fs := fmt.Sprintf("%d.%s", hclS[hclpt-1].num, hclS[hclpt-0].str)
f, err := strconv.ParseFloat(fs, 64) f, err := strconv.ParseFloat(fs, 64)
@ -535,8 +551,8 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 26: case 28:
//line parse.y:196 //line parse.y:206
{ {
fs := fmt.Sprintf("%d%s", hclS[hclpt-1].num, hclS[hclpt-0].str) fs := fmt.Sprintf("%d%s", hclS[hclpt-1].num, hclS[hclpt-0].str)
f, err := strconv.ParseFloat(fs, 64) f, err := strconv.ParseFloat(fs, 64)
@ -549,8 +565,8 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 27: case 29:
//line parse.y:209 //line parse.y:219
{ {
fs := fmt.Sprintf("%d.%s%s", hclS[hclpt-2].num, hclS[hclpt-1].str, hclS[hclpt-0].str) fs := fmt.Sprintf("%d.%s%s", hclS[hclpt-2].num, hclS[hclpt-1].str, hclS[hclpt-0].str)
f, err := strconv.ParseFloat(fs, 64) f, err := strconv.ParseFloat(fs, 64)
@ -563,28 +579,28 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 28:
//line parse.y:224
{
hclVAL.num = hclS[hclpt-0].num * -1
}
case 29:
//line parse.y:228
{
hclVAL.num = hclS[hclpt-0].num
}
case 30: case 30:
//line parse.y:234 //line parse.y:234
{ {
hclVAL.str = "e" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10) hclVAL.num = hclS[hclpt-0].num * -1
} }
case 31: case 31:
//line parse.y:238 //line parse.y:238
{ {
hclVAL.str = "e-" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10) hclVAL.num = hclS[hclpt-0].num
} }
case 32: case 32:
//line parse.y:244 //line parse.y:244
{
hclVAL.str = "e" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
}
case 33:
//line parse.y:248
{
hclVAL.str = "e-" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
}
case 34:
//line parse.y:254
{ {
hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10) hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
} }