Implement PCRE Matching (prefer left-branch) #2
@@ -12,18 +12,24 @@ var notDotChars []rune
|
|||||||
|
|
||||||
// A Reg represents the result of compiling a regular expression. It contains
|
// A Reg represents the result of compiling a regular expression. It contains
|
||||||
// the startState of the NFA representation of the regex, and the number of capturing
|
// the startState of the NFA representation of the regex, and the number of capturing
|
||||||
// groups in the regex.
|
// groups in the regex. It also contains the expression string.
|
||||||
type Reg struct {
|
type Reg struct {
|
||||||
start *nfaState
|
start *nfaState
|
||||||
numGroups int
|
numGroups int
|
||||||
|
str string
|
||||||
}
|
}
|
||||||
|
|
||||||
// numSubexp eturns the number of sub-expressions in the given [Reg]. This is equivalent
|
// NumSubexp returns the number of sub-expressions in the given [Reg]. This is equivalent
|
||||||
// to the number of capturing groups.
|
// to the number of capturing groups.
|
||||||
func (r Reg) NumSubexp() int {
|
func (r Reg) NumSubexp() int {
|
||||||
return r.numGroups
|
return r.numGroups
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the string used to compile the expression.
|
||||||
|
func (r Reg) String() string {
|
||||||
|
return r.str
|
||||||
|
}
|
||||||
|
|
||||||
const concatRune rune = 0xF0001
|
const concatRune rune = 0xF0001
|
||||||
|
|
||||||
// Flags for shuntingYard - control its behavior
|
// Flags for shuntingYard - control its behavior
|
||||||
@@ -1128,7 +1134,8 @@ func thompson(re []postfixNode) (Reg, error) {
|
|||||||
|
|
||||||
concatenate(nfa[0], &lastState)
|
concatenate(nfa[0], &lastState)
|
||||||
|
|
||||||
return Reg{nfa[0], numGroups}, nil
|
// The string is empty here, because we add it in Compile()
|
||||||
|
return Reg{nfa[0], numGroups, ""}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1146,6 +1153,7 @@ func Compile(re string, flags ...ReFlag) (Reg, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Reg{}, fmt.Errorf("error compiling regex: %w", err)
|
return Reg{}, fmt.Errorf("error compiling regex: %w", err)
|
||||||
}
|
}
|
||||||
|
reg.str = re
|
||||||
return reg, nil
|
return reg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user