Ripper Events

This is a documented list of events from Ruby's Ripper parser. They can be handled in subclasses of Ripper, using `on_*` methods of varying arity. (View this page's source code.)

Coverage: 30 / 183 (16%)

:BEGIN :CHAR :END :__end__ :alias :alias_error :aref :aref_field :arg_ambiguous :arg_paren :args_add :args_add_block :args_add_star :args_new :array :assign :assign_error :assoc_new :assoc_splat :assoclist_from_args :backref :backtick :bare_assoc_hash :begin :binary :block_var :block_var_add_block :block_var_add_star :blockarg :bodystmt :brace_block :break :call :case :class :class_name_error :comma :command :command_call :comment :const :const_path_field :const_path_ref :const_ref :cvar :def :defined :defs :do_block :dot2 :dot3 :dyna_symbol :else :elsif :embdoc :embdoc_beg :embdoc_end :embexpr_beg :embexpr_end :embvar :ensure :excessed_comma :fcall :field :float :for :gvar :hash :heredoc_beg :heredoc_dedent :heredoc_end :ident :if :if_mod :ifop :ignored_nl :imaginary :int :ivar :kw :label :label_end :lambda :lbrace :lbracket :lparen :magic_comment :massign :method_add_arg :method_add_block :mlhs_add :mlhs_add_star :mlhs_new :mlhs_paren :module :mrhs_add :mrhs_add_star :mrhs_new :mrhs_new_from_args :next :nl :op :opassign :operator_ambiguous :param_error :params :paren :parse_error :period :program :qsymbols_add :qsymbols_beg :qsymbols_new :qwords_add :qwords_beg :qwords_new :rational :rbrace :rbracket :redo :regexp_add :regexp_beg :regexp_end :regexp_literal :regexp_new :rescue :rescue_mod :rest_param :retry :return :return0 :rparen :sclass :semicolon :sp :stmts_add :stmts_new :string_add :string_concat :string_content :string_dvar :string_embexpr :string_literal :super :symbeg :symbol :symbol_literal :symbols_add :symbols_beg :symbols_new :tlambda :tlambeg :top_const_field :top_const_ref :tstring_beg :tstring_content :tstring_end :unary :undef :unless :unless_mod :until :until_mod :var_alias :var_field :var_ref :vcall :void_stmt :when :while :while_mod :word_add :word_new :words_add :words_beg :words_new :words_sep :xstring_add :xstring_literal :xstring_new :yield :yield0 :zsuper

# on_BEGIN(block) (:BEGIN, in Ripper::PARSER_EVENTS)

Adds a global initialization hook, always with curly braces.

Ripper.sexp_raw("BEGIN { do_stuff }")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:BEGIN,
#    [:stmts_add,
#     [:stmts_new],
#     [:vcall,
#      [:@ident, "do_stuff", [1, 8]]]]]]]

↑ top

# on_CHAR(string, position) (:CHAR, in Ripper::SCANNER_EVENTS)

A one-character string literal, with ?.

Ripper.lex("?a")
# [[[1, 0], :on_CHAR, "?a"]]

↑ top

# on_END(block) (:END, in Ripper::PARSER_EVENTS)

Adds a global shutdown hook, always with curly braces

Ripper.sexp_raw("END { do_stuff }")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:END,
#    [:stmts_add,
#     [:stmts_new],
#     [:vcall,
#      [:@ident, "do_stuff", [1, 6]]]]]]]

↑ top

# on___end__(string, position) (:__end__, in Ripper::SCANNER_EVENTS)

Delimits the Ruby script from the following data (DATA). Interestingly, the following data is absent from the parsed s-expression.

Ripper.sexp_raw("a\n__END__\nwacky stuff")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:vcall, [:@ident, "a", [1, 0]]]]]

Ripper.lex("a\n__END__\nwacky stuff")
# [[[1, 0], :on_ident, "a"],
#  [[1, 1], :on_nl, "\n"],
#  [[2, 0], :on___end__, "__END__\n"]]

↑ top

# on_alias(new_method, old_method) (:alias, in Ripper::PARSER_EVENTS)

alias keyword, whose arguments are always parsed as :symbol_literals

Ripper.sexp_raw("alias :a :b")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:alias,
#    [:symbol_literal,
#     [:symbol,
#      [:@ident, "a", [1, 7]]]],
#    [:symbol_literal,
#     [:symbol,
#      [:@ident, "b", [1, 10]]]]]]]

Ripper.sexp_raw("alias a b")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:alias,
#    [:symbol_literal,
#     [:@ident, "a", [1, 6]]],
#    [:symbol_literal,
#     [:@ident, "b", [1, 8]]]]]]

↑ top

# on_alias_error(alias_call) (:alias_error, in Ripper::PARSER_EVENTS)

Wraps an error call to alias.

Ripper.sexp_raw("alias $foo $2")
# nil

↑ top

# on_aref(receiver, arguments) (:aref, in Ripper::PARSER_EVENTS)

Value lookup with square brackets. (Short for “array reference”?)

Ripper.sexp_raw("a[:b]")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:aref,
#    [:vcall, [:@ident, "a", [1, 0]]],
#    [:args_add_block,
#     [:args_add,
#      [:args_new],
#      [:symbol_literal,
#       [:symbol,
#        [:@ident, "b", [1, 3]]]]],
#     false]]]]

Ripper.sexp_raw("c[0..2]")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:aref,
#    [:vcall, [:@ident, "c", [1, 0]]],
#    [:args_add_block,
#     [:args_add,
#      [:args_new],
#      [:dot2,
#       [:@int, "0", [1, 2]],
#       [:@int, "2", [1, 5]]]],
#     false]]]]

↑ top

# on_aref_field(receiver, arguments) (:aref_field, in Ripper::PARSER_EVENTS)

Square bracket access, but when used in the context of assignment.

Ripper.sexp_raw("[0,1,2][1] = 2")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:assign,
#    [:aref_field,
#     [:array,
#      [:args_add,
#       [:args_add,
#        [:args_add,
#         [:args_new],
#         [:@int, "0", [1, 1]]],
#        [:@int, "1", [1, 3]]],
#       [:@int, "2", [1, 5]]]],
#     [:args_add_block,
#      [:args_add,
#       [:args_new],
#       [:@int, "1", [1, 8]]],
#      false]],
#    [:@int, "2", [1, 13]]]]]

↑ top

# on_arg_ambiguous (:arg_ambiguous, in Ripper::PARSER_EVENTS)

undocumented

# on_arg_paren(arguments) (:arg_paren, in Ripper::PARSER_EVENTS)

Explicit parentheses for arguments

Ripper.sexp_raw("a(1)")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:method_add_arg,
#    [:fcall, [:@ident, "a", [1, 0]]],
#    [:arg_paren,
#     [:args_add_block,
#      [:args_add,
#       [:args_new],
#       [:@int, "1", [1, 2]]],
#      false]]]]]

↑ top

# on_args_add(_unknown, _unknown2) (:args_add, in Ripper::PARSER_EVENTS)

The beginning of a list of arguments.

Ripper.sexp_raw("a(1)")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:method_add_arg,
#    [:fcall, [:@ident, "a", [1, 0]]],
#    [:arg_paren,
#     [:args_add_block,
#      [:args_add,
#       [:args_new],
#       [:@int, "1", [1, 2]]],
#      false]]]]]

Ripper.sexp_raw("a(1, b: 2)")
# [:program,
#  [:stmts_add,
#   [:stmts_new],
#   [:method_add_arg,
#    [:fcall, [:@ident, "a", [1, 0]]],
#    [:arg_paren,
#     [:args_add_block,
#      [:args_add,
#       [:args_add,
#        [:args_new],
#        [:@int, "1", [1, 2]]],
#       [:bare_assoc_hash,
#        [[:assoc_new,
#          [:@label, "b:", [1, 5]],
#          [:@int, "2", [1, 8]]]]]],
#      false]]]]]