Skip to content
Newer
Older
100644 35 lines (25 sloc) 942 Bytes
94d50d6 @kschiess + bringing the power of rule to simple scripts
authored
1
0438b2e @kschiess . more comments!
authored
2 # An exploration of two ideas:
3 # a) Constructing a whole parser inline, without the artificial class around
4 # it.
5 # and:
6 # b) Constructing non-greedy or non-blind parsers by transforming the
7 # grammar.
8
5a9a753 @kschiess + Cleaning up txus patch
authored
9 $:.unshift File.dirname(__FILE__) + "/../lib"
46d7128 @txus . Make examples runnable from outside examples folder
txus authored
10
94d50d6 @kschiess + bringing the power of rule to simple scripts
authored
11 require 'parslet'
12 include Parslet
13
14 a = str('a').repeat >> str('aa')
15
16 # E1% E2
17 #
18 # S = E2 | E1 S
19
a98356a @kschiess + Now this can use Entity as well
authored
20 def this(name, &block); return Parslet::Atoms::Entity.new(name, &block) end
39fc3d9 @kschiess + prsnt? -> present?, absnt? -> absent?
authored
21 def epsilon; any.absent? end
94d50d6 @kschiess + bringing the power of rule to simple scripts
authored
22
03564fb @kschiess . comments
authored
23 # Traditional repetition will try as long as the pattern can be matched and
24 # then give up. This is greedy and blind.
a98356a @kschiess + Now this can use Entity as well
authored
25 a = str('a').as(:e) >> this('a') { a }.as(:rec) | epsilon
03564fb @kschiess . comments
authored
26
27 # Here's a pattern match that is greedy and non-blind. The first pattern
28 # 'a'* will be tried as many times as possible, while still matching the
29 # end pattern 'aa'.
a98356a @kschiess + Now this can use Entity as well
authored
30 b = str('aa').as(:e2) >> epsilon | str('a').as(:e1) >> this('b') { b }.as(:rec)
94d50d6 @kschiess + bringing the power of rule to simple scripts
authored
31
32 p a.parse('aaaa')
33 p b
34 p b.parse('aaaa')
Something went wrong with that request. Please try again.