Skip to content

Commit

Permalink
Rename noError to noPanic
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Dec 18, 2024
1 parent b05fe53 commit 20e2973
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (l *Lexer) NextToken() (err error) {
return
}

func (l *Lexer) nextToken(noError bool) {
func (l *Lexer) nextToken(noPanic bool) {
l.lastTokenKind = l.Token.Kind
l.Token = token.Token{}

Expand All @@ -75,7 +75,7 @@ func (l *Lexer) nextToken(noError bool) {
space = l.Buffer[i:l.pos]

i = l.pos
hasError := l.skipComment(noError)
hasError := l.skipComment(noPanic)

if l.pos == i {
break
Expand All @@ -101,16 +101,16 @@ func (l *Lexer) nextToken(noError bool) {
l.Token.Pos = token.Pos(l.pos)
i := l.pos
if l.dotIdent {
l.consumeFieldToken(noError)
l.consumeFieldToken(noPanic)
l.dotIdent = false
} else {
l.consumeToken(noError)
l.consumeToken(noPanic)
}
l.Token.Raw = l.Buffer[i:l.pos]
l.Token.End = token.Pos(l.pos)
}

func (l *Lexer) consumeToken(noError bool) {
func (l *Lexer) consumeToken(noPanic bool) {
if l.eof() {
l.Token.Kind = token.TokenEOF
return
Expand All @@ -125,7 +125,7 @@ func (l *Lexer) consumeToken(noError bool) {
case '.':
nextDotIdent := isNextDotIdent(l.lastTokenKind)
if !nextDotIdent && l.peekOk(1) && char.IsDigit(l.peek(1)) {
l.consumeNumber(noError)
l.consumeNumber(noPanic)
} else {
l.skip()
l.Token.Kind = "."
Expand Down Expand Up @@ -243,13 +243,13 @@ func (l *Lexer) consumeToken(noError bool) {
l.Token.Kind = token.TokenIdent

var hasError bool
l.Token.AsString, hasError = l.consumeQuotedContent("`", false, true, "identifier", noError)
l.Token.AsString, hasError = l.consumeQuotedContent("`", false, true, "identifier", noPanic)
if hasError {
l.Token.Kind = token.TokenBad
}
return
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
l.consumeNumber(noError)
l.consumeNumber(noPanic)
return
case 'B', 'b', 'R', 'r', '"', '\'':
bytes, raw := false, false
Expand All @@ -264,13 +264,13 @@ func (l *Lexer) consumeToken(noError bool) {
l.skipN(i)
switch {
case bytes && raw:
l.consumeRawBytes(noError)
l.consumeRawBytes(noPanic)
case bytes:
l.consumeBytes(noError)
l.consumeBytes(noPanic)
case raw:
l.consumeRawString(noError)
l.consumeRawString(noPanic)
default:
l.consumeString(noError)
l.consumeString(noPanic)
}
return
default:
Expand All @@ -296,7 +296,7 @@ func (l *Lexer) consumeToken(noError bool) {
return
}

if noError {
if noPanic {
l.skip()
l.Token.Kind = token.TokenBad
return
Expand All @@ -305,7 +305,7 @@ func (l *Lexer) consumeToken(noError bool) {
panic(l.errorf("illegal input character: %q", l.peek(0)))
}

func (l *Lexer) consumeFieldToken(noError bool) {
func (l *Lexer) consumeFieldToken(noPanic bool) {
if l.peekOk(0) && char.IsIdentPart(l.peek(0)) {
i := 0
for l.peekOk(i) && char.IsIdentPart(l.peek(i)) {
Expand All @@ -317,10 +317,10 @@ func (l *Lexer) consumeFieldToken(noError bool) {
return
}

l.consumeToken(noError)
l.consumeToken(noPanic)
}

func (l *Lexer) consumeNumber(noError bool) {
func (l *Lexer) consumeNumber(noPanic bool) {
// https://cloud.google.com/spanner/docs/lexical#integer-literals
// https://cloud.google.com/spanner/docs/lexical#floating-point-literals

Expand Down Expand Up @@ -373,7 +373,7 @@ func (l *Lexer) consumeNumber(noError bool) {
}

if l.peekOk(0) && char.IsIdentPart(l.peek(0)) {
if noError {
if noPanic {
l.Token.Kind = token.TokenBad
return
}
Expand All @@ -382,41 +382,41 @@ func (l *Lexer) consumeNumber(noError bool) {
}
}

func (l *Lexer) consumeRawBytes(noError bool) {
func (l *Lexer) consumeRawBytes(noPanic bool) {
l.Token.Kind = token.TokenBytes

var hasError bool
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), true, false, "raw bytes literal", noError)
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), true, false, "raw bytes literal", noPanic)
if hasError {
l.Token.Kind = token.TokenBad
}
}

func (l *Lexer) consumeBytes(noError bool) {
func (l *Lexer) consumeBytes(noPanic bool) {
l.Token.Kind = token.TokenBytes

var hasError bool
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), false, false, "bytes literal", noError)
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), false, false, "bytes literal", noPanic)
if hasError {
l.Token.Kind = token.TokenBad
}
}

func (l *Lexer) consumeRawString(noError bool) {
func (l *Lexer) consumeRawString(noPanic bool) {
l.Token.Kind = token.TokenString

var hasError bool
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), true, true, "raw string literal", noError)
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), true, true, "raw string literal", noPanic)
if hasError {
l.Token.Kind = token.TokenBad
}
}

func (l *Lexer) consumeString(noError bool) {
func (l *Lexer) consumeString(noPanic bool) {
l.Token.Kind = token.TokenString

var hasError bool
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), false, true, "string literal", noError)
l.Token.AsString, hasError = l.consumeQuotedContent(l.peekDelimiter(), false, true, "string literal", noPanic)
if hasError {
l.Token.Kind = token.TokenBad
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (l *Lexer) peekDelimiter() string {
}
}

func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, noError bool) (string, bool) {
func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, noPanic bool) (string, bool) {
// https://cloud.google.com/spanner/docs/lexical#string-and-bytes-literals

if len(q) == 3 {
Expand All @@ -464,7 +464,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
for l.peekOk(i) {
if l.slice(i, i+len(q)) == q {
if len(content) == 0 && name == "identifier" {
if noError {
if noPanic {
hasError = true
} else {
l.panicfAtPosition(token.Pos(l.pos), token.Pos(l.pos+i+len(q)), "invalid empty identifier")
Expand All @@ -482,7 +482,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
if c == '\\' {
i++
if !l.peekOk(i) {
if noError {
if noPanic {
hasError = true
continue
}
Expand Down Expand Up @@ -517,7 +517,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
case 'x', 'X':
for j := 0; j < 2; j++ {
if !(l.peekOk(i+j) && char.IsHexDigit(l.peek(i+j))) {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -526,7 +526,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
}
u, err := strconv.ParseUint(l.slice(i, i+2), 16, 8)
if err != nil {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -536,7 +536,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
i += 2
case 'u', 'U':
if !unicode {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -548,7 +548,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
}
for j := 0; j < size; j++ {
if !(l.peekOk(i+j) && char.IsHexDigit(l.peek(i+j))) {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -557,14 +557,14 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
}
u, err := strconv.ParseUint(l.slice(i, i+size), 16, 32)
if err != nil {
if noError {
if noPanic {
hasError = true
continue
}
l.panicfAtPosition(token.Pos(l.pos+i-2), token.Pos(l.pos+i+size), "invalid escape sequence: %v", err)
}
if 0xD800 <= u && u <= 0xDFFF || 0x10FFFF < u {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -577,7 +577,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
case '0', '1', '2', '3':
for j := 0; j < 2; j++ {
if !(l.peekOk(i+j) && char.IsOctalDigit(l.peek(i+j))) {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -586,7 +586,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
}
u, err := strconv.ParseUint(l.slice(i-1, i+2), 8, 8)
if err != nil {
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -595,7 +595,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
content = append(content, byte(u))
i += 2
default:
if noError {
if noPanic {
hasError = true
continue
}
Expand All @@ -606,7 +606,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
}

if c == '\n' && len(q) != 3 {
if noError {
if noPanic {
hasError = true
i++
continue
Expand All @@ -618,7 +618,7 @@ func (l *Lexer) consumeQuotedContent(q string, raw, unicode bool, name string, n
i++
}

if noError {
if noPanic {
l.skipN(i)
return "", true
}
Expand All @@ -638,19 +638,19 @@ func (l *Lexer) skipSpaces() {
}
}

func (l *Lexer) skipComment(noError bool) bool {
func (l *Lexer) skipComment(noPanic bool) bool {
r, _ := utf8.DecodeRuneInString(l.Buffer[l.pos:])
switch {
case r == '#' || r == '/' && l.peekIs(1, '/') || r == '-' && l.peekIs(1, '-'):
return l.skipCommentUntil("\n", false, noError)
return l.skipCommentUntil("\n", false, noPanic)
case r == '/' && l.peekIs(1, '*'):
return l.skipCommentUntil("*/", true, noError)
return l.skipCommentUntil("*/", true, noPanic)
default:
return false
}
}

func (l *Lexer) skipCommentUntil(end string, mustEnd bool, noError bool) bool {
func (l *Lexer) skipCommentUntil(end string, mustEnd bool, noPanic bool) bool {
pos := token.Pos(l.pos)
for !l.eof() {
if l.slice(0, len(end)) == end {
Expand All @@ -660,7 +660,7 @@ func (l *Lexer) skipCommentUntil(end string, mustEnd bool, noError bool) bool {
l.skip()
}
if mustEnd {
if noError {
if noPanic {
return true
}
l.panicfAtPosition(pos, token.Pos(l.pos), "unclosed comment")
Expand Down

0 comments on commit 20e2973

Please sign in to comment.