Class: Ripper::Lexer

Inherits:
Ripper show all
Defined in:
lib/ripper/lexer.rb

Overview

:nodoc: internal use only

Defined Under Namespace

Classes: Elem, State

Constant Summary

Constants inherited from Ripper

EVENTS, PARSER_EVENTS, SCANNER_EVENTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ripper

lex, parse, sexp, sexp_raw, slice, token_match, tokenize

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors



96
97
98
# File 'lib/ripper/lexer.rb', line 96

def errors
  @errors
end

Instance Method Details

#lexObject



102
103
104
# File 'lib/ripper/lexer.rb', line 102

def lex
  parse().sort_by(&:pos).map(&:to_a)
end

#parseObject



121
122
123
124
125
126
127
128
# File 'lib/ripper/lexer.rb', line 121

def parse
  @errors = []
  @buf = []
  @stack = []
  super
  @buf.flatten!
  @buf
end

#scanObject

parse the code and returns elements including errors.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ripper/lexer.rb', line 107

def scan
  result = (parse() + errors + @stack.flatten).uniq.sort_by {|e| [*e.pos, (e.message ? -1 : 0)]}
  result.each_with_index do |e, i|
    if e.event == :on_parse_error and e.tok.empty? and (pre = result[i-1]) and
      pre.pos[0] == e.pos[0] and (pre.pos[1] + pre.tok.size) == e.pos[1]
      e.tok = pre.tok
      e.pos[1] = pre.pos[1]
      result[i-1] = e
      result[i] = pre
    end
  end
  result
end

#tokenizeObject



98
99
100
# File 'lib/ripper/lexer.rb', line 98

def tokenize
  parse().sort_by(&:pos).map(&:tok)
end