pegasus v0.1.0
pegasus
A PEG parser library for Crystal
Installation
Add this to your application's shard.yml
:
dependencies:
pegasus:
github: pawandubey/pegasus
Usage
Let's look at a simple example for a URL query parser that can accept:
- an optional
&
at the end of the query - an optional
=
at the end of the query
require "pegasus"
parser = Pegasus::Parser.define do |p|
p.rule(:query) do |p| # complex base rule with alternatives, repetition and optional occurence
(p.rule(:pair) >> p.rule(:sep) >> p.rule(:pair).maybe?).repeat | p.rule(:pair)
end
p.rule(:pair) { |p| p.rule(:str).aka(:key) >> p.str("=") >> p.rule(:str).aka(:val).maybe? }
p.rule(:sep) { |p| p.str("&").aka(:sep) } #=> rename nodes with .aka
p.rule(:str) { |p| p.match(/\A[^\s\/\\\.&=]+/) } #=> use regex wherever it makes sense
p.root(:query)
end
res = parser.parse("name=ferret&color=purple") # get back the parse result
#=> #<Pegasus::MatchResult:0x1e418e0>
res.success? # find if parse was successful
#=> true
parse_tree = res.parse_tree # get the parse tree
#=> #<Pegasus::ParseTree:0x1e418c0>
parse_tree.dump # dump the parse tree to JSON
#=> {"label":"rep","children":[{"label":"seq","children":[{"label":"seq","children":[{"label":"seq","children":[{"label":"seq","children":[{"label":"key","item":"name"},{"label":"terminal","item":"="}]},{"label":"rep","children":[{"label":"val","item":"ferret"}]}]},{"label":"sep","item":"&"}]},{"label":"rep","children":[{"label":"seq","children":[{"label":"seq","children":[{"label":"key","item":"color"},{"label":"terminal","item":"="}]},{"label":"rep","children":[{"label":"val","item":"purple"}]}]}]}]}]}
For a more complex (and standard) example:
# simple calculator example
require "pegasus"
parser = Pegasus::Parser.define do |p|
p.rule(:add) do |p|
p.rule(:mul).aka(:l) >> (p.rule(:addop) >> p.rule(:mul)).repeat | p.rule(:mul)
end
p.rule(:mul) do |p|
p.rule(:int).aka(:l) >> (p.rule(:mulop) >> p.rule(:int)).repeat | p.rule(:int)
end
p.rule(:int) do |p|
p.rule(:digit).aka(:i) >> p.rule(:space?).ignore
end
p.rule(:addop) { |p| p.match(/\A[\+\-]/).aka(:o) >> p.rule(:space?).ignore }
p.rule(:mulop) { |p| p.match(/\A[\*\/]/).aka(:o) >> p.rule(:space?).ignore }
p.rule(:digit) { |p| p.match(/\A\d+/) }
p.rule(:space?) { |p| p.match(/\A\s*/) }
p.root(:add) # define root node to start the parse from
end
res = parser.parse("0-1 + 2 /4 * 51 ")
res.success? #=> true
res.error #=> nil ; this will give you more context on a parse failure.
Contributing
- Fork it ( https://github.com/pawandubey/pegasus/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- pawandubey Pawan Dubey - creator, maintainer
Repository
pegasus
Owner
Statistic
- 18
- 1
- 7
- 0
- 0
- over 6 years ago
- September 5, 2017
License
MIT License
Links
Synced at
Thu, 07 Nov 2024 09:02:27 GMT
Languages