/
token_generator.go
80 lines (63 loc) · 1.5 KB
/
token_generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package ghokin
import (
"errors"
gherkin "github.com/cucumber/gherkin/go/v28"
)
type tokenGenerator struct {
section *section
}
func (t *tokenGenerator) Build(tok *gherkin.Token) (bool, error) {
if tok == nil {
return false, errors.New("token is not defined")
}
if tok.IsEOF() {
return true, nil
}
switch true {
case t.section == nil:
t.section = §ion{kind: tok.Type, values: []*gherkin.Token{}}
case tok.Type != t.section.kind:
t.section.nex = §ion{kind: tok.Type, values: []*gherkin.Token{}, prev: t.section}
t.section = t.section.nex
}
t.section.values = append(t.section.values, tok)
return true, nil
}
func (t *tokenGenerator) StartRule(r gherkin.RuleType) (bool, error) {
return true, nil
}
func (t *tokenGenerator) EndRule(r gherkin.RuleType) (bool, error) {
return true, nil
}
func (t *tokenGenerator) Reset() {
}
type section struct {
kind gherkin.TokenType
values []*gherkin.Token
prev *section
nex *section
}
func (s *section) isExcluded(kind gherkin.TokenType, excluded []gherkin.TokenType) bool {
for _, e := range excluded {
if kind == e {
return true
}
}
return false
}
func (s *section) previous(excluded []gherkin.TokenType) *section {
for sec := s.prev; sec != nil; sec = sec.prev {
if !s.isExcluded(sec.kind, excluded) {
return sec
}
}
return nil
}
func (s *section) next(excluded []gherkin.TokenType) *section {
for sec := s.nex; sec != nil; sec = sec.nex {
if !s.isExcluded(sec.kind, excluded) {
return sec
}
}
return nil
}