Skip to content

Commit

Permalink
lex: remove leading 0's to not parse integers in octal (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo authored Nov 21, 2024
1 parent c1a2662 commit 1d4e301
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ func lexText(l *lexer) lexStateFn {

func lexNumber(l *lexer) lexStateFn {
numberBuf := make([]rune, 0, 10)

// // remove leading zeros
for l.nextRune == '0' {
_, eof := l.next()
if eof {
l.tokens <- token{tokNumber, "0"}
l.tokens <- token{typ: tokEOF}
return nil
}
}

for unicode.IsDigit(l.nextRune) {
r, eof := l.next()
numberBuf = append(numberBuf, r)
Expand All @@ -237,6 +248,11 @@ func lexNumber(l *lexer) lexStateFn {
}
}

if len(numberBuf) == 0 {
l.tokens <- token{tokNumber, "0"}
return lexInput
}

if len(numberBuf) > 0 {
l.tokens <- token{tokNumber, string(numberBuf)}
}
Expand Down

0 comments on commit 1d4e301

Please sign in to comment.