syntax - The Go Programming Language

Golang

Package syntax

import "regexp/syntax"
Overview
Index

Overview ?

Overview ?

Package syntax parses regular expressions into parse trees and compiles parse trees into programs. Most clients of regular expressions will use the facilities of package regexp (such as Compile and Match) instead of this package.

Index

func IsWordChar(r rune) bool
type EmptyOp
    func EmptyOpContext(r1, r2 rune) EmptyOp
type Error
    func (e *Error) Error() string
type ErrorCode
    func (e ErrorCode) String() string
type Flags
type Inst
    func (i *Inst) MatchEmptyWidth(before rune, after rune) bool
    func (i *Inst) MatchRune(r rune) bool
    func (i *Inst) String() string
type InstOp
type Op
type Prog
    func Compile(re *Regexp) (*Prog, error)
    func (p *Prog) Prefix() (prefix string, complete bool)
    func (p *Prog) StartCond() EmptyOp
    func (p *Prog) String() string
type Regexp
    func Parse(s string, flags Flags) (*Regexp, error)
    func (re *Regexp) CapNames() []string
    func (x *Regexp) Equal(y *Regexp) bool
    func (re *Regexp) MaxCap() int
    func (re *Regexp) Simplify() *Regexp
    func (re *Regexp) String() string

Package files

compile.go parse.go perl_groups.go prog.go regexp.go simplify.go

func IsWordChar

func IsWordChar(r rune) bool

IsWordChar reports whether r is consider a “word character” during the evaluation of the \b and \B zero-width assertions. These assertions are ASCII-only: the word characters are [A-Za-z0-9_].

type EmptyOp

type EmptyOp uint8

An EmptyOp specifies a kind or mixture of zero-width assertions.

const (
    EmptyBeginLine EmptyOp = 1 << iota
    EmptyEndLine
    EmptyBeginText
    EmptyEndText
    EmptyWordBoundary
    EmptyNoWordBoundary
)

func EmptyOpContext

func EmptyOpContext(r1, r2 rune) EmptyOp

EmptyOpContext returns the zero-width assertions satisfied at the position between the runes r1 and r2. Passing r1 == -1 indicates that the position is at the beginning of the text. Passing r2 == -1 indicates that the position is at the end of the text.

type Error

type Error struct {
    Code ErrorCode
    Expr string
}

An Error describes a failure to parse a regular expression and gives the offending expression.

func (*Error) Error

func (e *Error) Error() string

type ErrorCode

type ErrorCode string

An ErrorCode describes a failure to parse a regular expression.

const (
    // Unexpected error
    ErrInternalError ErrorCode = "regexp/syntax: internal error"

    // Parse errors
    ErrInvalidCharClass      ErrorCode = "invalid character class"
    ErrInvalidCharRange      ErrorCode = "invalid character class range"
    ErrInvalidEscape         ErrorCode = "invalid escape sequence"
    ErrInvalidNamedCapture   ErrorCode = "invalid named capture"
    ErrInvalidPerlOp         ErrorCode = "invalid or unsupported Perl syntax"
    ErrInvalidRepeatOp       ErrorCode = "invalid nested repetition operator"
    ErrInvalidRepeatSize     ErrorCode = "invalid repeat count"
    ErrInvalidUTF8           ErrorCode = "invalid UTF-8"
    ErrMissingBracket        ErrorCode = "missing closing ]"
    ErrMissingParen          ErrorCode = "missing closing )"
    ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator"
    ErrTrailingBackslash     ErrorCode = "trailing backslash at end of expression"
)

func (ErrorCode) String

func (e ErrorCode) String() string

type Flags

type Flags uint16

Flags control the behavior of the parser and record information about regexp context.

const (
    FoldCase      Flags = 1 << iota // case-insensitive match
    Literal                         // treat pattern as literal string
    ClassNL                         // allow character classes like [^a-z] and [[:space:]] to match newline
    DotNL                           // allow . to match newline
    OneLine                         // treat ^ and $ as only matching at beginning and end of text
    NonGreedy                       // make repetition operators default to non-greedy
    PerlX                           // allow Perl extensions
    UnicodeGroups                   // allow \p{Han}, \P{Han} for Unicode group and negation
    WasDollar                       // regexp OpEndText was $, not \z
    Simple                          // regexp contains no counted repetition

    MatchNL = ClassNL | DotNL

    Perl        = ClassNL | OneLine | PerlX | UnicodeGroups // as close to Perl as possible
    POSIX Flags = 0                                         // POSIX syntax
)

type Inst

type Inst struct {
    Op   InstOp
    Out  uint32 // all but InstMatch, InstFail
    Arg  uint32 // InstAlt, InstAltMatch, InstCapture, InstEmptyWidth
    Rune []rune
}

An Inst is a single instruction in a regular expression program.

func (*Inst) MatchEmptyWidth

func (i *Inst) MatchEmptyWidth(before rune, after rune) bool

MatchEmptyWidth returns true if the instruction matches an empty string between the runes before and after. It should only be called when i.Op == InstEmptyWidth.

func (*Inst) MatchRune

func (i *Inst) MatchRune(r rune) bool

MatchRune returns true if the instruction matches (and consumes) r. It should only be called when i.Op == InstRune.

func (*Inst) String

func (i *Inst) String() string

type InstOp

type InstOp uint8

An InstOp is an instruction opcode.

const (
    InstAlt InstOp = iota
    InstAltMatch
    InstCapture
    InstEmptyWidth
    InstMatch
    InstFail
    InstNop
    InstRune
    InstRune1
    InstRuneAny
    InstRuneAnyNotNL
)

type Op

type Op uint8

An Op is a single regular expression operator.

const (
    OpNoMatch        Op = 1 + iota // matches no strings
    OpEmptyMatch                   // matches empty string
    OpLiteral                      // matches Runes sequence
    OpCharClass                    // matches Runes interpreted as range pair list
    OpAnyCharNotNL                 // matches any character
    OpAnyChar                      // matches any character
    OpBeginLine                    // matches empty string at beginning of line
    OpEndLine                      // matches empty string at end of line
    OpBeginText                    // matches empty string at beginning of text
    OpEndText                      // matches empty string at end of text
    OpWordBoundary                 // matches word boundary `\b`
    OpNoWordBoundary               // matches word non-boundary `\B`
    OpCapture                      // capturing subexpression with index Cap, optional name Name
    OpStar                         // matches Sub[0] zero or more times
    OpPlus                         // matches Sub[0] one or more times
    OpQuest                        // matches Sub[0] zero or one times
    OpRepeat                       // matches Sub[0] at least Min times, at most Max (Max == -1 is no limit)
    OpConcat                       // matches concatenation of Subs
    OpAlternate                    // matches alternation of Subs
)

type Prog

type Prog struct {
    Inst   []Inst
    Start  int // index of start instruction
    NumCap int // number of InstCapture insts in re
}

A Prog is a compiled regular expression program.

func Compile

func Compile(re *Regexp) (*Prog, error)

Compile compiles the regexp into a program to be executed. The regexp should have been simplified already (returned from re.Simplify).

func (*Prog) Prefix

func (p *Prog) Prefix() (prefix string, complete bool)

Prefix returns a literal string that all matches for the regexp must start with. Complete is true if the prefix is the entire match.

func (*Prog) StartCond

func (p *Prog) StartCond() EmptyOp

StartCond returns the leading empty-width conditions that must be true in any match. It returns ^EmptyOp(0) if no matches are possible.

func (*Prog) String

func (p *Prog) String() string

type Regexp

type Regexp struct {
    Op       Op // operator
    Flags    Flags
    Sub      []*Regexp  // subexpressions, if any
    Sub0     [1]*Regexp // storage for short Sub
    Rune     []rune     // matched runes, for OpLiteral, OpCharClass
    Rune0    [2]rune    // storage for short Rune
    Min, Max int        // min, max for OpRepeat
    Cap      int        // capturing index, for OpCapture
    Name     string     // capturing name, for OpCapture
}

A Regexp is a node in a regular expression syntax tree.

func Parse

func Parse(s string, flags Flags) (*Regexp, error)

Parse parses a regular expression string s, controlled by the specified Flags, and returns a regular expression parse tree. The syntax is described in the top-level comment for package regexp.

func (*Regexp) CapNames

func (re *Regexp) CapNames() []string

CapNames walks the regexp to find the names of capturing groups.

func (*Regexp) Equal

func (x *Regexp) Equal(y *Regexp) bool

Equal returns true if x and y have identical structure.

func (*Regexp) MaxCap

func (re *Regexp) MaxCap() int

MaxCap walks the regexp to find the maximum capture index.

func (*Regexp) Simplify

func (re *Regexp) Simplify() *Regexp

Simplify returns a regexp equivalent to re but without counted repetitions and with various other simplifications, such as rewriting /(?:a+)+/ to /a+/. The resulting regexp will execute correctly but its string representation will not produce the same parse tree, because capturing parentheses may have been duplicated or removed. For example, the simplified form for /(x){1,2}/ is /(x)(x)?/ but both parentheses capture as $1. The returned regexp may share structure with or be the original.

func (*Regexp) String

func (re *Regexp) String() string