Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes, improvements, optimization & refactoring before parser generation #288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix bad float conformation error message + add coverage
  • Loading branch information
Peter Dolak authored and petee-d committed Dec 5, 2022
commit 5fa25c2d7d6fd93e5e11516e5e6f6f4f351f4dd2
2 changes: 1 addition & 1 deletion nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ func conform(t reflect.Type, values []reflect.Value) (out []reflect.Value, err e
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(v.String(), sizeOfKind(kind))
if err != nil {
return nil, fmt.Errorf("invalid integer %q: %s", v.String(), err)
return nil, fmt.Errorf("invalid float %q: %s", v.String(), err)
}
v = reflect.New(t).Elem()
v.SetFloat(n)
Expand Down
19 changes: 19 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"text/scanner"

require "github.com/alecthomas/assert/v2"

"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
Expand Down Expand Up @@ -1864,3 +1865,21 @@ func TestIssue255(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &I255Grammar{Union: &I255String{Value: `"Hello, World!"`}}, g)
}

func TestParseNumbers(t *testing.T) {
type grammar struct {
Int int8 `@('-'? Int)`
Uint uint16 `@('-'? Int)`
Float float64 `@Ident`
}
parser := participle.MustBuild[grammar]()
_, err := parser.ParseString("", `300 0 x`)
require.EqualError(t, err, `grammar.Int: invalid integer "300": strconv.ParseInt: parsing "300": value out of range`)
_, err = parser.ParseString("", `-2 -2 x`)
require.EqualError(t, err, `grammar.Uint: invalid integer "-2": strconv.ParseUint: parsing "-2": invalid syntax`)
_, err = parser.ParseString("", `0 0 nope`)
require.EqualError(t, err, `grammar.Float: invalid float "nope": strconv.ParseFloat: parsing "nope": invalid syntax`)
result, err := parser.ParseString("", `-30 3000 Inf`)
require.NoError(t, err)
require.Equal(t, grammar{Int: -30, Uint: 3000, Float: math.Inf(1)}, *result)
}