Skip to content

Commit

Permalink
Respect syntax error offset location
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed May 25, 2015
1 parent 10b3340 commit 597bf15
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/compiler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions lib/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/optimiser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ cleanMarkers = (str) -> str.replace /[\uEFEF\uEFFE\uEFFF]/g, ''
((str.replace /\uEFEF/g, '(INDENT)').replace /\uEFFE/g, '(DEDENT)').replace /\uEFFF/g, '(TERM)'

@formatParserError = (input, e) ->
realColumn = (cleanMarkers "#{(input.split '\n')[e.line - 1]}\n"[...e.column]).length
lines = input.split('\n')
line = e.line - 1 # switch to zero-indexed
column = e.column
offset = e.offset
while offset > 0
if offset > lines[line].length
offset -= lines[line].length
line += 1
else
column += offset
offset = 0
realColumn = (cleanMarkers "#{lines[line]}\n"[...column]).length
line += 1 # switch back to one-indexed
unless e.found?
return "Syntax error on line #{e.line}, column #{realColumn}: unexpected end of input"
return "Syntax error on line #{line}, column #{realColumn}: unexpected end of input"
found = JSON.stringify humanReadable e.found
found = ((found.replace /^"|"$/g, '').replace /'/g, '\\\'').replace /\\"/g, '"'
unicode = ((e.found.charCodeAt 0).toString 16).toUpperCase()
unicode = "\\u#{"0000"[unicode.length..]}#{unicode}"
message = "Syntax error on line #{e.line}, column #{realColumn}: unexpected '#{found}' (#{unicode})"
"#{message}\n#{pointToErrorLocation input, e.line, realColumn}"
message = "Syntax error on line #{line}, column #{realColumn}: unexpected '#{found}' (#{unicode})"
"#{message}\n#{pointToErrorLocation input, line, realColumn}"

@pointToErrorLocation = pointToErrorLocation = (source, line, column, numLinesOfContext = 3) ->
lines = source.split '\n'
Expand Down

0 comments on commit 597bf15

Please sign in to comment.