README.md in dolos-0.1.1 vs README.md in dolos-0.1.2
- old
+ new
@@ -9,35 +9,46 @@
### Parser combinator library for Ruby
It does not use exceptions and instead returns a result object.
Library is composable and concise.
+### Getting started
+
+#### Installation
+- Update Gemfile with `gem 'dolos'`
+- Run bundle install
+
+#### Usage
```ruby
+require 'dolos'
include Dolos
+ws = c(" ")
parser = c("Parsers") >> ws >> c("are") >> ws >> c("great!")
-parser.parse("Parsers are great!") # <Result::Success>
+parser.run("Parsers are great!") # <Result::Success>
greeter = c("Hello")
greet_and_speak = greeter >> c(", ") >> parser
-greet_and_speak.parse("Hello, Parsers are great!") # <Result::Success>
+greet_and_speak.run("Hello, Parsers are great!") # <Result::Success>
```
### Letter address parser example
```ruby
require 'dolos'
require 'dolos_common_parsers/common_parsers'
include Dolos
# Include common parsers
-# In future this can be more structured, moved them to separate module to prevent breaking changes
+# In future this can be more structured,
+# moved them to separate module to prevent breaking changes
include Dolos::CommonParsers
# Library usage example
# Parse out a name and address from a letter
-# For higher difficulty, we will not split this into multiple lines, but instead parse it all at once
+# For higher difficulty, we will not split this into multiple lines,
+# but instead parse it all at once
letter = <<-LETTER
Mr. Vardeniui Pavardeniui
AB „Lietuvos Paštas“
Totorių g. 8
01121 Vilnius
@@ -75,24 +86,32 @@
# 'char_while' will consume characters while passed predicate is true
# This could be an alternative to previous 'alpha_with_lt' approach
# After that result is captured and mapped to hash
# Mapping to hash so at the end its easy to tell tuples apart
# Also while mapping, doing some cleaning with '.strip'
-street_name = char_while(->(char) { !char.match(/\d/) }).capture!.map(&:first).map { |s| { street: s.strip } }
+street_name = char_while(->(char) { !char.match(/\d/) })
+ .capture!
+ .map(&:first)
+ .map { |s| { street: s.strip } }
building = digits.capture!.map(&:first).map { |s| { building: s.strip } }
address_line = ws.rep0 >> street_name >> building >> eol
# City line
-# All digits can be matched here or 'digits.rep(5)' could be used. Also joining with map.
+# All digits can be matched here or 'digits.rep(5)' could be used.
+# Also joining with map results.
postcode = digits.capture!.map(&:join).map { |s| { postcode: s.strip } }
city = alpha_with_lt.rep.capture!.map(&:join).map { |s| { city: s.strip } }
city_line = ws.rep0 >> postcode >> ws >> city >> eol
-# Full letter parser which is combined from all previous parsers. All previous parsers can be ran separately.
+# Full letter parser which is combined from all previous parsers.
+# Also, all previous parsers can be ran separately.
letter_parser = name_line >> second_line >> address_line >> city_line
result = letter_parser.run(letter)
+# List of tuples
pp result.captures
+# ["Vardeniui", "Pavardeniui", "Lietuvos Paštas", {:street=>"Totorių g."},
+# {:building=>"8"}, {:postcode=>"01121"}, {:city=>"Vilnius"}]
```
### Contributing
Contributors are welcome. Note: since library is not yet stable, I recommend getting in touch with me before starting to work on something.
\ No newline at end of file