Class: Walrat::SymbolParslet
- Defined in:
- lib/walrat/symbol_parslet.rb
Overview
A SymbolParslet allows for evaluation of a parslet to be deferred until runtime (or parse time, to be more precise).
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(symbol) ⇒ SymbolParslet
constructor
A new instance of SymbolParslet.
-
#parse(string, options = {}) ⇒ Object
SymbolParslets don’t actually know what Grammar they are associated with at the time of their definition.
-
#to_s ⇒ Object
We override the to_s method as it can make parsing error messages more readable.
Methods inherited from Parslet
Methods included from Memoizing
#check_left_recursion, #memoizing_parse
Methods included from ParsletCombining
#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|
Constructor Details
#initialize(symbol) ⇒ SymbolParslet
Returns a new instance of SymbolParslet.
31 32 33 34 35 36 37 |
# File 'lib/walrat/symbol_parslet.rb', line 31 def initialize symbol raise ArgumentError, 'nil symbol' if symbol.nil? @symbol = symbol # fixed offset to avoid collisions with @parseable objects @hash = @symbol.hash + 20 end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
29 30 31 |
# File 'lib/walrat/symbol_parslet.rb', line 29 def hash @hash end |
Instance Method Details
#==(other) ⇒ Object
69 70 71 |
# File 'lib/walrat/symbol_parslet.rb', line 69 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
73 74 75 |
# File 'lib/walrat/symbol_parslet.rb', line 73 def eql?(other) other.instance_of? SymbolParslet and other.symbol == @symbol end |
#parse(string, options = {}) ⇒ Object
SymbolParslets don’t actually know what Grammar they are associated with at the time of their definition. They expect the Grammar to be passed in with the options hash under the “:grammar” key. Raises if string is nil, or if the options hash does not include a :grammar key.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/walrat/symbol_parslet.rb', line 44 def parse string, = {} raise ArgumentError if string.nil? raise ArgumentError unless .has_key?(:grammar) grammar = [:grammar] = .clone [:rule_name] = @symbol [:skipping_override] = grammar.skipping_overrides[@symbol] if grammar.skipping_overrides.has_key?(@symbol) result = grammar.rules[@symbol].memoizing_parse(string, ) grammar.wrap(result, @symbol) end |
#to_s ⇒ Object
We override the to_s method as it can make parsing error messages more readable. Instead of messages like this:
predicate not satisfied (expected "#<Walrat::SymbolParslet:0x10cd504>")
while parsing "hello world"
We can print messages like this:
predicate not satisfied (expected "rule: end_of_input") while parsing
"hello world"
65 66 67 |
# File 'lib/walrat/symbol_parslet.rb', line 65 def to_s 'rule: ' + @symbol.to_s end |