diff --git a/ebnf/token/tokentype.go b/ebnf/token/tokentype.go new file mode 100644 index 0000000..cd7242e --- /dev/null +++ b/ebnf/token/tokentype.go @@ -0,0 +1,20 @@ +package token + +// types of Token +type TokenType int + +const ( + TokenError TokenType = iota + 1 + TokenEOF +) + +func (typ TokenType) String() string { + switch typ { + case TokenError: + return "ERROR" + case TokenEOF: + return "EOF" + default: + return "UNDEFINED" + } +} diff --git a/ebnf/token/tokentype_test.go b/ebnf/token/tokentype_test.go new file mode 100644 index 0000000..8ad1050 --- /dev/null +++ b/ebnf/token/tokentype_test.go @@ -0,0 +1,25 @@ +package token + +import ( + "fmt" + "testing" +) + +func TestTokenTypeToString(t *testing.T) { + var foo TokenType + + for _, o := range []struct { + typ TokenType + str string + }{ + {foo, "UNDEFINED"}, + {TokenError, "ERROR"}, + {TokenEOF, "EOF"}, + {1234, "UNDEFINED"}, + } { + str := fmt.Sprintf("%s", o.typ) + if str != o.str { + t.Errorf("TokenType:%v stringified as %s instead of %s.", int(o.typ), str, o.str) + } + } +}