Permalink
Browse files

+ How to extend parslets capabilities

  • Loading branch information...
1 parent a58fdf5 commit b87a5725ebe52b40a32131a4cd511b4839b5476d @kschiess committed Aug 26, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 example/ignore.rb
View
@@ -0,0 +1,33 @@
+# A small example on how to make parslet ignore parts of the parse tree.
+
+$:.unshift File.dirname(__FILE__) + "/../lib"
+require 'parslet'
+
+class IgnoreParslet < Parslet::Atoms::Base
+ def initialize(parslet)
+ @parslet = parslet
+ end
+ def to_s_inner(prec)
+ @parslet.to_s(prec)
+ end
+ def try(source, context)
+ result = @parslet.try(source, context)
+
+ return success(nil) unless result.error?
+ return result
+ end
+
+end
+module IgnoreDSL
+ def ignore
+ IgnoreParslet.new(self)
+ end
+end
+
+class Parslet::Atoms::Base
+ include IgnoreDSL
+end
+
+include Parslet
+p (str('a') >> str('b').ignore >> str('c')).
+ parse('abc')

1 comment on commit b87a572

Contributor

floere commented on b87a572 Aug 26, 2011

Nice :)

Please sign in to comment.