Skip to content

Commit 1e62f18

Browse files
committed
Coverage of parseNumber and parseString
1 parent 160b692 commit 1e62f18

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

parser/lexer.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,20 @@ func (x *yyLex) readNumber() (token int, value py.Object) {
517517
if len(x.line) == 0 {
518518
return eof, nil
519519
}
520+
// Starts with a digit
520521
r0 := x.line[0]
521-
r1 := byte(0)
522-
if len(x.line) > 1 {
523-
r1 = x.line[1]
522+
if '0' <= r0 && r0 <= '9' {
523+
goto isNumber
524524
}
525-
// Check if could be a start of a number
526-
if !(('0' <= r0 && r0 <= '9') || (r0 == '.' && '0' <= r1 && r1 <= '9')) {
527-
return eof, nil
525+
// Or starts with . then a digit
526+
if len(x.line) > 1 && r0 == '.' {
527+
if r1 := x.line[1]; '0' <= r1 && r1 <= '9' {
528+
goto isNumber
529+
}
528530
}
531+
return eof, nil
529532

533+
isNumber:
530534
var s string
531535
if s = octalInteger.FindString(x.line); s != "" {
532536
value = py.IntNew(py.IntType, py.Tuple{py.String(s[2:]), py.Int(8)}, nil)
@@ -568,7 +572,7 @@ func (x *yyLex) readNumber() (token int, value py.Object) {
568572
value = py.IntNew(py.IntType, py.Tuple{py.String(s), py.Int(10)}, nil)
569573
}
570574
} else {
571-
return eof, nil
575+
panic("Unparsed number")
572576
}
573577
x.line = x.line[len(s):]
574578
token = NUMBER
@@ -651,8 +655,7 @@ found:
651655
stringEnd = `'`
652656
x.line = x.line[1:]
653657
} else {
654-
x.Error("Bad string start")
655-
return eofError, nil
658+
panic("Bad string start")
656659
}
657660
buf := new(bytes.Buffer)
658661
for {

parser/lexer_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func TestLexerReadString(t *testing.T) {
202202
out string
203203
}{
204204
{``, eof, nil, ``},
205+
{`1`, eof, nil, `1`},
205206

206207
{`""a`, STRING, py.String(``), `a`},
207208
{`u"abc"`, STRING, py.String(`abc`), ``},
@@ -210,6 +211,7 @@ func TestLexerReadString(t *testing.T) {
210211
{`"a\"c"`, STRING, py.String(`a\"c`), ``},
211212
{`"a\\"+`, STRING, py.String(`a\\`), `+`},
212213
{`"a`, eofError, nil, `a`},
214+
{"\"a\n", eofError, nil, "a\n"},
213215
{"\"a\\\nb\"c", STRING, py.String(`ab`), `c`},
214216

215217
{`''a`, STRING, py.String(``), `a`},

0 commit comments

Comments
 (0)