Skip to content

Commit

Permalink
Merge pull request #2986 from ruby/fix-up-parse-result-constants
Browse files Browse the repository at this point in the history
Fix up lex result constants
  • Loading branch information
kddnewton authored Aug 15, 2024
2 parents 0c0a730 + 084baca commit 44bbdbf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ VALUE rb_cPrismParseError;
VALUE rb_cPrismParseWarning;
VALUE rb_cPrismResult;
VALUE rb_cPrismParseResult;
VALUE rb_cPrismLexResult;
VALUE rb_cPrismParseLexResult;

VALUE rb_cPrismDebugEncoding;
Expand Down Expand Up @@ -635,16 +636,16 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
rb_ary_push(offsets, ULONG2NUM(parser.newline_list.offsets[index]));
}

VALUE value;
VALUE result;
if (return_nodes) {
value = rb_ary_new_capa(2);
VALUE value = rb_ary_new_capa(2);
rb_ary_push(value, pm_ast_new(&parser, node, parse_lex_data.encoding, source));
rb_ary_push(value, parse_lex_data.tokens);
result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source);
} else {
value = parse_lex_data.tokens;
result = parse_result_create(rb_cPrismLexResult, &parser, parse_lex_data.tokens, parse_lex_data.encoding, source);
}

VALUE result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source);
pm_node_destroy(&parser, node);
pm_parser_free(&parser);

Expand All @@ -653,10 +654,10 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod

/**
* call-seq:
* Prism::lex(source, **options) -> ParseLexResult
* Prism::lex(source, **options) -> LexResult
*
* Return a ParseLexResult instance that contains an array of Token instances corresponding to the given string. For
* supported options, see Prism::parse.
* Return a LexResult instance that contains an array of Token instances
* corresponding to the given string. For supported options, see Prism::parse.
*/
static VALUE
lex(int argc, VALUE *argv, VALUE self) {
Expand All @@ -673,10 +674,10 @@ lex(int argc, VALUE *argv, VALUE self) {

/**
* call-seq:
* Prism::lex_file(filepath, **options) -> ParseLexResult
* Prism::lex_file(filepath, **options) -> LexResult
*
* Return a ParseLexResult instance that contains an array of Token instances corresponding to the given file. For
* supported options, see Prism::parse.
* Return a LexResult instance that contains an array of Token instances
* corresponding to the given file. For supported options, see Prism::parse.
*/
static VALUE
lex_file(int argc, VALUE *argv, VALUE self) {
Expand Down Expand Up @@ -1131,6 +1132,7 @@ Init_prism(void) {
rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject);
rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject);
rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult);
rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult);
rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult);

// Intern all of the IDs eagerly that we support so that we don't have to do
Expand Down
23 changes: 23 additions & 0 deletions test/prism/api/lex_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative "../test_helper"

module Prism
class LexTest < TestCase
def test_lex_result
result = Prism.lex("")
assert_kind_of LexResult, result

result = Prism.lex_file(__FILE__)
assert_kind_of LexResult, result
end

def test_parse_lex_result
result = Prism.parse_lex("")
assert_kind_of ParseLexResult, result

result = Prism.parse_lex_file(__FILE__)
assert_kind_of ParseLexResult, result
end
end
end
8 changes: 8 additions & 0 deletions test/prism/api/parse_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

module Prism
class ParseTest < TestCase
def test_parse_result
result = Prism.parse("")
assert_kind_of ParseResult, result

result = Prism.parse_file(__FILE__)
assert_kind_of ParseResult, result
end

def test_parse_empty_string
result = Prism.parse("")
assert_equal [], result.value.statements.body
Expand Down

0 comments on commit 44bbdbf

Please sign in to comment.