CodeLine Lex doesn't handle trailing if and unless
Closed this issue · 2 comments
schneems commented
The assumption was that if
and unless
lines need to be paired with ends, but that's not true if it's trailing:
+ end
+ class Main
+ @user_source_code = 'self' if @user_source_code == ''
+ end
+ end
schneems commented
irb(main):001:0> require 'ripper'
=> false
irb(main):002:0> pp Ripper.lex("puts 'lol' if foo")
[[[1, 0], :on_ident, "puts", CMDARG],
[[1, 4], :on_sp, " ", CMDARG],
[[1, 5], :on_tstring_beg, "'", CMDARG],
[[1, 6], :on_tstring_content, "lol", CMDARG],
[[1, 9], :on_tstring_end, "'", END],
[[1, 10], :on_sp, " ", END],
[[1, 11], :on_kw, "if", BEG|LABEL],
[[1, 13], :on_sp, " ", BEG|LABEL],
[[1, 14], :on_ident, "foo", CMDARG]]
schneems commented
I think that we can disambiguate a trailing end if it is a "BEG|LABEL" as a non-trailing end will just be a BEG
source = <<~EOM
if bar; puts "lol"; end
EOM
pp Ripper.lex(source)
[[[1, 0], :on_kw, "if", BEG],
[[1, 2], :on_sp, " ", BEG],
[[1, 3], :on_ident, "bar", CMDARG],
[[1, 6], :on_semicolon, ";", BEG],
[[1, 7], :on_sp, " ", BEG],
[[1, 8], :on_ident, "puts", CMDARG],
[[1, 12], :on_sp, " ", CMDARG],
[[1, 13], :on_tstring_beg, "\"", CMDARG],
[[1, 14], :on_tstring_content, "lol", CMDARG],
[[1, 17], :on_tstring_end, "\"", END],
[[1, 18], :on_semicolon, ";", BEG],
[[1, 19], :on_sp, " ", BEG],
[[1, 20], :on_kw, "end", END],
[[1, 23], :on_nl, "\n", BEG]]
source = <<~EOM
foo = if bar; "lol"; end
EOM
pp Ripper.lex(source)
[[[1, 0], :on_ident, "foo", CMDARG],
[[1, 3], :on_sp, " ", CMDARG],
[[1, 4], :on_op, "=", BEG],
[[1, 5], :on_sp, " ", BEG],
[[1, 6], :on_kw, "if", BEG],
[[1, 8], :on_sp, " ", BEG],
[[1, 9], :on_ident, "bar", CMDARG],
[[1, 12], :on_semicolon, ";", BEG],
[[1, 13], :on_sp, " ", BEG],
[[1, 14], :on_tstring_beg, "\"", BEG],
[[1, 15], :on_tstring_content, "lol", BEG],
[[1, 18], :on_tstring_end, "\"", END],
[[1, 19], :on_semicolon, ";", BEG],
[[1, 20], :on_sp, " ", BEG],
[[1, 21], :on_kw, "end", END],
[[1, 24], :on_nl, "\n", BEG]]