Moved register state to scopes
This commit is contained in:
14
src/build/token/Count.go
Normal file
14
src/build/token/Count.go
Normal file
@ -0,0 +1,14 @@
|
||||
package token
|
||||
|
||||
// Count counts how often the given token appears in the token list.
|
||||
func Count(tokens List, kind Kind, name string) int {
|
||||
count := 0
|
||||
|
||||
for _, t := range tokens {
|
||||
if t.Kind == Identifier && t.Text() == name {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
@ -95,6 +95,14 @@ func TestArray(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCount(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte(`a b b c c c`))
|
||||
assert.Equal(t, token.Count(tokens, token.Identifier, "a"), 1)
|
||||
assert.Equal(t, token.Count(tokens, token.Identifier, "b"), 2)
|
||||
assert.Equal(t, token.Count(tokens, token.Identifier, "c"), 3)
|
||||
assert.Equal(t, token.Count(tokens, token.Identifier, "d"), 0)
|
||||
}
|
||||
|
||||
func TestNewline(t *testing.T) {
|
||||
tokens := token.Tokenize([]byte("\n\n"))
|
||||
assert.DeepEqual(t, tokens, token.List{
|
||||
|
Reference in New Issue
Block a user