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',
  
  kw: 'Keyword',
  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.



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



70
71
72
73
74
75
# File 'lib/iro/ruby/parser.rb', line 70

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

Instance Method Details

#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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/iro/ruby/parser.rb', line 55

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