[ruby-list:49822] ripperのcompile_error呼び出しの挙動について
From:
Misao X <mail@...>
Date:
2014-05-15 23:30:55 UTC
List:
ruby-list #49822
はじめて質問させていただきます。
ぺけみさおと申します。
標準添付ライブラリのripperの挙動について質問させて下さい。
ripperはRipper.parseでコンパイルエラーが発生すると
compile_errorを呼び出すことがリファレンスに書かれています。
http://docs.ruby-lang.org/ja/1.9.3/class/Ripper.html
しかし、例えば以下のように明らかに不正なコードをパースしても
compile_errorが呼ばれないのは仕様なのでしょうか?
いわゆるsyntax errorに相当するソースコードの不正を
ripperで検出する方法はないのでしょうか。
~~~~
require 'ripper'
class MyRipper < Ripper
def compile_error(msg)
puts msg
end
end
src = <<SRC
42.times do
}
SRC
MyRipper.parse(src)
~~~~
Rubyのソースコードのtest_parser_events.rbに記載されている
コンパイルエラーのテストケースの入力では
compile_errorが呼び出されることは確認しています。
~~~~
require 'ripper'
class MyRipper < Ripper
def compile_error(msg)
puts msg
end
end
MyRipper.parse('/') # unterminated regexp meets end of file
MyRipper.parse('@1') # `@1' is not allowed as an instance variable name
MyRipper.parse('@%') # `@%' is not allowed as an instance variable name
MyRipper.parse('@@1') # `@@1' is not allowed as a class variable name
MyRipper.parse('@@%') # `@@%' is not allowed as a class variable name
MyRipper.parse('$%') # `$%' is not allowed as a global variable name
~~~~
よろしくお願いいたします。
----
xmisao(ぺけみさお)
mail@xmisao.com
http://www.xmisao.com/