comparison mercurial/parser.py @ 34152:a8994d08e4a2

doctest: use print_function and convert bytes to unicode where needed
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Sep 2017 14:56:31 +0900
parents 0fa781320203
children 7840d8bd0558
comparison
equal deleted inserted replaced
34151:414a3513c2bd 34152:a8994d08e4a2
14 # elements is a mapping of types to binding strength, primary, prefix, infix 14 # elements is a mapping of types to binding strength, primary, prefix, infix
15 # and suffix actions 15 # and suffix actions
16 # an action is a tree node name, a tree label, and an optional match 16 # an action is a tree node name, a tree label, and an optional match
17 # __call__(program) parses program into a labeled tree 17 # __call__(program) parses program into a labeled tree
18 18
19 from __future__ import absolute_import 19 from __future__ import absolute_import, print_function
20 20
21 from .i18n import _ 21 from .i18n import _
22 from . import ( 22 from . import (
23 encoding, 23 encoding,
24 error, 24 error,
218 return output 218 return output
219 219
220 def simplifyinfixops(tree, targetnodes): 220 def simplifyinfixops(tree, targetnodes):
221 """Flatten chained infix operations to reduce usage of Python stack 221 """Flatten chained infix operations to reduce usage of Python stack
222 222
223 >>> from . import pycompat
223 >>> def f(tree): 224 >>> def f(tree):
224 ... print prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',)) 225 ... s = prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',))
226 ... print(pycompat.sysstr(s))
225 >>> f((b'or', 227 >>> f((b'or',
226 ... (b'or', 228 ... (b'or',
227 ... (b'symbol', b'1'), 229 ... (b'symbol', b'1'),
228 ... (b'symbol', b'2')), 230 ... (b'symbol', b'2')),
229 ... (b'symbol', b'3'))) 231 ... (b'symbol', b'3')))
553 parsing rule is provided by ``_parse()``. 555 parsing rule is provided by ``_parse()``.
554 556
555 ``args`` is a list of alias argument names, or None if the alias 557 ``args`` is a list of alias argument names, or None if the alias
556 is declared as a symbol. 558 is declared as a symbol.
557 559
560 >>> from . import pycompat
558 >>> parsemap = { 561 >>> parsemap = {
559 ... b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')), 562 ... b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')),
560 ... b'$1 or $bar': 563 ... b'$1 or $bar':
561 ... (b'or', (b'symbol', b'$1'), (b'symbol', b'$bar')), 564 ... (b'or', (b'symbol', b'$1'), (b'symbol', b'$bar')),
562 ... b'$10 or baz': 565 ... b'$10 or baz':
567 >>> class aliasrules(basealiasrules): 570 >>> class aliasrules(basealiasrules):
568 ... _parse = staticmethod(parsemap.__getitem__) 571 ... _parse = staticmethod(parsemap.__getitem__)
569 ... _trygetfunc = staticmethod(lambda x: None) 572 ... _trygetfunc = staticmethod(lambda x: None)
570 >>> builddefn = aliasrules._builddefn 573 >>> builddefn = aliasrules._builddefn
571 >>> def pprint(tree): 574 >>> def pprint(tree):
572 ... print prettyformat(tree, (b'_aliasarg', b'string', b'symbol')) 575 ... s = prettyformat(tree, (b'_aliasarg', b'string', b'symbol'))
576 ... print(pycompat.sysstr(s))
573 >>> args = [b'$1', b'$2', b'foo'] 577 >>> args = [b'$1', b'$2', b'foo']
574 >>> pprint(builddefn(b'$1 or foo', args)) 578 >>> pprint(builddefn(b'$1 or foo', args))
575 (or 579 (or
576 (_aliasarg '$1') 580 (_aliasarg '$1')
577 (_aliasarg 'foo')) 581 (_aliasarg 'foo'))
578 >>> try: 582 >>> try:
579 ... builddefn(b'$1 or $bar', args) 583 ... builddefn(b'$1 or $bar', args)
580 ... except error.ParseError as inst: 584 ... except error.ParseError as inst:
581 ... print parseerrordetail(inst) 585 ... print(pycompat.sysstr(parseerrordetail(inst)))
582 invalid symbol '$bar' 586 invalid symbol '$bar'
583 >>> args = [b'$1', b'$10', b'foo'] 587 >>> args = [b'$1', b'$10', b'foo']
584 >>> pprint(builddefn(b'$10 or baz', args)) 588 >>> pprint(builddefn(b'$10 or baz', args))
585 (or 589 (or
586 (_aliasarg '$10') 590 (_aliasarg '$10')