Class: Rattler::Compiler::Optimizer::OptimizationContext
- Inherits:
-
Object
- Object
- Rattler::Compiler::Optimizer::OptimizationContext
- 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
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.[](attrs) ⇒ OptimizationContext
An optimization context with the given attributes.
Instance Method Summary collapse
-
#analysis ⇒ Rattler::Parsers::Analysis
An analysis of the parse rules.
-
#capturing? ⇒ Boolean
Whether this is a capturing context.
-
#initialize(attrs) ⇒ OptimizationContext
constructor
A new instance of OptimizationContext.
-
#inlineable?(rule_name) ⇒ Boolean
Whether the rule can be inlined.
-
#matching? ⇒ Boolean
Whether this is a matching context.
- #method_missing(symbol, *args) ⇒ Object
-
#relavent?(rule) ⇒ Boolean
Whether the rule is relavent to the optimized parser.
- #respond_to?(symbol) ⇒ Boolean
-
#start_rule ⇒ Symbol
The name of the start rule.
-
#with(new_attrs) ⇒ OptimizationContext
A new context with
new_attrs
added.
Constructor Details
#initialize(attrs) ⇒ OptimizationContext
Returns a new instance of OptimizationContext.
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
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
24 25 26 |
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 24 def rules @rules end |
#type ⇒ Object (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.
14 15 16 |
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 14 def self.[](attrs) @@cache[attrs] end |
Instance Method Details
#analysis ⇒ Rattler::Parsers::Analysis
Returns an analysis of the parse rules.
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.
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.
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.
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.
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
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_rule ⇒ Symbol
Returns 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.
64 65 66 |
# File 'lib/rattler/compiler/optimizer/optimization_context.rb', line 64 def with(new_attrs) self.class[@attrs.merge new_attrs] end |