sequence

package
v0.0.0-...-5467f26 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sequence implements matching for sequences of strings.

The primary way to use this package is by first making a Pattern:

pat := NewPattern("^", "/(cat|bear)/", "...", "hello")

Then you can use `pat` to match sequences of strings like:

pat.In("cat", "says", "hello", "friend") // true
pat.In("bear", "hello")                  // true
pat.In("dog", "hello")                   // false
pat.In("extra", "cat", "hello")          // false

See NewPattern for the types of tokens supported.

You can also manually assemble a Pattern from Matchers (including the special Ellipsis and Edge Matchers in this package), but it's anticipated that this will be overly verbose for tests (where we expect this package will see the most use).

Index

Constants

This section is empty.

Variables

View Source
var (
	// Ellipsis is a special Pattern Matcher which matches any number of tokens of
	// arbitrary length.
	Ellipsis = ellipsis{}

	// Edge is a special 0-width Pattern Matcher which only matches at the
	// beginning or the end of a command.
	Edge = edge{}
)

Functions

This section is empty.

Types

type LiteralMatcher

type LiteralMatcher string

LiteralMatcher matches a sequence element with exactly this content.

func (LiteralMatcher) Matches

func (l LiteralMatcher) Matches(tok string) bool

Matches implements Matcher.

type Matcher

type Matcher interface {
	Matches(tok string) bool
}

Matcher is a single element of a Pattern and can match a single element in a sequence.

There are also two 'special' Matchers:

  • Ellipsis - Unconditionally matches zero or more elements in a sequence.
  • Edge - Matches the beginning or end of a sequence with zero-width.

func ParseRegLiteral

func ParseRegLiteral(token string) (Matcher, error)

ParseRegLiteral parses `token` as either a regex or literal matcher.

If `token` starts and ends with `/` it's contents is parsed as a RegexpMatcher, otherwise `token` is returned as a LiteralMatcher.

type Pattern

type Pattern []Matcher

Pattern is a group of Matchers which can be matched against a string sequence.

func NewPattern

func NewPattern(patternTokens ...string) (Pattern, error)

NewPattern returns a Pattern from a series of tokens.

Tokens can be:

  • "/a regex/" - A regular expression surrounded by slashes.
  • "..." - An Ellipsis which matches any number of sequence entries.
  • "^" at index 0 - Zero-width matches at the beginning of the sequence.
  • "$" at index -1 - Zero-width matches at the end of the sequence.
  • "=string" - Literally match anything after the "=". Allows escaping special strings, e.g. "=/regex/", "=...", "=^", "=$", "==something".
  • "any other string" - Literally match without escaping.

func (Pattern) In

func (p Pattern) In(seq ...string) bool

In checks this pattern against the given sequence.

If this Pattern is malformed, panics; If you created Pattern with NewPattern and it didn't return an error, the Pattern is not malformed and this method will not panic.

By default In matches the pattern sequence anywhere; You can constrain this by setting Edge at the start or end of the pattern.

Examples:

// true
NewPattern("foo").In("foo", "bar", "baz")
NewPattern("/a/").In("foo", "bar", "baz")
NewPattern("foo", "bar").In("foo", "bar", "baz")
NewPattern("/o$/", "bar", "/^b/").In("foo", "bar", "baz")
NewPattern("foo", "...", "bar").In("foo", "a", "b", "bar")
NewPattern("foo", "...", "bar").In("foo", "bar")
NewPattern("^", "bar", "baz").In("bar", "baz", "foo")

// false
NewPattern("^", "bar", "baz").In("foo", "bar", "baz")

type RegexpMatcher

type RegexpMatcher struct{ R *regexp.Regexp }

RegexpMatcher matches a sequence element with this Regexp.

func (RegexpMatcher) Matches

func (r RegexpMatcher) Matches(tok string) bool

Matches implements Matcher.

Jump to

Keyboard shortcuts

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