Skip to content

Commit

Permalink
Allow omitted block parameter at beginning of block only
Browse files Browse the repository at this point in the history
  • Loading branch information
dan42 committed Sep 23, 2019
1 parent c20b6b0 commit 51e5fa4
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ struct parser_params {
int max_numparam;

unsigned int command_start:1;
unsigned int chk_omitted_arg:1;
unsigned int eofp: 1;
unsigned int ruby__end__seen: 1;
unsigned int debug: 1;
Expand Down Expand Up @@ -8545,6 +8546,14 @@ parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
return result;
}

static int
dot_ahead(struct parser_params *p)
{
int spaces = 0;
while (ISSPACE(peekc_n(p,spaces))) spaces++;
return peek_n(p, '.', spaces) ? TRUE : FALSE;
}

static enum yytokentype
parse_ident(struct parser_params *p, int c, int cmd_state)
{
Expand Down Expand Up @@ -8599,9 +8608,11 @@ parse_ident(struct parser_params *p, int c, int cmd_state)
if (kw->id[0] == keyword_do) {
if (lambda_beginning_p()) {
p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
p->chk_omitted_arg = dot_ahead(p);
return keyword_do_LAMBDA;
}
if (COND_P()) return keyword_do_cond;
p->chk_omitted_arg = dot_ahead(p);
if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
return keyword_do_block;
return keyword_do;
Expand Down Expand Up @@ -9075,6 +9086,8 @@ parser_yylex(struct parser_params *p)

case '.': {
int is_beg = IS_BEG();
int chk_omitted_arg = p->chk_omitted_arg;
p->chk_omitted_arg = FALSE;
SET_LEX_STATE(EXPR_BEG);
switch (c = nextc(p)) {
case '.':
Expand Down Expand Up @@ -9115,7 +9128,7 @@ parser_yylex(struct parser_params *p)
p->lex.ptok = p->lex.pcur;
goto retry;
}
if (is_beg) {
if (is_beg && chk_omitted_arg) {
if (parser_numbered_param(p, 0)) {
pushback(p, '.');
return tNUMPARAM;
Expand Down Expand Up @@ -9287,15 +9300,20 @@ parser_yylex(struct parser_params *p)
COND_PUSH(0);
CMDARG_PUSH(0);
p->lex.paren_nest++;
p->chk_omitted_arg = dot_ahead(p);
return tLAMBEG;
}
p->lex.paren_nest++;
if (IS_lex_state(EXPR_LABELED))
c = tLBRACE; /* hash */
else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN)) {
c = '{'; /* block (primary) */
else if (IS_lex_state(EXPR_ENDARG))
p->chk_omitted_arg = dot_ahead(p);
}
else if (IS_lex_state(EXPR_ENDARG)) {
c = tLBRACE_ARG; /* block (expr) */
p->chk_omitted_arg = dot_ahead(p);
}
else
c = tLBRACE; /* hash */
COND_PUSH(0);
Expand Down

0 comments on commit 51e5fa4

Please sign in to comment.