Class: RDoc::Parser::RipperStateLex

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/parser/ripper_state_lex.rb

Overview

Wrapper for Ripper lex states

Defined Under Namespace

Classes: InnerStateLex, Token

Constant Summary collapse

EXPR_END =
Ripper::EXPR_END
EXPR_ENDFN =
Ripper::EXPR_ENDFN
EXPR_ARG =
Ripper::EXPR_ARG
EXPR_FNAME =
Ripper::EXPR_FNAME

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ RipperStateLex

New lexer for code.



278
279
280
281
282
283
# File 'lib/rdoc/parser/ripper_state_lex.rb', line 278

def initialize(code)
  @buf = []
  @heredoc_queue = []
  @inner_lex = InnerStateLex.new(code)
  @tokens = @inner_lex.parse([])
end

Class Method Details

.end?(token) ⇒ Boolean

Returns true if lex state will be END after token.

Returns:

  • (Boolean)


299
300
301
# File 'lib/rdoc/parser/ripper_state_lex.rb', line 299

def self.end?(token)
  (token[:state] & EXPR_END)
end

.parse(code) ⇒ Object

Returns tokens parsed from code.



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rdoc/parser/ripper_state_lex.rb', line 286

def self.parse(code)
  lex = self.new(code)
  tokens = []
  begin
    while tk = lex.get_squashed_tk
      tokens.push tk
    end
  rescue StopIteration
  end
  tokens
end

Instance Method Details

#get_squashed_tkObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rdoc/parser/ripper_state_lex.rb', line 27

def get_squashed_tk
  if @buf.empty?
    tk = @tokens.shift
  else
    tk = @buf.shift
  end
  return nil if tk.nil?
  case tk[:kind]
  when :on_symbeg then
    tk = get_symbol_tk(tk)
  when :on_tstring_beg then
    tk = get_string_tk(tk)
  when :on_backtick then
    if (tk[:state] & (EXPR_FNAME | EXPR_ENDFN)) != 0
      tk[:kind] = :on_ident
      tk[:state] = Ripper::Lexer::State.new(EXPR_ARG)
    else
      tk = get_string_tk(tk)
    end
  when :on_regexp_beg then
    tk = get_regexp_tk(tk)
  when :on_embdoc_beg then
    tk = get_embdoc_tk(tk)
  when :on_heredoc_beg then
    @heredoc_queue << retrieve_heredoc_info(tk)
  when :on_nl, :on_ignored_nl, :on_comment, :on_heredoc_end then
    if !@heredoc_queue.empty?
      get_heredoc_tk(*@heredoc_queue.shift)
    elsif tk[:text].nil? # :on_ignored_nl sometimes gives nil
      tk[:text] = ''
    end
  when :on_words_beg then
    tk = get_words_tk(tk)
  when :on_qwords_beg then
    tk = get_words_tk(tk)
  when :on_symbols_beg then
    tk = get_words_tk(tk)
  when :on_qsymbols_beg then
    tk = get_words_tk(tk)
  when :on_op then
    if '&.' == tk[:text]
      tk[:kind] = :on_period
    else
      tk = get_op_tk(tk)
    end
  end
  tk
end