Module: Yoga::Parser::Helpers::ClassMethods
- Defined in:
- lib/yoga/parser/helpers.rb
Overview
The class methods for helpers for the parser. These are defined on the class of the parser.
Constant Summary collapse
- DEFAULT_SWITCH =
The default value for a switch. This matches no tokens and performs no actions. This cannot be modified.
{}.freeze
Instance Method Summary collapse
- #first(name, tokens = nil) ⇒ Object
-
#firsts ⇒ {::Symbol => ::Set<::Symbol>}
The first sets.
-
#switch(name, mapping = nil) ⇒ {::Symbol => {::Symbol => ::Proc}}
Predefines a switch logic.
Instance Method Details
#first(name) ⇒ ::Set<::Symbol> #first(name, tokens) ⇒ void
101 102 103 104 105 106 107 |
# File 'lib/yoga/parser/helpers.rb', line 101 def first(name, tokens = nil) if tokens firsts[name] = Set.new(Array(tokens)) else firsts.fetch(name) end end |
#firsts ⇒ {::Symbol => ::Set<::Symbol>}
The first sets. The key is the name of a node, and the value is a set of all tokens that can be at the start of that node.
78 79 80 |
# File 'lib/yoga/parser/helpers.rb', line 78 def firsts @firsts ||= Hash.new { |h, k| h[k] = Set.new } end |
#switch(name) ⇒ {::Symbol => ::Proc} #switch(name, mapping) ⇒ {::Symbol => {::Symbol => ::Proc}}
Predefines a switch logic. This is used to do set checking for the keys instead of array checking. This reduces the time it takes for a switch statement to be performed.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/yoga/parser/helpers.rb', line 60 def switch(name, mapping = nil) @_switches ||= {} name = name.intern return @_switches[name] || DEFAULT_SWITCH unless mapping switch = {} mapping.each do |k, v| v = v.is_a?(::Proc) ? v : proc { |*a| send(v, *a) } Array(k).each { |n| switch[n] = v } end first(name, switch.keys) @_switches[name] = switch end |