Skip to content

Commit 2589e2f

Browse files
committed
Initialize scanner in Lexer constructor
1 parent 850a565 commit 2589e2f

File tree

5 files changed

+17
-41
lines changed

5 files changed

+17
-41
lines changed

internal/common/lexer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"fmt"
5+
"strings"
56
"text/scanner"
67

78
"github.com/graph-gophers/graphql-go/errors"
@@ -20,7 +21,12 @@ type Ident struct {
2021
Loc errors.Location
2122
}
2223

23-
func NewLexer(sc *scanner.Scanner) *Lexer {
24+
func NewLexer(s string) *Lexer {
25+
sc := &scanner.Scanner{
26+
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
27+
}
28+
sc.Init(strings.NewReader(s))
29+
2430
return &Lexer{sc: sc}
2531
}
2632

internal/common/lexer_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package common_test
22

33
import (
4-
"strings"
54
"testing"
6-
"text/scanner"
75

86
"github.com/graph-gophers/graphql-go/internal/common"
97
)
@@ -27,21 +25,14 @@ world: String!
2725
}}
2826

2927
func TestConsume(t *testing.T) {
30-
setup := func(t *testing.T, def string) *common.Lexer {
31-
t.Helper()
32-
33-
sc := &scanner.Scanner{
34-
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
35-
}
36-
sc.Init(strings.NewReader(def))
37-
return common.NewLexer(sc)
38-
}
39-
4028
for _, test := range consumeTests {
4129
t.Run(test.description, func(t *testing.T) {
42-
lex := setup(t, test.definition)
30+
lex := common.NewLexer(test.definition)
4331

44-
lex.Consume()
32+
err := lex.CatchSyntaxError(lex.Consume)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
4536

4637
if test.expected != lex.DescComment() {
4738
t.Errorf("wanted: %q\ngot: %q", test.expected, lex.DescComment())

internal/query/query.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package query
22

33
import (
44
"fmt"
5-
"strings"
65
"text/scanner"
76

87
"github.com/graph-gophers/graphql-go/errors"
@@ -95,16 +94,10 @@ func (InlineFragment) isSelection() {}
9594
func (FragmentSpread) isSelection() {}
9695

9796
func Parse(queryString string) (*Document, *errors.QueryError) {
98-
sc := &scanner.Scanner{
99-
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
100-
}
101-
sc.Init(strings.NewReader(queryString))
97+
l := common.NewLexer(queryString)
10298

103-
l := common.NewLexer(sc)
10499
var doc *Document
105-
err := l.CatchSyntaxError(func() {
106-
doc = parseDocument(l)
107-
})
100+
err := l.CatchSyntaxError(func() { doc = parseDocument(l) })
108101
if err != nil {
109102
return nil, err
110103
}

internal/schema/schema.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package schema
22

33
import (
44
"fmt"
5-
"strings"
65
"text/scanner"
76

87
"github.com/graph-gophers/graphql-go/errors"
@@ -248,15 +247,9 @@ func New() *Schema {
248247

249248
// Parse the schema string.
250249
func (s *Schema) Parse(schemaString string) error {
251-
sc := &scanner.Scanner{
252-
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
253-
}
254-
sc.Init(strings.NewReader(schemaString))
250+
l := common.NewLexer(schemaString)
255251

256-
l := common.NewLexer(sc)
257-
err := l.CatchSyntaxError(func() {
258-
parseSchema(s, l)
259-
})
252+
err := l.CatchSyntaxError(func() { parseSchema(s, l) })
260253
if err != nil {
261254
return err
262255
}

internal/schema/schema_internal_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package schema
22

33
import (
4-
"strings"
54
"testing"
6-
"text/scanner"
75

86
"github.com/graph-gophers/graphql-go/errors"
97
"github.com/graph-gophers/graphql-go/internal/common"
@@ -160,12 +158,7 @@ func compareObjects(t *testing.T, expected, actual *Object) {
160158
func setup(t *testing.T, def string) *common.Lexer {
161159
t.Helper()
162160

163-
sc := &scanner.Scanner{
164-
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
165-
}
166-
sc.Init(strings.NewReader(def))
167-
168-
lex := common.NewLexer(sc)
161+
lex := common.NewLexer(def)
169162
lex.Consume()
170163

171164
return lex

0 commit comments

Comments
 (0)