Module: Rattler::HelperMethods

Included in:
Rattler, Rattler
Defined in:
lib/rattler.rb

Overview

Convenience methods for defining parsers

Constant Summary collapse

@@defaults =
{
  :type => :extended_packrat
}
@@parser_types =
{
  :recursive_descent  => :RecursiveDescentParser,
  :packrat            => :PackratParser,
  :extended_packrat   => :ExtendedPackratParser
}

Instance Method Summary collapse

Instance Method Details

#compile(mod, grammar, opts) ⇒ Module #compile(mod, parser, opts) ⇒ Module

Compile grammar source or a parser model into match methods in a module.

Overloads:

  • #compile(mod, grammar, opts) ⇒ Module

    Returns mod.

    Parameters:

    • mod (Module)

      the target module for the match methods

    • grammar (String)

      the grammar source to compile

    Returns:

    • (Module)

      mod

  • #compile(mod, parser, opts) ⇒ Module

    Returns mod.

    Parameters:

    Returns:

    • (Module)

      mod



44
45
46
# File 'lib/rattler.rb', line 44

def compile(mod, grammar_or_parser, opts={})
  Rattler::Compiler.compile(mod, grammar_or_parser, opts)
end

#compile_parser(*args) ⇒ Class

Define a parser with the given grammar and compile it into a parser class using the given options

Returns:

  • (Class)

    a new parser class



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rattler.rb', line 29

def compile_parser(*args)
  options = @@defaults.dup
  grammar = nil
  for arg in args
    case arg
    when Hash then options.merge!(arg)
    when String then grammar = arg
    end
  end
  base_class = options.delete(:class) ||
    (Rattler::Runtime::const_get @@parser_types[options[:type]])
  Rattler::Compiler.compile_parser(base_class, grammar, options)
end