Class: Parser::Context
- Inherits:
-
Object
- Object
- Parser::Context
- 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
- #stack ⇒ Object readonly
Instance Method Summary collapse
- #class_definition_allowed? ⇒ Boolean (also: #module_definition_allowed?, #dynamic_const_definition_allowed?)
- #empty? ⇒ Boolean
- #in_block? ⇒ Boolean
- #in_class? ⇒ Boolean
- #in_dynamic_block? ⇒ Boolean
- #in_lambda? ⇒ Boolean
- #indirectly_in_def? ⇒ Boolean
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #pop ⇒ Object
- #push(state) ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Context
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
#stack ⇒ Object (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?
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
35 36 37 |
# File 'lib/parser/context.rb', line 35 def empty? @stack.empty? end |
#in_block? ⇒ Boolean
56 57 58 |
# File 'lib/parser/context.rb', line 56 def in_block? @stack.last == :block end |
#in_class? ⇒ Boolean
39 40 41 |
# File 'lib/parser/context.rb', line 39 def in_class? @stack.last == :class end |
#in_dynamic_block? ⇒ Boolean
64 65 66 |
# File 'lib/parser/context.rb', line 64 def in_dynamic_block? in_block? || in_lambda? end |
#in_lambda? ⇒ Boolean
60 61 62 |
# File 'lib/parser/context.rb', line 60 def in_lambda? @stack.last == :lambda end |
#indirectly_in_def? ⇒ Boolean
43 44 45 |
# File 'lib/parser/context.rb', line 43 def indirectly_in_def? @stack.include?(:def) || @stack.include?(:defs) end |
#pop ⇒ Object
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 |
#reset ⇒ Object
31 32 33 |
# File 'lib/parser/context.rb', line 31 def reset @stack.clear end |