Class: Iro::Ruby::Parser

Inherits:
Base
  • Object
show all
Defined in:
lib/iro/ruby/parser.rb

Constant Summary collapse

EVENT_NAME_TO_HIGHLIGT_NAME =
{
  tstring_content: 'String',
  CHAR: 'Character',
  int: 'Number',
  float: 'Float',
  
  comment: 'Comment',
  embdoc: 'Comment',
  embdoc_beg: 'Comment',
  embdoc_end: 'Comment',

  regexp_beg: 'Delimiter',
  regexp_end: 'Delimiter',
  heredoc_beg: 'Delimiter',
  heredoc_end: 'Delimiter',
  tstring_beg: 'Delimiter',
  tstring_end: 'Delimiter',
  embexpr_beg: 'Delimiter',
  embexpr_end: 'Delimiter',
  backtick: 'Delimiter',

  symbeg: 'rubySymbolDelimiter',

  ivar: 'rubyInstanceVariable',
  cvar: 'rubyClassVariable',
  gvar: 'rubyGlobalVariable',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



46
47
48
49
# File 'lib/iro/ruby/parser.rb', line 46

def initialize(*)
  super
  @tokens = {}
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



44
45
46
# File 'lib/iro/ruby/parser.rb', line 44

def tokens
  @tokens
end

Class Method Details

.tokens(source) ⇒ Object



171
172
173
174
175
# File 'lib/iro/ruby/parser.rb', line 171

def self.tokens(source)
  parser = self.new(source)
  parser.parse
  parser.tokens
end

Instance Method Details

#highlight_keyword_like_method(ident) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/iro/ruby/parser.rb', line 160

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) ⇒ Object



101
102
103
104
105
106
# File 'lib/iro/ruby/parser.rb', line 101

def kw_group(str)
  {
    'def' => 'rubyDefine',
    'alias' => 'rubyDefine',
  }[str] || 'Keyword'
end

#on_command(ident, _) ⇒ Object



156
157
158
# File 'lib/iro/ruby/parser.rb', line 156

def on_command(ident, _)
  highlight_keyword_like_method(ident)
end

#on_const_path_ref(_base, name) ⇒ Object



147
148
149
150
# File 'lib/iro/ruby/parser.rb', line 147

def on_const_path_ref(_base, name)
  register_scanner_event 'Type', name
  nil
end

#on_def(name, params, body) ⇒ Object



108
109
110
111
112
# File 'lib/iro/ruby/parser.rb', line 108

def on_def(name, params, body)
  unhighlight! name if name.kw_type?
  register_scanner_event 'rubyFunction', name
  nil
end

#on_defs(recv, period, name, params, body) ⇒ Object



114
115
116
117
118
# File 'lib/iro/ruby/parser.rb', line 114

def on_defs(recv, period, name, params, body)
  unhighlight! name if name.kw_type?
  register_scanner_event 'rubyFunction', name
  nil
end

#on_fcall(ident) ⇒ Object



152
153
154
# File 'lib/iro/ruby/parser.rb', line 152

def on_fcall(ident)
  highlight_keyword_like_method(ident)
end

#on_kw(str) ⇒ Object



87
88
89
90
91
# File 'lib/iro/ruby/parser.rb', line 87

def on_kw(str)
  group = kw_group(str)
  register_token group, [lineno, column+1, str.size]
  super
end

#on_label(str) ⇒ Object

foo: bar ^^^ rubySymbol

^ no highlight


96
97
98
99
# File 'lib/iro/ruby/parser.rb', line 96

def on_label(str)
  register_token 'rubySymbol', [lineno, column+1, str.size-1]
  super
end

#on_symbol(node) ⇒ Object



120
121
122
123
124
# File 'lib/iro/ruby/parser.rb', line 120

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) ⇒ Object Also known as: on_const_ref



141
142
143
144
# File 'lib/iro/ruby/parser.rb', line 141

def on_top_const_ref(name)
  register_scanner_event 'Type', name
  nil
end

#on_var_field(name) ⇒ Object



136
137
138
139
# File 'lib/iro/ruby/parser.rb', line 136

def on_var_field(name)
  register_scanner_event 'Type', name if name.const_type?
  nil
end

#on_var_ref(name) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/iro/ruby/parser.rb', line 126

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

#register_scanner_event(group, event) ⇒ Object

TODO: Maybe multiline support is needed.



57
58
59
60
# File 'lib/iro/ruby/parser.rb', line 57

def register_scanner_event(group, event)
  pos = event.position
  register_token group, [pos[0], pos[1]+1, event.content.size]
end

#register_token(group, token) ⇒ Object



51
52
53
54
# File 'lib/iro/ruby/parser.rb', line 51

def register_token(group, token)
  @tokens[group] ||= []
  @tokens[group] << token
end

#unhighlight!(scanner_event) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/iro/ruby/parser.rb', line 62

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.size]
  t[1] += 1
  @tokens[group].reject! { |ev| ev == t }
end