Documentation ¶
Overview ¶
Package rules implements a flex-like rules for a table driven lexer.
Index ¶
- Variables
- func In(conds ...StartCondition) *ruleBuilder
- type Action
- type Option
- type Rules
- func (r *Rules) Add(conds []StartCondition, pat string, action Action) error
- func (r *Rules) AddRegexp(conds []StartCondition, re *regexp.Regexp, action Action) error
- func (r *Rules) Match(curr StartCondition, data []byte) (Action, []byte)
- func (r *Rules) MustAdd(conds []StartCondition, pat string, action Action)
- type ScanState
- type Scanner
- type StartCondition
Constants ¶
This section is empty.
Variables ¶
var EOFRegexp *regexp.Regexp // Regexp indicating a rule should match at EOF.
Functions ¶
func In ¶
func In(conds ...StartCondition) *ruleBuilder
In accepts a (possibly empty) list of start conditions during which to consider a rule.
Types ¶
type Option ¶
type Option func(*Rules)
Option is a callback to apply to the Rules object during construction.
func ExclusiveConditions ¶
func ExclusiveConditions(cond StartCondition, tail ...StartCondition) Option
ExclusiveConditions configures the Rules table so the provided StartConditions are considered exclusive.
func InclusiveConditions ¶
func InclusiveConditions(cond StartCondition, tail ...StartCondition) Option
InclusiveConditions configures the Rules table so the provided StartConditions are considered inclusive (the default).
type Rules ¶
type Rules struct {
// contains filtered or unexported fields
}
Rules is a collection of rules to match against an incoming text string and current StartCondtion.
func (*Rules) Add ¶
func (r *Rules) Add(conds []StartCondition, pat string, action Action) error
Add adds a rule matching the pattern and start conditions.
func (*Rules) AddRegexp ¶
AddRegexp adds a rule matching the regular expression and start conditions.
type ScanState ¶
type ScanState interface { Begin(StartCondition) // Transition the ScanState to the indicating start condition. Bytes() []byte // The currently matched bytes. Token() *lexer.Token // The lexer.Token being constructed. }
ScanState interface defines a minimal set of behaviors expected by an action callback.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner scans an underlying io.Reader, matching the text against the configured rules and retaining the appropriate action.
func NewScanner ¶
NewScanner returns a new action scanner, applying the provided rules to text obtained from the io.Reader.
func (*Scanner) Begin ¶
func (s *Scanner) Begin(cond StartCondition)
Begin transitions the scanner to the indicated start condition.
func (*Scanner) Bytes ¶
Bytes returns the text matched by the pattern associated with selected action.
func (*Scanner) Scan ¶
Scan reads text from the underlying reader, updates the current position and returns true if there is an action and corresponding bytes available.
func (*Scanner) SetPosition ¶
SetPosition sets the starting position of the scanner.
type StartCondition ¶
type StartCondition int
StartCondition indicates a particular lexer state in which a rule should apply. By default, start conditions are inclusive and will match rules belonging to an empty set of start conditions as well as those which are explicitly specified. Exclusive start conditions only match if the scanner is in the indicated state.
const ( InitialCondition StartCondition = 0 // Initial start condition for a scanner. EOFPattern = `` // String indicating a rule should match at EOF. )