title: Indentation sensitive parser using Parslet in Ruby?
body: <p>I am attempting to parse a simple indentation sensitive syntax using the <a href="http://kschiess.github.io/parslet/">Parslet</a> library within Ruby.</p>
  
  <p>The following is an example of the syntax I am attempting to parse:</p>
  
  <pre><code>level0child0
  level0child1
    level1child0
    level1child1
      level2child0
    level1child2
  </code></pre>
  
  <p>The resulting tree would look like so:</p>
  
  <pre><code>[
    {
      :identifier =&gt; "level0child0",
      :children =&gt; []
    },
    {
      :identifier =&gt; "level0child1",
      :children =&gt; [
        {
          :identifier =&gt; "level1child0",
          :children =&gt; []
        },
        {
          :identifier =&gt; "level1child1",
          :children =&gt; [
            {
              :identifier =&gt; "level2child0",
              :children =&gt; []
            }
          ]
        },
        {
          :identifier =&gt; "level1child2",
          :children =&gt; []
        },
      ]
    }
  ]
  </code></pre>
  
  <p>The parser that I have now can parse nesting level 0 and 1 nodes, but cannot parse past that:</p>
  
  <pre><code>require 'parslet'
  
  class IndentationSensitiveParser &lt; Parslet::Parser
  
    rule(:indent) { str('  ') }
    rule(:newline) { str("\n") }
    rule(:identifier) { match['A-Za-z0-9'].repeat.as(:identifier) }
  
    rule(:node) { identifier &gt;&gt; newline &gt;&gt; (indent &gt;&gt; identifier &gt;&gt; newline.maybe).repeat.as(:children) }
  
    rule(:document) { node.repeat }
  
    root :document
  
  end
  
  require 'ap'
  require 'pp'
  
  begin
    input = DATA.read
  
    puts '', '----- input ----------------------------------------------------------------------', ''
    ap input
  
    tree = IndentationSensitiveParser.new.parse(input)
  
    puts '', '----- tree -----------------------------------------------------------------------', ''
    ap tree
  
  rescue IndentationSensitiveParser::ParseFailed =&gt; failure
    puts '', '----- error ----------------------------------------------------------------------', ''
    puts failure.cause.ascii_tree
  end
  
  __END__
  user
    name
    age
  recipe
    name
  foo
  bar
  </code></pre>
  
  <p>It's clear that I need a dynamic counter that expects 3 indentation nodes to match a identifier on the nesting level 3.</p>
  
  <p>How can I implement an indentation sensitive syntax parser using Parslet in this way? Is it possible?</p>