Module: Spectre::ShortcutsMixin

Included in:
Grammar
Defined in:
lib/spectre/base/grammar.rb

Overview

If mixed into a class, it defines shortuct methods for all registered Parsers. Used by the Spectre standard parsers, e.g. char(‘k’) will be a shortcut for CharParser.new(‘k’). See std/std.rb for more details.

Class Method Summary collapse

Class Method Details

.register_shortcut(hsh) ⇒ Object

For each name => klass in hsh: Register the Parser klass with the name inside the Grammar class, so that a new Parser of that klass can be chained into a Grammar simply by calling

+name _arguments_+

inside the Grammar class definition. name must be a Symbol.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spectre/base/grammar.rb', line 103

def register_shortcut hsh
    hsh.each do |meth,klass|
        raise "class Grammar already has a singleton method named '#{meth.to_s}'" if
            Grammar.singleton_methods.include? meth.to_s

        block = lambda do |*args|
            parser = klass.new *args
            parser.to_p
        end

        # define it for the grammars
        self.class_def meth, &block
    end
end