← Back to Examples
Empty Rule Stub
RubyconceptualbeginnerParslet-compatible
Empty rule pattern for prototyping
How the Code Works
This section explains how the parsing rules map to the format semantics.
Empty Rule Definition
class MyParser < Parsanol::Parser
rule(:empty) { }
endRules can be defined without bodies: This creates a rule placeholder without implementation.
Error on Use
MyParser.new.empty.parslet
# => NotImplementedError: rule :empty not implementedCalling an empty rule raises `NotImplementedError`:
Use Case: Grammar Sketching
class MyParser < Parsanol::Parser
rule(:expression) { term >> (operator >> term).repeat }
rule(:term) { } # TODO: implement
rule(:operator) { } # TODO: implement
endEmpty rules help sketch out grammar structure: This lets you plan the structure before filling in details.
Use Case: Grammar Sketching
class MyParser < Parsanol::Parser
rule(:expression) { term >> (operator >> term).repeat }
rule(:term) { } # TODO: implement
rule(:operator) { } # TODO: implement
endEmpty rules help sketch out grammar structure: This lets you plan the structure before filling in details.
Source Code
Ruby
# frozen_string_literal: true
# Basically just demonstrates that you can leave rules empty and get a nice
# NotImplementedError. A way to quickly spec out your parser rules?
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require "parsanol/parslet"
class MyParser < Parsanol::Parser
rule(:empty) {}
end
MyParser.new.empty.parslet