-
Notifications
You must be signed in to change notification settings - Fork 23
/
regexp_format.go
152 lines (138 loc) · 4.03 KB
/
regexp_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright 2014 Zachary Klippenstein
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package regen
import (
"bytes"
"fmt"
"io"
"regexp/syntax"
)
// inspectRegexpToString returns a string describing a regular expression.
func inspectRegexpToString(r *syntax.Regexp) string {
var buffer bytes.Buffer
inspectRegexpToWriter(&buffer, r)
return buffer.String()
}
// inspectPatternsToString returns a string describing one or more regular expressions.
func inspectPatternsToString(simplify bool, patterns ...string) string {
var buffer bytes.Buffer
for _, pattern := range patterns {
inspectPatternsToWriter(simplify, &buffer, pattern)
}
return buffer.String()
}
func inspectPatternsToWriter(simplify bool, w io.Writer, patterns ...string) {
for _, pattern := range patterns {
inspectRegexpToWriter(w, parseOrPanic(simplify, pattern))
}
}
func inspectRegexpToWriter(w io.Writer, r ...*syntax.Regexp) {
for _, regexp := range r {
inspectWithIndent(regexp, "", w)
}
}
func inspectWithIndent(r *syntax.Regexp, indent string, w io.Writer) {
fmt.Fprintf(w, "%s{\n", indent)
fmt.Fprintf(w, "%s Op: %s\n", indent, opToString(r.Op))
fmt.Fprintf(w, "%s Flags: %x\n", indent, r.Flags)
if len(r.Sub) > 0 {
fmt.Fprintf(w, "%s Sub: [\n", indent)
for _, subR := range r.Sub {
inspectWithIndent(subR, indent+" ", w)
}
fmt.Fprintf(w, "%s ]\n", indent)
} else {
fmt.Fprintf(w, "%s Sub: []\n", indent)
}
fmt.Fprintf(w, "%s Rune: %s (%s)\n", indent, runesToString(r.Rune...), runesToDecimalString(r.Rune))
fmt.Fprintf(w, "%s [Min, Max]: [%d, %d]\n", indent, r.Min, r.Max)
fmt.Fprintf(w, "%s Cap: %d\n", indent, r.Cap)
fmt.Fprintf(w, "%s Name: %s\n", indent, r.Name)
}
// ParseOrPanic parses a regular expression into an AST.
// Panics on error.
func parseOrPanic(simplify bool, pattern string) *syntax.Regexp {
regexp, err := syntax.Parse(pattern, 0)
if err != nil {
panic(err)
}
if simplify {
regexp = regexp.Simplify()
}
return regexp
}
// runesToString converts a slice of runes to the string they represent.
func runesToString(runes ...rune) string {
defer func() {
if err := recover(); err != nil {
panic(fmt.Errorf("RunesToString panicked"))
}
}()
var buffer bytes.Buffer
for _, r := range runes {
buffer.WriteRune(r)
}
return buffer.String()
}
// RunesToDecimalString converts a slice of runes to their comma-separated decimal values.
func runesToDecimalString(runes []rune) string {
var buffer bytes.Buffer
for _, r := range runes {
buffer.WriteString(fmt.Sprintf("%d, ", r))
}
return buffer.String()
}
// opToString gets the string name of a regular expression operation.
func opToString(op syntax.Op) string {
switch op {
case syntax.OpNoMatch:
return "OpNoMatch"
case syntax.OpEmptyMatch:
return "OpEmptyMatch"
case syntax.OpLiteral:
return "OpLiteral"
case syntax.OpCharClass:
return "OpCharClass"
case syntax.OpAnyCharNotNL:
return "OpAnyCharNotNL"
case syntax.OpAnyChar:
return "OpAnyChar"
case syntax.OpBeginLine:
return "OpBeginLine"
case syntax.OpEndLine:
return "OpEndLine"
case syntax.OpBeginText:
return "OpBeginText"
case syntax.OpEndText:
return "OpEndText"
case syntax.OpWordBoundary:
return "OpWordBoundary"
case syntax.OpNoWordBoundary:
return "OpNoWordBoundary"
case syntax.OpCapture:
return "OpCapture"
case syntax.OpStar:
return "OpStar"
case syntax.OpPlus:
return "OpPlus"
case syntax.OpQuest:
return "OpQuest"
case syntax.OpRepeat:
return "OpRepeat"
case syntax.OpConcat:
return "OpConcat"
case syntax.OpAlternate:
return "OpAlternate"
}
panic(fmt.Sprintf("invalid op: %d", op))
}