rules

package
v0.0.0-...-cbd58c3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rules implements a flex-like rules for a table driven lexer.

Index

Constants

This section is empty.

Variables

View Source
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 Action

type Action func(ScanState) (bool, error)

Action is a callback intended to be invoked when the rule conditions match.

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 New

func New(opts ...Option) *Rules

New returns a new Rules table, after applying the provided options.

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

func (r *Rules) AddRegexp(conds []StartCondition, re *regexp.Regexp, action Action) error

AddRegexp adds a rule matching the regular expression and start conditions.

func (*Rules) Match

func (r *Rules) Match(curr StartCondition, data []byte) (Action, []byte)

Match considers applicable rules and returns the action associated with the longest matching pattern, as well as the portion of the data matched by that pattern.

func (*Rules) MustAdd

func (r *Rules) MustAdd(conds []StartCondition, pat string, action Action)

Add adds a rule matching the pattern 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

func NewScanner(rules *Rules, r io.Reader) *Scanner

NewScanner returns a new action scanner, applying the provided rules to text obtained from the io.Reader.

func (*Scanner) Action

func (s *Scanner) Action() Action

Action returns the most recently selected action.

func (*Scanner) Begin

func (s *Scanner) Begin(cond StartCondition)

Begin transitions the scanner to the indicated start condition.

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the text matched by the pattern associated with selected action.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the underlying error, if any.

func (*Scanner) Pos

func (s *Scanner) Pos() lexer.Position

Pos returns the current position of the scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

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

func (s *Scanner) SetPosition(pos lexer.Position)

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.

)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL