Indentation sensitive parser using Parslet in Ruby?

前端 未结 2 893
星月不相逢
星月不相逢 2020-12-25 08:47

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 pa

2条回答
  •  悲哀的现实
    2020-12-25 08:59

    There are a few approaches.

    1. Parse the document by recognising each line as a collection of indents and an identifier, then apply a transformation afterwards to reconstruct the hierarchy based on the number of indents.

    2. Use captures to store the current indent and expect the next node to include that indent plus more to match as a child (I didn't dig into this approach much as the next one occurred to me)

    3. Rules are just methods. So you can define 'node' as a method, which means you can pass parameters! (as follows)

    This lets you define node(depth) in terms of node(depth+1). The problem with this approach, however, is that the node method doesn't match a string, it generates a parser. So a recursive call will never finish.

    This is why dynamic exists. It returns a parser that isn't resolved until the point it tries to match it, allowing you to now recurse without problems.

    See the following code:

    require 'parslet'
    
    class IndentationSensitiveParser < Parslet::Parser
    
      def indent(depth)
        str('  '*depth)
      end
    
      rule(:newline) { str("\n") }
    
      rule(:identifier) { match['A-Za-z0-9'].repeat(1).as(:identifier) }
    
      def node(depth) 
        indent(depth) >> 
        identifier >> 
        newline.maybe >> 
        (dynamic{|s,c| node(depth+1).repeat(0)}).as(:children)
      end 
    
      rule(:document) { node(0).repeat }
    
      root :document
    end
    

    This is my favoured solution.

提交回复
热议问题