Commit 211cdf1e authored by Marvin Stenger's avatar Marvin Stenger Committed by Ian Lance Taylor

cmd/compile/internal/gc: cleaning lex.go

Cleaning along the way:
-convert variable types from int to bool
-remove unnecessary functions
-remove unnecessary type conversion
-remove unnecessary variable declarations
-transform struct{string,string} with lookup to map[string]string

This change passes go build -toolexec 'toolstash -cmp' -a std.

Change-Id: I259728fe4afd7f23b67f08fab856ce0abee57b21
Reviewed-on: https://go-review.googlesource.com/14435Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 19d262ff
...@@ -382,12 +382,12 @@ type Sig struct { ...@@ -382,12 +382,12 @@ type Sig struct {
type Io struct { type Io struct {
infile string infile string
bin *obj.Biobuf bin *obj.Biobuf
nlsemi int cp string // used for content when bin==nil
eofnl int
last int last int
peekc int peekc int
peekc1 int // second peekc for ... peekc1 int // second peekc for ...
cp string // used for content when bin==nil nlsemi bool
eofnl bool
importsafe bool importsafe bool
} }
...@@ -598,7 +598,7 @@ var incannedimport int ...@@ -598,7 +598,7 @@ var incannedimport int
var statuniqgen int // name generator for static temps var statuniqgen int // name generator for static temps
var loophack int var loophack bool
var iota_ int32 var iota_ int32
......
...@@ -2311,6 +2311,6 @@ func fixlbrace(lbr int) { ...@@ -2311,6 +2311,6 @@ func fixlbrace(lbr int) {
// set up for another one now that we're done. // set up for another one now that we're done.
// See comment in lex.C about loophack. // See comment in lex.C about loophack.
if lbr == LBODY { if lbr == LBODY {
loophack = 1 loophack = true
} }
} }
...@@ -26,7 +26,7 @@ var yyprev int ...@@ -26,7 +26,7 @@ var yyprev int
var yylast int var yylast int
var imported_unsafe int var imported_unsafe bool
var ( var (
goos string goos string
...@@ -60,26 +60,6 @@ var debugtab = []struct { ...@@ -60,26 +60,6 @@ var debugtab = []struct {
{"wb", &Debug_wb}, // print information about write barriers {"wb", &Debug_wb}, // print information about write barriers
} }
// Our own isdigit, isspace, isalpha, isalnum that take care
// of EOF and other out of range arguments.
func yy_isdigit(c int) bool {
return c >= 0 && c <= 0xFF && isdigit(c)
}
func yy_isspace(c int) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}
func yy_isalpha(c int) bool {
return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
}
func yy_isalnum(c int) bool {
return c >= 0 && c <= 0xFF && isalnum(c)
}
// Disallow use of isdigit etc.
const ( const (
EOF = -1 EOF = -1
) )
...@@ -334,8 +314,8 @@ func Main() { ...@@ -334,8 +314,8 @@ func Main() {
curio.peekc = 0 curio.peekc = 0
curio.peekc1 = 0 curio.peekc1 = 0
curio.nlsemi = 0 curio.nlsemi = false
curio.eofnl = 0 curio.eofnl = false
curio.last = 0 curio.last = 0
// Skip initial BOM if present. // Skip initial BOM if present.
...@@ -346,7 +326,7 @@ func Main() { ...@@ -346,7 +326,7 @@ func Main() {
block = 1 block = 1
iota_ = -1000000 iota_ = -1000000
imported_unsafe = 0 imported_unsafe = false
yyparse() yyparse()
if nsyntaxerrors != 0 { if nsyntaxerrors != 0 {
...@@ -587,7 +567,7 @@ func addidir(dir string) { ...@@ -587,7 +567,7 @@ func addidir(dir string) {
// is this path a local name? begins with ./ or ../ or / // is this path a local name? begins with ./ or ../ or /
func islocalname(name string) bool { func islocalname(name string) bool {
return strings.HasPrefix(name, "/") || return strings.HasPrefix(name, "/") ||
Ctxt.Windows != 0 && len(name) >= 3 && yy_isalpha(int(name[0])) && name[1] == ':' && name[2] == '/' || Ctxt.Windows != 0 && len(name) >= 3 && isAlpha(int(name[0])) && name[1] == ':' && name[2] == '/' ||
strings.HasPrefix(name, "./") || name == "." || strings.HasPrefix(name, "./") || name == "." ||
strings.HasPrefix(name, "../") || name == ".." strings.HasPrefix(name, "../") || name == ".."
} }
...@@ -702,7 +682,7 @@ func importfile(f *Val, line int) { ...@@ -702,7 +682,7 @@ func importfile(f *Val, line int) {
importpkg = mkpkg(f.U.(string)) importpkg = mkpkg(f.U.(string))
cannedimports("unsafe.o", unsafeimport) cannedimports("unsafe.o", unsafeimport)
imported_unsafe = 1 imported_unsafe = true
return return
} }
...@@ -803,19 +783,18 @@ func importfile(f *Val, line int) { ...@@ -803,19 +783,18 @@ func importfile(f *Val, line int) {
curio.peekc = 0 curio.peekc = 0
curio.peekc1 = 0 curio.peekc1 = 0
curio.infile = file curio.infile = file
curio.nlsemi = 0 curio.nlsemi = false
typecheckok = true typecheckok = true
var c int32
for { for {
c = int32(getc()) c := getc()
if c == EOF { if c == EOF {
break break
} }
if c != '$' { if c != '$' {
continue continue
} }
c = int32(getc()) c = getc()
if c == EOF { if c == EOF {
break break
} }
...@@ -854,7 +833,7 @@ func cannedimports(file string, cp string) { ...@@ -854,7 +833,7 @@ func cannedimports(file string, cp string) {
curio.peekc1 = 0 curio.peekc1 = 0
curio.infile = file curio.infile = file
curio.cp = cp curio.cp = cp
curio.nlsemi = 0 curio.nlsemi = false
curio.importsafe = false curio.importsafe = false
typecheckok = true typecheckok = true
...@@ -864,7 +843,7 @@ func cannedimports(file string, cp string) { ...@@ -864,7 +843,7 @@ func cannedimports(file string, cp string) {
func isfrog(c int) bool { func isfrog(c int) bool {
// complain about possibly invisible control characters // complain about possibly invisible control characters
if c < ' ' { if c < ' ' {
return !yy_isspace(c) // exclude good white space return !isSpace(c) // exclude good white space
} }
if 0x7f <= c && c <= 0xa0 { // DEL, unicode block including unbreakable space. if 0x7f <= c && c <= 0xa0 { // DEL, unicode block including unbreakable space.
...@@ -874,8 +853,8 @@ func isfrog(c int) bool { ...@@ -874,8 +853,8 @@ func isfrog(c int) bool {
} }
type Loophack struct { type Loophack struct {
v int
next *Loophack next *Loophack
v bool
} }
var _yylex_lstk *Loophack var _yylex_lstk *Loophack
...@@ -885,7 +864,6 @@ func _yylex(yylval *yySymType) int32 { ...@@ -885,7 +864,6 @@ func _yylex(yylval *yySymType) int32 {
var escflag int var escflag int
var v int64 var v int64
var cp *bytes.Buffer var cp *bytes.Buffer
var rune_ uint
var s *Sym var s *Sym
var h *Loophack var h *Loophack
var str string var str string
...@@ -894,8 +872,8 @@ func _yylex(yylval *yySymType) int32 { ...@@ -894,8 +872,8 @@ func _yylex(yylval *yySymType) int32 {
l0: l0:
c := getc() c := getc()
if yy_isspace(c) { if isSpace(c) {
if c == '\n' && curio.nlsemi != 0 { if c == '\n' && curio.nlsemi {
ungetc(c) ungetc(c)
if Debug['x'] != 0 { if Debug['x'] != 0 {
fmt.Printf("lex: implicit semi\n") fmt.Printf("lex: implicit semi\n")
...@@ -916,20 +894,20 @@ l0: ...@@ -916,20 +894,20 @@ l0:
goto talph goto talph
} }
if yy_isalpha(c) { if isAlpha(c) {
cp = &lexbuf cp = &lexbuf
cp.Reset() cp.Reset()
goto talph goto talph
} }
if yy_isdigit(c) { if isDigit(c) {
cp = &lexbuf cp = &lexbuf
cp.Reset() cp.Reset()
if c != '0' { if c != '0' {
for { for {
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
c = getc() c = getc()
if yy_isdigit(c) { if isDigit(c) {
continue continue
} }
if c == '.' { if c == '.' {
...@@ -951,7 +929,7 @@ l0: ...@@ -951,7 +929,7 @@ l0:
for { for {
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
c = getc() c = getc()
if yy_isdigit(c) { if isDigit(c) {
continue continue
} }
if c >= 'a' && c <= 'f' { if c >= 'a' && c <= 'f' {
...@@ -976,7 +954,7 @@ l0: ...@@ -976,7 +954,7 @@ l0:
c1 = 0 c1 = 0
for { for {
if !yy_isdigit(c) { if !isDigit(c) {
break break
} }
if c < '0' || c > '7' { if c < '0' || c > '7' {
...@@ -1014,7 +992,7 @@ l0: ...@@ -1014,7 +992,7 @@ l0:
case '.': case '.':
c1 = getc() c1 = getc()
if yy_isdigit(c1) { if isDigit(c1) {
cp = &lexbuf cp = &lexbuf
cp.Reset() cp.Reset()
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
...@@ -1048,8 +1026,7 @@ l0: ...@@ -1048,8 +1026,7 @@ l0:
if v < utf8.RuneSelf || escflag != 0 { if v < utf8.RuneSelf || escflag != 0 {
cp.WriteByte(byte(v)) cp.WriteByte(byte(v))
} else { } else {
rune_ = uint(v) cp.WriteRune(rune(v))
cp.WriteRune(rune(rune_))
} }
} }
...@@ -1106,23 +1083,23 @@ l0: ...@@ -1106,23 +1083,23 @@ l0:
case '/': case '/':
c1 = getc() c1 = getc()
if c1 == '*' { if c1 == '*' {
nl := 0 nl := false
for { for {
c = int(getr()) c = int(getr())
if c == '\n' { if c == '\n' {
nl = 1 nl = true
} }
for c == '*' { for c == '*' {
c = int(getr()) c = int(getr())
if c == '/' { if c == '/' {
if nl != 0 { if nl {
ungetc('\n') ungetc('\n')
} }
goto l0 goto l0
} }
if c == '\n' { if c == '\n' {
nl = 1 nl = true
} }
} }
...@@ -1308,15 +1285,15 @@ l0: ...@@ -1308,15 +1285,15 @@ l0:
* *
* when we see the keyword, the next * when we see the keyword, the next
* non-parenthesized '{' becomes an LBODY. * non-parenthesized '{' becomes an LBODY.
* loophack is normally 0. * loophack is normally false.
* a keyword makes it go up to 1. * a keyword sets it to true.
* parens push loophack onto a stack and go back to 0. * parens push loophack onto a stack and go back to false.
* a '{' with loophack == 1 becomes LBODY and disables loophack. * a '{' with loophack == true becomes LBODY and disables loophack.
* *
* i said it was clumsy. * i said it was clumsy.
*/ */
case '(', '[': case '(', '[':
if loophack != 0 || _yylex_lstk != nil { if loophack || _yylex_lstk != nil {
h = new(Loophack) h = new(Loophack)
if h == nil { if h == nil {
Flusherrors() Flusherrors()
...@@ -1327,7 +1304,7 @@ l0: ...@@ -1327,7 +1304,7 @@ l0:
h.v = loophack h.v = loophack
h.next = _yylex_lstk h.next = _yylex_lstk
_yylex_lstk = h _yylex_lstk = h
loophack = 0 loophack = false
} }
goto lx goto lx
...@@ -1342,11 +1319,11 @@ l0: ...@@ -1342,11 +1319,11 @@ l0:
goto lx goto lx
case '{': case '{':
if loophack == 1 { if loophack {
if Debug['x'] != 0 { if Debug['x'] != 0 {
fmt.Printf("%v lex: LBODY\n", Ctxt.Line(int(lexlineno))) fmt.Printf("%v lex: LBODY\n", Ctxt.Line(int(lexlineno)))
} }
loophack = 0 loophack = false
return LBODY return LBODY
} }
...@@ -1395,14 +1372,14 @@ talph: ...@@ -1395,14 +1372,14 @@ talph:
for { for {
if c >= utf8.RuneSelf { if c >= utf8.RuneSelf {
ungetc(c) ungetc(c)
rune_ = uint(getr()) r := rune(getr())
// 0xb7 · is used for internal names // 0xb7 · is used for internal names
if !unicode.IsLetter(rune(rune_)) && !unicode.IsDigit(rune(rune_)) && (importpkg == nil || rune_ != 0xb7) { if !unicode.IsLetter(r) && !unicode.IsDigit(r) && (importpkg == nil || r != 0xb7) {
Yyerror("invalid identifier character U+%04x", rune_) Yyerror("invalid identifier character U+%04x", r)
} }
cp.WriteRune(rune(rune_)) cp.WriteRune(r)
} else if !yy_isalnum(c) && c != '_' { } else if !isAlnum(c) && c != '_' {
break break
} else { } else {
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
...@@ -1419,7 +1396,7 @@ talph: ...@@ -1419,7 +1396,7 @@ talph:
goto l0 goto l0
case LFOR, LIF, LSWITCH, LSELECT: case LFOR, LIF, LSWITCH, LSELECT:
loophack = 1 // see comment about loophack above loophack = true // see comment about loophack above
} }
if Debug['x'] != 0 { if Debug['x'] != 0 {
...@@ -1450,7 +1427,7 @@ casedot: ...@@ -1450,7 +1427,7 @@ casedot:
for { for {
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
c = getc() c = getc()
if !yy_isdigit(c) { if !isDigit(c) {
break break
} }
} }
...@@ -1475,10 +1452,10 @@ caseep: ...@@ -1475,10 +1452,10 @@ caseep:
c = getc() c = getc()
} }
if !yy_isdigit(c) { if !isDigit(c) {
Yyerror("malformed floating point constant exponent") Yyerror("malformed floating point constant exponent")
} }
for yy_isdigit(c) { for isDigit(c) {
cp.WriteByte(byte(c)) cp.WriteByte(byte(c))
c = getc() c = getc()
} }
...@@ -1548,7 +1525,7 @@ func internString(b []byte) string { ...@@ -1548,7 +1525,7 @@ func internString(b []byte) string {
func more(pp *string) bool { func more(pp *string) bool {
p := *pp p := *pp
for p != "" && yy_isspace(int(p[0])) { for p != "" && isSpace(int(p[0])) {
p = p[1:] p = p[1:]
} }
*pp = p *pp = p
...@@ -1594,7 +1571,7 @@ func getlinepragma() int { ...@@ -1594,7 +1571,7 @@ func getlinepragma() int {
} }
if verb == "go:linkname" { if verb == "go:linkname" {
if imported_unsafe == 0 { if !imported_unsafe {
Yyerror("//go:linkname only allowed in Go files that import \"unsafe\"") Yyerror("//go:linkname only allowed in Go files that import \"unsafe\"")
} }
f := strings.Fields(cmd) f := strings.Fields(cmd)
...@@ -1711,7 +1688,7 @@ func getimpsym(pp *string) string { ...@@ -1711,7 +1688,7 @@ func getimpsym(pp *string) string {
return "" return ""
} }
i := 0 i := 0
for i < len(p) && !yy_isspace(int(p[i])) && p[i] != '"' { for i < len(p) && !isSpace(int(p[i])) && p[i] != '"' {
i++ i++
} }
sym := p[:i] sym := p[:i]
...@@ -1746,9 +1723,7 @@ func pragcgo(text string) { ...@@ -1746,9 +1723,7 @@ func pragcgo(text string) {
verb := text[3:] // skip "go:" verb := text[3:] // skip "go:"
if verb == "cgo_dynamic_linker" || verb == "dynlinker" { if verb == "cgo_dynamic_linker" || verb == "dynlinker" {
var ok bool p, ok := getquoted(&q)
var p string
p, ok = getquoted(&q)
if !ok { if !ok {
Yyerror("usage: //go:cgo_dynamic_linker \"path\"") Yyerror("usage: //go:cgo_dynamic_linker \"path\"")
return return
...@@ -1830,9 +1805,7 @@ func pragcgo(text string) { ...@@ -1830,9 +1805,7 @@ func pragcgo(text string) {
} }
if verb == "cgo_ldflag" { if verb == "cgo_ldflag" {
var ok bool p, ok := getquoted(&q)
var p string
p, ok = getquoted(&q)
if !ok { if !ok {
Yyerror("usage: //go:cgo_ldflag \"arg\"") Yyerror("usage: //go:cgo_ldflag \"arg\"")
return return
...@@ -1866,7 +1839,7 @@ func yyparse() { ...@@ -1866,7 +1839,7 @@ func yyparse() {
func yylex(yylval *yySymType) int32 { func yylex(yylval *yySymType) int32 {
lx := int(_yylex(yylval)) lx := int(_yylex(yylval))
if curio.nlsemi != 0 && lx == EOF { if curio.nlsemi && lx == EOF {
// 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.
lx = ';' lx = ';'
...@@ -1884,10 +1857,10 @@ func yylex(yylval *yySymType) int32 { ...@@ -1884,10 +1857,10 @@ func yylex(yylval *yySymType) int32 {
')', ')',
'}', '}',
']': ']':
curio.nlsemi = 1 curio.nlsemi = true
default: default:
curio.nlsemi = 0 curio.nlsemi = false
} }
// Track last two tokens returned by yylex. // Track last two tokens returned by yylex.
...@@ -1942,10 +1915,10 @@ check: ...@@ -1942,10 +1915,10 @@ check:
// insert \n at EOF // insert \n at EOF
case EOF: case EOF:
if curio.eofnl != 0 || curio.last == '\n' { if curio.eofnl || curio.last == '\n' {
return EOF return EOF
} }
curio.eofnl = 1 curio.eofnl = true
c = '\n' c = '\n'
fallthrough fallthrough
...@@ -2505,77 +2478,67 @@ func lexname(lex int) string { ...@@ -2505,77 +2478,67 @@ func lexname(lex int) string {
return fmt.Sprintf("LEX-%d", lex) return fmt.Sprintf("LEX-%d", lex)
} }
var yytfix = []struct { var yytfix = map[string]string{
have string "$end": "EOF",
want string "LASOP": "op=",
}{ "LBREAK": "break",
{"$end", "EOF"}, "LCASE": "case",
{"LASOP", "op="}, "LCHAN": "chan",
{"LBREAK", "break"}, "LCOLAS": ":=",
{"LCASE", "case"}, "LCONST": "const",
{"LCHAN", "chan"}, "LCONTINUE": "continue",
{"LCOLAS", ":="}, "LDDD": "...",
{"LCONST", "const"}, "LDEFAULT": "default",
{"LCONTINUE", "continue"}, "LDEFER": "defer",
{"LDDD", "..."}, "LELSE": "else",
{"LDEFAULT", "default"}, "LFALL": "fallthrough",
{"LDEFER", "defer"}, "LFOR": "for",
{"LELSE", "else"}, "LFUNC": "func",
{"LFALL", "fallthrough"}, "LGO": "go",
{"LFOR", "for"}, "LGOTO": "goto",
{"LFUNC", "func"}, "LIF": "if",
{"LGO", "go"}, "LIMPORT": "import",
{"LGOTO", "goto"}, "LINTERFACE": "interface",
{"LIF", "if"}, "LMAP": "map",
{"LIMPORT", "import"}, "LNAME": "name",
{"LINTERFACE", "interface"}, "LPACKAGE": "package",
{"LMAP", "map"}, "LRANGE": "range",
{"LNAME", "name"}, "LRETURN": "return",
{"LPACKAGE", "package"}, "LSELECT": "select",
{"LRANGE", "range"}, "LSTRUCT": "struct",
{"LRETURN", "return"}, "LSWITCH": "switch",
{"LSELECT", "select"}, "LTYPE": "type",
{"LSTRUCT", "struct"}, "LVAR": "var",
{"LSWITCH", "switch"}, "LANDAND": "&&",
{"LTYPE", "type"}, "LANDNOT": "&^",
{"LVAR", "var"}, "LBODY": "{",
{"LANDAND", "&&"}, "LCOMM": "<-",
{"LANDNOT", "&^"}, "LDEC": "--",
{"LBODY", "{"}, "LINC": "++",
{"LCOMM", "<-"}, "LEQ": "==",
{"LDEC", "--"}, "LGE": ">=",
{"LINC", "++"}, "LGT": ">",
{"LEQ", "=="}, "LLE": "<=",
{"LGE", ">="}, "LLT": "<",
{"LGT", ">"}, "LLSH": "<<",
{"LLE", "<="}, "LRSH": ">>",
{"LLT", "<"}, "LOROR": "||",
{"LLSH", "<<"}, "LNE": "!=",
{"LRSH", ">>"},
{"LOROR", "||"},
{"LNE", "!="},
// spell out to avoid confusion with punctuation in error messages // spell out to avoid confusion with punctuation in error messages
{"';'", "semicolon or newline"}, "';'": "semicolon or newline",
{"','", "comma"}, "','": "comma",
} }
func init() { func init() {
yyErrorVerbose = true yyErrorVerbose = true
Outer:
for i, s := range yyToknames { for i, s := range yyToknames {
// Apply yytfix if possible. // Apply yytfix if possible.
for _, fix := range yytfix { if fix, ok := yytfix[s]; ok {
if s == fix.have { yyToknames[i] = fix
yyToknames[i] = fix.want } else if len(s) == 3 && s[0] == '\'' && s[2] == '\'' {
continue Outer // Turn 'x' into x.
}
}
// Turn 'x' into x.
if len(s) == 3 && s[0] == '\'' && s[2] == '\'' {
yyToknames[i] = s[1:2] yyToknames[i] = s[1:2]
continue
} }
} }
} }
......
...@@ -122,7 +122,7 @@ func Yyerror(format string, args ...interface{}) { ...@@ -122,7 +122,7 @@ func Yyerror(format string, args ...interface{}) {
// An unexpected EOF caused a syntax error. Use the previous // An unexpected EOF caused a syntax error. Use the previous
// line number since getc generated a fake newline character. // line number since getc generated a fake newline character.
if curio.eofnl != 0 { if curio.eofnl {
lexlineno = prevlineno lexlineno = prevlineno
} }
......
...@@ -18,15 +18,19 @@ func atoi(s string) int { ...@@ -18,15 +18,19 @@ func atoi(s string) int {
return int(n) return int(n)
} }
func isalnum(c int) bool { func isSpace(c int) bool {
return isalpha(c) || isdigit(c) return c == ' ' || c == '\t' || c == '\n' || c == '\r'
} }
func isalpha(c int) bool { func isAlnum(c int) bool {
return isAlpha(c) || isDigit(c)
}
func isAlpha(c int) bool {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
} }
func isdigit(c int) bool { func isDigit(c int) bool {
return '0' <= c && c <= '9' return '0' <= c && c <= '9'
} }
......
...@@ -160,7 +160,7 @@ func fixlbrace(lbr int) { ...@@ -160,7 +160,7 @@ func fixlbrace(lbr int) {
// set up for another one now that we're done. // set up for another one now that we're done.
// See comment in lex.C about loophack. // See comment in lex.C about loophack.
if lbr == LBODY { if lbr == LBODY {
loophack = 1 loophack = true
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment