diff --git a/test/yarp/fixtures/newline_terminated.txt b/test/yarp/fixtures/newline_terminated.txt new file mode 100644 index 0000000000..27e7c62e8e --- /dev/null +++ b/test/yarp/fixtures/newline_terminated.txt @@ -0,0 +1,2 @@ +%r +foo diff --git a/test/yarp/snapshots/newline_terminated.txt b/test/yarp/snapshots/newline_terminated.txt new file mode 100644 index 0000000000..e68ea1658e --- /dev/null +++ b/test/yarp/snapshots/newline_terminated.txt @@ -0,0 +1,6 @@ +ProgramNode(0...7)( + [], + StatementsNode(0...7)( + [RegularExpressionNode(0...7)((0...3), (3...6), (6...7), "foo", 0)] + ) +) diff --git a/yarp/util/yp_newline_list.c b/yarp/util/yp_newline_list.c index 8b24f82a02..ad9d99d0ab 100644 --- a/yarp/util/yp_newline_list.c +++ b/yarp/util/yp_newline_list.c @@ -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 diff --git a/yarp/util/yp_newline_list.h b/yarp/util/yp_newline_list.h index 095acd5168..b7c8c1f3aa 100644 --- a/yarp/util/yp_newline_list.h +++ b/yarp/util/yp_newline_list.h @@ -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. diff --git a/yarp/yarp.c b/yarp/yarp.c index df6bde14b3..1725e108a0 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -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)); }