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) + :def_open_args - in the arglist of the method definition

keep in mind that it's set **only** after reducing the first argument,
if you need to handle the first argument check `lex_state == expr_fname`

+ :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.



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

def initialize
  @stack = []
  freeze
end

Instance Attribute Details

#stackObject (readonly)



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

def stack
  @stack
end

Instance Method Details

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

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/parser/context.rb', line 50

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)


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

def empty?
  @stack.empty?
end

#in_block?Boolean

Returns:

  • (Boolean)


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

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

#in_class?Boolean

Returns:

  • (Boolean)


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

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

#in_def_open_args?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/parser/context.rb', line 71

def in_def_open_args?
  @stack.last == :def_open_args
end

#in_dynamic_block?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/parser/context.rb', line 67

def in_dynamic_block?
  in_block? || in_lambda?
end

#in_lambda?Boolean

Returns:

  • (Boolean)


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

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

#indirectly_in_def?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/parser/context.rb', line 46

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

#popObject



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

def pop
  @stack.pop
end

#push(state) ⇒ Object



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

def push(state)
  @stack << state
end

#resetObject



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

def reset
  @stack.clear
end