Class: Iro::Ruby::Parser

Inherits:
Ripper::SexpBuilderPP
  • 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',
  const: 'Type',
  
  regexp_beg: 'Delimiter',
  regexp_end: 'Delimiter',
  heredoc_beg: 'Delimiter',
  heredoc_end: 'Delimiter',
  tstring_beg: 'Delimiter',
  tstring_end: 'Delimiter',
  embexpr_beg: 'Delimiter',
  embexpr_end: 'Delimiter',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



30
31
32
33
# File 'lib/iro/ruby/parser.rb', line 30

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

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



28
29
30
# File 'lib/iro/ruby/parser.rb', line 28

def tokens
  @tokens
end

Class Method Details

.tokens(source) ⇒ Object



81
82
83
84
85
86
# File 'lib/iro/ruby/parser.rb', line 81

def self.tokens(source)
  parser = self.new(source)
  sexp = parser.parse
  parser.traverse(sexp) unless parser.error?
  parser.tokens
end

Instance Method Details

#kw_group(str) ⇒ Object



59
60
61
62
63
64
# File 'lib/iro/ruby/parser.rb', line 59

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

#on_kw(str) ⇒ Object



54
55
56
57
# File 'lib/iro/ruby/parser.rb', line 54

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

#register_token(group, token) ⇒ Object



35
36
37
38
# File 'lib/iro/ruby/parser.rb', line 35

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

#traverse(node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/iro/ruby/parser.rb', line 66

def traverse(node)
  return if node.scanner_event?

  if node.var_ref_type?
    ident = node.children.first
    if ident.ident_type?
      pos = ident.position
      register_token 'rubyLocalVariable', [pos[0], pos[1]+1, ident.content.size]
    end
  end
  node.children.each do |child|
    traverse(child) if child.is_a?(Array)
  end
end