Details
Details
- Reviewers
pulkit - Group Reviewers
hg-reviewers - Commits
- rHG0a2ce5b43574: parser: replace bespoke _brepr with stringutil.pprint
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
pulkit |
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
# and suffix actions | # and suffix actions | ||||
# an action is a tree node name, a tree label, and an optional match | # an action is a tree node name, a tree label, and an optional match | ||||
# __call__(program) parses program into a labeled tree | # __call__(program) parses program into a labeled tree | ||||
from __future__ import absolute_import, print_function | from __future__ import absolute_import, print_function | ||||
from .i18n import _ | from .i18n import _ | ||||
from . import ( | from . import ( | ||||
encoding, | |||||
error, | error, | ||||
pycompat, | pycompat, | ||||
util, | util, | ||||
) | ) | ||||
from .utils import ( | from .utils import ( | ||||
stringutil, | stringutil, | ||||
) | ) | ||||
def unescapestr(s): | def unescapestr(s): | ||||
try: | try: | ||||
return stringutil.unescapestr(s) | return stringutil.unescapestr(s) | ||||
except ValueError as e: | except ValueError as e: | ||||
# mangle Python's exception into our format | # mangle Python's exception into our format | ||||
raise error.ParseError(pycompat.bytestr(e).lower()) | raise error.ParseError(pycompat.bytestr(e).lower()) | ||||
def _brepr(obj): | |||||
if isinstance(obj, bytes): | |||||
return b"'%s'" % stringutil.escapestr(obj) | |||||
return encoding.strtolocal(repr(obj)) | |||||
def _prettyformat(tree, leafnodes, level, lines): | def _prettyformat(tree, leafnodes, level, lines): | ||||
if not isinstance(tree, tuple): | if not isinstance(tree, tuple): | ||||
lines.append((level, _brepr(tree))) | lines.append((level, stringutil.pprint(tree))) | ||||
elif tree[0] in leafnodes: | elif tree[0] in leafnodes: | ||||
rs = map(_brepr, tree[1:]) | rs = map(stringutil.pprint, tree[1:]) | ||||
lines.append((level, '(%s %s)' % (tree[0], ' '.join(rs)))) | lines.append((level, '(%s %s)' % (tree[0], ' '.join(rs)))) | ||||
else: | else: | ||||
lines.append((level, '(%s' % tree[0])) | lines.append((level, '(%s' % tree[0])) | ||||
for s in tree[1:]: | for s in tree[1:]: | ||||
_prettyformat(s, leafnodes, level + 1, lines) | _prettyformat(s, leafnodes, level + 1, lines) | ||||
lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')] | lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')] | ||||
def prettyformat(tree, leafnodes): | def prettyformat(tree, leafnodes): |