l43_peg 0.1.5 → 0.1.7

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: 373ca9885275389a284235de2dc5f95eb2f470aeece99bf47e9b54ed6e3fdf60
4
+ data.tar.gz: 62ac1897c26b540da5205d7b0fcb6aa1ff472f036e36ca5389c3ac4767fbf926
5
5
  SHA512:
6
- metadata.gz: 518b0be6499f49df9ec63c334e4067f1b679d592eed92150b17057f71b4eaa54527339e30e2c48f1a7dbd2538cffc2b9c1df16de318857309b097be7ce475c4e
7
- data.tar.gz: 3be2dfb70569649ae360192b0fca8f6beb923127a8020992881b158cc11fc2954848a424ba7ee8801410a4882c4b3246a9ec9716e31eaf482c1ba5b98d2b8326
6
+ metadata.gz: b51cd93714348ba6be2394cd63543603af953820394a6cc201a39c4721d1a10a4ec4bf224ad84c3a9eae49f426126da35dcc0b8cd751f9d6e6265f0341172daf
7
+ data.tar.gz: edbcd0d28cedcb395a6721a28f5f2284fefdaa28c1d7eb6449d8f53a90a00219177aace6e15208f87bd12488ab01712aa015b75179d0195decc34b1d78bba1a7
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
@@ -2,13 +2,11 @@
2
2
 
3
3
  module L43Peg
4
4
  class Success
5
- def _init
6
- return if @position
7
- @position = _position
8
- end
9
5
  extend L43::OpenObject
10
6
 
11
- attributes ast: nil, cache: nil, position: nil, rest: nil
7
+ attributes ast: nil, cache: nil, position: nil, rest: nil do
8
+ @position ||= _position
9
+ end
12
10
 
13
11
  def map(&mapper)
14
12
  self.class.new(ast: mapper.(ast), cache:, position:, rest:)
@@ -16,7 +14,6 @@ module L43Peg
16
14
 
17
15
  private
18
16
 
19
-
20
17
  def _position
21
18
  case @rest
22
19
  when Tokens
data/lib/l43_peg.rb CHANGED
@@ -1,10 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'l43/open_object'
4
+
3
5
  require_relative 'l43/require_helper'
4
- require_relative 'l43/open_object'
5
6
  require_subdir {}
6
7
 
7
- module L43Peg
8
- VERSION = "0.1.5"
8
+ module L43Peg extend self
9
+ VERSION = "0.1.7"
10
+
11
+ def parse_string(parser, input, lnb: 1, col: 1, context: {})
12
+ input = Input.new(input:, col:, lnb:, context:)
13
+ _parse(parser, input)
14
+ end
15
+
16
+ def parse_tokens(parser, tokens, tnb: 1, context: {})
17
+ tokens = Tokens.new(tokens:, tnb:, context:)
18
+ _parse(parser, tokens)
19
+ end
20
+
21
+ private
22
+
23
+ def _parse(parser, input)
24
+ case parser.(input)
25
+ in Success => success
26
+ [:ok, success.ast]
27
+ in Failure => failure
28
+ [:error, failure.reason]
29
+ end
30
+ end
9
31
  end
10
32
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,15 +1,29 @@
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.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-23 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: l43_open_object
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.1
13
27
  description: |
14
28
  A (still incomplete) PEG Library
15
29
 
@@ -22,7 +36,6 @@ files:
22
36
  - LICENSE
23
37
  - README.md
24
38
  - lib/l43/match_data_extenstion.rb
25
- - lib/l43/open_object.rb
26
39
  - lib/l43/require_helper.rb
27
40
  - lib/l43_peg.rb
28
41
  - lib/l43_peg/cache.rb
@@ -65,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
78
  - !ruby/object:Gem::Version
66
79
  version: '0'
67
80
  requirements: []
68
- rubygems_version: 3.5.6
81
+ rubygems_version: 3.5.8
69
82
  signing_key:
70
83
  specification_version: 4
71
84
  summary: A (still incomplete) PEG Library
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module L43
4
- module OpenObject
5
- def attributes(*atts, **defaults)
6
- attr_reader(*atts)
7
- attr_reader(*defaults.keys)
8
-
9
- define_method :== do |other|
10
- other&.to_h == to_h
11
- end
12
-
13
- define_method :initialize do |**kwds|
14
- missing = atts - kwds.keys
15
- raise ArgumentError, "missing required keyword parameters: #{missing.inspect}" unless missing.empty?
16
- spurious = kwds.keys - atts - defaults.keys
17
- raise ArgumentError, "spurious keyword parameters: #{spurious.inspect}" unless spurious.empty?
18
-
19
- values = defaults.merge(kwds)
20
- values.each do |key, value|
21
- instance_variable_set("@#{key}", value)
22
- end
23
- _init if respond_to?(:_init)
24
- end
25
-
26
- define_method :to_h do |*|
27
- first = atts.inject Hash.new do |h, attribute|
28
- h.update(attribute => send(attribute))
29
- end
30
- defaults.keys.inject first do |h, attribute|
31
- h.update(attribute => send(attribute))
32
- end
33
- end
34
- alias_method :deconstruct_keys, :to_h
35
- end
36
- end
37
- end
38
- # SPDX-License-Identifier: AGPL-3.0-or-later