summaryrefslogtreecommitdiff
path: root/ext/ripper/lib/ripper/sexp.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ripper/lib/ripper/sexp.rb')
-rw-r--r--ext/ripper/lib/ripper/sexp.rb29
1 files changed, 25 insertions, 4 deletions
diff --git a/ext/ripper/lib/ripper/sexp.rb b/ext/ripper/lib/ripper/sexp.rb
index e71d52cd45e..27b86e4af0d 100644
--- a/ext/ripper/lib/ripper/sexp.rb
+++ b/ext/ripper/lib/ripper/sexp.rb
@@ -28,10 +28,16 @@ class Ripper
# [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil]],
# [:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]
#
- def Ripper.sexp(src, filename = '-', lineno = 1)
+ def Ripper.sexp(src, filename = '-', lineno = 1, raise_errors: false)
builder = SexpBuilderPP.new(src, filename, lineno)
sexp = builder.parse
- sexp unless builder.error?
+ if builder.error?
+ if raise_errors
+ raise SyntaxError, builder.error
+ end
+ else
+ sexp
+ end
end
# [EXPERIMENTAL]
@@ -54,13 +60,21 @@ class Ripper
# nil,
# nil]]]]
#
- def Ripper.sexp_raw(src, filename = '-', lineno = 1)
+ def Ripper.sexp_raw(src, filename = '-', lineno = 1, raise_errors: false)
builder = SexpBuilder.new(src, filename, lineno)
sexp = builder.parse
- sexp unless builder.error?
+ if builder.error?
+ if raise_errors
+ raise SyntaxError, builder.error
+ end
+ else
+ sexp
+ end
end
class SexpBuilder < ::Ripper #:nodoc:
+ attr_reader :error
+
private
def dedent_element(e, width)
@@ -107,6 +121,13 @@ class Ripper
end
End
end
+
+ def on_error(mesg)
+ @error = mesg
+ end
+ remove_method :on_parse_error
+ alias on_parse_error on_error
+ alias compile_error on_error
end
class SexpBuilderPP < SexpBuilder #:nodoc: