hcl: support booleans

This commit is contained in:
Mitchell Hashimoto 2014-08-04 22:23:11 -07:00
parent 398c583f25
commit 0d84234df1
4 changed files with 118 additions and 80 deletions

View File

@ -157,6 +157,16 @@ func (x *hclLex) lexId(yylval *hclSymType) int {
} }
yylval.str = b.String() yylval.str = b.String()
switch yylval.str {
case "true":
yylval.b = true
return BOOL
case "false":
yylval.b = false
return BOOL
}
return IDENTIFIER return IDENTIFIER
} }

View File

@ -13,6 +13,7 @@ import (
%} %}
%union { %union {
b bool
item ast.Node item ast.Node
list []ast.Node list []ast.Node
alist []ast.AssignmentNode alist []ast.AssignmentNode
@ -32,6 +33,7 @@ import (
%type <obj> object %type <obj> object
%type <str> blockId frac %type <str> blockId frac
%token <b> BOOL
%token <num> NUMBER %token <num> NUMBER
%token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING MINUS %token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD %token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD
@ -75,6 +77,16 @@ objectitem:
Value: $3, Value: $3,
} }
} }
| IDENTIFIER EQUAL BOOL
{
$$ = ast.AssignmentNode{
K: $1,
Value: ast.LiteralNode{
Type: ast.ValueTypeBool,
Value: $3,
},
}
}
| IDENTIFIER EQUAL STRING | IDENTIFIER EQUAL STRING
{ {
$$ = ast.AssignmentNode{ $$ = ast.AssignmentNode{

View File

@ -3,3 +3,5 @@ bar = 7
baz = [1,2,3] baz = [1,2,3]
foo = -12 foo = -12
bar = 3.14159 bar = 3.14159
foo = true
bar = false

174
hcl/y.go
View File

@ -14,6 +14,7 @@ import (
//line parse.y:15 //line parse.y:15
type hclSymType struct { type hclSymType struct {
yys int yys int
b bool
item ast.Node item ast.Node
list []ast.Node list []ast.Node
alist []ast.AssignmentNode alist []ast.AssignmentNode
@ -24,20 +25,22 @@ type hclSymType struct {
str string str string
} }
const NUMBER = 57346 const BOOL = 57346
const COMMA = 57347 const NUMBER = 57347
const IDENTIFIER = 57348 const COMMA = 57348
const EQUAL = 57349 const IDENTIFIER = 57349
const NEWLINE = 57350 const EQUAL = 57350
const STRING = 57351 const NEWLINE = 57351
const MINUS = 57352 const STRING = 57352
const LEFTBRACE = 57353 const MINUS = 57353
const RIGHTBRACE = 57354 const LEFTBRACE = 57354
const LEFTBRACKET = 57355 const RIGHTBRACE = 57355
const RIGHTBRACKET = 57356 const LEFTBRACKET = 57356
const PERIOD = 57357 const RIGHTBRACKET = 57357
const PERIOD = 57358
var hclToknames = []string{ var hclToknames = []string{
"BOOL",
"NUMBER", "NUMBER",
"COMMA", "COMMA",
"IDENTIFIER", "IDENTIFIER",
@ -57,7 +60,7 @@ const hclEofCode = 1
const hclErrCode = 2 const hclErrCode = 2
const hclMaxDepth = 200 const hclMaxDepth = 200
//line parse.y:200 //line parse.y:212
//line yacctab:1 //line yacctab:1
var hclExca = []int{ var hclExca = []int{
@ -66,59 +69,59 @@ var hclExca = []int{
-2, 0, -2, 0,
} }
const hclNprod = 24 const hclNprod = 25
const hclPrivate = 57344 const hclPrivate = 57344
var hclTokenNames []string var hclTokenNames []string
var hclStates []string var hclStates []string
const hclLast = 46 const hclLast = 49
var hclAct = []int{ var hclAct = []int{
24, 3, 18, 20, 8, 32, 28, 2, 15, 19, 25, 3, 19, 29, 8, 15, 21, 2, 33, 34,
12, 9, 17, 4, 31, 4, 7, 33, 7, 30, 1, 16, 20, 12, 9, 18, 4, 32, 4, 7,
21, 22, 29, 8, 13, 20, 20, 7, 10, 12, 22, 7, 31, 30, 8, 4, 10, 13, 7, 21,
26, 19, 19, 34, 4, 25, 5, 7, 16, 1, 7, 23, 12, 21, 35, 20, 17, 26, 27, 20,
27, 6, 23, 11, 0, 14, 28, 5, 6, 24, 0, 0, 0, 14, 11,
} }
var hclPact = []int{ var hclPact = []int{
28, -1000, 28, -1000, 4, -1000, 18, -1000, -1000, -1, 11, -1000, 11, -1000, 6, -1000, 20, -1000, -1000, 1,
-1000, -1000, 9, -1000, -1000, -1000, -1000, 21, -9, 22, -1000, -1000, 18, -1000, -1000, -1000, -1000, -1000, 28, -13,
-1000, 7, -1000, 0, -1000, -1000, -1000, -1000, 13, -1000, 24, -1000, 9, -1000, 2, -1000, -1000, -1000, -1000, 4,
-1000, -1000, 21, -1000, -1000, -1000, -1000, -1000, 28, -1000, -1000,
} }
var hclPgo = []int{ var hclPgo = []int{
0, 35, 42, 7, 1, 36, 0, 2, 28, 41, 0, 37, 43, 7, 1, 41, 0, 2, 26, 42,
40, 39, 40, 10,
} }
var hclR1 = []int{ var hclR1 = []int{
0, 11, 3, 3, 8, 8, 4, 4, 4, 4, 0, 11, 3, 3, 8, 8, 4, 4, 4, 4,
4, 5, 5, 9, 9, 2, 2, 6, 6, 1, 4, 4, 5, 5, 9, 9, 2, 2, 6, 6,
1, 7, 7, 10, 1, 1, 7, 7, 10,
} }
var hclR2 = []int{ var hclR2 = []int{
0, 1, 1, 2, 3, 2, 3, 3, 3, 5, 0, 1, 1, 2, 3, 2, 3, 3, 3, 3,
1, 2, 2, 1, 1, 1, 3, 1, 1, 1, 5, 1, 2, 2, 1, 1, 1, 3, 1, 1,
2, 2, 1, 2, 1, 2, 2, 1, 2,
} }
var hclChk = []int{ var hclChk = []int{
-1000, -11, -3, -4, 6, -5, -9, 9, -4, 7, -1000, -11, -3, -4, 7, -5, -9, 10, -4, 8,
-8, -5, 11, 6, -1, 9, -8, 13, -7, 10, -8, -5, 12, 7, -1, 4, 10, -8, 14, -7,
4, -3, 12, -2, -6, -1, 9, -10, 15, -7, 11, 5, -3, 13, -2, -6, -1, 10, -10, 16,
12, 14, 5, 4, -6, -7, 13, 15, 6, 5, -6,
} }
var hclDef = []int{ var hclDef = []int{
0, -2, 1, 2, 13, 10, 0, 14, 3, 0, 0, -2, 1, 2, 14, 11, 0, 15, 3, 0,
11, 12, 0, 13, 6, 7, 8, 0, 19, 0, 12, 13, 0, 14, 6, 7, 8, 9, 0, 20,
22, 0, 5, 0, 15, 17, 18, 20, 0, 21, 0, 23, 0, 5, 0, 16, 18, 19, 21, 0,
4, 9, 0, 23, 16, 22, 4, 10, 0, 24, 17,
} }
var hclTok1 = []int{ var hclTok1 = []int{
@ -127,7 +130,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, 12, 13, 14, 15, 16,
} }
var hclTok3 = []int{ var hclTok3 = []int{
0, 0,
@ -359,7 +362,7 @@ hcldefault:
switch hclnt { switch hclnt {
case 1: case 1:
//line parse.y:43 //line parse.y:45
{ {
hclResult = &ast.ObjectNode{ hclResult = &ast.ObjectNode{
K: "", K: "",
@ -367,27 +370,27 @@ hcldefault:
} }
} }
case 2: case 2:
//line parse.y:52 //line parse.y:54
{ {
hclVAL.alist = []ast.AssignmentNode{hclS[hclpt-0].aitem} hclVAL.alist = []ast.AssignmentNode{hclS[hclpt-0].aitem}
} }
case 3: case 3:
//line parse.y:56 //line parse.y:58
{ {
hclVAL.alist = append(hclS[hclpt-1].alist, hclS[hclpt-0].aitem) hclVAL.alist = append(hclS[hclpt-1].alist, hclS[hclpt-0].aitem)
} }
case 4: case 4:
//line parse.y:62 //line parse.y:64
{ {
hclVAL.obj = ast.ObjectNode{Elem: hclS[hclpt-1].alist} hclVAL.obj = ast.ObjectNode{Elem: hclS[hclpt-1].alist}
} }
case 5: case 5:
//line parse.y:66 //line parse.y:68
{ {
hclVAL.obj = ast.ObjectNode{} hclVAL.obj = ast.ObjectNode{}
} }
case 6: case 6:
//line parse.y:72 //line parse.y:74
{ {
hclVAL.aitem = ast.AssignmentNode{ hclVAL.aitem = ast.AssignmentNode{
K: hclS[hclpt-2].str, K: hclS[hclpt-2].str,
@ -395,7 +398,18 @@ hcldefault:
} }
} }
case 7: case 7:
//line parse.y:79 //line parse.y:81
{
hclVAL.aitem = ast.AssignmentNode{
K: hclS[hclpt-2].str,
Value: ast.LiteralNode{
Type: ast.ValueTypeBool,
Value: hclS[hclpt-0].b,
},
}
}
case 8:
//line parse.y:91
{ {
hclVAL.aitem = ast.AssignmentNode{ hclVAL.aitem = ast.AssignmentNode{
K: hclS[hclpt-2].str, K: hclS[hclpt-2].str,
@ -405,29 +419,29 @@ hcldefault:
}, },
} }
} }
case 8: case 9:
//line parse.y:89 //line parse.y:101
{ {
hclVAL.aitem = ast.AssignmentNode{ hclVAL.aitem = ast.AssignmentNode{
K: hclS[hclpt-2].str, K: hclS[hclpt-2].str,
Value: hclS[hclpt-0].obj, Value: hclS[hclpt-0].obj,
} }
} }
case 9: case 10:
//line parse.y:96 //line parse.y:108
{ {
hclVAL.aitem = ast.AssignmentNode{ hclVAL.aitem = ast.AssignmentNode{
K: hclS[hclpt-4].str, K: hclS[hclpt-4].str,
Value: ast.ListNode{Elem: hclS[hclpt-1].list}, Value: ast.ListNode{Elem: hclS[hclpt-1].list},
} }
} }
case 10: case 11:
//line parse.y:103 //line parse.y:115
{ {
hclVAL.aitem = hclS[hclpt-0].aitem hclVAL.aitem = hclS[hclpt-0].aitem
} }
case 11: case 12:
//line parse.y:109 //line parse.y:121
{ {
hclS[hclpt-0].obj.K = hclS[hclpt-1].str hclS[hclpt-0].obj.K = hclS[hclpt-1].str
hclVAL.aitem = ast.AssignmentNode{ hclVAL.aitem = ast.AssignmentNode{
@ -435,8 +449,8 @@ hcldefault:
Value: hclS[hclpt-0].obj, Value: hclS[hclpt-0].obj,
} }
} }
case 12: case 13:
//line parse.y:117 //line parse.y:129
{ {
obj := ast.ObjectNode{ obj := ast.ObjectNode{
K: hclS[hclpt-0].aitem.Key(), K: hclS[hclpt-0].aitem.Key(),
@ -448,49 +462,49 @@ hcldefault:
Value: obj, Value: obj,
} }
} }
case 13:
//line parse.y:131
{
hclVAL.str = hclS[hclpt-0].str
}
case 14: case 14:
//line parse.y:135 //line parse.y:143
{ {
hclVAL.str = hclS[hclpt-0].str hclVAL.str = hclS[hclpt-0].str
} }
case 15: case 15:
//line parse.y:141 //line parse.y:147
{
hclVAL.str = hclS[hclpt-0].str
}
case 16:
//line parse.y:153
{ {
hclVAL.list = []ast.Node{hclS[hclpt-0].listitem} hclVAL.list = []ast.Node{hclS[hclpt-0].listitem}
} }
case 16: case 17:
//line parse.y:145 //line parse.y:157
{ {
hclVAL.list = append(hclS[hclpt-2].list, hclS[hclpt-0].listitem) hclVAL.list = append(hclS[hclpt-2].list, hclS[hclpt-0].listitem)
} }
case 17: case 18:
//line parse.y:151 //line parse.y:163
{ {
hclVAL.listitem = hclS[hclpt-0].item hclVAL.listitem = hclS[hclpt-0].item
} }
case 18: case 19:
//line parse.y:155 //line parse.y:167
{ {
hclVAL.listitem = ast.LiteralNode{ hclVAL.listitem = ast.LiteralNode{
Type: ast.ValueTypeString, Type: ast.ValueTypeString,
Value: hclS[hclpt-0].str, Value: hclS[hclpt-0].str,
} }
} }
case 19: case 20:
//line parse.y:164 //line parse.y:176
{ {
hclVAL.item = ast.LiteralNode{ hclVAL.item = ast.LiteralNode{
Type: ast.ValueTypeInt, Type: ast.ValueTypeInt,
Value: hclS[hclpt-0].num, Value: hclS[hclpt-0].num,
} }
} }
case 20: case 21:
//line parse.y:171 //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)
@ -503,18 +517,18 @@ hcldefault:
Value: f, Value: f,
} }
} }
case 21: case 22:
//line parse.y:186 //line parse.y:198
{ {
hclVAL.num = hclS[hclpt-0].num * -1 hclVAL.num = hclS[hclpt-0].num * -1
} }
case 22: case 23:
//line parse.y:190 //line parse.y:202
{ {
hclVAL.num = hclS[hclpt-0].num hclVAL.num = hclS[hclpt-0].num
} }
case 23: case 24:
//line parse.y:196 //line parse.y:208
{ {
hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10) hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10)
} }