Skip to content

Commit

Permalink
Show more descriptive errors (with location tip)
Browse files Browse the repository at this point in the history
This should make errors a whole lot easier to diagnose.  Previously, no
position information was ever available.  Although tracing the code
emitted from macros isn't yet supported, this helps pinpoint stuff like
illegal identifiers.
  • Loading branch information
anko committed Sep 26, 2015
1 parent f0e72cc commit e8a3e99
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
48 changes: 44 additions & 4 deletions src/cli.ls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ spawn = (require \child_process).spawn
esl = require \./index
require! <[ fs path nopt ]>

{ InvalidAstError } = require \esvalid

print-version = ->
try
console.log (require \../package.json .version)
Expand Down Expand Up @@ -64,7 +66,38 @@ compiler-opts = {}
if parsed-options.transform
compiler-opts.transform-macros = that .map require

compile-and-show = -> console.log esl it, compiler-opts
compile-and-show = (code) ->
code .= to-string!
try
console.log esl code, compiler-opts
catch err
if err instanceof InvalidAstError
console.error "[Error]" err.message
point-at-problem code, err.node
else throw err

# Use the node's location data (if present) to show the lines on which the
# problem occurred.
point-at-problem = (input, problematic-node) ->
{ location } = problematic-node
switch typeof! location
| \String =>
stringified-node = JSON.stringify do
problematic-node
(k, v) -> if k is \location then undefined else v
console.error " #stringified-node"
console.error " [ #location ]"
| \Object =>
{ start, end } = location
line = input
.split "\n"
.slice (start.line - 1), end.line
.join "\n"
underline = " " * (start.offset - 1) +
"^" * (end.offset - start.offset)
console.error " " + line
console.error " " + underline
| _ => throw Error "Internal error: unexpected location type"

if target-path
e, esl-code <- fs.read-file target-path, encoding : \utf8
Expand All @@ -91,6 +124,13 @@ else
# NOTE: will fail on older nodejs due to paren wrapping logic; see
# SO http://stackoverflow.com/questions/19182057/node-js-repl-funny-behavior-with-custom-eval-function
# GH https://github.com/nodejs/node-v0.x-archive/commit/9ef9a9dee54a464a46739b14e8a348bec673c5a5
stateful-compiler cmd
|> vm.run-in-this-context
|> callback null, _
try
stateful-compiler cmd
|> vm.run-in-this-context
|> callback null, _
catch err
if err instanceof InvalidAstError
console.error "[Error]" err.message
point-at-problem cmd, err.node
callback null
else throw err
3 changes: 1 addition & 2 deletions src/import-macro.ls
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ compilerify-macro = (env, func) ->

errors = ast-errors sm-ast
if errors
console.error "AST error at" sm-ast
throw Error errors.0
throw errors.0

sm-ast

Expand Down
2 changes: 0 additions & 2 deletions src/translate.ls
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ module.exports = (root-env, ast, options={}) ->
err = errors program-ast
if err.length
first-error = err.0
console.error "[Error] #{first-error.message}\n\
Node: #{JSON.stringify first-error.node}"
throw first-error
else
return program-ast

0 comments on commit e8a3e99

Please sign in to comment.