diff --git a/yarp/yarp.c b/yarp/yarp.c index 5957182d78aa6c..b725c051a3a42f 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -12900,6 +12900,40 @@ yp_parse_serialize(const char *source, size_t size, yp_buffer_t *buffer) { yp_parser_free(&parser); } +static yp_code_position_t +yp_code_position(yp_newline_list_t newline_list, size_t loc) { + int line = 0; + while (line < (int)newline_list.size && newline_list.offsets[line] < loc) { + line++; + } + + if (line > 0) { + line--; + } + + int column = (int) (loc - newline_list.offsets[line]); + + yp_code_position_t position; + position.lineno = line; + position.column = column; + + return position; +} + +yp_code_location_t +yp_location_for_node(yp_parser_t *parser, yp_node_t *node) { + yp_newline_list_t newline_list = parser->newline_list; + + yp_code_position_t start = yp_code_position(newline_list, (size_t)(node->location.start - parser->start)); + yp_code_position_t end = yp_code_position(newline_list, (size_t)(node->location.end - parser->start)); + + yp_code_location_t location; + location.beg_pos = start; + location.end_pos = end; + + return location; +} + #undef YP_CASE_KEYWORD #undef YP_CASE_OPERATOR #undef YP_CASE_WRITABLE diff --git a/yarp/yarp.h b/yarp/yarp.h index 4bbffdbb1060d6..a41182ae12c7a3 100644 --- a/yarp/yarp.h +++ b/yarp/yarp.h @@ -66,4 +66,23 @@ YP_EXPORTED_FUNCTION void yp_parse_serialize(const char *source, size_t size, yp // Returns a string representation of the given token type. YP_EXPORTED_FUNCTION const char * yp_token_type_to_str(yp_token_type_t token_type); +// This reprsents a line, column position in the parsed source code +// The layouts of this struct should not change because they mirror +// rb_code_position_struct in ruby/ruby which allows us to type pun +typedef struct yp_code_position_struct { + int lineno; + int column; +} yp_code_position_t; + +// This reprsents a begin and end of anything in the AST that has a +// location from the parsed source code. +// The layouts of this struct should not change because they mirror +// rb_code_position_struct in ruby/ruby which allows us to type pun +typedef struct yp_code_location_struct { + yp_code_position_t beg_pos; + yp_code_position_t end_pos; +} yp_code_location_t; + +yp_code_location_t yp_location_for_node(yp_parser_t *parser, yp_node_t *node); + #endif