3 Commits

3 changed files with 36 additions and 9 deletions

View File

@@ -24,9 +24,10 @@ const CONCAT rune = '~'
type ReFlag int
const (
RE_NO_FLAGS ReFlag = iota
RE_CASE_INSENSITIVE
RE_MULTILINE
RE_NO_FLAGS ReFlag = iota
RE_CASE_INSENSITIVE // Case insensitive matching
RE_MULTILINE // '^' and '$' assert at start and end of _line_, rather than start and end of input string
RE_SINGLE_LINE // Dot metacharacter matches newline characters.
)
func isOperator(c rune) bool {
@@ -78,6 +79,8 @@ func getPOSIXClass(str []rune) (bool, string) {
var caseInsensitive bool
// Stores whether the multiline flag has been enabled.
// In multi-line mode, '^' and '$' assert position at the start and
// end of a _line_ rather than the entire input.
var multilineMode bool
/*
@@ -93,10 +96,10 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
caseInsensitive = false
multilineMode = false
// In Multiline mode, the newline character is considered a
// 'dot' character ie. the dot metacharacter matches a newline as well.
if slices.Contains(flags, RE_MULTILINE) {
multilineMode = true
}
if slices.Contains(flags, RE_SINGLE_LINE) {
notDotChars = []rune{}
} else {
notDotChars = []rune{'\n'}
@@ -965,6 +968,12 @@ func thompson(re []postfixNode) (Reg, error) {
})...)
}
s.content = rune2Contents(nodeContents)
if len(node.except) > 0 {
s.allChars = true
s.except = slices.Concat(Map(node.except, func(n postfixNode) []rune {
return n.contents
})...)
}
return &s
})
// Reduce the list of states down to a single state by alternating them

View File

@@ -29,7 +29,7 @@ func main() {
// These flags have to be passed to the Compile function
if *multiLineFlag {
flagsToCompile = append(flagsToCompile, RE_MULTILINE)
flagsToCompile = append(flagsToCompile, RE_MULTILINE, RE_SINGLE_LINE)
}
if *caseInsensitiveFlag {
flagsToCompile = append(flagsToCompile, RE_CASE_INSENSITIVE)

View File

@@ -196,9 +196,9 @@ var reTests = []struct {
{`a.*b`, nil, "acc\nccb", []Group{}},
{`a.{4,5}b`, nil, "acc\nccb", []Group{}},
{`a.b`, nil, "a\rb", []Group{{0, 3}}},
{`a.b`, []ReFlag{RE_MULTILINE}, "a\nb", []Group{{0, 3}}},
{`a.*b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
{`a.{4,5}b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
{`a.b`, []ReFlag{RE_SINGLE_LINE}, "a\nb", []Group{{0, 3}}},
{`a.*b`, []ReFlag{RE_SINGLE_LINE}, "acc\nccb", []Group{{0, 7}}},
{`a.{4,5}b`, []ReFlag{RE_SINGLE_LINE}, "acc\nccb", []Group{{0, 7}}},
{`)`, nil, ``, nil},
{`^$`, nil, ``, []Group{{0, 0}}},
@@ -446,6 +446,21 @@ var reTests = []struct {
{`$`, nil, "jkl\n123abc\nxyz", []Group{{14, 14}}},
{`$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{3, 3}, {10, 10}, {14, 14}}},
{`a.b`, nil, "a\nb", []Group{}},
{`a.b`, []ReFlag{RE_SINGLE_LINE}, "a\nb", []Group{{0, 3}}},
{`\w+`, nil, `--ab_cd0123--`, []Group{{2, 11}}},
{`[\w]+`, nil, `--ab_cd0123--`, []Group{{2, 11}}},
{`\D+`, nil, `1234abc5678`, []Group{{4, 7}}},
{`[\D]+`, nil, `1234abc5678`, []Group{{4, 7}}},
{`[\D5]+`, nil, `1234abc5678`, []Group{{4, 8}}},
{`[\da-fA-F]+`, nil, `123abc`, []Group{{0, 6}}},
{`\xff`, nil, "\u00ff", []Group{{0, 1}}},
{`\xFF`, nil, "\u00ff", []Group{{0, 1}}},
{`\x00ff`, nil, "\u00ff", []Group{}},
{`\x{0000ff}`, nil, "\u00ff", []Group{{0, 1}}},
{`\x{0000FF}`, nil, "\u00ff", []Group{{0, 1}}},
// Todo - add numeric range tests
}
@@ -588,6 +603,9 @@ var groupTests = []struct {
{`(?<!-):(.*)(?<!-):`, nil, `a:bc-:de:f`, []Match{[]Group{{1, 9}, {2, 8}}}},
{`(?<!\\):(.*)(?<!\\):`, nil, `a:bc\:de:f`, []Match{[]Group{{1, 9}, {2, 8}}}},
{`(?<!\?)'(.*)(?<!\?)'`, nil, `a'bc?'de'f`, []Match{[]Group{{1, 9}, {2, 8}}}},
{`([\s]*)([\S]*)([\s]*)`, nil, ` testing!1972`, []Match{[]Group{{0, 13}, {0, 1}, {1, 13}, {13, 13}}, []Group{{13, 13}, {13, 13}, {13, 13}, {13, 13}}}},
{`(\s*)(\S*)(\s*)`, nil, ` testing!1972`, []Match{[]Group{{0, 13}, {0, 1}, {1, 13}, {13, 13}}, []Group{{13, 13}, {13, 13}, {13, 13}, {13, 13}}}},
}
func TestFindAllMatches(t *testing.T) {