Compare commits
3 Commits
c700b01e80
...
bde153ce08
| Author | SHA1 | Date | |
|---|---|---|---|
| bde153ce08 | |||
| 4c3c7e21c5 | |||
| 61a128e27a |
19
compile.go
19
compile.go
@@ -24,9 +24,10 @@ const CONCAT rune = '~'
|
|||||||
type ReFlag int
|
type ReFlag int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RE_NO_FLAGS ReFlag = iota
|
RE_NO_FLAGS ReFlag = iota
|
||||||
RE_CASE_INSENSITIVE
|
RE_CASE_INSENSITIVE // Case insensitive matching
|
||||||
RE_MULTILINE
|
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 {
|
func isOperator(c rune) bool {
|
||||||
@@ -78,6 +79,8 @@ func getPOSIXClass(str []rune) (bool, string) {
|
|||||||
var caseInsensitive bool
|
var caseInsensitive bool
|
||||||
|
|
||||||
// Stores whether the multiline flag has been enabled.
|
// 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
|
var multilineMode bool
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -93,10 +96,10 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
|
|||||||
|
|
||||||
caseInsensitive = false
|
caseInsensitive = false
|
||||||
multilineMode = 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) {
|
if slices.Contains(flags, RE_MULTILINE) {
|
||||||
multilineMode = true
|
multilineMode = true
|
||||||
|
}
|
||||||
|
if slices.Contains(flags, RE_SINGLE_LINE) {
|
||||||
notDotChars = []rune{}
|
notDotChars = []rune{}
|
||||||
} else {
|
} else {
|
||||||
notDotChars = []rune{'\n'}
|
notDotChars = []rune{'\n'}
|
||||||
@@ -965,6 +968,12 @@ func thompson(re []postfixNode) (Reg, error) {
|
|||||||
})...)
|
})...)
|
||||||
}
|
}
|
||||||
s.content = rune2Contents(nodeContents)
|
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
|
return &s
|
||||||
})
|
})
|
||||||
// Reduce the list of states down to a single state by alternating them
|
// Reduce the list of states down to a single state by alternating them
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -29,7 +29,7 @@ func main() {
|
|||||||
|
|
||||||
// These flags have to be passed to the Compile function
|
// These flags have to be passed to the Compile function
|
||||||
if *multiLineFlag {
|
if *multiLineFlag {
|
||||||
flagsToCompile = append(flagsToCompile, RE_MULTILINE)
|
flagsToCompile = append(flagsToCompile, RE_MULTILINE, RE_SINGLE_LINE)
|
||||||
}
|
}
|
||||||
if *caseInsensitiveFlag {
|
if *caseInsensitiveFlag {
|
||||||
flagsToCompile = append(flagsToCompile, RE_CASE_INSENSITIVE)
|
flagsToCompile = append(flagsToCompile, RE_CASE_INSENSITIVE)
|
||||||
|
|||||||
24
re_test.go
24
re_test.go
@@ -196,9 +196,9 @@ var reTests = []struct {
|
|||||||
{`a.*b`, nil, "acc\nccb", []Group{}},
|
{`a.*b`, nil, "acc\nccb", []Group{}},
|
||||||
{`a.{4,5}b`, nil, "acc\nccb", []Group{}},
|
{`a.{4,5}b`, nil, "acc\nccb", []Group{}},
|
||||||
{`a.b`, nil, "a\rb", []Group{{0, 3}}},
|
{`a.b`, nil, "a\rb", []Group{{0, 3}}},
|
||||||
{`a.b`, []ReFlag{RE_MULTILINE}, "a\nb", []Group{{0, 3}}},
|
{`a.b`, []ReFlag{RE_SINGLE_LINE}, "a\nb", []Group{{0, 3}}},
|
||||||
{`a.*b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
|
{`a.*b`, []ReFlag{RE_SINGLE_LINE}, "acc\nccb", []Group{{0, 7}}},
|
||||||
{`a.{4,5}b`, []ReFlag{RE_MULTILINE}, "acc\nccb", []Group{{0, 7}}},
|
{`a.{4,5}b`, []ReFlag{RE_SINGLE_LINE}, "acc\nccb", []Group{{0, 7}}},
|
||||||
|
|
||||||
{`)`, nil, ``, nil},
|
{`)`, nil, ``, nil},
|
||||||
{`^$`, nil, ``, []Group{{0, 0}}},
|
{`^$`, nil, ``, []Group{{0, 0}}},
|
||||||
@@ -446,6 +446,21 @@ var reTests = []struct {
|
|||||||
{`$`, nil, "jkl\n123abc\nxyz", []Group{{14, 14}}},
|
{`$`, nil, "jkl\n123abc\nxyz", []Group{{14, 14}}},
|
||||||
{`$`, []ReFlag{RE_MULTILINE}, "jkl\n123abc\nxyz", []Group{{3, 3}, {10, 10}, {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
|
// 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}}}},
|
{`(?<!\\):(.*)(?<!\\):`, 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) {
|
func TestFindAllMatches(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user