Permalink
Browse files

Factor out asdl/pretty.py because it uses the 're' module.

We want convert regexes to re2c and break the dependency.
  • Loading branch information...
Andy Chu
Andy Chu committed May 29, 2018
1 parent 2d2e322 commit 2a65b3cc0d1cc028af12af129582cdc63718bed7
Showing with 52 additions and 44 deletions.
  1. +5 −23 asdl/format.py
  2. +45 −0 asdl/pretty.py
  3. +2 −21 core/cmd_exec.py
View
@@ -15,9 +15,8 @@
- abbreviated, unnamed fields
"""
import re
from asdl import asdl_ as asdl
from asdl import pretty
from core import util
import os
@@ -320,23 +319,6 @@ def MakeTree(obj, abbrev_hook=None, omit_empty=True):
return out_node
# This is word characters, - and _, as well as path name characters . and /.
_PLAIN_RE = re.compile(r'^[a-zA-Z0-9\-_./]+$')
# NOTE: Turning JSON back on can be a cheap hack for detecting invalid unicode.
# But we want to write our own AST walker for that.
def _PrettyString(s):
if '\n' in s:
#return json.dumps(s) # account for the fact that $ matches the newline
return repr(s)
if _PLAIN_RE.match(s):
return s
else:
#return json.dumps(s)
return repr(s)
INDENT = 2
def _PrintWrappedArray(array, prefix_len, f, indent, max_col):
@@ -483,11 +465,11 @@ def PrintTree(node, f, indent=0, max_col=100):
return
if isinstance(node, str):
f.write(ind + _PrettyString(node))
f.write(ind + pretty.Str(node))
elif isinstance(node, _ColoredString):
f.PushColor(node.str_type)
f.write(_PrettyString(node.s))
f.write(pretty.Str(node.s))
f.PopColor()
elif isinstance(node, _Obj):
@@ -540,11 +522,11 @@ def _TrySingleLine(node, f, max_chars):
If False, you can't use the value of f.
"""
if isinstance(node, str):
f.write(_PrettyString(node))
f.write(pretty.Str(node))
elif isinstance(node, _ColoredString):
f.PushColor(node.str_type)
f.write(_PrettyString(node.s))
f.write(pretty.Str(node.s))
f.PopColor()
elif isinstance(node, list): # Can we fit the WHOLE list on the line?
View
@@ -0,0 +1,45 @@
#!/usr/bin/python
"""
pretty.py
"""
try:
import fastlex
except ImportError:
fastlex = None
# Word characters, - and _, as well as path name characters . and /.
PLAIN_WORD_RE = r'^[a-zA-Z0-9\-_./]+'
if 0:
#if fastlex:
IsPlainWord = fastlex.IsPlainWord
else:
import re
_PLAIN_WORD_RE = re.compile(PLAIN_WORD_RE + '$')
def IsPlainWord(s):
if '\n' in s: # account for the fact that $ matches the newline
return False
return _PLAIN_WORD_RE.match(s)
# NOTE: bash prints \' for single quote, repr() prints "'". Gah. This is also
# used for printf %q and ${var@q} (bash 4.4).
def Str(s):
"""Return a human-friendly representation of an arbitrary shell string.
Used for ASDL pretty printing as well as the 'xtrace' feature in
core/cmd_exec.py.
"""
if IsPlainWord(s):
return s
else:
return repr(s)
# NOTE: Converting strings to JSON and can be a cheap hack for detecting
# invalid unicode. But we want to write our own AST walker for that.
View
@@ -23,6 +23,7 @@
import time
from asdl import const
from asdl import pretty
from core import alloc
from core import args
@@ -1497,7 +1498,7 @@ def OnSimpleCommand(self, argv):
return
first_char, prefix = self._EvalPS4()
cmd = ' '.join(_PrettyString(a) for a in argv)
cmd = ' '.join(pretty.Str(a) for a in argv)
print('%s%s%s' % (first_char, prefix, cmd), file=sys.stderr)
def OnAssignment(self, lval, val, flags, lookup_mode):
@@ -1520,23 +1521,3 @@ def Event(self):
- We should desugar to SetVar like mksh
"""
pass
# Copied from asdl/format.py. We're not using it directly because that is
# debug output, and this is real input.
# TODO: Is this slow?
# NOTE: bash prints \' for single quote, repr() prints "'". Gah. This is also
# used for printf %q and ${var@q} (bash 4.4).
_PLAIN_RE = re.compile(r'^[a-zA-Z0-9\-_./]+$')
def _PrettyString(s):
if '\n' in s:
#return json.dumps(s) # account for the fact that $ matches the newline
return repr(s)
if _PLAIN_RE.match(s):
return s
else:
#return json.dumps(s)
return repr(s)

0 comments on commit 2a65b3c

Please sign in to comment.