summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/prism.rb16
-rw-r--r--lib/prism/ffi.rb15
2 files changed, 30 insertions, 1 deletions
diff --git a/lib/prism.rb b/lib/prism.rb
index 909b71d66d..b9e615df6c 100644
--- a/lib/prism.rb
+++ b/lib/prism.rb
@@ -64,6 +64,22 @@ module Prism
def self.load(source, serialized)
Serialize.load(source, serialized)
end
+
+ # :call-seq:
+ # Prism::parse_failure?(source, **options) -> bool
+ #
+ # Returns true if the source is invalid Ruby code.
+ def self.parse_failure?(source, **options)
+ !parse_success?(source, **options)
+ end
+
+ # :call-seq:
+ # Prism::parse_file_failure?(filepath, **options) -> bool
+ #
+ # Returns true if the file at filepath is invalid Ruby code.
+ def self.parse_file_failure?(filepath, **options)
+ !parse_file_success?(filepath, **options)
+ end
end
require_relative "prism/node"
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb
index 36f1c398de..8324f722a7 100644
--- a/lib/prism/ffi.rb
+++ b/lib/prism/ffi.rb
@@ -72,7 +72,8 @@ module Prism
"pm_serialize_parse",
"pm_serialize_parse_comments",
"pm_serialize_lex",
- "pm_serialize_parse_lex"
+ "pm_serialize_parse_lex",
+ "pm_parse_success_p"
)
load_exported_functions_from(
@@ -268,6 +269,18 @@ module Prism
end
end
+ # Mirror the Prism.parse_success? API by using the serialization API.
+ def parse_success?(code, **options)
+ LibRubyParser.pm_parse_success_p(code, code.bytesize, dump_options(options))
+ end
+
+ # Mirror the Prism.parse_file_success? API by using the serialization API.
+ def parse_file_success?(filepath, **options)
+ LibRubyParser::PrismString.with(filepath) do |string|
+ parse_success?(string.read, **options, filepath: filepath)
+ end
+ end
+
private
# Convert the given options into a serialized options string.