[ruby/yarp] prefactor: extract yp_newline_list_check_append

https://github.com/ruby/yarp/commit/149c74291b
This commit is contained in:
Mike Dalessio 2023-08-19 14:25:54 -04:00 committed by Jemma Issroff
parent 67cd60ed97
commit dcc8afe9a4
5 changed files with 27 additions and 21 deletions

View File

@ -0,0 +1,2 @@
%r
foo

View File

@ -0,0 +1,6 @@
ProgramNode(0...7)(
[],
StatementsNode(0...7)(
[RegularExpressionNode(0...7)((0...3), (3...6), (6...7), "foo", 0)]
)
)

View File

@ -38,6 +38,15 @@ yp_newline_list_append(yp_newline_list_t *list, const char *cursor) {
return true;
}
// Conditionally append a new offset to the newline list, if the value passed in is a newline.
bool
yp_newline_list_check_append(yp_newline_list_t *list, const char *cursor) {
if (*cursor != '\n') {
return true;
}
return yp_newline_list_append(list, cursor);
}
// Returns the line and column of the given offset, assuming we don't have any
// information about the previous index that we found.
static yp_line_column_t

View File

@ -47,6 +47,9 @@ bool yp_newline_list_init(yp_newline_list_t *list, const char *start, size_t cap
// the offsets succeeds (if one was necessary), otherwise returns false.
bool yp_newline_list_append(yp_newline_list_t *list, const char *cursor);
// Conditionally append a new offset to the newline list, if the value passed in is a newline.
bool yp_newline_list_check_append(yp_newline_list_t *list, const char *cursor);
// Returns the line and column of the given offset. If the offset is not in the
// list, the line and column of the closest offset less than the given offset
// are returned.

View File

@ -6183,9 +6183,7 @@ parser_lex(yp_parser_t *parser) {
parser->current.end++;
}
if (*parser->current.end == '\n') {
yp_newline_list_append(&parser->newline_list, parser->current.end);
}
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
parser->current.end++;
LEX(YP_TOKEN_STRING_BEGIN);
@ -6215,9 +6213,7 @@ parser_lex(yp_parser_t *parser) {
if (parser->current.end < parser->end) {
lex_mode_push_regexp(parser, lex_mode_incrementor(*parser->current.end), lex_mode_terminator(*parser->current.end));
if (parser->current.end == '\n') {
yp_newline_list_append(&parser->newline_list, parser->current.end);
}
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
parser->current.end++;
}
@ -6465,9 +6461,7 @@ parser_lex(yp_parser_t *parser) {
// If the result is an escaped newline, then we need to
// track that newline.
if (breakpoint[difference - 1] == '\n') {
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
}
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
continue;
@ -6580,9 +6574,7 @@ parser_lex(yp_parser_t *parser) {
// If the result is an escaped newline, then we need to
// track that newline.
if (breakpoint[difference - 1] == '\n') {
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
}
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
continue;
@ -6673,9 +6665,7 @@ parser_lex(yp_parser_t *parser) {
parser->current.end = breakpoint + 2;
yp_newline_list_append(&parser->newline_list, breakpoint + 1);
} else {
if (*parser->current.end == '\n') {
yp_newline_list_append(&parser->newline_list, parser->current.end);
}
yp_newline_list_check_append(&parser->newline_list, parser->current.end);
parser->current.end = breakpoint + 1;
}
@ -6725,9 +6715,7 @@ parser_lex(yp_parser_t *parser) {
// If the result is an escaped newline, then we need to
// track that newline.
if (breakpoint[difference - 1] == '\n') {
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
}
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
break;
@ -6898,9 +6886,7 @@ parser_lex(yp_parser_t *parser) {
yp_unescape_type_t unescape_type = (quote == YP_HEREDOC_QUOTE_SINGLE) ? YP_UNESCAPE_MINIMAL : YP_UNESCAPE_ALL;
size_t difference = yp_unescape_calculate_difference(breakpoint, parser->end, unescape_type, false, &parser->error_list);
if (breakpoint[difference - 1] == '\n') {
yp_newline_list_append(&parser->newline_list, breakpoint + difference - 1);
}
yp_newline_list_check_append(&parser->newline_list, breakpoint + difference - 1);
breakpoint = yp_strpbrk(parser, breakpoint + difference, breakpoints, parser->end - (breakpoint + difference));
}