-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathDeclFilter.v3
143 lines (137 loc) · 4.14 KB
/
DeclFilter.v3
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
// Copyright 2021 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Filters members based on names and indices using a pattern list given as a string.
// The syntax of the pattern string is:
// pat_list := [ match (, match)* ]
// match := glob [ . pat ] | seq
// pat := glob | seq
// seq := #num [ .. num ]
class DeclFilter(pattern: string, list: List<(Matcher, Matcher)>) {
def matches(module: Module, decl: Decl) -> bool {
if (pattern == null) return true;
var names = module.names;
var mname = module.name;
if (mname == null && names != null) mname = names.getModuleName();
var fname: string, index: int;
match (decl) {
x: SigDecl => index = x.heaptype_index;
x: StructDecl => index = x.heaptype_index;
x: ArrayDecl => index = x.heaptype_index;
x: FuncDecl => {
index = x.func_index;
if (names != null) fname = names.getFuncName(x.func_index);
}
x: TableDecl => index = x.table_index;
x: MemoryDecl => index = x.memory_index;
x: GlobalDecl => index = x.global_index;
x: TagDecl => index = x.tag_index;
}
for (l = list; l != null; l = l.tail) {
if (l.head.0.matches(mname, -1) && l.head.1.matches(fname, index)) return true;
}
return false;
}
def matchesStr(mname: string, fname: string) -> bool {
if (pattern == null) return true;
for (l = list; l != null; l = l.tail) {
var mfilter = l.head.0, ffilter = l.head.1;
if (l.head.0.matches(mname, -1) && l.head.1.matches(fname, -1)) return true;
}
return false;
}
def render(buf: StringBuilder) -> StringBuilder {
if (pattern == null) return buf.puts("(*, *)");
Trace.renderCspList(buf, list, renderMatcherPair);
return buf;
}
}
def renderMatcherPair(t: (Matcher, Matcher), buf: StringBuilder) -> StringBuilder {
buf.put2("(%q, %q)", t.0.render, t.1.render);
return buf;
}
def parseDeclFilter(input: string) -> List<(Matcher, Matcher)> {
if (input == null) return List.new((Matcher.All, Matcher.All), null);
var p = TextReader.new("<option>", input);
var list: List<(Matcher, Matcher)>;
while (p.pos < p.limit) {
var pair = DeclFilters.parseMatcherPair(p);
list = List.new(pair, list);
p.opt1(',');
}
return list;
}
def parseGlob(p: TextReader) -> Matcher {
var m: Matcher;
var start = p.pos, pos = p.pos;
while (pos < p.limit) {
var ch = p.data[pos];
if (ch == ',' || ch == '.') break;
pos++;
}
if (pos > start) {
var pattern = Arrays.range(p.data, start, pos);
p.advance(pos - start);
if (Strings.equal(pattern, "*")) m = Matcher.All; // saves a glob match
else m = Matcher.Glob(GlobMatcher.new(pattern));
}
return m;
}
// Different kinds of matchers, e.g. by name or sequence of indices.
type Matcher {
case None;
case All;
case Glob(matcher: GlobMatcher);
case Sequence(min: u32, max: u32);
def matches(name: string, index: int) -> bool {
match (this) {
None => return false;
All => return true;
Glob(matcher) => return name != null && matcher.matches(name);
Sequence(min, max) => return index >= min && index <= max;
}
}
def render(buf: StringBuilder) -> StringBuilder {
match (this) {
None => ;
All => buf.puts("*");
Glob(matcher) => buf.puts(matcher.pattern);
Sequence(min, max) => buf.put2("#%d..%d", min, max);
}
return buf;
}
}
component DeclFilters {
def parseString(pattern: string) -> DeclFilter {
return DeclFilter.new(pattern, parseDeclFilter(pattern));
}
def parseMatcherPair(p: TextReader) -> (Matcher, Matcher) {
if (p.char == '#') return (Matcher.All, Matcher.Sequence(parseSequence(p.advance1())));
var m = parseGlob(p);
if (p.opt1('.') > 0) {
var f = if (p.char == '#', Matcher.Sequence(parseSequence(p)), parseGlob(p));
return (m, f);
}
return (Matcher.All, m);
}
def parseSequence(p: TextReader) -> (u32, u32) {
var t = Ints.parsePosDecimal(p.data, p.pos);
var min = 0u, max = 0u;
if (t.0 > 0) {
min = max = t.1;
p.advance(t.0);
} else {
p.fail("invalid number");
return (0, 0);
}
if (p.optN("...") > 0 || p.optN("..") > 0) {
var t = Ints.parsePosDecimal(p.data, p.pos);
if (t.0 > 0) {
max = t.1;
p.advance(t.0);
} else {
p.fail("invalid number");
}
}
return (min, max);
}
}