From 604ecfaed21b90b42017b70445abdf6f384c0bc7 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Wed, 30 Aug 2023 20:00:29 +0000 Subject: [PATCH] parser: introduce Token and TokenType enum Signed-off-by: Alejandro Mery --- go.mod | 3 ++- go.sum | 2 ++ parser/token.go | 41 ++++++++++++++++++++++++++++++++++++++ parser/tokentype_string.go | 28 ++++++++++++++++++++++++++ tools/tools.go | 1 + 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 parser/token.go create mode 100644 parser/tokentype_string.go diff --git a/go.mod b/go.mod index afbcf7f..ce1e7e0 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( asciigoat.org/core v0.3.6 github.com/mgechev/revive v1.3.3 + golang.org/x/tools v0.12.0 ) require ( @@ -19,6 +20,6 @@ require ( github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/pkg/errors v0.9.1 // indirect + golang.org/x/mod v0.12.0 // indirect golang.org/x/sys v0.11.0 // indirect - golang.org/x/tools v0.12.0 // indirect ) diff --git a/go.sum b/go.sum index 7d0556b..b76ff81 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= diff --git a/parser/token.go b/parser/token.go new file mode 100644 index 0000000..871c672 --- /dev/null +++ b/parser/token.go @@ -0,0 +1,41 @@ +package parser + +//go:generate go run golang.org/x/tools/cmd/stringer -type=TokenType +import ( + "fmt" + + "asciigoat.org/core/lexer" +) + +// A TokenType is a type of Token +type TokenType uint + +const ( + // TokenUnknown represents a Token that hasn't been identified + TokenUnknown TokenType = iota + // TokenSectionName represents the section name between the squared brackets + TokenSectionName + // TokenSectionSubname represents a secondary name in the section represented + // between quotes after the section name. + // e.g. + // [section_name "section_subname"] + TokenSectionSubname + // TokenComment represents a comment, including the initial ';' or '#' until + // the end of the line + TokenComment + // TokenFieldKey represents a field name in a `key = value` entry + TokenFieldKey + // TokenFieldValue represents a field value in a `key = value` entry + TokenFieldValue +) + +// A Token is an element from the document +type Token struct { + Type TokenType + Position lexer.Position + Value string +} + +func (t Token) String() string { + return fmt.Sprintf("%s:%v:%v: %q", t.Type, t.Position.Line, t.Position.Column, t.Value) +} diff --git a/parser/tokentype_string.go b/parser/tokentype_string.go new file mode 100644 index 0000000..3d95f8e --- /dev/null +++ b/parser/tokentype_string.go @@ -0,0 +1,28 @@ +// Code generated by "stringer -type=TokenType"; DO NOT EDIT. + +package parser + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[TokenUnknown-0] + _ = x[TokenSectionName-1] + _ = x[TokenSectionSubname-2] + _ = x[TokenComment-3] + _ = x[TokenFieldKey-4] + _ = x[TokenFieldValue-5] +} + +const _TokenType_name = "TokenUnknownTokenSectionNameTokenSectionSubnameTokenCommentTokenFieldKeyTokenFieldValue" + +var _TokenType_index = [...]uint8{0, 12, 28, 47, 59, 72, 87} + +func (i TokenType) String() string { + if i >= TokenType(len(_TokenType_index)-1) { + return "TokenType(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _TokenType_name[_TokenType_index[i]:_TokenType_index[i+1]] +} diff --git a/tools/tools.go b/tools/tools.go index a3379f0..a4d1df9 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -4,4 +4,5 @@ package tools import ( _ "github.com/mgechev/revive" + _ "golang.org/x/tools/cmd/stringer" )