aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-07-22 16:22:03 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-07-22 16:22:03 +0900
commita30fe0c6abd9ffbaff20af3da00eeea50d407f42 (patch)
tree2a1b03a4736f20c98f0fb7563b2b08b2dafe98ef
parentSupport passive mode transition (diff)
downloadtre-a30fe0c6abd9ffbaff20af3da00eeea50d407f42.tar.gz
tre-a30fe0c6abd9ffbaff20af3da00eeea50d407f42.tar.xz
Add spec.EscapePattern function
-rw-r--r--driver/lexer_test.go28
-rw-r--r--spec/util.go21
2 files changed, 49 insertions, 0 deletions
diff --git a/driver/lexer_test.go b/driver/lexer_test.go
index 33edbc0..4dfed99 100644
--- a/driver/lexer_test.go
+++ b/driver/lexer_test.go
@@ -665,6 +665,34 @@ func TestLexer_Next(t *testing.T) {
return nil
},
},
+ {
+ lspec: &spec.LexSpec{
+ Entries: []*spec.LexEntry{
+ newLexEntryDefaultNOP("dot", spec.EscapePattern(`.`)),
+ newLexEntryDefaultNOP("star", spec.EscapePattern(`*`)),
+ newLexEntryDefaultNOP("plus", spec.EscapePattern(`+`)),
+ newLexEntryDefaultNOP("question", spec.EscapePattern(`?`)),
+ newLexEntryDefaultNOP("vbar", spec.EscapePattern(`|`)),
+ newLexEntryDefaultNOP("lparen", spec.EscapePattern(`(`)),
+ newLexEntryDefaultNOP("rparen", spec.EscapePattern(`)`)),
+ newLexEntryDefaultNOP("lbrace", spec.EscapePattern(`[`)),
+ newLexEntryDefaultNOP("backslash", spec.EscapePattern(`\`)),
+ },
+ },
+ src: `.*+?|()[\`,
+ tokens: []*Token{
+ newTokenDefault(1, "dot", newByteSequence([]byte(`.`))),
+ newTokenDefault(2, "star", newByteSequence([]byte(`*`))),
+ newTokenDefault(3, "plus", newByteSequence([]byte(`+`))),
+ newTokenDefault(4, "question", newByteSequence([]byte(`?`))),
+ newTokenDefault(5, "vbar", newByteSequence([]byte(`|`))),
+ newTokenDefault(6, "lparen", newByteSequence([]byte(`(`))),
+ newTokenDefault(7, "rparen", newByteSequence([]byte(`)`))),
+ newTokenDefault(8, "lbrace", newByteSequence([]byte(`[`))),
+ newTokenDefault(9, "backslash", newByteSequence([]byte(`\`))),
+ newEOFTokenDefault(),
+ },
+ },
}
for i, tt := range test {
for compLv := compiler.CompressionLevelMin; compLv <= compiler.CompressionLevelMax; compLv++ {
diff --git a/spec/util.go b/spec/util.go
new file mode 100644
index 0000000..56e9121
--- /dev/null
+++ b/spec/util.go
@@ -0,0 +1,21 @@
+package spec
+
+import "strings"
+
+var rep = strings.NewReplacer(
+ `.`, `\.`,
+ `*`, `\*`,
+ `+`, `\+`,
+ `?`, `\?`,
+ `|`, `\|`,
+ `(`, `\(`,
+ `)`, `\)`,
+ `[`, `\[`,
+ `\`, `\\`,
+)
+
+// EscapePattern escapes the special characters.
+// For example, EscapePattern(`+`) returns `\+`.
+func EscapePattern(s string) string {
+ return rep.Replace(s)
+}