Class: Rattler::Compiler::Optimizer::OptimizationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/compiler/optimizer/optimization_context.rb

Overview

OptimizationContext provides contextual information to optimzations.

Constant Summary collapse

@@cache =
Hash.new {|h, attrs| h[attrs] = self.new(attrs) }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ OptimizationContext

Returns a new instance of OptimizationContext.

Parameters:

  • attrs (Hash)

    attributes of an optimization context



19
20
21
22
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 19

def initialize(attrs)
  @attrs = attrs
  @rules = @attrs[:rules]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 69

def method_missing(symbol, *args) #:nodoc:
  if args.empty? and @attrs.has_key? symbol
    @attrs[symbol]
  else
    super
  end
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



24
25
26
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 24

def rules
  @rules
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 24

def type
  @type
end

Class Method Details

.[](attrs) ⇒ OptimizationContext

Returns an optimization context with the given attributes.

Parameters:

  • attrs (Hash)

    attributes of an optimization context

Returns:



14
15
16
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 14

def self.[](attrs)
  @@cache[attrs]
end

Instance Method Details

#analysisRattler::Parsers::Analysis

Returns an analysis of the parse rules.

Returns:



32
33
34
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 32

def analysis
  rules && rules.analysis
end

#capturing?Boolean

Returns whether this is a capturing context.

Returns:

  • (Boolean)

    whether this is a capturing context



37
38
39
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 37

def capturing?
  @attrs[:type] == :capturing
end

#inlineable?(rule_name) ⇒ Boolean

Returns whether the rule can be inlined.

Parameters:

  • rule_name (Symbol)

    the name of a rule in the context

Returns:

  • (Boolean)

    whether the rule can be inlined



48
49
50
51
52
53
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 48

def inlineable?(rule_name)
  if rule = rules[rule_name]
    rule.attrs[:inline] and
    analysis.regular? rule_name
  end
end

#matching?Boolean

Returns whether this is a matching context.

Returns:

  • (Boolean)

    whether this is a matching context



42
43
44
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 42

def matching?
  @attrs[:type] == :matching
end

#relavent?(rule) ⇒ Boolean

Returns whether the rule is relavent to the optimized parser.

Parameters:

Returns:

  • (Boolean)

    whether the rule is relavent to the optimized parser



57
58
59
60
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 57

def relavent?(rule)
  !rule.attrs[:inline] or
  analysis.referenced?(rule.name)
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 78

def respond_to?(symbol) #:nodoc:
  super or @attrs.has_key? symbol
end

#start_ruleSymbol

Returns the name of the start rule.

Returns:

  • (Symbol)

    the name of the start rule



27
28
29
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 27

def start_rule
  rules && rules.start_rule
end

#with(new_attrs) ⇒ OptimizationContext

Returns a new context with new_attrs added.

Parameters:

  • new_attrs (Hash)

    additional attributes

Returns:



64
65
66
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 64

def with(new_attrs)
  self.class[@attrs.merge new_attrs]
end