Rubyは標準ライブラリにRuby lexer/parserがあるのがいい

これは特筆すべき特徴だと思う。ヒアドキュメントもちゃんと処理できる。

require "ripper"
require "pp"

src = <<'END'
def foo
  p [<<-"A", <<-"B"]
    Hello, %{"Ruby"} world!
  A
    one, two three
  B
end
END

pp Ripper.lex(src)

結果:

[[[1, 0], :on_kw, "def"],
 [[1, 3], :on_sp, " "],
 [[1, 4], :on_ident, "foo"],
 [[1, 7], :on_nl, "\n"],
 [[2, 0], :on_sp, "  "],
 [[2, 2], :on_ident, "p"],
 [[2, 3], :on_sp, " "],
 [[2, 4], :on_lbracket, "["],
 [[2, 5], :on_heredoc_beg, "<<-\"A\""],
 [[2, 11], :on_comma, ","],
 [[2, 12], :on_sp, " "],
 [[2, 13], :on_heredoc_beg, "<<-\"B\""],
 [[2, 19], :on_rbracket, "]"],
 [[2, 20], :on_nl, "\n"],
 [[3, 0], :on_tstring_content, "    Hello, %{\"Ruby\"} world!\n"],
 [[4, 0], :on_heredoc_end, "  A\n"],
 [[5, 0], :on_tstring_content, "    one, two three\n"],
 [[6, 0], :on_heredoc_end, "  B\n"],
 [[7, 0], :on_kw, "end"],
 [[7, 3], :on_nl, "\n"]]

しかし、せっかくこのようなすばらしいツールがあるのに 品質の高い tidy や lint はないようだ。惜しいことである。