dolos 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21d4d37cda494b06bfa565d59b3d9d2a157fa4d0c71b09fc1d252e8867fb17f9
4
- data.tar.gz: 9abf404eb2a0da18548f42e5de345776dd8f5c11296b77cee4c81114f3e8ba1e
3
+ metadata.gz: 68d26c878ffd86122b9c12273645cf351f5b20d6e1306e140c53727c29e49b2b
4
+ data.tar.gz: 6bda399416128f5692e2966a46194591209f61602408e6873cb4e41f37a22ec4
5
5
  SHA512:
6
- metadata.gz: 1a5fc2ba17cd43ccb87f3c1b7b8948e27148dfecebfc53a9e46ddf92cb97e1c0c98b5841a87437fe2ab2e9b5b71f65d5753f006c96f9da506290e7fb2d6299a9
7
- data.tar.gz: 419cd3a8d8b64a6eb384dd706647f58182154f6ccb84aa2e83485912374261bc6ee89ea827e394dda621ffb43a2a6fa5bc07c8107de3e9b9727880f4a56c1bac
6
+ metadata.gz: 7ae6b3a4750cc5bfb5f54a84d518f8d4176eaf10dfef4482f45aaadc3db422a14f04cabb65ee0e1fe85b7dfb95fa1b54c8daf4f91083f4f39beac32c70c25616
7
+ data.tar.gz: e4ab436619a690d257fdaba829fcbc00ec74c56cd8fda0c754fd5f1985fe2d3b21adcda1db69f7ca116437ccf8cf9c6455f03cb05ab44b8107033db590266398
data/README.md CHANGED
@@ -11,15 +11,24 @@
11
11
  It does not use exceptions and instead returns a result object.
12
12
  Library is composable and concise.
13
13
 
14
+ ### Getting started
15
+
16
+ #### Installation
17
+ - Update Gemfile with `gem 'dolos'`
18
+ - Run bundle install
19
+
20
+ #### Usage
14
21
  ```ruby
22
+ require 'dolos'
15
23
  include Dolos
16
24
 
25
+ ws = c(" ")
17
26
  parser = c("Parsers") >> ws >> c("are") >> ws >> c("great!")
18
- parser.parse("Parsers are great!") # <Result::Success>
27
+ parser.run("Parsers are great!") # <Result::Success>
19
28
 
20
29
  greeter = c("Hello")
21
30
  greet_and_speak = greeter >> c(", ") >> parser
22
- greet_and_speak.parse("Hello, Parsers are great!") # <Result::Success>
31
+ greet_and_speak.run("Hello, Parsers are great!") # <Result::Success>
23
32
  ```
24
33
 
25
34
  ### Letter address parser example
@@ -30,12 +39,14 @@ require 'dolos_common_parsers/common_parsers'
30
39
  include Dolos
31
40
 
32
41
  # Include common parsers
33
- # In future this can be more structured, moved them to separate module to prevent breaking changes
42
+ # In future this can be more structured,
43
+ # moved them to separate module to prevent breaking changes
34
44
  include Dolos::CommonParsers
35
45
 
36
46
  # Library usage example
37
47
  # Parse out a name and address from a letter
38
- # For higher difficulty, we will not split this into multiple lines, but instead parse it all at once
48
+ # For higher difficulty, we will not split this into multiple lines,
49
+ # but instead parse it all at once
39
50
  letter = <<-LETTER
40
51
  Mr. Vardeniui Pavardeniui
41
52
  AB „Lietuvos Paštas“
@@ -77,21 +88,29 @@ second_line = ws.rep0 >> company_info >> eol
77
88
  # After that result is captured and mapped to hash
78
89
  # Mapping to hash so at the end its easy to tell tuples apart
79
90
  # Also while mapping, doing some cleaning with '.strip'
80
- street_name = char_while(->(char) { !char.match(/\d/) }).capture!.map(&:first).map { |s| { street: s.strip } }
91
+ street_name = char_while(->(char) { !char.match(/\d/) })
92
+ .capture!
93
+ .map(&:first)
94
+ .map { |s| { street: s.strip } }
81
95
  building = digits.capture!.map(&:first).map { |s| { building: s.strip } }
82
96
  address_line = ws.rep0 >> street_name >> building >> eol
83
97
 
84
98
  # City line
85
- # All digits can be matched here or 'digits.rep(5)' could be used. Also joining with map.
99
+ # All digits can be matched here or 'digits.rep(5)' could be used.
100
+ # Also joining with map results.
86
101
  postcode = digits.capture!.map(&:join).map { |s| { postcode: s.strip } }
87
102
  city = alpha_with_lt.rep.capture!.map(&:join).map { |s| { city: s.strip } }
88
103
  city_line = ws.rep0 >> postcode >> ws >> city >> eol
89
104
 
90
- # Full letter parser which is combined from all previous parsers. All previous parsers can be ran separately.
105
+ # Full letter parser which is combined from all previous parsers.
106
+ # Also, all previous parsers can be ran separately.
91
107
  letter_parser = name_line >> second_line >> address_line >> city_line
92
108
  result = letter_parser.run(letter)
93
109
 
110
+ # List of tuples
94
111
  pp result.captures
112
+ # ["Vardeniui", "Pavardeniui", "Lietuvos Paštas", {:street=>"Totorių g."},
113
+ # {:building=>"8"}, {:postcode=>"01121"}, {:city=>"Vilnius"}]
95
114
 
96
115
  ```
97
116
 
data/lib/dolos/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dolos
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/dolos.rb CHANGED
@@ -109,7 +109,8 @@ module Dolos
109
109
  # repeat(n_min: 2) # 2 or more
110
110
  def repeat(n_min:, n_max: Float::INFINITY)
111
111
  Parser.new do |state|
112
- results = []
112
+ values = []
113
+ captures = []
113
114
  count = 0
114
115
 
115
116
  while count < n_max
@@ -117,7 +118,8 @@ module Dolos
117
118
 
118
119
  break if result.failure?
119
120
 
120
- results << result.value
121
+ values << result.value
122
+ captures.concat(result.captures)
121
123
  state.input.advance(result.length)
122
124
  count += 1
123
125
  end
@@ -125,7 +127,7 @@ module Dolos
125
127
  if count < n_min
126
128
  Failure.new("Expected parser to match at least #{n_min} times but matched only #{count} times", false)
127
129
  else
128
- Success.new(results, 0) # Passing 0, because we already advanced the input and flatmap will advance it again
130
+ Success.new(values, 0, captures)
129
131
  end
130
132
  end
131
133
  end
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - benetis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-15 00:00:00.000000000 Z
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description: Parser combinators library for Ruby. In active development, not stable
14
+ yet.
14
15
  email:
15
16
  - git@benetis.me
16
17
  executables: []
@@ -23,7 +24,6 @@ files:
23
24
  - README.md
24
25
  - Rakefile
25
26
  - docs/dolos_stable_diff.png
26
- - dolos.gemspec
27
27
  - lib/dolos.rb
28
28
  - lib/dolos/parser_state.rb
29
29
  - lib/dolos/parsers.rb
@@ -65,5 +65,5 @@ requirements: []
65
65
  rubygems_version: 3.4.15
66
66
  signing_key:
67
67
  specification_version: 4
68
- summary: Parser combinator library for Ruby.
68
+ summary: Parser combinators library for Ruby.
69
69
  test_files: []
data/dolos.gemspec DELETED
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/dolos/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "dolos"
7
- spec.version = Dolos::VERSION
8
- spec.authors = ["benetis"]
9
- spec.licenses = ['MIT']
10
- spec.email = ["git@benetis.me"]
11
- spec.files = Dir["lib/**/*"]
12
-
13
- spec.summary = "Parser combinator library for Ruby."
14
- spec.homepage = "https://github.com/benetis/dolos"
15
- spec.required_ruby_version = ">= 3.1.0"
16
-
17
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
-
19
- spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/benetis/dolos"
21
- spec.metadata["changelog_uri"] = "https://github.com/benetis/dolos/blob/master/CHANGELOG.md"
22
-
23
- # Specify which files should be added to the gem when it is released.
24
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(__dir__) do
26
- `git ls-files -z`.split("\x0").reject do |f|
27
- (File.expand_path(f) == __FILE__) ||
28
- f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29
- end
30
- end
31
- spec.bindir = "exe"
32
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
34
-
35
- # Uncomment to register a new dependency of your gem
36
- # spec.add_dependency "example-gem", "~> 1.0"
37
-
38
- # For more information and examples about making a new gem, check out our
39
- # guide at: https://bundler.io/guides/creating_gem.html
40
- end