-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFind.java
173 lines (162 loc) · 4.47 KB
/
Find.java
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
package edu.lsu.cct.piraha;
import java.util.ArrayList;
import java.util.List;
//import edu.lsu.cct.piraha.examples.Calc;
public class Find {
static Grammar g = new Grammar();
static {
g.compile("index","[0-9]+");
g.compile("quoted", "([^'\\\\]|\\\\[^\n])+");
g.compile("value","[a-zA-Z0-9_]+|'{quoted}'");
g.compile("op","![=~]|=~?|[<>]=?");
g.compile("name","[a-zA-Z0-9_*]+");
g.compile("elem","{name}(\\[{index}\\]|)({op}{value}|)");
g.compile("elemseq","\\.{elem}(\\|{elem})*");
g.compile("quant","\\({elemseq}\\)\\*");
g.compile("pat", "({quant}|{elemseq})({pat}|{quant}|)");
g.compile("full", "{pat}$");
}
Group patc;
public Find(String pattern) {
Matcher m = g.matcher(pattern);
if(!m.matches())
throw new RuntimeException(m.near().toString());
patc = m.group().group(0);
}
public List<Group> find(Group g) {
List<Group> li = new ArrayList<Group>();
find(patc,g,li);
return li;
}
private void find(Group p,Group g,List<Group> li) {
String pn = p.getPatternName();
if("pat".equals(pn)) {
List<Group> li2 = p.groupCount()==1 ? li :new ArrayList<Group>();
find(p.group(0),g,li2);
if(p.groupCount()==2) {
for(Group gg : li2) {
for(int i=0;i<gg.groupCount();i++) {
find(p.group(1),gg.group(i),li);
}
}
}
} else if("elemseq".equals(pn)) {
for(int i=0;i<p.groupCount();i++) {
find(p.group(i),g,li);
}
} else if("elem".equals(pn)) {
// p.dumpMatches();
boolean add = true;
int m = wildmatch(p.group(0).substring(),g.getPatternName());
if(m != g.getPatternName().length()) {
//li.add(g);
add = false;
}
if(add && p.groupCount()==3) {
String op = p.group(1).substring();
String val = p.group(2).groupCount()>0 ? p.group(2).group(0).substring() : p.group(2).substring();
if("=".equals(op)) {
if(!g.substring().equals(val)) {
add = false;
}
} else if("!=".equals(op)) {
if(g.substring().equals(val)) {
add = false;
}
} else if("=~".equals(op)) {
if(wildmatch(val,g.substring()) != g.substring().length()) {
add = false;
}
} else if("!~".equals(op)) {
if(wildmatch(val,g.substring()) == g.substring().length()) {
add = false;
}
} else {
if("<".equals(op)) {
int intstr = Integer.parseInt(g.substring());
if(intstr >= Integer.parseInt(val)) {
add = false;
}
} else if(">".equals(op)) {
int intstr = Integer.parseInt(g.substring());
if(intstr <= Integer.parseInt(val)) {
add = false;
}
} else if("<=".equals(op)) {
int intstr = Integer.parseInt(g.substring());
if(intstr > Integer.parseInt(val)) {
add = false;
}
} else if(">=".equals(op)) {
int intstr = Integer.parseInt(g.substring());
if(intstr < Integer.parseInt(val)) {
add = false;
}
}
}
}
if(add)
li.add(g);
} else if("quant".equals(pn)) {
List<Group> lg = new ArrayList<Group>();
find(p.group(0),g,lg);
if(lg.size()>0) {
if(p.groupCount()==1) {
for(Group g2 : lg) {
li.add(g2);
}
}
for(int i=0;i<g.groupCount();i++) {
find(p,g.group(i),li);
}
}
if(p.groupCount()==2) {
find(p.group(1),g,li);
}
}
}
static int wildmatch(String pattern,String text) {
return wildmatch(pattern,0,text,0);
}
static int wildmatch(String pattern,int i,String text,int j) {
while(i < pattern.length() && j < text.length()) {
if(pattern.charAt(i) == '*') {
if(pattern.length()-1==i)
return text.length();
int n = wildmatch(pattern,i+1,text,j);
if(n >= 0) return n;
j++;
} else if(pattern.charAt(i) == text.charAt(j)) {
i++; j++;
} else {
return -1;
}
}
if(i==pattern.length())
return j;
else
return -1;
}
public static void check(boolean b) {
if(!b) throw new Error();
}
/*
public static void main(String[] args) {
Grammar tg = Calc.makeMath();
Matcher mg = tg.matcher("3+9*(1+9)-4");
mg.matches();
Group res = mg.group();
res.dumpMatches();
Find f = new Find("(.*)*.num<=3");
List<Group> fgl = f.find(res);
check(fgl.size()==2);
f = new Find("(.*)*.num");
fgl = f.find(res);
check(fgl.size()==5);
f = new Find("(.*)*.addop|mulop");
fgl = f.find(res);
check(fgl.size()==4);
System.out.println("test complete");
}
*/
}