Skip to content

Commit

Permalink
[ruby/yarp] Add source to ParseResult
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmaissroff authored and matzbot committed Jul 7, 2023
1 parent 61efa9c commit 31f83a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/yarp.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/yarp/lex_compat.rb
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions yarp/extension.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 31f83a6

Please sign in to comment.