Sha256: 408673dc5d222f289e638f3497d81e0ef69b5c460f3c1f4a5d91e99bc766ac34

Contents?: true

Size: 827 Bytes

Versions: 5

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

module Parser
  # Context of parsing that is represented by a stack of scopes.
  #
  # Supported states:
  # + :class - in the class body (class A; 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 (-> {})
  #
  class Context
    attr_reader :stack

    def initialize
      @stack = []
      freeze
    end

    def push(state)
      @stack << state
    end

    def pop
      @stack.pop
    end

    def reset
      @stack.clear
    end

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
parser-2.5.0.5 lib/parser/context.rb
parser-2.5.0.3 lib/parser/context.rb
parser-2.5.0.2 lib/parser/context.rb
parser-2.5.0.1 lib/parser/context.rb
parser-2.5.0.0 lib/parser/context.rb