Skip to content

Commit 6bc1224

Browse files
authored
fix: Fix the issue where the isNumber method incorrectly identifies Inf as a number, causing the parser to misinterpret the Inf identifier. (#146)
1 parent d1cdb8f commit 6bc1224

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

parser_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,29 @@ enum Value {
229229
t.Fatal(err) // <input>:7:16: found "-" but expected [range integer]
230230
}
231231
}
232+
233+
func TestParseInfMessage(t *testing.T) {
234+
const def = `
235+
message Inf {
236+
string field = 1;
237+
}
238+
message NaN {
239+
string field = 1;
240+
}
241+
242+
message Infinity {
243+
string field = 1;
244+
}
245+
message ExampelMessage {
246+
Inf inf_field = 1;
247+
NaN nan_field = 2;
248+
Infinity infinity_field = 3;
249+
}
250+
`
251+
252+
p := NewParser(strings.NewReader(def))
253+
_, err := p.Parse()
254+
if err != nil {
255+
t.Fatal(err) // <input>:7:16: found "-" but expected [range integer]
256+
}
257+
}

token.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ func isComment(lit string) bool {
124124
}
125125

126126
func isNumber(lit string) bool {
127+
if lit == "NaN" || lit == "nan" || lit == "Inf" || lit == "Infinity" || lit == "inf" || lit == "infinity" {
128+
return false
129+
}
127130
if strings.HasPrefix(lit, "0x") || strings.HasPrefix(lit, "0X") {
128131
_, err := strconv.ParseInt(lit, 0, 64)
129132
return err == nil

token_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ func TestIsNumber(t *testing.T) {
6363
{`a1`, false},
6464
{`0x12`, true},
6565
{`0X77777`, true},
66+
{`NaN`, false},
67+
{`nan`, false},
68+
{`Inf`, false},
69+
{`Infinity`, false},
70+
{`inf`, false},
71+
{`infinity`, false},
6672
} {
6773
got := isNumber(each.input)
6874
if got != each.isNumber {

0 commit comments

Comments
 (0)