Module: Rattler::HelperMethods

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

Overview

Convenience methods for defining parsers

Author:

  • Jason Arhart

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 = nil, &block) ⇒ Object

Define a parser with the given block and compile it into match methods in the given module



66
67
68
69
# File 'lib/rattler.rb', line 66

def compile(mod, grammar=nil, &block)
  grammar_or_rules = grammar || Rattler::Parsers.define(&block)
  Rattler::BackEnd::Compiler.compile(mod, grammar_or_rules)
end

#compile_parser(*args, &block) ⇒ Class

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

Returns:

  • (Class)

    a new parser class



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rattler.rb', line 48

def compile_parser(*args, &block)
  options = @@defaults
  grammar = nil
  for arg in args
    case arg
    when Hash then options = options.merge(arg)
    when String then grammar = arg
    end
  end
  base_class = options.fetch(:class) do
    Rattler::Runtime::const_get @@parser_types[options[:type]]
  end
  Rattler::BackEnd::Compiler.compile_parser(base_class,
    grammar || Rattler::Parsers.define(&block))
end

#define_rules(&block) ⇒ Rattler::Parsers::Rules

Define parse rules with the given block

Returns:

  • (Rattler::Parsers::Rules)

    a set of parse rules



40
41
42
# File 'lib/rattler.rb', line 40

def define_rules(&block)
  Rattler::Parsers.define(&block)
end