Skip to content

Commit

Permalink
fixed parsing of portions with whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jul 26, 2024
1 parent 68ca479 commit cb1a075
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
61 changes: 61 additions & 0 deletions parser/__snapshots__/parser_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1601,3 +1601,64 @@ parser.Program{
},
}
---

[TestWhitespaceInRatio - 1]
parser.Program{
Vars: nil,
Statements: {
&parser.SendStatement{
Range: parser.Range{
Start: parser.Position{Character:0, Line:1},
End: parser.Position{Character:1, Line:6},
},
SentValue: &parser.SentValueLiteral{
Monetary: &parser.VariableLiteral{
Range: parser.Range{
Start: parser.Position{Character:5, Line:1},
End: parser.Position{Character:9, Line:1},
},
Name: "var",
},
},
Source: &parser.AccountLiteral{
Range: parser.Range{
Start: parser.Position{Character:11, Line:2},
End: parser.Position{Character:17, Line:2},
},
Name: "world",
},
Destination: &parser.DestinationAllotment{
Range: parser.Range{
Start: parser.Position{Character:16, Line:3},
End: parser.Position{Character:3, Line:5},
},
Items: {
{
Range: parser.Range{
Start: parser.Position{Character:4, Line:4},
End: parser.Position{Character:22, Line:4},
},
Allotment: &parser.RatioLiteral{
Range: parser.Range{
Start: parser.Position{Character:4, Line:4},
End: parser.Position{Character:9, Line:4},
},
Numerator: 0x1,
Denominator: 0x6,
},
To: &parser.DestinationTo{
Destination: &parser.AccountLiteral{
Range: parser.Range{
Start: parser.Position{Character:13, Line:4},
End: parser.Position{Character:22, Line:4},
},
Name: "player:1",
},
},
},
},
},
},
},
}
---
4 changes: 2 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ func parseSource(sourceCtx parser.ISourceContext) Source {
func parseRatio(source string, range_ Range) *RatioLiteral {
split := strings.Split(source, "/")

num, err := strconv.ParseUint(split[0], 0, 64)
num, err := strconv.ParseUint(strings.TrimSpace(split[0]), 0, 64)
if err != nil {
panic(err)
}

den, err := strconv.ParseUint(split[1], 0, 64)
den, err := strconv.ParseUint(strings.TrimSpace(split[1]), 0, 64)
if err != nil {
panic(err)
}
Expand Down
12 changes: 12 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,15 @@ func TestSendAll(t *testing.T) {
snaps.MatchSnapshot(t, p.Value)
assert.Empty(t, p.Errors)
}

func TestWhitespaceInRatio(t *testing.T) {
p := parser.Parse(`
send $var (
source = @world
destination = {
1 / 6 to @player:1
}
)
`)
snaps.MatchSnapshot(t, p.Value)
}

0 comments on commit cb1a075

Please sign in to comment.