Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f0ddee7cb9e3
Choose a base ref
...
head repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 18a5e590665d
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on May 8, 2015

  1. Copy the full SHA
    60c0019 View commit details
  2. Copy the full SHA
    18a5e59 View commit details
Showing with 35 additions and 22 deletions.
  1. +5 −2 pyparser/__init__.py
  2. +30 −20 pyparser/parser.py
7 changes: 5 additions & 2 deletions pyparser/__init__.py
Original file line number Diff line number Diff line change
@@ -23,9 +23,12 @@ def parse(source, filename='<unknown>', mode='exec',
if the source code is not well-formed
"""
buffer = pyparser.source.Buffer(source, filename)
lexer = pyparser.lexer.Lexer(buffer, version)
parser = pyparser.parser.Parser(lexer)

lexer = pyparser.lexer.Lexer(buffer, version)
if mode in ('single', 'eval'):
lexer.interactive = True

parser = pyparser.parser.Parser(lexer)
parser.add_flags(flags)

if mode == 'exec':
50 changes: 30 additions & 20 deletions pyparser/parser.py
Original file line number Diff line number Diff line change
@@ -360,6 +360,9 @@ def _save(self):
return self._index

def _restore(self, data, rule):
if self._index == data:
return

self._index = data
self._token = self._tokens[self._index]

@@ -1341,27 +1344,34 @@ def arglist(self, pre_args, rest):
call.args.append(arg)
return call

@action(Seq(Rule('test'), Loc('='), Rule('test')))
def argument_1(self, lhs, equals_loc, rhs):
if not isinstance(lhs, ast.Name):
error = diagnostic.Diagnostic(
"error", "keyword must be an identifier", {}, lhs.loc)
raise diagnostic.DiagnosticException(error)
return ast.keyword(arg=lhs.id, value=rhs,
loc=lhs.loc.join(rhs.loc),
arg_loc=lhs.loc, equals_loc=equals_loc)

@action(Seq(Rule('test'), Opt(Rule('gen_for'))))
def argument_2(self, lhs, compose_opt):
if compose_opt:
generators = compose_opt([])
return ast.GeneratorExp(elt=lhs, generators=generators,
begin_loc=None, end_loc=None,
loc=lhs.loc.join(generators[-1].loc))
return lhs
@action(Seq(Loc('='), Rule('test')))
def argument_1(self, equals_loc, rhs):
def thunk(lhs):
if not isinstance(lhs, ast.Name):
error = diagnostic.Diagnostic(
"error", "keyword must be an identifier", {}, lhs.loc)
raise diagnostic.DiagnosticException(error)
return ast.keyword(arg=lhs.id, value=rhs,
loc=lhs.loc.join(rhs.loc),
arg_loc=lhs.loc, equals_loc=equals_loc)
return thunk

@action(Opt(Rule('gen_for')))
def argument_2(self, compose_opt):
def thunk(lhs):
if compose_opt:
generators = compose_opt([])
return ast.GeneratorExp(elt=lhs, generators=generators,
begin_loc=None, end_loc=None,
loc=lhs.loc.join(generators[-1].loc))
return lhs
return thunk

argument = Alt(argument_1, argument_2)
"""argument: test [gen_for] | test '=' test # Really [keyword '='] test"""
@action(Seq(Rule('test'), Alt(argument_1, argument_2)))
def argument(self, lhs, thunk):
# This rule is reformulated to avoid exponential backtracking.
"""argument: test [gen_for] | test '=' test # Really [keyword '='] test"""
return thunk(lhs)

list_iter = Alt(Rule("list_for"), Rule("list_if"))
"""list_iter: list_for | list_if"""