Skip to content

Commit

Permalink
parse floats 1.02 properly [hashicorpGH-19]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Nov 13, 2014
1 parent 88ef419 commit e51eabc
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 219 deletions.
11 changes: 11 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func TestDecode_interface(t *testing.T) {
"foo": "bar\"baz\\n",
},
},
{
"float.hcl",
false,
map[string]interface{}{
"a": 1.02,
},
},
{
"multiline_bad.hcl",
false,
Expand Down Expand Up @@ -209,6 +216,10 @@ func TestDecode_equal(t *testing.T) {
"basic.hcl",
"basic.json",
},
{
"float.hcl",
"float.json",
},
/*
{
"structure.hcl",
Expand Down
30 changes: 24 additions & 6 deletions hcl/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,21 @@ func (x *hclLex) lexHeredoc(yylval *hclSymType) int {
// lexNumber lexes out a number
func (x *hclLex) lexNumber(yylval *hclSymType) int {
var b bytes.Buffer
gotPeriod := false
for {
c := x.next()
if c == lexEOF {
break
}

// No more numeric characters
if c < '0' || c > '9' {
if c == '.' {
if gotPeriod {
x.backup()
break
}

gotPeriod = true
} else if c < '0' || c > '9' {
x.backup()
break
}
Expand All @@ -325,14 +332,25 @@ func (x *hclLex) lexNumber(yylval *hclSymType) int {
}
}

v, err := strconv.ParseInt(b.String(), 0, 0)
if !gotPeriod {
v, err := strconv.ParseInt(b.String(), 0, 0)
if err != nil {
x.createErr(fmt.Sprintf("Expected number: %s", err))
return lexEOF
}

yylval.num = int(v)
return NUMBER
}

f, err := strconv.ParseFloat(b.String(), 64)
if err != nil {
x.createErr(fmt.Sprintf("Expected number: %s", err))
x.createErr(fmt.Sprintf("Expected float: %s", err))
return lexEOF
}

yylval.num = int(v)
return NUMBER
yylval.f = float64(f)
return FLOAT
}

// lexString extracts a string from the input
Expand Down
35 changes: 18 additions & 17 deletions hcl/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import (

%union {
b bool
f float64
num int
str string
obj *Object
objlist []*Object
}

%type <f> float
%type <num> int
%type <objlist> list listitems objectlist
%type <obj> block number object objectitem
%type <obj> listitem
%type <str> blockId exp objectkey frac
%type <str> blockId exp objectkey

%token <b> BOOL
%token <f> FLOAT
%token <num> NUMBER
%token <str> COMMA COMMAEND IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD
Expand Down Expand Up @@ -189,17 +192,11 @@ number:
Value: $1,
}
}
| int frac
| float
{
fs := fmt.Sprintf("%d.%s", $1, $2)
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
panic(err)
}

$$ = &Object{
Type: ValueTypeFloat,
Value: f,
Value: $1,
}
}
| int exp
Expand All @@ -215,9 +212,9 @@ number:
Value: f,
}
}
| int frac exp
| float exp
{
fs := fmt.Sprintf("%d.%s%s", $1, $2, $3)
fs := fmt.Sprintf("%f%s", $1, $2)
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
panic(err)
Expand All @@ -239,6 +236,16 @@ int:
$$ = $1
}

float:
MINUS float
{
$$ = $2 * -1
}
| FLOAT
{
$$ = $1
}

exp:
EPLUS NUMBER
{
Expand All @@ -249,10 +256,4 @@ exp:
$$ = "e-" + strconv.FormatInt(int64($2), 10)
}

frac:
PERIOD NUMBER
{
$$ = strconv.FormatInt(int64($2), 10)
}

%%
Loading

0 comments on commit e51eabc

Please sign in to comment.