diff --git a/decoder_test.go b/decoder_test.go index 25e87fb..10fb6df 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -51,6 +51,9 @@ func TestDecode_interface(t *testing.T) { "a": 1e-10, "b": 1e+10, "c": 1e10, + "d": 1.2e-10, + "e": 1.2e+10, + "f": 1.2e10, }, }, { @@ -60,6 +63,9 @@ func TestDecode_interface(t *testing.T) { "a": 1e-10, "b": 1e+10, "c": 1e10, + "d": 1.2e-10, + "e": 1.2e+10, + "f": 1.2e10, }, }, { diff --git a/hcl/parse.y b/hcl/parse.y index 5b20326..8c7a047 100644 --- a/hcl/parse.y +++ b/hcl/parse.y @@ -193,6 +193,19 @@ number: panic(err) } + $$ = &Object{ + Type: ValueTypeFloat, + Value: f, + } + } +| int frac exp + { + fs := fmt.Sprintf("%d.%s%s", $1, $2, $3) + f, err := strconv.ParseFloat(fs, 64) + if err != nil { + panic(err) + } + $$ = &Object{ Type: ValueTypeFloat, Value: f, diff --git a/hcl/y.go b/hcl/y.go index fd2b31b..3d8b58c 100644 --- a/hcl/y.go +++ b/hcl/y.go @@ -58,7 +58,7 @@ const hclEofCode = 1 const hclErrCode = 2 const hclMaxDepth = 200 -//line parse.y:228 +//line parse.y:241 //line yacctab:1 var hclExca = []int{ @@ -67,63 +67,65 @@ var hclExca = []int{ -2, 0, } -const hclNprod = 30 +const hclNprod = 31 const hclPrivate = 57344 var hclTokenNames []string var hclStates []string -const hclLast = 56 +const hclLast = 60 var hclAct = []int{ - 32, 3, 19, 41, 8, 27, 28, 29, 15, 22, - 10, 33, 40, 22, 16, 21, 12, 22, 20, 21, - 17, 14, 34, 21, 35, 8, 4, 31, 4, 7, - 13, 7, 36, 7, 24, 12, 2, 4, 22, 9, - 7, 5, 42, 34, 21, 39, 38, 37, 11, 23, - 1, 25, 26, 6, 30, 18, + 32, 26, 3, 19, 42, 8, 27, 28, 29, 28, + 29, 15, 22, 41, 22, 5, 9, 16, 21, 12, + 21, 20, 11, 22, 40, 35, 8, 37, 34, 21, + 1, 4, 4, 31, 7, 7, 13, 36, 24, 7, + 22, 12, 2, 43, 4, 34, 21, 7, 10, 33, + 39, 38, 25, 6, 30, 23, 18, 0, 17, 14, } var hclPact = []int{ - 30, -1000, 30, -1000, 31, -1000, 23, -1000, -1000, 4, - -1000, -1000, 21, -1000, -1000, -1000, -1000, -1000, -1000, -11, - 12, 8, -1000, 19, -1000, -1000, -1000, 42, 41, 40, - -3, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, 33, -1000, + 37, -1000, 37, -1000, 8, -1000, 29, -1000, -1000, 7, + -1000, -1000, 25, -1000, -1000, -1000, -1000, -1000, -1000, -10, + 18, 9, -1000, 24, -1000, -8, -1000, 46, 45, 19, + -2, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, + -1000, -1000, 35, -1000, } var hclPgo = []int{ - 0, 2, 55, 54, 36, 41, 11, 10, 1, 0, - 53, 52, 51, 50, + 0, 3, 56, 54, 42, 15, 49, 48, 2, 0, + 53, 1, 52, 30, } var hclR1 = []int{ 0, 13, 4, 4, 7, 7, 8, 8, 8, 8, 8, 8, 5, 5, 10, 10, 2, 2, 3, 3, - 9, 9, 6, 6, 6, 1, 1, 11, 11, 12, + 9, 9, 6, 6, 6, 6, 1, 1, 11, 11, + 12, } var hclR2 = []int{ 0, 1, 1, 2, 3, 2, 3, 3, 3, 3, 3, 1, 2, 2, 1, 1, 3, 2, 1, 3, - 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, + 1, 1, 1, 2, 2, 3, 2, 1, 2, 2, + 2, } var hclChk = []int{ -1000, -13, -4, -8, 7, -5, -10, 10, -8, 8, -7, -5, 12, 7, -6, 4, 10, -7, -2, -1, 14, 11, 5, -4, 13, -12, -11, 16, 17, 18, - -3, 15, -9, -6, 10, -1, 13, 5, 5, 5, - 15, 6, -9, + -3, 15, -9, -6, 10, -1, 13, -11, 5, 5, + 5, 15, 6, -9, } var hclDef = []int{ 0, -2, 1, 2, 14, 11, 0, 15, 3, 0, 12, 13, 0, 14, 6, 7, 8, 9, 10, 22, - 0, 0, 26, 0, 5, 23, 24, 0, 0, 0, - 0, 17, 18, 20, 21, 25, 4, 29, 27, 28, - 16, 0, 19, + 0, 0, 27, 0, 5, 23, 24, 0, 0, 0, + 0, 17, 18, 20, 21, 26, 4, 25, 30, 28, + 29, 16, 0, 19, } var hclTok1 = []int{ @@ -535,27 +537,41 @@ hcldefault: } } case 25: - //line parse.y:204 + //line parse.y:202 + { + fs := fmt.Sprintf("%d.%s%s", hclS[hclpt-2].num, hclS[hclpt-1].str, hclS[hclpt-0].str) + f, err := strconv.ParseFloat(fs, 64) + if err != nil { + panic(err) + } + + hclVAL.obj = &Object{ + Type: ValueTypeFloat, + Value: f, + } + } + case 26: + //line parse.y:217 { hclVAL.num = hclS[hclpt-0].num * -1 } - case 26: - //line parse.y:208 + case 27: + //line parse.y:221 { hclVAL.num = hclS[hclpt-0].num } - case 27: - //line parse.y:214 + case 28: + //line parse.y:227 { hclVAL.str = "e" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10) } - case 28: - //line parse.y:218 + case 29: + //line parse.y:231 { hclVAL.str = "e-" + strconv.FormatInt(int64(hclS[hclpt-0].num), 10) } - case 29: - //line parse.y:224 + case 30: + //line parse.y:237 { hclVAL.str = strconv.FormatInt(int64(hclS[hclpt-0].num), 10) } diff --git a/json/parse.y b/json/parse.y index dc8af85..436938e 100644 --- a/json/parse.y +++ b/json/parse.y @@ -161,6 +161,19 @@ number: panic(err) } + $$ = &hcl.Object{ + Type: hcl.ValueTypeFloat, + Value: f, + } + } +| int frac exp + { + fs := fmt.Sprintf("%d.%s%s", $1, $2, $3) + f, err := strconv.ParseFloat(fs, 64) + if err != nil { + panic(err) + } + $$ = &hcl.Object{ Type: hcl.ValueTypeFloat, Value: f, diff --git a/json/y.go b/json/y.go index 11d4671..cd062d7 100644 --- a/json/y.go +++ b/json/y.go @@ -65,7 +65,7 @@ const jsonEofCode = 1 const jsonErrCode = 2 const jsonMaxDepth = 200 -//line parse.y:196 +//line parse.y:209 //line yacctab:1 var jsonExca = []int{ @@ -74,59 +74,60 @@ var jsonExca = []int{ -2, 0, } -const jsonNprod = 26 +const jsonNprod = 27 const jsonPrivate = 57344 var jsonTokenNames []string var jsonStates []string -const jsonLast = 50 +const jsonLast = 53 var jsonAct = []int{ - 12, 23, 26, 27, 28, 23, 3, 13, 3, 20, - 21, 29, 17, 18, 19, 22, 7, 23, 5, 22, - 7, 9, 31, 13, 3, 37, 21, 8, 17, 18, - 19, 22, 32, 36, 6, 10, 35, 34, 38, 33, - 15, 2, 1, 24, 11, 25, 4, 30, 16, 14, + 12, 23, 25, 26, 27, 28, 23, 13, 3, 20, + 21, 29, 17, 18, 19, 22, 3, 23, 27, 28, + 22, 7, 31, 13, 3, 38, 21, 33, 17, 18, + 19, 22, 32, 37, 9, 7, 6, 5, 10, 39, + 8, 36, 35, 34, 15, 2, 11, 1, 24, 4, + 30, 16, 14, } var jsonPact = []int{ - -5, -1000, -1000, 6, 15, -1000, -1000, 30, -1000, 10, + 5, -1000, -1000, 25, 28, -1000, -1000, 33, -1000, 11, 13, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -17, -3, 1, -1000, -1000, -1000, 35, 33, 32, -1000, - 19, -1000, -1000, -1000, -1000, -1000, -1000, 13, -1000, + -16, -3, 2, -1000, -2, -1000, 39, 38, 37, -1000, + 19, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 13, -1000, } var jsonPgo = []int{ - 0, 9, 49, 40, 34, 0, 48, 47, 46, 45, - 43, 42, + 0, 9, 52, 44, 36, 0, 51, 50, 49, 2, + 48, 47, } var jsonR1 = []int{ 0, 11, 3, 3, 8, 8, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 2, 2, - 2, 1, 1, 9, 9, 10, + 2, 2, 1, 1, 9, 9, 10, } var jsonR2 = []int{ 0, 1, 3, 2, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 3, 1, 2, - 2, 2, 1, 2, 2, 2, + 2, 3, 2, 1, 2, 2, 2, } var jsonChk = []int{ -1000, -11, -3, 11, -8, 12, -4, 10, 12, 6, 5, -4, -5, 10, -2, -3, -6, 15, 16, 17, -1, 13, 18, 4, -10, -9, 19, 20, 21, 14, - -7, -5, -1, 4, 4, 4, 14, 6, -5, + -7, -5, -1, -9, 4, 4, 4, 14, 6, -5, } var jsonDef = []int{ 0, -2, 1, 0, 0, 3, 4, 0, 2, 0, 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, - 18, 0, 0, 22, 19, 20, 0, 0, 0, 14, - 0, 16, 21, 25, 23, 24, 15, 0, 17, + 18, 0, 0, 23, 19, 20, 0, 0, 0, 14, + 0, 16, 22, 21, 26, 24, 25, 15, 0, 17, } var jsonTok1 = []int{ @@ -507,27 +508,41 @@ jsondefault: } } case 21: - //line parse.y:172 + //line parse.y:170 + { + fs := fmt.Sprintf("%d.%s%s", jsonS[jsonpt-2].num, jsonS[jsonpt-1].str, jsonS[jsonpt-0].str) + f, err := strconv.ParseFloat(fs, 64) + if err != nil { + panic(err) + } + + jsonVAL.obj = &hcl.Object{ + Type: hcl.ValueTypeFloat, + Value: f, + } + } + case 22: + //line parse.y:185 { jsonVAL.num = jsonS[jsonpt-0].num * -1 } - case 22: - //line parse.y:176 + case 23: + //line parse.y:189 { jsonVAL.num = jsonS[jsonpt-0].num } - case 23: - //line parse.y:182 + case 24: + //line parse.y:195 { jsonVAL.str = "e" + strconv.FormatInt(int64(jsonS[jsonpt-0].num), 10) } - case 24: - //line parse.y:186 + case 25: + //line parse.y:199 { jsonVAL.str = "e-" + strconv.FormatInt(int64(jsonS[jsonpt-0].num), 10) } - case 25: - //line parse.y:192 + case 26: + //line parse.y:205 { jsonVAL.str = strconv.FormatInt(int64(jsonS[jsonpt-0].num), 10) } diff --git a/test-fixtures/scientific.hcl b/test-fixtures/scientific.hcl index 156ed0f..b9eca28 100644 --- a/test-fixtures/scientific.hcl +++ b/test-fixtures/scientific.hcl @@ -1,3 +1,6 @@ a = 1e-10 b = 1e+10 c = 1e10 +d = 1.2e-10 +e = 1.2e+10 +f = 1.2e10 diff --git a/test-fixtures/scientific.json b/test-fixtures/scientific.json index eb29895..c1fce3c 100644 --- a/test-fixtures/scientific.json +++ b/test-fixtures/scientific.json @@ -1,5 +1,8 @@ { "a": 1e-10, "b": 1e+10, - "c": 1e10 + "c": 1e10, + "d": 1.2e-10, + "e": 1.2e+10, + "f": 1.2e10 }