Permalink
Browse files

Print the source location for an unexpected return at the top level.

Factor out PrintFilenameAndLine.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 24, 2017
1 parent 4130307 commit 9269eb06940f0f75645f787114d7028e82261dd9
Showing with 37 additions and 31 deletions.
  1. +3 −3 core/cmd_exec.py
  2. +27 −28 core/ui.py
  3. +7 −0 gold/return-top-level.sh
View
@@ -984,12 +984,12 @@ def Execute(self, node, fork_external=True):
try:
status = self._Execute(node, fork_external=fork_external)
except _ControlFlow as e:
# TODO: pretty print error with e.token
# NOTE: in bash return is a warning. Maybe have a sane-* option?
ui.PrintFilenameAndLine(e.token.span_id, self.arena)
log('osh failed: Unexpected %r at top level' % e.token.val)
status = 1
except util.FatalRuntimeError as e:
# TODO:
ui.PrettyPrintError(e, self.arena, sys.stderr)
ui.PrettyPrintError(e, self.arena)
print('osh failed: %s' % e.UserErrorString(), file=sys.stderr)
status = e.exit_status if e.exit_status is not None else 1
View
@@ -90,7 +90,26 @@ def MakeStatusLines():
return [StatusLine(row_num=i) for i in range(3, 10)]
def PrettyPrintError(parse_error, arena, f):
def PrintFilenameAndLine(span_id, arena, f=sys.stderr):
line_span = arena.GetLineSpan(span_id)
line_id = line_span.line_id
line = arena.GetLine(line_id)
path, line_num = arena.GetDebugInfo(line_id)
col = line_span.col
length = line_span.length
print('Line %d of %r' % (line_num+1, path), file=f)
print(' ' + line.rstrip(), file=f)
f.write(' ')
# preserve tabs
for c in line[:col]:
f.write('\t' if c == '\t' else ' ')
f.write('^')
f.write('~' * (length-1))
f.write('\n')
def PrettyPrintError(parse_error, arena, f=sys.stderr):
#print(parse_error)
if parse_error.span_id != const.NO_INTEGER:
span_id = parse_error.span_id
@@ -101,34 +120,14 @@ def PrettyPrintError(parse_error, arena, f):
elif parse_error.word:
span_id = word.LeftMostSpanForWord(parse_error.word)
else:
span_id = const.NO_INTEGER # invalid
if span_id == const.NO_INTEGER:
line = '<no position info for token>'
path = '<unknown>'
line_num = -1
col = -1
length = -1
else:
line_span = arena.GetLineSpan(span_id)
line_id = line_span.line_id
line = arena.GetLine(line_id)
path, line_num = arena.GetDebugInfo(line_id)
col = line_span.col
length = line_span.length
span_id = const.NO_INTEGER
print('Line %d of %r' % (line_num+1, path), file=f)
print(' ' + line.rstrip(), file=f)
if col != -1:
f.write(' ')
# preserve tabs
for c in line[:col]:
f.write('\t' if c == '\t' else ' ')
f.write('^')
f.write('~' * (length-1))
f.write('\n')
#print(error_stack, file=f)
if span_id == const.NO_INTEGER: # Any clause above might return this.
# This is usually a bug.
print('*** Error has no source location info ***', file=f)
return
PrintFilenameAndLine(span_id, arena, f=f)
def PrintErrorStack(error_stack, arena, f):
View
@@ -0,0 +1,7 @@
#!/bin/bash
echo hi
return # This is like exit?
# Bash gets here.
echo should not get here

0 comments on commit 9269eb0

Please sign in to comment.