Module: EBNF::PEG::Parser::ClassMethods

Defined in:
lib/ebnf/peg/parser.rb

Overview

DSL for creating terminals and productions

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ebnf/peg/parser.rb', line 160

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

Instance Method Details

#eval_with_binding(object) ⇒ Object

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.

Parameters:

  • object (Object)

Returns:

  • (Object)


153
154
155
156
# File 'lib/ebnf/peg/parser.rb', line 153

def eval_with_binding(object)
  @delegate = object
  object.instance_eval {yield}
end

#production(term, clear_packrat: false) {|result, data, block| ... } ⇒ Object

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.

Yield to generate a triple

Parameters:

  • term (Symbol)

    Term which is a key in the branch table

  • clear_packrat (Boolean) (defaults to: false)

    (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.

Yields:

  • (result, data, block)

Yield Parameters:

  • result (Object)

    The result from sucessfully parsing the production.

  • data (Hash)

    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

  • block (Proc)

    Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize

  • **options (Hash)

    Other data that may be passed to the production

Yield Returns:

  • (Object)

    the result of this production.



144
145
146
# File 'lib/ebnf/peg/parser.rb', line 144

def production(term, clear_packrat: false, &block)
  production_handlers[term] = [block, clear_packrat]
end

#production_handlersObject



55
# File 'lib/ebnf/peg/parser.rb', line 55

def production_handlers; (@production_handlers ||= {}); end

#start_handlersObject



53
# File 'lib/ebnf/peg/parser.rb', line 53

def start_handlers; (@start_handlers ||= {}); end

#start_optionsObject



54
# File 'lib/ebnf/peg/parser.rb', line 54

def start_options; (@start_hoptions ||= {}); end

#start_production(term, **options) {|data, block| ... } ⇒ Object

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.

Yield to generate a triple

Parameters:

  • term (Symbol)

    The rule name

  • options (Hash{Symbol => Object})

    Options which are returned from EBNF::PEG::Parser#onStart.

  • options[:upper, (Hash)

    a customizable set of options

Options Hash (**options):

  • :as_hash (Boolean) — default: 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.

Yields:

  • (data, block)

Yield Parameters:

  • data (Hash)

    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

  • block (Proc)

    Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize



114
115
116
117
# File 'lib/ebnf/peg/parser.rb', line 114

def start_production(term, **options, &block)
  start_handlers[term] = block
  start_options[term] = options.freeze
end

#terminal(term, regexp, **options) {|value, prod| ... } ⇒ Object

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.

Parameters:

  • term (Symbol)

    The terminal name.

  • regexp (Regexp, Proc)

    Pattern used to scan for this terminal. Passing a Proc will evaluate that proc to retrieve a regular expression.

  • options (Hash)

Options Hash (**options):

  • :unescape (Boolean)

    Cause strings and codepoints to be unescaped.

Yields:

  • (value, prod)

Yield Parameters:

  • value (String)

    The scanned terminal value.

  • prod (Symbol)

    A symbol indicating the production which referenced this terminal

  • block (Proc)

    Block passed to initialization for yielding to calling parser. Should conform to the yield specs for #initialize



85
86
87
88
89
# File 'lib/ebnf/peg/parser.rb', line 85

def terminal(term, regexp, **options, &block)
  terminal_regexps[term] = regexp
  terminal_handlers[term] = block if block_given?
  terminal_options[term] = options.freeze
end

#terminal_handlersObject



56
# File 'lib/ebnf/peg/parser.rb', line 56

def terminal_handlers; (@terminal_handlers ||= {}); end

#terminal_optionsObject



58
# File 'lib/ebnf/peg/parser.rb', line 58

def terminal_options; (@terminal_options ||= {}); end

#terminal_regexpsObject



57
# File 'lib/ebnf/peg/parser.rb', line 57

def terminal_regexps; (@terminal_regexps ||= {}); end