Change rb_ast_compile to a function that simply creates a parser and sets options

This commit is contained in:
ydah 2024-09-30 13:48:31 +09:00 committed by Yudai Takada
parent b33c1e4bb2
commit 4b02a7b794
Notes: git 2025-01-03 12:26:40 +00:00

26
ast.c
View File

@ -116,16 +116,13 @@ ast_parse_done(VALUE ast_value)
} }
static 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 vparser = ast_parse_new();
VALUE ast_value = Qnil;
if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser); if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser);
if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser); if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser); if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
ast_value = compile_func(vparser, Qnil, source, 1); return vparser;
return ast_parse_done(ast_value);
} }
static VALUE static VALUE
@ -137,8 +134,11 @@ ast_s_parse(rb_execution_context_t *ec, VALUE module, VALUE str, VALUE keep_scri
static VALUE static VALUE
rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
{ {
VALUE ast_value = Qnil;
StringValue(str); 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 static VALUE
@ -150,21 +150,27 @@ ast_s_parse_file(rb_execution_context_t *ec, VALUE module, VALUE path, VALUE kee
static VALUE static VALUE
rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens)
{ {
VALUE f;
VALUE ast_value = Qnil; VALUE ast_value = Qnil;
VALUE f = rb_file_open_str(path, "r");
rb_encoding *enc = rb_utf8_encoding(); 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("-")); 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); rb_io_close(f);
return ast_value; return ast_parse_done(ast_value);
} }
static VALUE static VALUE
rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, VALUE keep_tokens) 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); 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*); static VALUE node_children(VALUE, const NODE*);