Ripper Event structure

February 9, 2014 Rasheed Abdul-Aziz

If you know about Ripper, you know that it’s a bridge between the ruby language parser and your own ruby app. It lets you parse ruby files just as ruby would see them. It’s currently marked as experimental, but its been in ruby since 1.9.

It’s quite hard going getting what you want out of it, one of the problems is the multiple arities of the events it produces. Another is its documentation, it’s really weak. Sven Fuchs wrote a pretty decent article to get you going.

In regards to the arities, you can discover the token<->event structure by firing up irb:

irb(main):001:0> require 'ripper.so' # To include the _TABLE constants
=> true
irb(main):002:0> require 'awesome_print' # for awesome output
=> true
irb(main):010:0> ap({ scanner: Ripper::SCANNER_EVENT_TABLE, parser: Ripper::PARSER_EVENT_TABLE })
{ :scanner => {
                 :CHAR => 1,
              :__end__ => 1,
              :backref => 1,
             :backtick => 1,
                :comma => 1,
              :comment => 1,
                :const => 1,
                 :cvar => 1,
               :embdoc => 1,
           :embdoc_beg => 1,
           :embdoc_end => 1,
          :embexpr_beg => 1,
          :embexpr_end => 1,
               :embvar => 1,
                :float => 1,
                 :gvar => 1,
          :heredoc_beg => 1,
          :heredoc_end => 1,
                :ident => 1,
           :ignored_nl => 1,
                  :int => 1,
                 :ivar => 1,
                   :kw => 1,
                :label => 1,
               :lbrace => 1,
             :lbracket => 1,
               :lparen => 1,
                   :nl => 1,
                   :op => 1,
               :period => 1,
         :qsymbols_beg => 1,
           :qwords_beg => 1,
               :rbrace => 1,
             :rbracket => 1,
           :regexp_beg => 1,
           :regexp_end => 1,
               :rparen => 1,
            :semicolon => 1,
                   :sp => 1,
               :symbeg => 1,
          :symbols_beg => 1,
              :tlambda => 1,
              :tlambeg => 1,
          :tstring_beg => 1,
      :tstring_content => 1,
          :tstring_end => 1,
            :words_beg => 1,
            :words_sep => 1},
:parser => {
                :BEGIN => 1,
                  :END => 1,
                :alias => 2,
          :alias_error => 1,
                 :aref => 2,
           :aref_field => 2,
        :arg_ambiguous => 0,
            :arg_paren => 1,
             :args_add => 2,
       :args_add_block => 2,
        :args_add_star => 2,
             :args_new => 0,
                :array => 1,
               :assign => 2,
         :assign_error => 1,
            :assoc_new => 2,
          :assoc_splat => 1,
  :assoclist_from_args =&gtgt; 1,
      :bare_assoc_hash => 1,
                :begin => 1,
               :binary => 3,
            :block_var => 2,
  :block_var_add_block => 2,
   :block_var_add_star => 2,
             :blockarg => 1,
             :bodystmt => 4,
          :brace_block => 2,
                :break => 1,
                 :call => 3,
                 :case => 2,
                :class => 3,
     :class_name_error => 1,
              :command => 2,
         :command_call => 4,
     :const_path_field => 2,
       :const_path_ref => 2,
            :const_ref => 1,
                  :def => 3,
              :defined => 1,
                 :defs => 5,
             :do_block => 2,
                 :dot2 => 2,
                 :dot3 => 2,
          :dyna_symbol => 1,
                 :else => 1,
                :elsif => 3,
               :ensure => 1,
       :excessed_comma => 1,
                :fcall => 1,
                :field => 3,
                  :for => 3,
                 :hash => 1,
                   :if => 3,
               :if_mod => 2,
                 :ifop => 3,
               :lambda => 2,
        :magic_comment => 2,
              :massign => 2,
       :method_add_arg => 2,
     :method_add_block => 2,
             :mlhs_add => 2,
        :mlhs_add_star => 2,
             :mlhs_new => 0,
           :mlhs_paren => 1,
               :module => 2,
             :mrhs_add => 2,
        :mrhs_add_star => 2,
             :mrhs_new => 0,
   :mrhs_new_from_args => 1,
                 :next => 1,
             :opassign => 3,
   :operator_ambiguous => 2,
          :param_error => 1,
               :params => 7,
                :paren => 1,
          :parse_error => 1,
              :program => 1,
         :qsymbols_add => 2,
         :qsymbols_new => 0,
           :qwords_add => 2,
           :qwords_new => 0,
                 :redo => 0,
           :regexp_add => 2,
       :regexp_literal => 2,
           :regexp_new => 0,
               :rescue => 4,
           :rescue_mod => 2,
           :rest_param => 1,
                :retry => 0,
               :return => 1,
              :return0 => 0,
               :sclass => 2,
            :stmts_add => 2,
            :stmts_new => 0,
           :string_add => 2,
        :string_concat => 2,
       :string_content => 0,
          :string_dvar => 1,
       :string_embexpr => 1,
       :string_literal => 1,
                :super => 1,
               :symbol => 1,
       :symbol_literal => 1,
          :symbols_add => 2,
          :symbols_new => 0,
      :top_const_field => 1,
        :top_const_ref => 1,
                :unary => 2,
                :undef => 1,
               :unless => 3,
           :unless_mod => 2,
                :until => 2,
            :until_mod => 2,
            :var_alias => 2,
            :var_field => 1,
              :var_ref => 1,
                :vcall => 1,
            :void_stmt => 0,
                 :when => 3,
                :while => 2,
            :while_mod => 2,
             :word_add => 2,
             :word_new => 0,
            :words_add => 2,
            :words_new => 0,
          :xstring_add => 2,
      :xstring_literal => 1,
          :xstring_new => 0,
                :yield => 1,
               :yield0 => 0,
               :zsuper => 0}}
=> nil

About the Author

Biography

More Content by Rasheed Abdul-Aziz
Previous
Agile – I know it when I see it
Agile – I know it when I see it

What does an agile software development team look like? Many software development teams claim to be “agile”...

Next
Spec "Helper"
Spec "Helper"

I spent years writing tests for my Rails apps, and every single test file began with this one, seemingly in...

Enter curious. Exit smarter.

Learn More