Class: Parser::Context

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

Overview

Context of parsing that is represented by a stack of scopes.

Supported states: + :class - in the class body (class A; end) + :module - in the module body (module M; end) + :sclass - in the singleton class body (class << obj; end) + :def - in the method body (def m; end) + :defs - in the singleton method body (def self.m; end) + :block - in the block body (tap {}) + :lambda - in the lambda body (-> {})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



18
19
20
21
# File 'lib/parser/context.rb', line 18

def initialize
  @stack = []
  freeze
end

Instance Attribute Details

#stackObject (readonly)



16
17
18
# File 'lib/parser/context.rb', line 16

def stack
  @stack
end

Instance Method Details

#class_definition_allowed?Boolean Also known as: module_definition_allowed?, dynamic_const_definition_allowed?

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/parser/context.rb', line 47

def class_definition_allowed?
  def_index = stack.rindex { |item| [:def, :defs].include?(item) }
  sclass_index = stack.rindex(:sclass)

  def_index.nil? || (!sclass_index.nil? && sclass_index > def_index)
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/parser/context.rb', line 35

def empty?
  @stack.empty?
end

#in_block?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/parser/context.rb', line 56

def in_block?
  @stack.last == :block
end

#in_class?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/parser/context.rb', line 39

def in_class?
  @stack.last == :class
end

#in_dynamic_block?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/parser/context.rb', line 64

def in_dynamic_block?
  in_block? || in_lambda?
end

#in_lambda?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/parser/context.rb', line 60

def in_lambda?
  @stack.last == :lambda
end

#indirectly_in_def?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/parser/context.rb', line 43

def indirectly_in_def?
  @stack.include?(:def) || @stack.include?(:defs)
end

#popObject



27
28
29
# File 'lib/parser/context.rb', line 27

def pop
  @stack.pop
end

#push(state) ⇒ Object



23
24
25
# File 'lib/parser/context.rb', line 23

def push(state)
  @stack << state
end

#resetObject



31
32
33
# File 'lib/parser/context.rb', line 31

def reset
  @stack.clear
end