Class: Opal::ParserScope

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/parser/parser_scope.rb

Overview

ParserScope is used during lexing to keep track of local variables created inside a scope. A lexer scope can be asked if it has a local variable defined, and it can also check its parent scope if applicable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ ParserScope

Create new parse scope. Valid types are :block, :class, :module, :def.

Parameters:

  • type (Symbol)

    scope type



12
13
14
15
16
# File 'opal/lib/opal/parser/parser_scope.rb', line 12

def initialize(type)
  @block  = type == :block
  @locals = []
  @parent = nil
end

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals



6
7
8
# File 'opal/lib/opal/parser/parser_scope.rb', line 6

def locals
  @locals
end

#parentObject

Returns the value of attribute parent



7
8
9
# File 'opal/lib/opal/parser/parser_scope.rb', line 7

def parent
  @parent
end

Instance Method Details

#add_local(local) ⇒ Object



18
19
20
# File 'opal/lib/opal/parser/parser_scope.rb', line 18

def add_local(local)
  @locals << local
end

#has_local?(local) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'opal/lib/opal/parser/parser_scope.rb', line 22

def has_local?(local)
  return true if @locals.include? local
  return @parent.has_local?(local) if @parent and @block
  false
end