l43_peg 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 733dffa78d9d7b41f75297f5d9f3b2d8e77212df079cd0643d39fc876b87f91d
4
- data.tar.gz: c392f1ece61f72a537f673ec9f4f0b235eb6319ea0a90cf553ce11415f396d7f
3
+ metadata.gz: e5b92019011d80442cfe66f4b3a841a31bba54547efd479a2d19b1f7e687adef
4
+ data.tar.gz: 75cb9467e13dc72e773bebe345b68b05d431bdbfc6671c7bc215bd063a86cd6c
5
5
  SHA512:
6
- metadata.gz: 518b0be6499f49df9ec63c334e4067f1b679d592eed92150b17057f71b4eaa54527339e30e2c48f1a7dbd2538cffc2b9c1df16de318857309b097be7ce475c4e
7
- data.tar.gz: 3be2dfb70569649ae360192b0fca8f6beb923127a8020992881b158cc11fc2954848a424ba7ee8801410a4882c4b3246a9ec9716e31eaf482c1ba5b98d2b8326
6
+ metadata.gz: ab3a4841408511554c96725a7659d7b5928336d195323e06dd8c707f247d48f5e2b7d1831768e018de7fdcfbd3d2dfba0aa791d10b1ab422fb08f8b225797066
7
+ data.tar.gz: be59e8841fcc4e9f22194555fa40c8573805a589f119a728a216e256d729fcdd4ce4abaac76df0547a109415b83208e333fc209ef00bb9e46fcd03ec3dafbd85
data/README.md CHANGED
@@ -101,7 +101,49 @@ Then parsing the following input
101
101
  assert_parse_success(wh_parser, input, ast:, rest: %w[w:74])
102
102
  ```
103
103
 
104
- ## Author
104
+ ### Context: User Interface
105
+
106
+ #### Context: Exposing the args_parser
107
+
108
+ Above we have seen that we had to include an internal module so to get access to the `args_parser`.
109
+ Client code might not want to use these intrusive methods and therefore the parsers are also exposed
110
+ as module methods
111
+
112
+ Given an _exposed_ `args_parser`
113
+ ```ruby
114
+ let :parser do
115
+ L43Peg::Parsers.args_parser(
116
+ {
117
+ negative: "(-\\d+)",
118
+ positive: "\\+?(\\d+)"
119
+ },
120
+ &:to_i
121
+ )
122
+ end
123
+ ```
124
+
125
+ But we are also not interested in the internal representation of success and failure of parsing which was
126
+ used in the speculations above. Nor do we want to transform our input into the internal representations
127
+ as was done above by the helpers. (If you need to see the details of this you can inspect the
128
+ file [`parser_test.rb` in `spec/support`](spec/support/parser_test.rb))
129
+
130
+ Then we can uses the interface of `L43Peg`
131
+
132
+ ```ruby
133
+ L43Peg.parse_tokens(parser, %w[43 -44 +45]) => :ok, result
134
+ expect(result).to eq(positive: [43, 45], negative: -44)
135
+ ```
136
+
137
+ And if we get an error the result is as follows
138
+
139
+ ```ruby
140
+ parser = L43Peg::Parsers.char_parser('a')
141
+ L43Peg.parse_string(parser, 'b') => :error, message
142
+ expect(message).to eq("char \"b\"")
143
+ ```
144
+
145
+
146
+ # Author
105
147
 
106
148
  Copyright © 2024 Robert Dober
107
149
  robert.dober@gmail.com
@@ -5,7 +5,8 @@ require_subdir {}
5
5
 
6
6
  module L43Peg
7
7
  module Parsers extend self
8
- def args_parser(args, name: nil) = L43Peg::Parsers::ArgsParser.new(args, name:)
8
+ def args_parser(args, name: nil, stop: nil, &blk) =
9
+ L43Peg::Combinators.args_parser(args, name:, stop:, &blk)
9
10
  def char_parser(charset = nil) = L43Peg::Parsers::CharParser.new(charset)
10
11
  def end_parser = L43Peg::Parsers::EndParser.instance
11
12
  def failure_parser = L43Peg::Parsers::FailureParser.instance
data/lib/l43_peg.rb CHANGED
@@ -4,7 +4,28 @@ require_relative 'l43/require_helper'
4
4
  require_relative 'l43/open_object'
5
5
  require_subdir {}
6
6
 
7
- module L43Peg
8
- VERSION = "0.1.5"
7
+ module L43Peg extend self
8
+ VERSION = "0.1.6"
9
+
10
+ def parse_string(parser, input, lnb: 1, col: 1, context: {})
11
+ input = Input.new(input:, col:, lnb:, context:)
12
+ _parse(parser, input)
13
+ end
14
+
15
+ def parse_tokens(parser, tokens, tnb: 1, context: {})
16
+ tokens = Tokens.new(tokens:, tnb:, context:)
17
+ _parse(parser, tokens)
18
+ end
19
+
20
+ private
21
+
22
+ def _parse(parser, input)
23
+ case parser.(input)
24
+ in Success => success
25
+ [:ok, success.ast]
26
+ in Failure => failure
27
+ [:error, failure.reason]
28
+ end
29
+ end
9
30
  end
10
31
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l43_peg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober