Sha256: a2545d9f41d4102cddb82e665754ff17fc9d7c8572e64ba5bb348670a20b4101

Contents?: true

Size: 1.39 KB

Versions: 18

Compression:

Stored size: 1.39 KB

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)
  # + :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 (-> {})
  #
  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

    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
    alias module_definition_allowed? class_definition_allowed?
    alias dynamic_const_definition_allowed? class_definition_allowed?

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

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

    def in_dynamic_block?
      in_block? || in_lambda?
    end
  end
end

Version data entries

18 entries across 18 versions & 5 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/parser-2.7.2.0/lib/parser/context.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/parser-2.7.2.0/lib/parser/context.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.2.0/lib/parser/context.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/parser-2.7.2.0/lib/parser/context.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.2.0/lib/parser/context.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.2.0/lib/parser/context.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.2.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/parser-2.7.1.4/lib/parser/context.rb
parser-2.7.2.0 lib/parser/context.rb
parser-2.7.1.5 lib/parser/context.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/parser-2.7.1.4/lib/parser/context.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/parser-2.7.1.4/lib/parser/context.rb
parser-2.7.1.4 lib/parser/context.rb