Skip to content

Commit 1cfce46

Browse files
committed
Handle pound terminator in isolation
1 parent 6951453 commit 1cfce46

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/yarp.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6672,10 +6672,13 @@ parser_lex(yp_parser_t *parser) {
66726672
LEX(YP_TOKEN_EOF);
66736673
}
66746674

6675+
// Get a reference to the current mode.
6676+
yp_lex_mode_t *mode = parser->lex_modes.current;
6677+
66756678
// These are the places where we need to split up the content of the
66766679
// regular expression. We'll use strpbrk to find the first of these
66776680
// characters.
6678-
const char *breakpoints = parser->lex_modes.current->as.regexp.breakpoints;
6681+
const char *breakpoints = mode->as.regexp.breakpoints;
66796682
const char *breakpoint = yp_strpbrk(parser, parser->current.end, breakpoints, parser->end - parser->current.end);
66806683

66816684
while (breakpoint != NULL) {
@@ -6700,14 +6703,15 @@ parser_lex(yp_parser_t *parser) {
67006703
break;
67016704
}
67026705
case '#': {
6703-
yp_token_type_t type = lex_interpolation(parser, breakpoint);
6704-
if (type != YP_TOKEN_NOT_PROVIDED) {
6705-
LEX(type);
6706-
}
6706+
// If the terminator is #, then we need to fall into the
6707+
// default case. Otherwise we'll attempt to lex
6708+
// interpolation.
6709+
if (mode->as.regexp.terminator != '#') {
6710+
yp_token_type_t type = lex_interpolation(parser, breakpoint);
6711+
if (type != YP_TOKEN_NOT_PROVIDED) {
6712+
LEX(type);
6713+
}
67076714

6708-
// We need to check if the terminator was # before skipping over
6709-
// to the next breakpoint
6710-
if (parser->lex_modes.current->as.regexp.terminator != '#') {
67116715
// If we haven't returned at this point then we had something
67126716
// that looked like an interpolated class or instance variable
67136717
// like "#@" but wasn't actually. In this case we'll just skip
@@ -6718,11 +6722,11 @@ parser_lex(yp_parser_t *parser) {
67186722
}
67196723
/* fallthrough */
67206724
default: {
6721-
if (*breakpoint == parser->lex_modes.current->as.regexp.incrementor) {
6725+
if (*breakpoint == mode->as.regexp.incrementor) {
67226726
// If we've hit the incrementor, then we need to skip past it and
67236727
// find the next breakpoint.
67246728
breakpoint = yp_strpbrk(parser, breakpoint + 1, breakpoints, parser->end - (breakpoint + 1));
6725-
parser->lex_modes.current->as.regexp.nesting++;
6729+
mode->as.regexp.nesting++;
67266730
break;
67276731
}
67286732

@@ -6731,7 +6735,7 @@ parser_lex(yp_parser_t *parser) {
67316735
// that in the list of newlines.
67326736
yp_newline_list_append(&parser->newline_list, breakpoint);
67336737

6734-
if (parser->lex_modes.current->as.regexp.terminator != '\n') {
6738+
if (mode->as.regexp.terminator != '\n') {
67356739
// If the terminator is not a newline, then we
67366740
// can set the next breakpoint and continue.
67376741
breakpoint = yp_strpbrk(parser, breakpoint + 1, breakpoints, parser->end - (breakpoint + 1));
@@ -6742,11 +6746,11 @@ parser_lex(yp_parser_t *parser) {
67426746
// terminator so we need to continue on.
67436747
}
67446748

6745-
assert(*breakpoint == parser->lex_modes.current->as.regexp.terminator);
6749+
assert(*breakpoint == mode->as.regexp.terminator);
67466750

6747-
if (parser->lex_modes.current->as.regexp.nesting > 0) {
6751+
if (mode->as.regexp.nesting > 0) {
67486752
breakpoint = yp_strpbrk(parser, breakpoint + 1, breakpoints, parser->end - (breakpoint + 1));
6749-
parser->lex_modes.current->as.regexp.nesting--;
6753+
mode->as.regexp.nesting--;
67506754
break;
67516755
}
67526756

0 commit comments

Comments
 (0)