forked from kschiess/parslet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.rb
38 lines (29 loc) · 938 Bytes
/
readme.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'parslet'
include Parslet
# parslet parses strings
str('foo').
parse('foo') # => "foo"@0
# it matches character sets
match['abc'].parse('a') # => "a"@0
match['abc'].parse('b') # => "b"@0
match['abc'].parse('c') # => "c"@0
# and it annotates its output
str('foo').as(:important_bit).
parse('foo') # => {:important_bit=>"foo"@0}
# you can construct parsers with just a few lines
quote = str('"')
simple_string = quote >> (quote.absent? >> any).repeat >> quote
simple_string.
parse('"Simple Simple Simple"') # => "\"Simple Simple Simple\""@0
# or by making a fuss about it
class Smalltalk < Parslet::Parser
root :smalltalk
rule(:smalltalk) { statements }
rule(:statements) {
# insert smalltalk parser here (outside of the scope of this readme)
}
end
# and then
Smalltalk.new.parse('smalltalk')
# be sure to read our 'Get started' tutorial at
# http://kschiess.github.com/parslet/get-started.html