Skip to content

Commit

Permalink
Remove lexer options.
Browse files Browse the repository at this point in the history
The two existing options offer only marginal benefit and removing them
simplifies code generation.
  • Loading branch information
alecthomas committed Sep 27, 2022
1 parent e080216 commit a314b80
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 51 deletions.
8 changes: 4 additions & 4 deletions lexer/simple.go
Expand Up @@ -9,19 +9,19 @@ type SimpleRule struct {
// MustSimple creates a new Stateful lexer with only a single root state.
//
// It panics if there is an error.
func MustSimple(rules []SimpleRule, options ...Option) *StatefulDefinition {
def, err := NewSimple(rules, options...)
func MustSimple(rules []SimpleRule) *StatefulDefinition {
def, err := NewSimple(rules)
if err != nil {
panic(err)
}
return def
}

// NewSimple creates a new Stateful lexer with only a single root state.
func NewSimple(rules []SimpleRule, options ...Option) (*StatefulDefinition, error) {
func NewSimple(rules []SimpleRule) (*StatefulDefinition, error) {
fullRules := make([]Rule, len(rules))
for i, rule := range rules {
fullRules[i] = Rule{Name: rule.Name, Pattern: rule.Pattern}
}
return New(Rules{"Root": fullRules}, options...)
return New(Rules{"Root": fullRules})
}
35 changes: 6 additions & 29 deletions lexer/stateful.go
Expand Up @@ -17,9 +17,6 @@ var (
backrefReplace = regexp.MustCompile(`(\\+)(\d)`)
)

// Option for modifying how the Lexer works.
type Option func(d *StatefulDefinition)

// A Rule matching input and possibly changing state.
type Rule struct {
Name string `json:"name"`
Expand Down Expand Up @@ -143,21 +140,6 @@ type RulesAction interface {
applyRules(state string, rule int, rules compiledRules) error
}

// InitialState overrides the default initial state of "Root".
func InitialState(state string) Option {
return func(d *StatefulDefinition) {
d.initialState = state
}
}

// MatchLongest causes the Lexer to continue checking rules past the first match.
// If any subsequent rule has a longer match, it will be used instead.
func MatchLongest() Option {
return func(d *StatefulDefinition) {
d.matchLongest = true
}
}

// ActionPop pops to the previous state when the Rule matches.
type ActionPop struct{}

Expand Down Expand Up @@ -233,21 +215,20 @@ type StatefulDefinition struct {
symbols map[string]TokenType
// Map of key->*regexp.Regexp
backrefCache sync.Map
initialState string
matchLongest bool
}

// MustStateful creates a new stateful lexer and panics if it is incorrect.
func MustStateful(rules Rules, options ...Option) *StatefulDefinition {
def, err := New(rules, options...)
func MustStateful(rules Rules) *StatefulDefinition {
def, err := New(rules)
if err != nil {
panic(err)
}
return def
}

// New constructs a new stateful lexer from rules.
func New(rules Rules, options ...Option) (*StatefulDefinition, error) {
func New(rules Rules) (*StatefulDefinition, error) {
compiled := compiledRules{}
for key, set := range rules {
for i, rule := range set {
Expand Down Expand Up @@ -303,12 +284,8 @@ restart:
}
}
d := &StatefulDefinition{
initialState: "Root",
rules: compiled,
symbols: symbols,
}
for _, option := range options {
option(d)
rules: compiled,
symbols: symbols,
}
return d, nil
}
Expand All @@ -333,7 +310,7 @@ func (d *StatefulDefinition) LexString(filename string, s string) (Lexer, error)
return &StatefulLexer{
def: d,
data: s,
stack: []lexerState{{name: d.initialState}},
stack: []lexerState{{name: "Root"}},
pos: Position{
Filename: filename,
Line: 1,
Expand Down
19 changes: 1 addition & 18 deletions lexer/stateful_test.go
Expand Up @@ -45,7 +45,6 @@ func TestStatefulLexer(t *testing.T) {
tests := []struct {
name string
rules lexer.Rules
lngst bool
input string
tokens []string
err string
Expand Down Expand Up @@ -149,18 +148,6 @@ func TestStatefulLexer(t *testing.T) {
input: `a apple`,
tokens: []string{"a", "a", "pple"},
},
{name: "MatchLongest",
rules: lexer.Rules{
"Root": {
{"A", `a`, nil},
{"Ident", `\w+`, nil},
{"whitespace", `\s+`, nil},
},
},
lngst: true,
input: `a apple`,
tokens: []string{"a", "apple"},
},
{name: "NoMatchNoMutatorError",
rules: lexer.Rules{
"Root": {
Expand All @@ -186,11 +173,7 @@ func TestStatefulLexer(t *testing.T) {
// nolint: scopelint
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var opts []lexer.Option
if test.lngst {
opts = append(opts, lexer.MatchLongest())
}
def, err := lexer.New(test.rules, opts...)
def, err := lexer.New(test.rules)
require.NoError(t, err)
lex, err := def.Lex("", strings.NewReader(test.input))
require.NoError(t, err)
Expand Down

0 comments on commit a314b80

Please sign in to comment.