Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/revset.py @ 28873:2ca3b7c563f3
parser: move alias definition parser to common rule-set class
The original _parsealiasdefn() function is split into common _builddefn()
and revset-specific _parsealiasdefn(). revset._relabelaliasargs() is removed
as it is no longer used.
The doctests are ported by using the dummy parse().
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 29 Feb 2016 18:10:07 +0900 |
parents | 5f31d2248745 |
children | 552eabef663b |
comparison
equal
deleted
inserted
replaced
28872:5f31d2248745 | 28873:2ca3b7c563f3 |
---|---|
2248 tree, pos = p.parse(_tokenizealias(decl)) | 2248 tree, pos = p.parse(_tokenizealias(decl)) |
2249 if pos != len(decl): | 2249 if pos != len(decl): |
2250 raise error.ParseError(_('invalid token'), pos) | 2250 raise error.ParseError(_('invalid token'), pos) |
2251 return parser.simplifyinfixops(tree, ('list',)) | 2251 return parser.simplifyinfixops(tree, ('list',)) |
2252 | 2252 |
2253 def _relabelaliasargs(tree, args): | 2253 def _parsealiasdefn(defn): |
2254 return _aliasrules._relabelargs(tree, args) | 2254 """Parse alias definition ``defn``""" |
2255 | |
2256 def _parsealiasdefn(defn, args): | |
2257 """Parse alias definition ``defn`` | |
2258 | |
2259 This function marks alias argument references as ``_aliasarg``. | |
2260 | |
2261 ``args`` is a list of alias argument names, or None if the alias | |
2262 is declared as a symbol. | |
2263 | |
2264 This returns "tree" as parsing result. | |
2265 | |
2266 >>> def prettyformat(tree): | |
2267 ... return parser.prettyformat(tree, ('_aliasarg', 'string', 'symbol')) | |
2268 >>> args = ['$1', '$2', 'foo'] | |
2269 >>> print prettyformat(_parsealiasdefn('$1 or foo', args)) | |
2270 (or | |
2271 ('_aliasarg', '$1') | |
2272 ('_aliasarg', 'foo')) | |
2273 >>> try: | |
2274 ... _parsealiasdefn('$1 or $bar', args) | |
2275 ... except error.ParseError, inst: | |
2276 ... print parser.parseerrordetail(inst) | |
2277 '$' not for alias arguments | |
2278 >>> args = ['$1', '$10', 'foo'] | |
2279 >>> print prettyformat(_parsealiasdefn('$10 or foobar', args)) | |
2280 (or | |
2281 ('_aliasarg', '$10') | |
2282 ('symbol', 'foobar')) | |
2283 >>> print prettyformat(_parsealiasdefn('"$1" or "foo"', args)) | |
2284 (or | |
2285 ('string', '$1') | |
2286 ('string', 'foo')) | |
2287 """ | |
2288 if args: | |
2289 args = set(args) | |
2290 else: | |
2291 args = set() | |
2292 | |
2293 p = parser.parser(elements) | 2255 p = parser.parser(elements) |
2294 tree, pos = p.parse(_tokenizealias(defn)) | 2256 tree, pos = p.parse(_tokenizealias(defn)) |
2295 if pos != len(defn): | 2257 if pos != len(defn): |
2296 raise error.ParseError(_('invalid token'), pos) | 2258 raise error.ParseError(_('invalid token'), pos) |
2297 tree = parser.simplifyinfixops(tree, ('list', 'or')) | 2259 return parser.simplifyinfixops(tree, ('list', 'or')) |
2298 return _relabelaliasargs(tree, args) | |
2299 | 2260 |
2300 class _aliasrules(parser.basealiasrules): | 2261 class _aliasrules(parser.basealiasrules): |
2301 """Parsing and expansion rule set of revset aliases""" | 2262 """Parsing and expansion rule set of revset aliases""" |
2302 _section = _('revset alias') | 2263 _section = _('revset alias') |
2303 _parsedecl = staticmethod(_parsealiasdecl) | 2264 _parsedecl = staticmethod(_parsealiasdecl) |
2265 _parsedefn = staticmethod(_parsealiasdefn) | |
2304 _getlist = staticmethod(getlist) | 2266 _getlist = staticmethod(getlist) |
2305 | 2267 |
2306 class revsetalias(object): | 2268 class revsetalias(object): |
2307 # whether own `error` information is already shown or not. | 2269 # whether own `error` information is already shown or not. |
2308 # this avoids showing same warning multiple times at each `findaliases`. | 2270 # this avoids showing same warning multiple times at each `findaliases`. |
2320 self.error = _('failed to parse the declaration of revset alias' | 2282 self.error = _('failed to parse the declaration of revset alias' |
2321 ' "%s": %s') % (self.name, self.error) | 2283 ' "%s": %s') % (self.name, self.error) |
2322 return | 2284 return |
2323 | 2285 |
2324 try: | 2286 try: |
2325 self.replacement = _parsealiasdefn(value, self.args) | 2287 self.replacement = _aliasrules._builddefn(value, self.args) |
2326 except error.ParseError as inst: | 2288 except error.ParseError as inst: |
2327 self.error = _('failed to parse the definition of revset alias' | 2289 self.error = _('failed to parse the definition of revset alias' |
2328 ' "%s": %s') % (self.name, | 2290 ' "%s": %s') % (self.name, |
2329 parser.parseerrordetail(inst)) | 2291 parser.parseerrordetail(inst)) |
2330 | 2292 |