Skip to content

Commit

Permalink
Add support for expansions to source.Range.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Aug 7, 2015
1 parent db8947c commit 6749f63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 12 additions & 3 deletions pythonparser/diagnostic.py
Expand Up @@ -91,8 +91,17 @@ def render(self, only_line=False, colored=False):
else:
location = str(self.location)

notes = list(self.notes)
if self.level != "note":
expanded_location = self.location.expanded_from
while expanded_location is not None:
notes.append(Diagnostic("note",
"expanded from here", {},
self.location.expanded_from))
expanded_location = expanded_location.expanded_from

rendered_notes = reduce(list.__add__, [note.render(only_line, colored)
for note in self.notes], [])
for note in notes], [])
if colored:
if self.level in ("error", "fatal"):
level_color = 31 # red
Expand All @@ -105,13 +114,13 @@ def render(self, only_line=False, colored=False):
format(location, level_color, self.level, self.message()),
source_line,
"\x1b[1;32m{}\x1b[0m".format(highlight_line.decode("utf-8"))
]
] + rendered_notes
else:
return [
"{}: {}: {}".format(location, self.level, self.message()),
source_line,
highlight_line.decode("utf-8")
]
] + rendered_notes


class Error(Exception):
Expand Down
12 changes: 11 additions & 1 deletion pythonparser/source.py
Expand Up @@ -72,11 +72,13 @@ class Range:
:ivar begin_pos: (integer) offset of the first character
:ivar end_pos: (integer) offset of the character before the last
:ivar expanded_from: (Range or None) the range from which this range was expanded
"""
def __init__(self, source_buffer, begin_pos, end_pos):
def __init__(self, source_buffer, begin_pos, end_pos, expanded_from=None):
self.source_buffer = source_buffer
self.begin_pos = begin_pos
self.end_pos = end_pos
self.expanded_from = expanded_from

def __repr__(self):
"""
Expand All @@ -85,6 +87,14 @@ def __repr__(self):
return "Range(\"%s\", %d, %d)" % \
(self.source_buffer.name, self.begin_pos, self.end_pos)

def chain(self, expanded_from):
"""
Returns a range identical to this one, but indicating that
it was expanded from the range `expanded_from`.
"""
return Range(self.source_buffer, self.begin_pos, self.begin_pos,
expanded_from=expanded_from)

def begin(self):
"""
Returns a zero-length range located just before the beginning of this range.
Expand Down

0 comments on commit 6749f63

Please sign in to comment.