summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2020-11-17 21:15:50 -0800
committerGitHub <noreply@github.com>2020-11-17 21:15:50 -0800
commitcd0877a93e91fecb3066984b3fa2a762e6977caf (patch)
treeadbad42573151ede2116e087447485c2ae41c753 /ext
parenta776032ef183d6bc1236b2306ada5611b4d6849f (diff)
Support raise_errors keyword for Ripper.{lex,tokenize,sexp,sexp_raw}
Implements [Feature #17276]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3774 Merged-By: jeremyevans <code@jeremyevans.net>
Diffstat (limited to 'ext')
-rw-r--r--ext/ripper/lib/ripper/lexer.rb22
-rw-r--r--ext/ripper/lib/ripper/sexp.rb29
2 files changed, 43 insertions, 8 deletions
diff --git a/ext/ripper/lib/ripper/lexer.rb b/ext/ripper/lib/ripper/lexer.rb
index 9f613c3..e7a0787 100644
--- a/ext/ripper/lib/ripper/lexer.rb
+++ b/ext/ripper/lib/ripper/lexer.rb
@@ -18,8 +18,15 @@ class Ripper
# p Ripper.tokenize("def m(a) nil end")
# # => ["def", " ", "m", "(", "a", ")", " ", "nil", " ", "end"]
#
- def Ripper.tokenize(src, filename = '-', lineno = 1)
- Lexer.new(src, filename, lineno).tokenize
+ def Ripper.tokenize(src, filename = '-', lineno = 1, raise_errors: false)
+ r = Lexer.new(src, filename, lineno)
+ ret = r.tokenize
+
+ if raise_errors && !r.errors.empty?
+ raise SyntaxError, r.errors.map(&:message).join(' ;')
+ end
+
+ ret
end
# Tokenizes the Ruby program and returns an array of an array,
@@ -41,8 +48,15 @@ class Ripper
# [[1, 12], :on_sp, " ", END ],
# [[1, 13], :on_kw, "end", END ]]
#
- def Ripper.lex(src, filename = '-', lineno = 1)
- Lexer.new(src, filename, lineno).lex
+ def Ripper.lex(src, filename = '-', lineno = 1, raise_errors: false)
+ r = Lexer.new(src, filename, lineno)
+ ret = r.lex
+
+ if raise_errors && !r.errors.empty?
+ raise SyntaxError, r.errors.map(&:message).join(' ;')
+ end
+
+ ret
end
class Lexer < ::Ripper #:nodoc: internal use only
diff --git a/ext/ripper/lib/ripper/sexp.rb b/ext/ripper/lib/ripper/sexp.rb
index e71d52c..27b86e4 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: