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',
  backtick: 'Delimiter',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



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

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

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Class Method Details

.tokens(source) ⇒ Object



92
93
94
95
96
97
# File 'lib/iro/ruby/parser.rb', line 92

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



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

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

#on_kw(str) ⇒ Object



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

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

#register_token(group, token) ⇒ Object



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

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

#traverse(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/iro/ruby/parser.rb', line 68

def traverse(node)
  if node.kw_type?
    str = node.content
    t = node.position + [str.size]
    t[1] += 1
    @tokens[kw_group(str)]&.reject! do |token|
      token == t
    end
  end

  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