File: simple.go

package info (click to toggle)
golang-github-alecthomas-participle-v2 2.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: javascript: 1,164; sh: 41; makefile: 7
file content (29 lines) | stat: -rw-r--r-- 747 bytes parent folder | download
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
package lexer

// SimpleRule is a named regular expression.
type SimpleRule struct {
	Name    string
	Pattern string
}

// MustSimple creates a new Stateful lexer with only a single root state.
// The rules are tried in order.
//
// It panics if there is an error.
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.
// The rules are tried in order.
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})
}