Skip to content

Conversation

@schneems
Copy link
Collaborator

When there is an "endless" method definition, we don't want to count it as having a keyword, otherwise when we try to expand it will search for a matching end an possibly introduce a problem in the search.

To detect this case I use the same logic introduced in ruby/irb@826ae90

For reference here are lex of a regular method definition (first line only) and then an "endless" definition:

irb(main):015:0> Ripper.lex("def square(x)")
=>
[[[1, 0], :on_kw, "def", FNAME],
 [[1, 3], :on_sp, " ", FNAME],
 [[1, 4], :on_ident, "square", ENDFN],
 [[1, 10], :on_lparen, "(", BEG|LABEL],
 [[1, 11], :on_ident, "x", ARG],
 [[1, 12], :on_rparen, ")", ENDFN]]
irb(main):016:0> Ripper.lex("def square(x) = x * x")
=>
[[[1, 0], :on_kw, "def", FNAME],
 [[1, 3], :on_sp, " ", FNAME],
 [[1, 4], :on_ident, "square", ENDFN],
 [[1, 10], :on_lparen, "(", BEG|LABEL],
 [[1, 11], :on_ident, "x", ARG],
 [[1, 12], :on_rparen, ")", ENDFN],
 [[1, 13], :on_sp, " ", BEG],
 [[1, 14], :on_op, "=", BEG],
 [[1, 15], :on_sp, " ", BEG],
 [[1, 16], :on_ident, "x", END|LABEL],
 [[1, 17], :on_sp, " ", END|LABEL],
 [[1, 18], :on_op, "*", BEG],
 [[1, 19], :on_sp, " ", BEG],
 [[1, 20], :on_ident, "x", END|LABEL]]

The detection uses a state machine. It knows when it sees an ENDFN state that a function is being defined, but it's not known if it's a "regular" function or not. If the ENDFN is followed by a BEG with a token of = and then a END then it's assumed to be a full "oneliner" AKA "endless" method definition.

@schneems schneems force-pushed the schneems/endless-method branch from 465b214 to 119157e Compare October 11, 2021 19:55
When there is an "endless" method definition, we don't want to count it as having a keyword, otherwise when we try to expand it will search for a matching `end` an possibly introduce a problem in the search.

To detect this case I use the same logic introduced in ruby/irb@826ae90

For reference here are lex of a regular method definition (first line only) and then an "endless" definition:

```
irb(main):015:0> Ripper.lex("def square(x)")
=>
[[[1, 0], :on_kw, "def", FNAME],
 [[1, 3], :on_sp, " ", FNAME],
 [[1, 4], :on_ident, "square", ENDFN],
 [[1, 10], :on_lparen, "(", BEG|LABEL],
 [[1, 11], :on_ident, "x", ARG],
 [[1, 12], :on_rparen, ")", ENDFN]]
irb(main):016:0> Ripper.lex("def square(x) = x * x")
=>
[[[1, 0], :on_kw, "def", FNAME],
 [[1, 3], :on_sp, " ", FNAME],
 [[1, 4], :on_ident, "square", ENDFN],
 [[1, 10], :on_lparen, "(", BEG|LABEL],
 [[1, 11], :on_ident, "x", ARG],
 [[1, 12], :on_rparen, ")", ENDFN],
 [[1, 13], :on_sp, " ", BEG],
 [[1, 14], :on_op, "=", BEG],
 [[1, 15], :on_sp, " ", BEG],
 [[1, 16], :on_ident, "x", END|LABEL],
 [[1, 17], :on_sp, " ", END|LABEL],
 [[1, 18], :on_op, "*", BEG],
 [[1, 19], :on_sp, " ", BEG],
 [[1, 20], :on_ident, "x", END|LABEL]]
```

The detection uses a state machine. It knows when it sees an `ENDFN` state that a function is being defined, but it's not known if it's a "regular" function or not. If the `ENDFN` is followed by a `BEG` with a token of `=` and then a `END` then it's assumed to be a full "oneliner" AKA "endless" method definition.
@schneems schneems force-pushed the schneems/endless-method branch from 119157e to daac496 Compare October 11, 2021 19:56
@schneems schneems merged commit 61de689 into main Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants