Permalink
Please sign in to comment.
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...
Showing
with
52 additions
and 44 deletions.
- +5 −23 asdl/format.py
- +45 −0 asdl/pretty.py
- +2 −21 core/cmd_exec.py
| @@ -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. | ||
0 comments on commit
2a65b3c