forked from stathissideris/ditaa
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgridpattern.go
228 lines (201 loc) · 6.29 KB
/
gridpattern.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//WIP
package main
import (
"regexp"
)
/*
This is a TextGrid (usually 3x3) that contains the equivalent
of a 2D reqular expression (which uses custom syntax to make
things more visual, but standard syntax is also possible).
The custom syntax is:
. means anything
b means any boundary (any of - = / \ + | :)
! means not boundary (none of - = / \ + | :)
- means - or =
| means | or :
[ means not | nor :
~ means not - nor =
^ means a boundary but not - nor =
( means a boundary but not | nor :
s means a straight boundary (one of - = + | :)
S means not a straight boundary (none of - = + | :)
1 means a cell that has entry point 1
2 means a cell that has entry point 2
3 means a cell that has entry point 3
etc. up to number 8
%1 means a cell that does not have entry point 1 etc.
See below for an explanation of entry points
+, \, / and the space are literal (as is any other character)
Entry points
1 2 3
*--*--*
| |
8* *4
| |
*--*--*
7 6 5
We number the entry points for each cell as in the diagram above.
If a cell is occupied by a character, we define as entry points
the points of the above diagram that the character can touch with
the end of its lines. For example:
- has entry points 8 and 4,
| and : have entry points 2 and 6,
/ has 3 and 7,
\ has 1 and 5,
+ has 2, 6, 8 and 4
etc.
*/
var gridPatternChars = map[byte]string{
'[': "[^|:]",
'|': "[|:]",
'-': "[-=]",
'!': "[^-=\\/\\\\+|:]",
'b': "[-=\\/\\\\+|:]",
'^': "[\\/\\\\+|:]",
'(': "[-=\\/\\\\+]",
'~': ".",
'+': "\\+",
'\\': "\\\\",
's': "[-=+|:]",
'S': "[\\/\\\\]",
'*': "\\*",
//entry points
'1': "[\\\\]",
'2': "[|:+\\/\\\\]",
'3': "[\\/]",
'4': "[-=+\\/\\\\]",
'5': "[\\\\]",
'6': "[|:+\\/\\\\]",
'7': "[\\/]",
'8': "[-=+\\/\\\\]",
}
var gridPatternCharsInv = map[byte]string{
'1': "[^\\\\]",
'2': "[^|:+\\/\\\\]",
'3': "[^\\/]",
'4': "[^-=+\\/\\\\]",
'5': "[^\\\\]",
'6': "[^|:+\\/\\\\]",
'7': "[^\\/]",
'8': "[^-=+\\/\\\\]",
}
type GridPattern [3]*regexp.Regexp
type Criteria []GridPattern
func NewCriterion(rowtop, rowmid, rowbot string) Criteria {
return Criteria{NewGridPattern(rowtop, rowmid, rowbot)}
}
func NewCriteria(criteria ...Criteria) Criteria {
c := Criteria{}
for _, newc := range criteria {
c = append(c, newc...)
}
return c
}
func (c Criteria) AnyMatch(g *TextGrid) bool {
for _, p := range c {
if p.Match(g) {
return true
}
}
return false
}
func NewGridPattern(rowtop, rowmid, rowbot string) GridPattern {
return GridPattern{
mustCompileRow(rowtop),
mustCompileRow(rowmid),
mustCompileRow(rowbot),
}
}
func mustCompileRow(pattern string) *regexp.Regexp {
re := ""
for i := 0; i < len(pattern); i++ {
c := pattern[i]
if elem, ok := gridPatternChars[c]; ok {
re += elem
continue
}
if c != '%' {
re += string(c)
continue
}
// c=='%' -- "negation" of following character
i++
c = pattern[i]
if elem, ok := gridPatternCharsInv[c]; ok {
re += elem
}
}
return regexp.MustCompile(re)
}
func (p *GridPattern) Match(t *TextGrid) bool {
for i, re := range *p {
if !re.MatchString(string(t.Rows[i])) {
return false
}
}
return true
}
var (
crossCriteria = NewCriterion(".6.", "4+8", ".2.")
KCriteria = NewCriterion(".6.", "%4+8", ".2.")
inverseKCriteria = NewCriterion(".6.", "4+%8", ".2.")
TCriteria = NewCriterion(".%6.", "4+8", ".2.")
inverseTCriteria = NewCriterion(".6.", "4+8", ".%2.")
// ****** normal corners *******
normalCorner1Criteria = NewCriterion(".[.", "~+(", ".^.")
normalCorner2Criteria = NewCriterion(".[.", "(+~", ".^.")
normalCorner3Criteria = NewCriterion(".^.", "(+~", ".[.")
normalCorner4Criteria = NewCriterion(".^.", "~+(", ".[.")
// ******* round corners *******
roundCorner1Criteria = NewCriterion(".[.", "~/4", ".2.")
roundCorner2Criteria = NewCriterion(".[.", "4\\~", ".2.")
roundCorner3Criteria = NewCriterion(".6.", "4/~", ".[.")
roundCorner4Criteria = NewCriterion(".6.", "~\\8", ".[.")
// stubs
stubCriteria = NewCriteria(
NewCriterion("!^!", "!+!", ".!."),
NewCriterion("!^!", "!+!", ".-."),
NewCriterion("!!.", "(+!", "!!."),
NewCriterion("!!.", "(+|", "!!."),
NewCriterion(".!.", "!+!", "!^!"),
NewCriterion(".-.", "!+!", "!^!"),
NewCriterion(".!!", "!+(", ".!!"),
NewCriterion(".!!", "|+(", ".!!"))
// ****** ends of lines ******
verticalLinesEndCriteria = NewCriteria(
NewCriterion(".^.", ".|.", ".!."),
NewCriterion(".^.", ".|.", ".-."),
NewCriterion(".!.", ".|.", ".^."),
NewCriterion(".-.", ".|.", ".^."))
horizontalLinesEndCriteria = NewCriteria(
NewCriterion("...", "(-!", "..."),
NewCriterion("...", "(-|", "..."),
NewCriterion("...", "!-(", "..."),
NewCriterion("...", "|-(", "..."))
// ****** others *******
horizontalCrossOnLineCriteria = NewCriterion("...", "(+(", "...")
verticalCrossOnLineCriteria = NewCriterion(".^.", ".+.", ".^.")
horizontalStarOnLineCriteria = NewCriteria(
NewCriterion("...", "(*(", "..."),
NewCriterion("...", "!*(", "..."),
NewCriterion("...", "(*!", "..."))
verticalStarOnLineCriteria = NewCriteria(
NewCriterion(".^.", ".*.", ".^."),
NewCriterion(".!.", ".*.", ".^."),
NewCriterion(".^.", ".*.", ".!."))
loneDiagonalCriteria = NewCriteria(
NewCriterion(".%6%7", "%4/%8", "%3%2."),
NewCriterion("%1%6.", "%4\\%8", ".%2%5"))
// groups
intersectionCriteria = NewCriteria(crossCriteria, KCriteria, TCriteria, inverseKCriteria, inverseTCriteria)
normalCornerCriteria = NewCriteria(normalCorner1Criteria, normalCorner2Criteria, normalCorner3Criteria, normalCorner4Criteria)
roundCornerCriteria = NewCriteria(roundCorner1Criteria, roundCorner2Criteria, roundCorner3Criteria, roundCorner4Criteria)
corner1Criteria = NewCriteria(normalCorner1Criteria, roundCorner1Criteria)
corner2Criteria = NewCriteria(normalCorner2Criteria, roundCorner2Criteria)
corner3Criteria = NewCriteria(normalCorner3Criteria, roundCorner3Criteria)
corner4Criteria = NewCriteria(normalCorner4Criteria, roundCorner4Criteria)
cornerCriteria = NewCriteria(normalCornerCriteria, roundCornerCriteria)
crossOnLineCriteria = NewCriteria(horizontalCrossOnLineCriteria, verticalCrossOnLineCriteria)
starOnLineCriteria = NewCriteria(horizontalStarOnLineCriteria, verticalStarOnLineCriteria)
linesEndCriteria = NewCriteria(horizontalLinesEndCriteria, verticalLinesEndCriteria, stubCriteria)
)