Show More
@@ -1,140 +1,177 b'' | |||||
1 | # fileset.py - file set queries for mercurial |
|
1 | # fileset.py - file set queries for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2010 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2010 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | import parser, error |
|
8 | import parser, error, util | |
9 | from i18n import _ |
|
9 | from i18n import _ | |
10 |
|
10 | |||
11 | elements = { |
|
11 | elements = { | |
12 | "(": (20, ("group", 1, ")"), ("func", 1, ")")), |
|
12 | "(": (20, ("group", 1, ")"), ("func", 1, ")")), | |
13 | "-": (5, ("negate", 19), ("minus", 5)), |
|
13 | "-": (5, ("negate", 19), ("minus", 5)), | |
14 | "not": (10, ("not", 10)), |
|
14 | "not": (10, ("not", 10)), | |
15 | "!": (10, ("not", 10)), |
|
15 | "!": (10, ("not", 10)), | |
16 | "and": (5, None, ("and", 5)), |
|
16 | "and": (5, None, ("and", 5)), | |
17 | "&": (5, None, ("and", 5)), |
|
17 | "&": (5, None, ("and", 5)), | |
18 | "or": (4, None, ("or", 4)), |
|
18 | "or": (4, None, ("or", 4)), | |
19 | "|": (4, None, ("or", 4)), |
|
19 | "|": (4, None, ("or", 4)), | |
20 | "+": (4, None, ("or", 4)), |
|
20 | "+": (4, None, ("or", 4)), | |
21 | ",": (2, None, ("list", 2)), |
|
21 | ",": (2, None, ("list", 2)), | |
22 | ")": (0, None, None), |
|
22 | ")": (0, None, None), | |
23 | "symbol": (0, ("symbol",), None), |
|
23 | "symbol": (0, ("symbol",), None), | |
24 | "string": (0, ("string",), None), |
|
24 | "string": (0, ("string",), None), | |
25 | "end": (0, None, None), |
|
25 | "end": (0, None, None), | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | keywords = set(['and', 'or', 'not']) |
|
28 | keywords = set(['and', 'or', 'not']) | |
29 |
|
29 | |||
30 | globchars = ".*{}[]?/\\" |
|
30 | globchars = ".*{}[]?/\\" | |
31 |
|
31 | |||
32 | def tokenize(program): |
|
32 | def tokenize(program): | |
33 | pos, l = 0, len(program) |
|
33 | pos, l = 0, len(program) | |
34 | while pos < l: |
|
34 | while pos < l: | |
35 | c = program[pos] |
|
35 | c = program[pos] | |
36 | if c.isspace(): # skip inter-token whitespace |
|
36 | if c.isspace(): # skip inter-token whitespace | |
37 | pass |
|
37 | pass | |
38 | elif c in "(),-|&+!": # handle simple operators |
|
38 | elif c in "(),-|&+!": # handle simple operators | |
39 | yield (c, None, pos) |
|
39 | yield (c, None, pos) | |
40 | elif (c in '"\'' or c == 'r' and |
|
40 | elif (c in '"\'' or c == 'r' and | |
41 | program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings |
|
41 | program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings | |
42 | if c == 'r': |
|
42 | if c == 'r': | |
43 | pos += 1 |
|
43 | pos += 1 | |
44 | c = program[pos] |
|
44 | c = program[pos] | |
45 | decode = lambda x: x |
|
45 | decode = lambda x: x | |
46 | else: |
|
46 | else: | |
47 | decode = lambda x: x.decode('string-escape') |
|
47 | decode = lambda x: x.decode('string-escape') | |
48 | pos += 1 |
|
48 | pos += 1 | |
49 | s = pos |
|
49 | s = pos | |
50 | while pos < l: # find closing quote |
|
50 | while pos < l: # find closing quote | |
51 | d = program[pos] |
|
51 | d = program[pos] | |
52 | if d == '\\': # skip over escaped characters |
|
52 | if d == '\\': # skip over escaped characters | |
53 | pos += 2 |
|
53 | pos += 2 | |
54 | continue |
|
54 | continue | |
55 | if d == c: |
|
55 | if d == c: | |
56 | yield ('string', decode(program[s:pos]), s) |
|
56 | yield ('string', decode(program[s:pos]), s) | |
57 | break |
|
57 | break | |
58 | pos += 1 |
|
58 | pos += 1 | |
59 | else: |
|
59 | else: | |
60 | raise error.ParseError(_("unterminated string"), s) |
|
60 | raise error.ParseError(_("unterminated string"), s) | |
61 | elif c.isalnum() or c in globchars or ord(c) > 127: |
|
61 | elif c.isalnum() or c in globchars or ord(c) > 127: | |
62 | # gather up a symbol/keyword |
|
62 | # gather up a symbol/keyword | |
63 | s = pos |
|
63 | s = pos | |
64 | pos += 1 |
|
64 | pos += 1 | |
65 | while pos < l: # find end of symbol |
|
65 | while pos < l: # find end of symbol | |
66 | d = program[pos] |
|
66 | d = program[pos] | |
67 | if not (d.isalnum() or d in globchars or ord(d) > 127): |
|
67 | if not (d.isalnum() or d in globchars or ord(d) > 127): | |
68 | break |
|
68 | break | |
69 | pos += 1 |
|
69 | pos += 1 | |
70 | sym = program[s:pos] |
|
70 | sym = program[s:pos] | |
71 | if sym in keywords: # operator keywords |
|
71 | if sym in keywords: # operator keywords | |
72 | yield (sym, None, s) |
|
72 | yield (sym, None, s) | |
73 | else: |
|
73 | else: | |
74 | yield ('symbol', sym, s) |
|
74 | yield ('symbol', sym, s) | |
75 | pos -= 1 |
|
75 | pos -= 1 | |
76 | else: |
|
76 | else: | |
77 | raise error.ParseError(_("syntax error"), pos) |
|
77 | raise error.ParseError(_("syntax error"), pos) | |
78 | pos += 1 |
|
78 | pos += 1 | |
79 | yield ('end', None, pos) |
|
79 | yield ('end', None, pos) | |
80 |
|
80 | |||
81 | parse = parser.parser(tokenize, elements).parse |
|
81 | parse = parser.parser(tokenize, elements).parse | |
82 |
|
82 | |||
83 | def getstring(x, err): |
|
83 | def getstring(x, err): | |
84 | if x and (x[0] == 'string' or x[0] == 'symbol'): |
|
84 | if x and (x[0] == 'string' or x[0] == 'symbol'): | |
85 | return x[1] |
|
85 | return x[1] | |
86 | raise error.ParseError(err) |
|
86 | raise error.ParseError(err) | |
87 |
|
87 | |||
88 | def getset(mctx, x): |
|
88 | def getset(mctx, x): | |
89 | if not x: |
|
89 | if not x: | |
90 | raise error.ParseError(_("missing argument")) |
|
90 | raise error.ParseError(_("missing argument")) | |
91 | return methods[x[0]](mctx, *x[1:]) |
|
91 | return methods[x[0]](mctx, *x[1:]) | |
92 |
|
92 | |||
93 | def stringset(mctx, x): |
|
93 | def stringset(mctx, x): | |
94 | m = mctx.matcher([x]) |
|
94 | m = mctx.matcher([x]) | |
95 | return [f for f in mctx.subset if m(f)] |
|
95 | return [f for f in mctx.subset if m(f)] | |
96 |
|
96 | |||
97 | def andset(mctx, x, y): |
|
97 | def andset(mctx, x, y): | |
98 | return getset(mctx.narrow(getset(mctx, x)), y) |
|
98 | return getset(mctx.narrow(getset(mctx, x)), y) | |
99 |
|
99 | |||
100 | def orset(mctx, x, y): |
|
100 | def orset(mctx, x, y): | |
101 | # needs optimizing |
|
101 | # needs optimizing | |
102 | xl = getset(mctx, x) |
|
102 | xl = getset(mctx, x) | |
103 | yl = getset(mctx, y) |
|
103 | yl = getset(mctx, y) | |
104 | return xl + [f for f in yl if f not in xl] |
|
104 | return xl + [f for f in yl if f not in xl] | |
105 |
|
105 | |||
106 | def notset(mctx, x): |
|
106 | def notset(mctx, x): | |
107 | s = set(getset(mctx, x)) |
|
107 | s = set(getset(mctx, x)) | |
108 | return [r for r in mctx.subset if r not in s] |
|
108 | return [r for r in mctx.subset if r not in s] | |
109 |
|
109 | |||
110 | def listset(mctx, a, b): |
|
110 | def listset(mctx, a, b): | |
111 | raise error.ParseError(_("can't use a list in this context")) |
|
111 | raise error.ParseError(_("can't use a list in this context")) | |
112 |
|
112 | |||
|
113 | def func(mctx, a, b): | |||
|
114 | if a[0] == 'symbol' and a[1] in symbols: | |||
|
115 | return symbols[a[1]](mctx, b) | |||
|
116 | raise error.ParseError(_("not a function: %s") % a[1]) | |||
|
117 | ||||
|
118 | def getlist(x): | |||
|
119 | if not x: | |||
|
120 | return [] | |||
|
121 | if x[0] == 'list': | |||
|
122 | return getlist(x[1]) + [x[2]] | |||
|
123 | return [x] | |||
|
124 | ||||
|
125 | def getargs(x, min, max, err): | |||
|
126 | l = getlist(x) | |||
|
127 | if len(l) < min or len(l) > max: | |||
|
128 | raise error.ParseError(err) | |||
|
129 | return l | |||
|
130 | ||||
|
131 | def binary(mctx, x): | |||
|
132 | getargs(x, 0, 0, _("binary takes no arguments")) | |||
|
133 | return [f for f in mctx.subset if util.binary(mctx.ctx[f].data())] | |||
|
134 | ||||
|
135 | def exec_(mctx, x): | |||
|
136 | getargs(x, 0, 0, _("exec takes no arguments")) | |||
|
137 | return [f for f in mctx.subset if mctx.ctx.flags(f) == 'x'] | |||
|
138 | ||||
|
139 | def symlink(mctx, x): | |||
|
140 | getargs(x, 0, 0, _("symlink takes no arguments")) | |||
|
141 | return [f for f in mctx.subset if mctx.ctx.flags(f) == 'l'] | |||
|
142 | ||||
|
143 | symbols = { | |||
|
144 | 'binary': binary, | |||
|
145 | 'exec': exec_, | |||
|
146 | 'symlink': symlink, | |||
|
147 | } | |||
|
148 | ||||
113 | methods = { |
|
149 | methods = { | |
114 | 'string': stringset, |
|
150 | 'string': stringset, | |
115 | 'symbol': stringset, |
|
151 | 'symbol': stringset, | |
116 | 'and': andset, |
|
152 | 'and': andset, | |
117 | 'or': orset, |
|
153 | 'or': orset, | |
118 | 'list': listset, |
|
154 | 'list': listset, | |
119 | 'group': getset, |
|
155 | 'group': getset, | |
120 | 'not': notset |
|
156 | 'not': notset, | |
|
157 | 'func': func, | |||
121 | } |
|
158 | } | |
122 |
|
159 | |||
123 | class matchctx(object): |
|
160 | class matchctx(object): | |
124 | def __init__(self, ctx, subset=None): |
|
161 | def __init__(self, ctx, subset=None): | |
125 | self.ctx = ctx |
|
162 | self.ctx = ctx | |
126 | self.subset = subset |
|
163 | self.subset = subset | |
127 | if subset is None: |
|
164 | if subset is None: | |
128 | self.subset = ctx.walk(self.matcher([])) # optimize this later |
|
165 | self.subset = ctx.walk(self.matcher([])) # optimize this later | |
129 | def matcher(self, patterns): |
|
166 | def matcher(self, patterns): | |
130 | return self.ctx.match(patterns) |
|
167 | return self.ctx.match(patterns) | |
131 | def filter(self, files): |
|
168 | def filter(self, files): | |
132 | return [f for f in files if f in self.subset] |
|
169 | return [f for f in files if f in self.subset] | |
133 | def narrow(self, files): |
|
170 | def narrow(self, files): | |
134 | return matchctx(self.ctx, self.filter(files)) |
|
171 | return matchctx(self.ctx, self.filter(files)) | |
135 |
|
172 | |||
136 | def getfileset(ctx, expr): |
|
173 | def getfileset(ctx, expr): | |
137 | tree, pos = parse(expr) |
|
174 | tree, pos = parse(expr) | |
138 | if (pos != len(expr)): |
|
175 | if (pos != len(expr)): | |
139 | raise error.ParseError("invalid token", pos) |
|
176 | raise error.ParseError("invalid token", pos) | |
140 | return getset(matchctx(ctx), tree) |
|
177 | return getset(matchctx(ctx), tree) |
General Comments 0
You need to be logged in to leave comments.
Login now