Skip to content

Commit

Permalink
Improves import formatting, error raising
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Jan 29, 2016
1 parent cd47167 commit 886d3fa
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions coconut/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,12 +1762,15 @@ def classlist_repl(self, original, location, tokens):
else:
raise CoconutException("invalid classlist tokens", tokens)

def import_repl(self, tokens):
def import_repl(self, original, location, tokens):
"""Universalizes imports."""
if len(tokens) == 1:
imp_from, imports = None, tokens[0]
elif len(tokens) == 2:
imp_from, imports = tokens
if imp_from == "__future__":
raise CoconutSyntaxError("illegal __future__ statement (Coconut does these automatically)",
original, location, self.adjust(lineno(location, original)))
else:
raise CoconutException("invalid import tokens", tokens)
importmap = []
Expand Down Expand Up @@ -1804,10 +1807,15 @@ def import_repl(self, tokens):
for paths, impas in importmap:
if len(paths) == 1:
import_as_index, more_stmts = gen_imports(import_as_index, paths[0], impas)
if stmts and stmts[-1] == closeindent:
more_stmts[0] = stmts.pop() + more_stmts[0]
stmts.extend(more_stmts)
else:
first, second = paths
stmts.append("try:")
if stmts and stmts[-1] == closeindent:
stmts[-1] += "try:"
else:
stmts.append("try:")
import_as_index, more_stmts = gen_imports(import_as_index, first, impas)
more_stmts[0] = openindent + more_stmts[0]
stmts.extend(more_stmts)
Expand Down

0 comments on commit 886d3fa

Please sign in to comment.