module EBNF::LL1::Parser::ClassMethods

DSL for creating terminals and productions

Public Instance Methods

eval_with_binding(object) { || ... } click to toggle source

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/ll1/parser.rb, line 156
def eval_with_binding(object)
  @delegate = object
  object.instance_eval {yield}
end
patterns() click to toggle source
# File lib/ebnf/ll1/parser.rb, line 65
def patterns; @patterns || []; end
production(term, &block) click to toggle source

Defines a production called when production of associated terminals and 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

@yield [input, current, block] @yieldparam [Hash] input

A Hash containing input from the parent production

@yieldparam [Hash] current

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/ll1/parser.rb, line 146
def production(term, &block)
  @production_handlers ||= {}
  @production_handlers[term] = block
end
production_handlers() click to toggle source
# File lib/ebnf/ll1/parser.rb, line 63
def production_handlers; @production_handlers || {}; end
start_handlers() click to toggle source
# File lib/ebnf/ll1/parser.rb, line 62
def start_handlers; @start_handlers || {}; end
start_production(term, &block) click to toggle source

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

Term which is a key in the branch table

@yield [input, current, block] @yieldparam [Hash] input

A Hash containing input from the parent production

@yieldparam [Hash] current

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/ll1/parser.rb, line 121
def start_production(term, &block)
  @start_handlers ||= {}
  @start_handlers[term] = block
end
terminal(term, regexp, **options, &block) click to toggle source

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.

@param [Symbol, String] term

Defines a terminal production, which appears as within a sequence in the branch table

@param [Regexp] regexp

Pattern used to scan for this terminal

@param [Hash] options @option options [Hash{String => String}] :map ({})

A mapping from terminals, in lower-case form, to
their canonical value

@option options [Boolean] :unescape

Cause strings and codepoints to be unescaped.

@yield [term, token, input, block] @yieldparam [Symbol] term

A symbol indicating the production which referenced this terminal

@yieldparam [String] token

The scanned token

@yieldparam [Hash] input

A Hash containing input from the parent production

@yieldparam [Proc] block

Block passed to initialization for yielding to calling parser.
Should conform to the yield specs for #initialize
# File lib/ebnf/ll1/parser.rb, line 94
def terminal(term, regexp, **options, &block)
  @patterns ||= []
  # Passed in order to define evaulation sequence
  @patterns << EBNF::LL1::Lexer::Terminal.new(term, regexp, **options)
  @terminal_handlers ||= {}
  @terminal_handlers[term] = block if block_given?
end
terminal_handlers() click to toggle source
# File lib/ebnf/ll1/parser.rb, line 64
def terminal_handlers; @terminal_handlers || {}; end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/ebnf/ll1/parser.rb, line 163
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