mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
parser.py: PEP8
This commit is contained in:
parent
b4dbdaedff
commit
c43014514b
1 changed files with 215 additions and 206 deletions
|
@ -25,10 +25,12 @@ class Location(object):
|
|||
s += ':{0}'.format(self.column)
|
||||
return s
|
||||
|
||||
|
||||
def has_elts(x):
|
||||
"""Tests if x is an AST node with elements."""
|
||||
return isinstance(x, ast.AST) and hasattr(x, 'elts')
|
||||
|
||||
|
||||
def ensure_has_elts(x, lineno=1, col_offset=1):
|
||||
"""Ensures that x is an AST node with elements."""
|
||||
if not has_elts(x):
|
||||
|
@ -38,23 +40,27 @@ def ensure_has_elts(x, lineno=1, col_offset=1):
|
|||
col_offset=col_offset)
|
||||
return x
|
||||
|
||||
|
||||
def empty_list(lineno=None, col=None):
|
||||
"""Creates the AST node for an empty list."""
|
||||
return ast.List(elts=[], ctx=ast.Load(), lineno=lineno, col_offset=col)
|
||||
|
||||
|
||||
def binop(x, op, y, lineno=None, col=None):
|
||||
"""Creates the AST node for a binary operation."""
|
||||
return ast.BinOp(left=x, op=op, right=y, lineno=lineno, col_offset=col)
|
||||
|
||||
|
||||
def call_split_lines(x, lineno=None, col=None):
|
||||
"""Creates the AST node for calling the 'splitlines' attribute of an
|
||||
object, nominally a string.
|
||||
"""
|
||||
return ast.Call(func=ast.Attribute(value=x, attr='splitlines',
|
||||
ctx=ast.Load(), lineno=lineno, col_offset=col),
|
||||
ctx=ast.Load(), lineno=lineno, col_offset=col),
|
||||
args=[], keywords=[], starargs=None, kwargs=None,
|
||||
lineno=lineno, col_offset=col)
|
||||
|
||||
|
||||
def ensure_list_from_str_or_list(x, lineno=None, col=None):
|
||||
"""Creates the AST node for the following expression::
|
||||
|
||||
|
@ -64,15 +70,18 @@ def ensure_list_from_str_or_list(x, lineno=None, col=None):
|
|||
"""
|
||||
return ast.IfExp(test=ast.Call(func=ast.Name(id='isinstance',
|
||||
ctx=ast.Load(),
|
||||
lineno=lineno, col_offset=col),
|
||||
lineno=lineno,
|
||||
col_offset=col),
|
||||
args=[x, ast.Name(id='str', ctx=ast.Load(),
|
||||
lineno=lineno, col_offset=col)],
|
||||
lineno=lineno,
|
||||
col_offset=col)],
|
||||
keywords=[], starargs=None, kwargs=None,
|
||||
lineno=lineno, col_offset=col),
|
||||
body=ast.List(elts=[x], ctx=ast.Load(), lineno=lineno,
|
||||
col_offset=col),
|
||||
orelse=x, lineno=lineno, col_offset=col)
|
||||
|
||||
|
||||
def xonsh_call(name, args, lineno=None, col=None):
|
||||
"""Creates the AST node for calling a function of a given name."""
|
||||
return ast.Call(func=ast.Name(id=name, ctx=ast.Load(), lineno=lineno,
|
||||
|
@ -80,18 +89,22 @@ def xonsh_call(name, args, lineno=None, col=None):
|
|||
args=args, keywords=[], starargs=None, kwargs=None,
|
||||
lineno=lineno, col_offset=col)
|
||||
|
||||
|
||||
def xonsh_help(x, lineno=None, col=None):
|
||||
"""Creates the AST node for calling the __xonsh_help__() function."""
|
||||
return xonsh_call('__xonsh_help__', [x], lineno=lineno, col=col)
|
||||
|
||||
|
||||
def xonsh_superhelp(x, lineno=None, col=None):
|
||||
"""Creates the AST node for calling the __xonsh_superhelp__() function."""
|
||||
return xonsh_call('__xonsh_superhelp__', [x], lineno=lineno, col=col)
|
||||
|
||||
|
||||
def xonsh_regexpath(x, lineno=None, col=None):
|
||||
"""Creates the AST node for calling the __xonsh_regexpath__() function."""
|
||||
return xonsh_call('__xonsh_regexpath__', [x], lineno=lineno, col=col)
|
||||
|
||||
|
||||
def load_ctx(x):
|
||||
"""Recursively sets ctx to ast.Load()"""
|
||||
if not hasattr(x, 'ctx'):
|
||||
|
@ -103,6 +116,7 @@ def load_ctx(x):
|
|||
elif isinstance(x, ast.Starred):
|
||||
load_ctx(x.value)
|
||||
|
||||
|
||||
def store_ctx(x):
|
||||
"""Recursively sets ctx to ast.Store()"""
|
||||
if not hasattr(x, 'ctx'):
|
||||
|
@ -114,6 +128,7 @@ def store_ctx(x):
|
|||
elif isinstance(x, ast.Starred):
|
||||
store_ctx(x.value)
|
||||
|
||||
|
||||
def empty_list_if_newline(x):
|
||||
return [] if x == '\n' else x
|
||||
|
||||
|
@ -344,8 +359,6 @@ class Parser(object):
|
|||
# Precedence of operators
|
||||
#
|
||||
precedence = (
|
||||
#('left', 'LOGIC_OR'),
|
||||
#('left', 'LOGIC_AND'),
|
||||
('left', 'PIPE'),
|
||||
('left', 'XOR'),
|
||||
('left', 'AMPERSAND'),
|
||||
|
@ -438,10 +451,8 @@ class Parser(object):
|
|||
for a in p2[1:]:
|
||||
p0 = ast.Attribute(value=p0, attr=a, ctx=ast.Load(),
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
|
||||
p[0] = p0
|
||||
|
||||
|
||||
def p_decorator(self, p):
|
||||
"""decorator : AT attr_name NEWLINE
|
||||
| AT attr_name func_call NEWLINE
|
||||
|
@ -522,7 +533,7 @@ class Parser(object):
|
|||
p9 = p[9] if lenp > 9 else None
|
||||
p10 = p[10] if lenp > 10 else None
|
||||
p0 = ast.arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[],
|
||||
kwarg=None, defaults=[])
|
||||
kwarg=None, defaults=[])
|
||||
if lenp == 3:
|
||||
p0.kwarg = p2
|
||||
elif lenp == 4:
|
||||
|
@ -641,7 +652,7 @@ class Parser(object):
|
|||
p9 = p[9] if lenp > 9 else None
|
||||
p10 = p[10] if lenp > 10 else None
|
||||
p0 = ast.arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[],
|
||||
kwarg=None, defaults=[])
|
||||
kwarg=None, defaults=[])
|
||||
if lenp == 3:
|
||||
p0.kwarg = p2
|
||||
elif lenp == 4:
|
||||
|
@ -719,7 +730,6 @@ class Parser(object):
|
|||
p0 = [p1]
|
||||
if p2 is not None and p2 != ';':
|
||||
p0 += p2
|
||||
#self.lineno += 1 # needs to be at the end
|
||||
p[0] = p0
|
||||
|
||||
def p_small_stmt(self, p):
|
||||
|
@ -1027,7 +1037,9 @@ class Parser(object):
|
|||
names = [p2]
|
||||
if p3 is not None:
|
||||
names += p3
|
||||
p[0] = ast.Nonlocal(names=names, lineno=self.lineno, col_offset=self.col)
|
||||
p[0] = ast.Nonlocal(names=names,
|
||||
lineno=self.lineno,
|
||||
col_offset=self.col)
|
||||
|
||||
def p_comma_test(self, p):
|
||||
"""comma_test : COMMA test"""
|
||||
|
@ -1070,7 +1082,7 @@ class Parser(object):
|
|||
| IF test COLON suite elif_part_list_opt else_part
|
||||
"""
|
||||
lastif = ast.If(test=p[2], body=p[4], orelse=[],
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
p0 = [lastif]
|
||||
p5 = p[5]
|
||||
p6 = p[6] if len(p) > 6 else []
|
||||
|
@ -1709,7 +1721,9 @@ class Parser(object):
|
|||
p1 = ast.Tuple(elts=[p1], ctx=ast.Load(), lineno=self.lineno,
|
||||
col_offset=self.col)
|
||||
else:
|
||||
p1 = ensure_has_elts(p1, lineno=self.lineno, col_offset=self.col)
|
||||
p1 = ensure_has_elts(p1,
|
||||
lineno=self.lineno,
|
||||
col_offset=self.col)
|
||||
p2 = p[2] if lenp > 2 else []
|
||||
p2 = [] if p2 == ',' else p2
|
||||
p1.elts += p2
|
||||
|
@ -1764,7 +1778,7 @@ class Parser(object):
|
|||
def p_classdef(self, p):
|
||||
"""classdef : CLASS NAME func_call_opt COLON suite"""
|
||||
p3 = p[3]
|
||||
b, kw = ([], []) if p3 is None else (p3['args'], p3['keywords'])
|
||||
b, kw = ([], []) if p3 is None else (p3['args'], p3['keywords'])
|
||||
c = ast.ClassDef(name=p[2], bases=b, keywords=kw, starargs=None,
|
||||
kwargs=None, body=p[5], decorator_list=[],
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
|
@ -1882,12 +1896,6 @@ class Parser(object):
|
|||
p0['comps'] = p3.get('comps', [])
|
||||
p[0] = p0
|
||||
|
||||
#def p_encoding_decl(self, p):
|
||||
# """encoding_decl : NAME"""
|
||||
# # not used in grammar, but may appear in "node" passed from
|
||||
# # Parser to Compiler
|
||||
# p[0] = p[1]
|
||||
|
||||
def p_yield_expr(self, p):
|
||||
"""yield_expr : YIELD yield_arg_opt"""
|
||||
p2 = p[2]
|
||||
|
@ -1923,13 +1931,13 @@ class Parser(object):
|
|||
p1, p2 = p[1], p[2]
|
||||
col = self.col
|
||||
lineno = self.lineno
|
||||
if lenp == 3: # $NAME
|
||||
if lenp == 3: # $NAME
|
||||
p0 = self._envvar_by_name(p2, lineno=lineno, col=col)
|
||||
elif p1 == '${':
|
||||
xenv = self._xenv(lineno=lineno, col=col)
|
||||
idx = ast.Index(value=p2)
|
||||
p0 = ast.Subscript(value=xenv, slice=idx, ctx=ast.Load(),
|
||||
lineno=lineno, col_offset=col)
|
||||
lineno=lineno, col_offset=col)
|
||||
elif p1 == '$(':
|
||||
p0 = xonsh_call('__xonsh_subproc_captured__', p2,
|
||||
lineno=lineno, col=col)
|
||||
|
@ -1964,7 +1972,8 @@ class Parser(object):
|
|||
lineno=lineno, col=col)
|
||||
currlist.elts.append(arg)
|
||||
elif action == 'extend':
|
||||
cliargs = binop(cliargs, ast.Add(), arg, lineno=lineno, col=col)
|
||||
cliargs = binop(cliargs, ast.Add(), arg,
|
||||
lineno=lineno, col=col)
|
||||
currlist = None
|
||||
elif action == 'splitlines':
|
||||
sl = call_split_lines(arg, lineno=lineno, col=col)
|
||||
|
@ -2018,7 +2027,8 @@ class Parser(object):
|
|||
if len(p1) > 1 and hasattr(p1[-2], 's') and p1[-2].s != '|':
|
||||
msg = 'additional redirect following non-pipe redirect'
|
||||
self._parse_error(msg, self.currloc(lineno=lineno, column=col))
|
||||
p0 = p1 + [p[2], self._subproc_cliargs(p[3], lineno=lineno, col=col)]
|
||||
cliargs = self._subproc_cliargs(p[3], lineno=lineno, col=col)
|
||||
p0 = p1 + [p[2], aliargs]
|
||||
# return arguments list
|
||||
p[0] = p0
|
||||
|
||||
|
@ -2077,7 +2087,7 @@ class Parser(object):
|
|||
xenv = self._xenv(lineno=self.lineno, col=self.col)
|
||||
idx = ast.Index(value=p[2])
|
||||
p0 = ast.Subscript(value=xenv, slice=idx, ctx=ast.Load(),
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
lineno=self.lineno, col_offset=self.col)
|
||||
p0._cliarg_action = 'append'
|
||||
elif p1 == '$(':
|
||||
p0 = xonsh_call('__xonsh_subproc_captured__', args=p[2],
|
||||
|
@ -2146,5 +2156,4 @@ class Parser(object):
|
|||
else:
|
||||
msg = 'code: {0}'.format(p.value),
|
||||
self._parse_error(msg, self.currloc(lineno=p.lineno,
|
||||
column=self.lexer.token_col(p)))
|
||||
|
||||
column=self.lexer.token_col(p)))
|
||||
|
|
Loading…
Add table
Reference in a new issue