M go.mod => go.mod +2 -0
@@ 1,3 1,5 @@
module git.sr.ht/~mna/fastpeg
go 1.12
+
+require github.com/stretchr/testify v1.3.0
A go.sum => go.sum +7 -0
@@ 0,0 1,7 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
M internal/bootstrap/token/token.go => internal/bootstrap/token/token.go +20 -0
@@ 88,11 88,31 @@ var stringTokens = [...]string{
Rparen: ")",
}
+var reverseToken map[string]Token
+
+func init() {
+ reverseToken = make(map[string]Token, len(stringTokens))
+ for t, s := range stringTokens {
+ if s != "" {
+ reverseToken[s] = Token(t)
+ }
+ }
+}
+
// IsLiteral returns true if token t is an identifier or a literal.
func (t Token) IsLiteral() bool {
return t > litStart && t < litEnd
}
+// FromString returns the token that has a string representation of s.
+func FromString(s string) Token {
+ t, ok := reverseToken[s]
+ if !ok {
+ t = Illegal
+ }
+ return t
+}
+
// String returns the string representation of token t.
func (t Token) String() string {
var s string
A internal/bootstrap/token/token_test.go => internal/bootstrap/token/token_test.go +23 -0
@@ 0,0 1,23 @@
+package token
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestStringReverse(t *testing.T) {
+ // <= symEnd to test beyond the last token
+ for tk := Illegal; tk <= symEnd; tk++ {
+ t.Run(tk.String(), func(t *testing.T) {
+ s := tk.String()
+ rev := FromString(s)
+ if strings.HasPrefix(s, "token(") {
+ require.Equal(t, Illegal, rev)
+ } else {
+ require.Equal(t, tk, rev)
+ }
+ })
+ }
+}