Sha256: 676fb0ae4aece1044c7f4fe573d9f714fe9ab82b60cfcc0150458669dbb0c81c

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# Allows specifying rules as strings using the exact same grammar that treetop
# does, minus the actions. This is on one hand a good example of a fully
# fledged parser and on the other hand might even turn out really useful. 
#
# This can be viewed as an extension to parslet and might even be hosted in
# its own gem one fine day. 
# 
# NOT FINISHED & EXPERIMENTAL
#
class Parslet::Expression
  include Parslet
  
  autoload :Treetop, 'parslet/expression/treetop'
  
  # Creates a parslet from a foreign language expression. 
  #
  # Example: 
  #   
  #   Parslet::Expression.new("'a' 'b'")
  #
  def initialize(str, opts={}, context=self)
    @type = opts[:type] || :treetop
    @exp = str
    @parslet = transform(
      parse(str))
  end
  
  # Transforms the parse tree into a parslet expression. 
  #
  def transform(tree)
    transform = Treetop::Transform.new
    
    # pp tree
    transform.apply(tree)
  rescue 
    warn "Could not transform: " + tree.inspect
    raise
  end
  
  # Parses the string and returns a parse tree.
  #
  def parse(str)
    parser = Treetop::Parser.new
    parser.parse(str)
  end

  # Turns this expression into a parslet.
  #
  def to_parslet
    @parslet
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
parslet-1.3.0 lib/parslet/expression.rb
parslet-1.2.3 lib/parslet/expression.rb
parslet-1.2.1 lib/parslet/expression.rb
parslet-1.2.0 lib/parslet/expression.rb
parslet-1.1.1 lib/parslet/expression.rb
parslet-1.1.0 lib/parslet/expression.rb