module EBNF::PEG::Parser::ClassMethods
DSL for creating terminals and productions
Public Instance Methods
Evaluate a handler, delegating to the specified object. This is necessary so that handlers can operate within the binding context of the parser in which they’re invoked. @param [Object] object @return [Object]
# File lib/ebnf/peg/parser.rb, line 152 def eval_with_binding(object) @delegate = object object.instance_eval {yield} end
Defines a production called when production of associated non-terminals has completed with data from previous production along with data defined for the current production. Block is called in an evaluation block from the enclosing parser.
@param [Symbol] term
Term which is a key in the branch table
@param [Boolean] clear_packrat (false)
Clears the packrat state on completion to reduce memory requirements of parser. Use only on a top-level rule when it is determined that no further backtracking is necessary.
@yield [result, data, block] @yieldparam [Object] result
The result from sucessfully parsing the production.
@yieldparam [Hash] data
A Hash defined for the current production, during :start may be initialized with data to pass to further productions, during :finish, it contains data placed by earlier productions
@yieldparam [Proc] block
Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize
@yieldreturn [Object] the result of this production. Yield to generate a triple
# File lib/ebnf/peg/parser.rb, line 143 def production(term, clear_packrat: false, &block) production_handlers[term] = [block, clear_packrat] end
# File lib/ebnf/peg/parser.rb, line 55 def production_handlers; (@production_handlers ||= {}); end
# File lib/ebnf/peg/parser.rb, line 53 def start_handlers; (@start_handlers ||= {}); end
# File lib/ebnf/peg/parser.rb, line 54 def start_options; (@start_hoptions ||= {}); end
Defines a production called at the beggining of a particular production with data from previous production along with data defined for the current production. Block is called in an evaluation block from the enclosing parser.
@param [Symbol] term
The rule name
@param [Hash{Symbol => Object}] options
Options which are returned from {Parser#onStart}.
@option options [Boolean] :as_hash (false)
If the production is a `seq`, causes the value to be represented as a single hash, rather than an array of individual hashes for each sub-production. Note that this is not always advisable due to the possibility of repeated productions within the sequence.
@option options[:upper, :lower] :insensitive_strings
Perform case-insensitive match of strings not defined as terminals, and map to either upper or lower case.
@yield [data, block] @yieldparam [Hash] data
A Hash defined for the current production, during :start may be initialized with data to pass to further productions, during :finish, it contains data placed by earlier productions
@yieldparam [Proc] block
Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize
Yield to generate a triple
# File lib/ebnf/peg/parser.rb, line 115 def start_production(term, **options, &block) start_handlers[term] = block start_options[term] = options.freeze end
Defines the pattern for a terminal node and a block to be invoked when ther terminal is encountered. If the block is missing, the value of the terminal will be placed on the input hash to be returned to a previous production. Block is called in an evaluation block from the enclosing parser.
If no block is provided, then the value which would have been passed to the block is used as the result directly.
@param [Symbol] term
The terminal name.
@param [Regexp] regexp (nil)
Pattern used to scan for this terminal, defaults to the expression defined in the associated rule. If unset, the terminal rule is used for matching.
@param [Hash] options @option options [Boolean] :unescape
Cause strings and codepoints to be unescaped.
@yield [value, prod] @yieldparam [String] value
The scanned terminal value.
@yieldparam [Symbol] prod
A symbol indicating the production which referenced this terminal
@yieldparam [Proc] block
Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize
# File lib/ebnf/peg/parser.rb, line 86 def terminal(term, regexp = nil, **options, &block) terminal_regexps[term] = regexp if regexp terminal_handlers[term] = block if block_given? terminal_options[term] = options.freeze end
# File lib/ebnf/peg/parser.rb, line 56 def terminal_handlers; (@terminal_handlers ||= {}); end
# File lib/ebnf/peg/parser.rb, line 58 def terminal_options; (@terminal_options ||= {}); end
# File lib/ebnf/peg/parser.rb, line 57 def terminal_regexps; (@terminal_regexps ||= {}); end
Private Instance Methods
# File lib/ebnf/peg/parser.rb, line 159 def method_missing(method, *args, &block) if @delegate ||= nil # special handling when last arg is **options params = @delegate.method(method).parameters if params.any? {|t, _| t == :keyrest} && args.last.is_a?(Hash) opts = args.pop @delegate.send(method, *args, **opts, &block) else @delegate.send(method, *args, &block) end else super end end