diff --git a/lib/yarp.rb b/lib/yarp.rb index d74ad47db32ca3..3ad5cc47810091 100644 --- a/lib/yarp.rb +++ b/lib/yarp.rb @@ -143,13 +143,14 @@ def deconstruct_keys(keys) # the AST, any comments that were encounters, and any errors that were # encountered. class ParseResult - attr_reader :value, :comments, :errors, :warnings + attr_reader :value, :comments, :errors, :warnings, :source - def initialize(value, comments, errors, warnings) + def initialize(value, comments, errors, warnings, source) @value = value @comments = comments @errors = errors @warnings = warnings + @source = source end def deconstruct_keys(keys) @@ -224,6 +225,10 @@ def pretty_print(q) def self.load(source, serialized) Serialize.load(source, serialized) end + + def self.newlines(source) + YARP.parse(source).source.offsets + end end require_relative "yarp/lex_compat" diff --git a/lib/yarp/lex_compat.rb b/lib/yarp/lex_compat.rb index a455d9c6fa4a6a..053b012270d56a 100644 --- a/lib/yarp/lex_compat.rb +++ b/lib/yarp/lex_compat.rb @@ -700,7 +700,7 @@ def result raise StandardError, "Lost tokens when performing lex_compat" end - ParseResult.new(tokens, result.comments, result.errors, result.warnings) + ParseResult.new(tokens, result.comments, result.errors, result.warnings, []) end end diff --git a/yarp/extension.c b/yarp/extension.c index f0d22c7f8cb2d0..034be79e4dc2cc 100644 --- a/yarp/extension.c +++ b/yarp/extension.c @@ -391,10 +391,11 @@ lex_input(input_t *input, const char *filepath) { lex_data.tokens, parser_comments(&parser, source), parser_errors(&parser, lex_data.encoding, source), - parser_warnings(&parser, lex_data.encoding, source) + parser_warnings(&parser, lex_data.encoding, source), + source }; - VALUE result = rb_class_new_instance(4, result_argv, rb_cYARPParseResult); + VALUE result = rb_class_new_instance(5, result_argv, rb_cYARPParseResult); yp_node_destroy(&parser, node); yp_parser_free(&parser); @@ -446,10 +447,11 @@ parse_input(input_t *input, const char *filepath) { yp_ast_new(&parser, node, encoding), parser_comments(&parser, source), parser_errors(&parser, encoding, source), - parser_warnings(&parser, encoding, source) + parser_warnings(&parser, encoding, source), + source }; - VALUE result = rb_class_new_instance(4, result_argv, rb_cYARPParseResult); + VALUE result = rb_class_new_instance(5, result_argv, rb_cYARPParseResult); yp_node_destroy(&parser, node); yp_parser_free(&parser);