Sha256: 36155afe19e2fae7439a99c07be81b75ba6b3f21df5186c3abf030d8f7316b4a

Contents?: true

Size: 1.16 KB

Versions: 25

Compression:

Stored size: 1.16 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)
  # + :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?
  end
end

Version data entries

25 entries across 23 versions & 6 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/parser-2.6.3.0/lib/parser/context.rb
parser-2.6.3.0 lib/parser/context.rb
parser-2.6.2.1 lib/parser/context.rb
parser-2.6.2.0 lib/parser/context.rb
parser-2.6.0.0 lib/parser/context.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/parser-2.5.3.0/lib/parser/context.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/parser-2.5.3.0/lib/parser/context.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/parser-2.5.3.0/lib/parser/context.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/parser-2.5.3.0/lib/parser/context.rb
parser-2.5.3.0 lib/parser/context.rb