diff options
author | ydah <[email protected]> | 2024-09-30 13:48:31 +0900 |
---|---|---|
committer | Yudai Takada <[email protected]> | 2025-01-03 21:26:21 +0900 |
commit | 4b02a7b794b6c565eae0ed3c8dfedc1b8daf4e49 (patch) | |
tree | 938870797ef39b7562fe85d1958187042ad3a3cf /ast.c | |
parent | b33c1e4bb2943779b9e7a8034cb6763ef9c25e67 (diff) |
Change `rb_ast_compile` to a function that simply creates a parser and sets options
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/11717
Diffstat (limited to 'ast.c')
-rw-r--r-- | ast.c | 26 |
1 files changed, 16 insertions, 10 deletions
@@ -116,16 +116,13 @@ ast_parse_done(VALUE ast_value) } static VALUE -rb_ast_compile(VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens, VALUE source, VALUE compile_func(VALUE, VALUE, VALUE, int)) +setup_vparser(VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) { VALUE vparser = ast_parse_new(); - VALUE ast_value = Qnil; - if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser); if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser); if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser); - ast_value = compile_func(vparser, Qnil, source, 1); - return ast_parse_done(ast_value); + return vparser; } static VALUE @@ -137,8 +134,11 @@ ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_scri static VALUE rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) { + VALUE ast_value = Qnil; StringValue(str); - return rb_ast_compile(keep_script_lines, error_tolerant, keep_tokens, str, rb_parser_compile_string_path); + VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens); + ast_value = rb_parser_compile_string_path(vparser, Qnil, str, 1); + return ast_parse_done(ast_value); } static VALUE @@ -150,21 +150,27 @@ ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE kee static VALUE rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) { + VALUE f; VALUE ast_value = Qnil; - VALUE f = rb_file_open_str(path, "r"); rb_encoding *enc = rb_utf8_encoding(); + f = rb_file_open_str(path, "r"); rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-")); - ast_value = rb_ast_compile(keep_script_lines, error_tolerant, keep_tokens, f, rb_parser_compile_file_path); + VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens); + ast_value = rb_parser_compile_file_path(vparser, Qnil, f, 1); rb_io_close(f); - return ast_value; + return ast_parse_done(ast_value); } static VALUE rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) { + VALUE ast_value = Qnil; + array = rb_check_array_type(array); - return rb_ast_compile(keep_script_lines, error_tolerant, keep_tokens, array, rb_parser_compile_array); + VALUE vparser = setup_vparser(keep_script_lines, error_tolerant, keep_tokens); + ast_value = rb_parser_compile_array(vparser, Qnil, array, 1); + return ast_parse_done(ast_value); } static VALUE node_children(VALUE, const NODE*); |