Skip to content

Commit a66b19f

Browse files
committed
replace GetTokenKindDesc with String() method
1 parent dd57171 commit a66b19f

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

language/lexer/lexer.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,6 @@ const (
3636
AMP
3737
)
3838

39-
// NAME -> keyword relationship
40-
const (
41-
FRAGMENT = "fragment"
42-
QUERY = "query"
43-
MUTATION = "mutation"
44-
SUBSCRIPTION = "subscription"
45-
SCHEMA = "schema"
46-
SCALAR = "scalar"
47-
TYPE = "type"
48-
INTERFACE = "interface"
49-
UNION = "union"
50-
ENUM = "enum"
51-
INPUT = "input"
52-
EXTEND = "extend"
53-
DIRECTIVE = "directive"
54-
)
55-
5639
var tokenDescription = map[TokenKind]string{
5740
EOF: "EOF",
5841
BANG: "!",
@@ -76,6 +59,27 @@ var tokenDescription = map[TokenKind]string{
7659
AMP: "&",
7760
}
7861

62+
func (kind TokenKind) String() string {
63+
return tokenDescription[kind]
64+
}
65+
66+
// NAME -> keyword relationship
67+
const (
68+
FRAGMENT = "fragment"
69+
QUERY = "query"
70+
MUTATION = "mutation"
71+
SUBSCRIPTION = "subscription"
72+
SCHEMA = "schema"
73+
SCALAR = "scalar"
74+
TYPE = "type"
75+
INTERFACE = "interface"
76+
UNION = "union"
77+
ENUM = "enum"
78+
INPUT = "input"
79+
EXTEND = "extend"
80+
DIRECTIVE = "directive"
81+
)
82+
7983
// Token is a representation of a lexed Token. Value only appears for non-punctuation
8084
// tokens: NAME, INT, FLOAT, and STRING.
8185
type Token struct {
@@ -646,11 +650,7 @@ func positionAfterWhitespace(body []byte, startPosition int) (position int, rune
646650

647651
func GetTokenDesc(token Token) string {
648652
if token.Value == "" {
649-
return GetTokenKindDesc(token.Kind)
653+
return token.Kind.String()
650654
}
651-
return fmt.Sprintf("%s \"%s\"", GetTokenKindDesc(token.Kind), token.Value)
652-
}
653-
654-
func GetTokenKindDesc(kind TokenKind) string {
655-
return tokenDescription[kind]
655+
return fmt.Sprintf("%s \"%s\"", token.Kind.String(), token.Value)
656656
}

language/parser/parser.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func init() {
2020
tokenDefinitionFn = make(map[string]parseDefinitionFn)
2121
{
2222
// for sign
23-
tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.BRACE_L)] = parseOperationDefinition
24-
tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.STRING)] = parseTypeSystemDefinition
25-
tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.BLOCK_STRING)] = parseTypeSystemDefinition
26-
tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.NAME)] = parseTypeSystemDefinition
23+
tokenDefinitionFn[lexer.BRACE_L.String()] = parseOperationDefinition
24+
tokenDefinitionFn[lexer.STRING.String()] = parseTypeSystemDefinition
25+
tokenDefinitionFn[lexer.BLOCK_STRING.String()] = parseTypeSystemDefinition
26+
tokenDefinitionFn[lexer.NAME.String()] = parseTypeSystemDefinition
2727
// for NAME
2828
tokenDefinitionFn[lexer.FRAGMENT] = parseFragmentDefinition
2929
tokenDefinitionFn[lexer.QUERY] = parseOperationDefinition
@@ -144,15 +144,9 @@ func parseDocument(parser *Parser) (*ast.Document, error) {
144144
} else if skp {
145145
break
146146
}
147-
switch parser.Token.Kind {
148-
case lexer.BRACE_L:
149-
item = tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.BRACE_L)]
150-
case lexer.NAME:
151-
item = tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.NAME)]
152-
case lexer.STRING:
153-
item = tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.STRING)]
154-
case lexer.BLOCK_STRING:
155-
item = tokenDefinitionFn[lexer.GetTokenKindDesc(lexer.BLOCK_STRING)]
147+
switch kind := parser.Token.Kind; kind {
148+
case lexer.BRACE_L, lexer.NAME, lexer.STRING, lexer.BLOCK_STRING:
149+
item = tokenDefinitionFn[kind.String()]
156150
default:
157151
return nil, unexpected(parser, lexer.Token{})
158152
}
@@ -1541,7 +1535,7 @@ func expect(parser *Parser, kind lexer.TokenKind) (lexer.Token, error) {
15411535
if token.Kind == kind {
15421536
return token, advance(parser)
15431537
}
1544-
descp := fmt.Sprintf("Expected %s, found %s", lexer.GetTokenKindDesc(kind), lexer.GetTokenDesc(token))
1538+
descp := fmt.Sprintf("Expected %s, found %s", kind, lexer.GetTokenDesc(token))
15451539
return token, gqlerrors.NewSyntaxError(parser.Source, token.Start, descp)
15461540
}
15471541

@@ -1568,10 +1562,7 @@ func unexpected(parser *Parser, atToken lexer.Token) error {
15681562
}
15691563

15701564
func unexpectedEmpty(parser *Parser, beginLoc int, openKind, closeKind lexer.TokenKind) error {
1571-
description := fmt.Sprintf("Unexpected empty IN %s%s",
1572-
lexer.GetTokenKindDesc(openKind),
1573-
lexer.GetTokenKindDesc(closeKind),
1574-
)
1565+
description := fmt.Sprintf("Unexpected empty IN %s%s", openKind, closeKind)
15751566
return gqlerrors.NewSyntaxError(parser.Source, beginLoc, description)
15761567
}
15771568

0 commit comments

Comments
 (0)