class Iro::Ruby::Parser

Constants

EVENT_NAME_TO_HIGHLIGT_NAME

Attributes

tokens[R]

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/iro/ruby/parser.rb, line 48
def initialize(*)
  super
  @tokens = {}
  @end_stack = []
end
tokens(source) click to toggle source
# File lib/iro/ruby/parser.rb, line 194
def self.tokens(source)
  parser = self.new(source)
  parser.parse
  parser.tokens
end

Public Instance Methods

highlight_end_as(group) click to toggle source
# File lib/iro/ruby/parser.rb, line 72
def highlight_end_as(group)
  register_scanner_event group, @end_stack.pop
end
highlight_keyword_like_method(ident) click to toggle source
# File lib/iro/ruby/parser.rb, line 183
def highlight_keyword_like_method(ident)
  case ident.content
  when 'private', 'public', 'protected', 'private_class_method',
       'attr_reader', 'attr_writer', 'attr_accessor', 'attr',
       'include', 'extend', 'prepend', 'module_function', 'refine', 'using',
       'raise', 'fail', 'catch', 'throw',
       'require', 'require_relative'
    register_scanner_event 'Keyword', ident # TODO: Change highlight group
  end
end
kw_group(str) click to toggle source
# File lib/iro/ruby/parser.rb, line 121
def kw_group(str)
  {
    'def' => 'rubyDefine',
    'alias' => 'rubyDefine',
  }[str] || 'Keyword'
end
on_command(ident, _) click to toggle source
# File lib/iro/ruby/parser.rb, line 179
def on_command(ident, _)
  highlight_keyword_like_method(ident)
end
on_const_path_field(*, name)
Alias for: on_top_const_ref
on_const_path_ref(*, name)
Alias for: on_top_const_ref
on_const_ref(*, name)
Alias for: on_top_const_ref
on_def(name, params, body) click to toggle source
# File lib/iro/ruby/parser.rb, line 128
def on_def(name, params, body)
  unhighlight! name if name.kw_type?
  register_scanner_event 'rubyFunction', name
  highlight_end_as 'rubyDefine'
  nil
end
on_defs(recv, period, name, params, body) click to toggle source
# File lib/iro/ruby/parser.rb, line 135
def on_defs(recv, period, name, params, body)
  unhighlight! name if name.kw_type?
  register_scanner_event 'rubyFunction', name
  highlight_end_as 'rubyDefine'
  nil
end
on_fcall(ident) click to toggle source
# File lib/iro/ruby/parser.rb, line 171
def on_fcall(ident)
  highlight_keyword_like_method(ident)
end
on_kw(str) click to toggle source
Calls superclass method
# File lib/iro/ruby/parser.rb, line 102
def on_kw(str)
  super.tap do |result|
    if str == 'end'
      @end_stack << result
    else
      group = kw_group(str)
      register_token group, [lineno, column+1, str.bytesize]
    end
  end
end
on_label(str) click to toggle source

foo: bar ^^^ rubySymbol

^ no highlight
Calls superclass method
# File lib/iro/ruby/parser.rb, line 116
def on_label(str)
  register_token 'rubySymbol', [lineno, column+1, str.bytesize-1]
  super
end
on_symbol(node) click to toggle source
# File lib/iro/ruby/parser.rb, line 142
def on_symbol(node)
  unhighlight! node if node.gvar_type? || node.ivar_type? || node.cvar_type? || node.kw_type?
  register_scanner_event 'rubySymbol', node
  nil
end
on_top_const_ref(*, name) click to toggle source
# File lib/iro/ruby/parser.rb, line 163
def on_top_const_ref(*, name)
  register_scanner_event 'Type', name
  nil
end
on_var_field(name) click to toggle source
# File lib/iro/ruby/parser.rb, line 158
def on_var_field(name)
  register_scanner_event 'Type', name if name.const_type?
  nil
end
on_var_ref(name) click to toggle source
# File lib/iro/ruby/parser.rb, line 148
def on_var_ref(name)
  case name.type
  when :@ident
    register_scanner_event 'rubyLocalVariable', name
  when :@const
    register_scanner_event 'Type', name
  end
  nil
end
on_vcall(ident) click to toggle source
# File lib/iro/ruby/parser.rb, line 175
def on_vcall(ident)
  highlight_keyword_like_method(ident)
end
parse() click to toggle source
Calls superclass method
# File lib/iro/ruby/parser.rb, line 54
def parse
  super
  @end_stack.each do |end_kw|
    register_scanner_event 'Keyword', end_kw
  end
end
register_scanner_event(group, event) click to toggle source

TODO: Maybe multiline support is needed.

# File lib/iro/ruby/parser.rb, line 67
def register_scanner_event(group, event)
  pos = event.position
  register_token group, [pos[0], pos[1]+1, event.content.bytesize]
end
register_token(group, token) click to toggle source
# File lib/iro/ruby/parser.rb, line 61
def register_token(group, token)
  @tokens[group] ||= []
  @tokens[group] << token
end
unhighlight!(scanner_event) click to toggle source
# File lib/iro/ruby/parser.rb, line 76
def unhighlight!(scanner_event)
  t = scanner_event.type[1..-1].to_sym
  group = EVENT_NAME_TO_HIGHLIGT_NAME[t] ||
    (scanner_event.kw_type? && kw_group(scanner_event.content))
  raise 'bug' unless group

  t = scanner_event.position + [scanner_event.content.bytesize]
  t[1] += 1
  @tokens[group].reject! { |ev| ev == t }
  @end_stack.reject!{|e| e == scanner_event} if scanner_event.kw_type? && scanner_event.content == 'end'
end