Mercurial > hg
comparison mercurial/revset.py @ 29073:81bac118f9e2
revset: factor out common parsing function
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 17 Apr 2016 13:03:23 +0900 |
parents | f86fa7168059 |
children | e7c679738503 |
comparison
equal
deleted
inserted
replaced
29072:f86fa7168059 | 29073:81bac118f9e2 |
---|---|
2215 # the set of valid characters for the initial letter of symbols in | 2215 # the set of valid characters for the initial letter of symbols in |
2216 # alias declarations and definitions | 2216 # alias declarations and definitions |
2217 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)] | 2217 _aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)] |
2218 if c.isalnum() or c in '._@$' or ord(c) > 127) | 2218 if c.isalnum() or c in '._@$' or ord(c) > 127) |
2219 | 2219 |
2220 def _parsewith(spec, lookup=None, syminitletters=None): | |
2221 """Generate a parse tree of given spec with given tokenizing options | |
2222 | |
2223 >>> _parsewith('foo($1)', syminitletters=_aliassyminitletters) | |
2224 ('func', ('symbol', 'foo'), ('symbol', '$1')) | |
2225 >>> _parsewith('$1') | |
2226 Traceback (most recent call last): | |
2227 ... | |
2228 ParseError: ("syntax error in revset '$1'", 0) | |
2229 >>> _parsewith('foo bar') | |
2230 Traceback (most recent call last): | |
2231 ... | |
2232 ParseError: ('invalid token', 4) | |
2233 """ | |
2234 p = parser.parser(elements) | |
2235 tree, pos = p.parse(tokenize(spec, lookup=lookup, | |
2236 syminitletters=syminitletters)) | |
2237 if pos != len(spec): | |
2238 raise error.ParseError(_('invalid token'), pos) | |
2239 return parser.simplifyinfixops(tree, ('list', 'or')) | |
2240 | |
2220 def _parsealias(spec): | 2241 def _parsealias(spec): |
2221 """Parse alias declaration/definition ``spec`` | 2242 """Parse alias declaration/definition ``spec`` |
2222 | 2243 |
2223 This allows symbol names to use also ``$`` as an initial letter | 2244 This allows symbol names to use also ``$`` as an initial letter |
2224 (for backward compatibility), and callers of this function should | 2245 (for backward compatibility), and callers of this function should |
2225 examine whether ``$`` is used also for unexpected symbols or not. | 2246 examine whether ``$`` is used also for unexpected symbols or not. |
2226 | 2247 """ |
2227 >>> _parsealias('foo($1)') | 2248 return _parsewith(spec, syminitletters=_aliassyminitletters) |
2228 ('func', ('symbol', 'foo'), ('symbol', '$1')) | |
2229 >>> _parsealias('foo bar') | |
2230 Traceback (most recent call last): | |
2231 ... | |
2232 ParseError: ('invalid token', 4) | |
2233 """ | |
2234 p = parser.parser(elements) | |
2235 tree, pos = p.parse(tokenize(spec, syminitletters=_aliassyminitletters)) | |
2236 if pos != len(spec): | |
2237 raise error.ParseError(_('invalid token'), pos) | |
2238 return parser.simplifyinfixops(tree, ('list', 'or')) | |
2239 | 2249 |
2240 class _aliasrules(parser.basealiasrules): | 2250 class _aliasrules(parser.basealiasrules): |
2241 """Parsing and expansion rule set of revset aliases""" | 2251 """Parsing and expansion rule set of revset aliases""" |
2242 _section = _('revset alias') | 2252 _section = _('revset alias') |
2243 _parse = staticmethod(_parsealias) | 2253 _parse = staticmethod(_parsealias) |
2278 return ('string', ''.join(l)) | 2288 return ('string', ''.join(l)) |
2279 else: | 2289 else: |
2280 return tuple(foldconcat(t) for t in tree) | 2290 return tuple(foldconcat(t) for t in tree) |
2281 | 2291 |
2282 def parse(spec, lookup=None): | 2292 def parse(spec, lookup=None): |
2283 p = parser.parser(elements) | 2293 return _parsewith(spec, lookup=lookup) |
2284 tree, pos = p.parse(tokenize(spec, lookup=lookup)) | |
2285 if pos != len(spec): | |
2286 raise error.ParseError(_("invalid token"), pos) | |
2287 return parser.simplifyinfixops(tree, ('list', 'or')) | |
2288 | 2294 |
2289 def posttreebuilthook(tree, repo): | 2295 def posttreebuilthook(tree, repo): |
2290 # hook for extensions to execute code on the optimized tree | 2296 # hook for extensions to execute code on the optimized tree |
2291 pass | 2297 pass |
2292 | 2298 |