I am attempting to parse a simple indentation sensitive syntax using the Parslet library within Ruby.
The following is an example of the syntax I am attempting to parse:
level0child0
level0child1
  level1child0
  level1child1
    level2child0
  level1child2
The resulting tree would look like so:
[
  {
    :identifier => "level0child0",
    :children => []
  },
  {
    :identifier => "level0child1",
    :children => [
      {
        :identifier => "level1child0",
        :children => []
      },
      {
        :identifier => "level1child1",
        :children => [
          {
            :identifier => "level2child0",
            :children => []
          }
        ]
      },
      {
        :identifier => "level1child2",
        :children => []
      },
    ]
  }
]
The parser that I have now can parse nesting level 0 and 1 nodes, but cannot parse past that:
require 'parslet'
class IndentationSensitiveParser < Parslet::Parser
  rule(:indent) { str('  ') }
  rule(:newline) { str("\n") }
  rule(:identifier) { match['A-Za-z0-9'].repeat.as(:identifier) }
  rule(:node) { identifier >> newline >> (indent >> identifier >> 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 => failure
  puts '', '----- error ----------------------------------------------------------------------', ''
  puts failure.cause.ascii_tree
end
__END__
user
  name
  age
recipe
  name
foo
bar
It's clear that I need a dynamic counter that expects 3 indentation nodes to match a identifier on the nesting level 3.
How can I implement an indentation sensitive syntax parser using Parslet in this way? Is it possible?