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 +4 -4
- data/README.md +43 -1
- data/lib/l43_peg/parsers.rb +2 -1
- data/lib/l43_peg.rb +23 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5b92019011d80442cfe66f4b3a841a31bba54547efd479a2d19b1f7e687adef
|
4
|
+
data.tar.gz: 75cb9467e13dc72e773bebe345b68b05d431bdbfc6671c7bc215bd063a86cd6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/l43_peg/parsers.rb
CHANGED
@@ -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
|
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.
|
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
|