Method: Parslet::Parser.root

Defined in:
lib/parslet/parser.rb

.root(name) ⇒ Object

Define the parsers #root function. This is the place where you start parsing; if you have a rule for ‘file’ that describes what should be in a file, this would be your root declaration:

class Parser
  root :file
  rule(:file) { ... }
end

#root declares a ‘parse’ function that works just like the parse function that you can call on a simple parslet, taking a string as input and producing parse output.

In a way, #root is a shorthand for:

def parse(str)
  your_parser_root.parse(str)
end


53
54
55
56
57
58
# File 'lib/parslet/parser.rb', line 53

def root(name)
  undef_method :root if method_defined? :root
  define_method(:root) do
    self.send(name)
  end
end