1
0
mirror of https://github.com/golang/go synced 2024-11-19 15:24:46 -07:00

cmd/compile: simplify lexer.ungetr

Change-Id: Id041c1b2d364aecd7a6613a53237f7de2c650a7e
Reviewed-on: https://go-review.googlesource.com/20599
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2016-03-11 13:55:53 -08:00
parent e2836935bb
commit e78ff3823e

View File

@ -885,7 +885,8 @@ const (
type lexer struct { type lexer struct {
// source // source
bin *bufio.Reader bin *bufio.Reader
prevlineno int32 // line no. of most recently read character
nlsemi bool // if set, '\n' and EOF translate to ';' nlsemi bool // if set, '\n' and EOF translate to ';'
@ -1003,7 +1004,7 @@ l0:
switch c { switch c {
case EOF: case EOF:
l.ungetr(EOF) // return EOF again in future next call l.ungetr()
// Treat EOF as "end of line" for the purposes // Treat EOF as "end of line" for the purposes
// of inserting a semicolon. // of inserting a semicolon.
if nlsemi { if nlsemi {
@ -1023,7 +1024,7 @@ l0:
case '.': case '.':
c1 = l.getr() c1 = l.getr()
if isDigit(c1) { if isDigit(c1) {
l.ungetr(c1) l.ungetr()
l.number('.') l.number('.')
return return
} }
@ -1036,7 +1037,7 @@ l0:
goto lx goto lx
} }
l.ungetr(c1) l.ungetr()
c1 = '.' c1 = '.'
} }
@ -1086,7 +1087,7 @@ l0:
c = l.getlinepragma() c = l.getlinepragma()
for { for {
if c == '\n' || c == EOF { if c == '\n' || c == EOF {
l.ungetr(c) l.ungetr()
goto l0 goto l0
} }
@ -1240,7 +1241,7 @@ l0:
goto l0 goto l0
} }
l.ungetr(c1) l.ungetr()
lx: lx:
if Debug['x'] != 0 { if Debug['x'] != 0 {
@ -1269,7 +1270,7 @@ binop:
c1 = l.getr() c1 = l.getr()
binop1: binop1:
if c1 != '=' { if c1 != '=' {
l.ungetr(c1) l.ungetr()
l.op = op l.op = op
l.prec = prec l.prec = prec
goto lx goto lx
@ -1312,7 +1313,7 @@ func (l *lexer) ident(c rune) {
} }
cp = nil cp = nil
l.ungetr(c) l.ungetr()
name := lexbuf.Bytes() name := lexbuf.Bytes()
@ -1478,7 +1479,7 @@ func (l *lexer) number(c rune) {
} }
} }
l.ungetr(c) l.ungetr()
if isInt { if isInt {
if malformedOctal { if malformedOctal {
@ -1591,7 +1592,7 @@ func (l *lexer) rune() {
if c := l.getr(); c != '\'' { if c := l.getr(); c != '\'' {
Yyerror("missing '") Yyerror("missing '")
l.ungetr(c) l.ungetr()
} }
x := new(Mpint) x := new(Mpint)
@ -1892,6 +1893,7 @@ func pragcgo(text string) {
func (l *lexer) getr() rune { func (l *lexer) getr() rune {
redo: redo:
l.prevlineno = lexlineno
r, w, err := l.bin.ReadRune() r, w, err := l.bin.ReadRune()
if err != nil { if err != nil {
if err != io.EOF { if err != io.EOF {
@ -1918,11 +1920,9 @@ redo:
return r return r
} }
func (l *lexer) ungetr(r rune) { func (l *lexer) ungetr() {
l.bin.UnreadRune() l.bin.UnreadRune()
if r == '\n' && importpkg == nil { lexlineno = l.prevlineno
lexlineno--
}
} }
// onechar lexes a single character within a rune or interpreted string literal, // onechar lexes a single character within a rune or interpreted string literal,
@ -1932,12 +1932,12 @@ func (l *lexer) onechar(quote rune) (r rune, b byte, ok bool) {
switch c { switch c {
case EOF: case EOF:
Yyerror("eof in string") Yyerror("eof in string")
l.ungetr(EOF) l.ungetr()
return return
case '\n': case '\n':
Yyerror("newline in string") Yyerror("newline in string")
l.ungetr('\n') l.ungetr()
return return
case '\\': case '\\':
@ -1971,7 +1971,7 @@ func (l *lexer) onechar(quote rune) (r rune, b byte, ok bool) {
} }
Yyerror("non-octal character in escape sequence: %c", c) Yyerror("non-octal character in escape sequence: %c", c)
l.ungetr(c) l.ungetr()
} }
if x > 255 { if x > 255 {
@ -2029,7 +2029,7 @@ func (l *lexer) hexchar(n int) uint32 {
d = uint32(c - 'A' + 10) d = uint32(c - 'A' + 10)
default: default:
Yyerror("non-hex character in escape sequence: %c", c) Yyerror("non-hex character in escape sequence: %c", c)
l.ungetr(c) l.ungetr()
return x return x
} }
x = x*16 + d x = x*16 + d