hcl: trailing comma support in HCL

This commit is contained in:
Mitchell Hashimoto 2014-09-30 14:58:50 -07:00
parent b00a1b045d
commit 6606366746
5 changed files with 108 additions and 69 deletions

View File

@ -16,11 +16,12 @@ const lexEOF = 0
type hclLex struct { type hclLex struct {
Input string Input string
lastNumber bool lastNumber bool
pos int pos int
width int width int
col, line int col, line int
err error lastCol, lastLine int
err error
} }
// The parser calls this method to get each new token. // The parser calls this method to get each new token.
@ -82,7 +83,7 @@ func (x *hclLex) Lex(yylval *hclSymType) int {
case '-': case '-':
return MINUS return MINUS
case ',': case ',':
return COMMA return x.lexComma()
case '=': case '=':
return EQUAL return EQUAL
case '[': case '[':
@ -161,6 +162,27 @@ func (x *hclLex) consumeComment(c rune) bool {
} }
} }
// lexComma reads the comma
func (x *hclLex) lexComma() int {
for {
c := x.peek()
// Consume space
if unicode.IsSpace(c) {
x.next()
continue
}
if c == ']' {
return COMMAEND
}
break
}
return COMMA
}
// lexId lexes an identifier // lexId lexes an identifier
func (x *hclLex) lexId(yylval *hclSymType) int { func (x *hclLex) lexId(yylval *hclSymType) int {
var b bytes.Buffer var b bytes.Buffer

View File

@ -26,7 +26,7 @@ import (
%token <b> BOOL %token <b> BOOL
%token <num> NUMBER %token <num> NUMBER
%token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING MINUS %token <str> COMMA COMMAEND IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD %token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD
%token <str> EPLUS EMINUS %token <str> EPLUS EMINUS
@ -153,6 +153,10 @@ listitems:
{ {
$$ = append($1, $3) $$ = append($1, $3)
} }
| listitems COMMAEND
{
$$ = $1
}
listitem: listitem:
number number

View File

@ -27,6 +27,10 @@ func TestParse(t *testing.T) {
"empty.hcl", "empty.hcl",
false, false,
}, },
{
"list_comma.hcl",
false,
},
{ {
"multiple.hcl", "multiple.hcl",
false, false,

View File

@ -0,0 +1 @@
foo = [1, 2, "foo",]

132
hcl/y.go
View File

@ -22,23 +22,25 @@ type hclSymType struct {
const BOOL = 57346 const BOOL = 57346
const NUMBER = 57347 const NUMBER = 57347
const COMMA = 57348 const COMMA = 57348
const IDENTIFIER = 57349 const COMMAEND = 57349
const EQUAL = 57350 const IDENTIFIER = 57350
const NEWLINE = 57351 const EQUAL = 57351
const STRING = 57352 const NEWLINE = 57352
const MINUS = 57353 const STRING = 57353
const LEFTBRACE = 57354 const MINUS = 57354
const RIGHTBRACE = 57355 const LEFTBRACE = 57355
const LEFTBRACKET = 57356 const RIGHTBRACE = 57356
const RIGHTBRACKET = 57357 const LEFTBRACKET = 57357
const PERIOD = 57358 const RIGHTBRACKET = 57358
const EPLUS = 57359 const PERIOD = 57359
const EMINUS = 57360 const EPLUS = 57360
const EMINUS = 57361
var hclToknames = []string{ var hclToknames = []string{
"BOOL", "BOOL",
"NUMBER", "NUMBER",
"COMMA", "COMMA",
"COMMAEND",
"IDENTIFIER", "IDENTIFIER",
"EQUAL", "EQUAL",
"NEWLINE", "NEWLINE",
@ -58,7 +60,7 @@ const hclEofCode = 1
const hclErrCode = 2 const hclErrCode = 2
const hclMaxDepth = 200 const hclMaxDepth = 200
//line parse.y:244 //line parse.y:248
//line yacctab:1 //line yacctab:1
var hclExca = []int{ var hclExca = []int{
@ -67,65 +69,66 @@ var hclExca = []int{
-2, 0, -2, 0,
} }
const hclNprod = 32 const hclNprod = 33
const hclPrivate = 57344 const hclPrivate = 57344
var hclTokenNames []string var hclTokenNames []string
var hclStates []string var hclStates []string
const hclLast = 60 const hclLast = 61
var hclAct = []int{ var hclAct = []int{
32, 26, 3, 19, 42, 8, 27, 28, 29, 28, 32, 26, 3, 19, 9, 8, 27, 28, 29, 28,
29, 15, 22, 41, 22, 5, 9, 16, 21, 12, 29, 15, 22, 4, 22, 40, 7, 22, 16, 21,
21, 20, 11, 22, 40, 35, 8, 37, 34, 21, 12, 21, 20, 34, 21, 35, 8, 37, 31, 42,
1, 4, 4, 31, 7, 7, 13, 36, 24, 7, 43, 1, 4, 39, 4, 7, 38, 7, 36, 41,
22, 12, 2, 43, 4, 34, 21, 7, 10, 33, 24, 13, 22, 44, 7, 2, 12, 10, 34, 21,
39, 38, 25, 6, 30, 23, 18, 0, 17, 14, 33, 25, 5, 6, 30, 18, 0, 17, 23, 11,
14,
} }
var hclPact = []int{ var hclPact = []int{
37, -1000, 37, -1000, 8, -1000, 29, -1000, -1000, 7, 5, -1000, 5, -1000, -5, -1000, 33, -1000, -1000, 7,
-1000, -1000, 25, -1000, -1000, -1000, -1000, -1000, -1000, -10, -1000, -1000, 26, -1000, -1000, -1000, -1000, -1000, -1000, -11,
18, 9, -1000, 24, -1000, -8, -1000, 46, 45, 19, 12, 9, -1000, 24, -1000, -9, -1000, 31, 28, 10,
-2, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 23, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000,
-1000, -1000, 35, -1000, -1000, -1000, 37, -1000, -1000,
} }
var hclPgo = []int{ var hclPgo = []int{
0, 3, 56, 54, 42, 15, 49, 48, 2, 0, 0, 3, 55, 54, 45, 52, 50, 47, 2, 0,
53, 1, 52, 30, 53, 1, 51, 31,
} }
var hclR1 = []int{ var hclR1 = []int{
0, 13, 13, 4, 4, 7, 7, 8, 8, 8, 0, 13, 13, 4, 4, 7, 7, 8, 8, 8,
8, 8, 8, 5, 5, 10, 10, 2, 2, 3, 8, 8, 8, 5, 5, 10, 10, 2, 2, 3,
3, 9, 9, 6, 6, 6, 6, 1, 1, 11, 3, 3, 9, 9, 6, 6, 6, 6, 1, 1,
11, 12, 11, 11, 12,
} }
var hclR2 = []int{ var hclR2 = []int{
0, 0, 1, 1, 2, 3, 2, 3, 3, 3, 0, 0, 1, 1, 2, 3, 2, 3, 3, 3,
3, 3, 1, 2, 2, 1, 1, 3, 2, 1, 3, 3, 1, 2, 2, 1, 1, 3, 2, 1,
3, 1, 1, 1, 2, 2, 3, 2, 1, 2, 3, 2, 1, 1, 1, 2, 2, 3, 2, 1,
2, 2, 2, 2, 2,
} }
var hclChk = []int{ var hclChk = []int{
-1000, -13, -4, -8, 7, -5, -10, 10, -8, 8, -1000, -13, -4, -8, 8, -5, -10, 11, -8, 9,
-7, -5, 12, 7, -6, 4, 10, -7, -2, -1, -7, -5, 13, 8, -6, 4, 11, -7, -2, -1,
14, 11, 5, -4, 13, -12, -11, 16, 17, 18, 15, 12, 5, -4, 14, -12, -11, 17, 18, 19,
-3, 15, -9, -6, 10, -1, 13, -11, 5, 5, -3, 16, -9, -6, 11, -1, 14, -11, 5, 5,
5, 15, 6, -9, 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, 15, 12, 0, 16, 4, 0,
13, 14, 0, 15, 7, 8, 9, 10, 11, 23, 13, 14, 0, 15, 7, 8, 9, 10, 11, 24,
0, 0, 28, 0, 6, 24, 25, 0, 0, 0, 0, 0, 29, 0, 6, 25, 26, 0, 0, 0,
0, 18, 19, 21, 22, 27, 5, 26, 31, 29, 0, 18, 19, 22, 23, 28, 5, 27, 32, 30,
30, 17, 0, 20, 31, 17, 0, 21, 20,
} }
var hclTok1 = []int{ var hclTok1 = []int{
@ -134,7 +137,7 @@ var hclTok1 = []int{
var hclTok2 = []int{ var hclTok2 = []int{
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 12, 13, 14, 15, 16, 17, 18, 19,
} }
var hclTok3 = []int{ var hclTok3 = []int{
0, 0,
@ -493,28 +496,33 @@ hcldefault:
hclVAL.objlist = append(hclS[hclpt-2].objlist, hclS[hclpt-0].obj) hclVAL.objlist = append(hclS[hclpt-2].objlist, hclS[hclpt-0].obj)
} }
case 21: case 21:
//line parse.y:159 //line parse.y:157
{ {
hclVAL.obj = hclS[hclpt-0].obj hclVAL.objlist = hclS[hclpt-1].objlist
} }
case 22: case 22:
//line parse.y:163 //line parse.y:163
{
hclVAL.obj = hclS[hclpt-0].obj
}
case 23:
//line parse.y:167
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Type: ValueTypeString, Type: ValueTypeString,
Value: hclS[hclpt-0].str, Value: hclS[hclpt-0].str,
} }
} }
case 23: case 24:
//line parse.y:172 //line parse.y:176
{ {
hclVAL.obj = &Object{ hclVAL.obj = &Object{
Type: ValueTypeInt, Type: ValueTypeInt,
Value: hclS[hclpt-0].num, Value: hclS[hclpt-0].num,
} }
} }
case 24: case 25:
//line parse.y:179 //line parse.y:183
{ {
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)
@ -527,8 +535,8 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 25: case 26:
//line parse.y:192 //line parse.y:196
{ {
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)
@ -541,8 +549,8 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 26: case 27:
//line parse.y:205 //line parse.y:209
{ {
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)
@ -555,28 +563,28 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 27:
//line parse.y:220
{
hclVAL.num = hclS[hclpt-0].num * -1
}
case 28: case 28:
//line parse.y:224 //line parse.y:224
{ {
hclVAL.num = hclS[hclpt-0].num hclVAL.num = hclS[hclpt-0].num * -1
} }
case 29: case 29:
//line parse.y:230 //line parse.y:228
{ {
hclVAL.str = "e" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10) 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.str = "e" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
} }
case 31: case 31:
//line parse.y:240 //line parse.y:238
{
hclVAL.str = "e-" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
}
case 32:
//line parse.y:244
{ {
hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10) hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
} }