Skip to content

Commit 1de6569

Browse files
committed
defer chan close and removed dead code
1 parent df9cb3a commit 1de6569

4 files changed

Lines changed: 12 additions & 17 deletions

File tree

lexer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ func lex(input string) *lexer {
108108

109109
// run runs the state machine for the lexer.
110110
func (l *lexer) run() {
111+
defer close(l.items)
111112
for state := lexStart; state != nil; {
112113
state = state(l)
113114
}
114-
close(l.items)
115115
}
116116

117117
func lexStart(l *lexer) lexStateFn {
@@ -209,11 +209,6 @@ func lexEOF(l *lexer) lexStateFn {
209209
return lexIdentifier
210210
}
211211

212-
// isSpace reports whether r is a space character.
213-
func isSpace(r rune) bool {
214-
return r == ' ' || r == '\t'
215-
}
216-
217212
// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
218213
func isValidIdentifierRune(r rune) bool {
219214
return strings.ContainsRune("_-", r) || unicode.IsLetter(r) || unicode.IsDigit(r)

lexer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ var lexTests = []lexTest{
4141
{"error", "somekey.", []item{mkItem(itemIdentifier, "somekey"), iDot, iEOF}},
4242
}
4343

44-
// collect gathers the emitted items into a slice.
45-
func collect(t *lexTest) (items []item) {
44+
// collectItems gathers the emitted items into a slice.
45+
func collectItems(t *lexTest) (items []item) {
4646
l := lex(t.input)
4747
for {
4848
item := l.nextItem()
@@ -54,7 +54,7 @@ func collect(t *lexTest) (items []item) {
5454
return
5555
}
5656

57-
func equal(i1, i2 []item, checkPos bool) bool {
57+
func itemEqual(i1, i2 []item, checkPos bool) bool {
5858
if len(i1) != len(i2) {
5959
return false
6060
}
@@ -71,8 +71,8 @@ func equal(i1, i2 []item, checkPos bool) bool {
7171

7272
func TestLex(t *testing.T) {
7373
for _, test := range lexTests {
74-
items := collect(&test)
75-
if !equal(items, test.items, false) {
74+
items := collectItems(&test)
75+
if !itemEqual(items, test.items, false) {
7676
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, items, test.items)
7777
}
7878
}

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func (p *parser) nextItem() token {
5656
}
5757

5858
func (p *parser) run() {
59+
defer close(p.tokens)
5960
for state := parseStart; state != nil; {
6061
state = state(p)
6162
}
62-
close(p.tokens)
6363
}
6464

6565
func (p *parser) getBufOrNext() item {

parser_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ var parseTests = []parseTest{
3737
{"somekey.", []token{mkToken(tokenIdentifier, "somekey"), tEnd}},
3838
}
3939

40-
// collect gathers the emitted items into a slice.
41-
func pcollect(t *parseTest) (tokens []token) {
40+
// collectTokens gathers the emitted items into a slice.
41+
func collectTokens(t *parseTest) (tokens []token) {
4242
p := parse(t.input)
4343
for {
4444
token := p.nextItem()
@@ -50,7 +50,7 @@ func pcollect(t *parseTest) (tokens []token) {
5050
return
5151
}
5252

53-
func pequal(i1, i2 []token) bool {
53+
func tokenEqual(i1, i2 []token) bool {
5454
if len(i1) != len(i2) {
5555
return false
5656
}
@@ -67,8 +67,8 @@ func pequal(i1, i2 []token) bool {
6767

6868
func TestParse(t *testing.T) {
6969
for _, test := range parseTests {
70-
tokens := pcollect(&test)
71-
if !pequal(tokens, test.tokens) {
70+
tokens := collectTokens(&test)
71+
if !tokenEqual(tokens, test.tokens) {
7272
t.Errorf("got\n\t%+v\nexpected\n\t%v", tokens, test.tokens)
7373
}
7474
}

0 commit comments

Comments
 (0)