l43_peg 0.1.4 → 0.1.5

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: 1fd34fe9a8f6cc7dbcd31983b498f89d08b5a8cbb2ead84f279bfa1fdc97af2e
4
- data.tar.gz: 8ec2a0361e0a254e484648260d6835971a3c210b454bd30be5dc060e8a77518b
3
+ metadata.gz: 733dffa78d9d7b41f75297f5d9f3b2d8e77212df079cd0643d39fc876b87f91d
4
+ data.tar.gz: c392f1ece61f72a537f673ec9f4f0b235eb6319ea0a90cf553ce11415f396d7f
5
5
  SHA512:
6
- metadata.gz: dbe9a870df4681c29c1fc65b173e831a31efbf15af88ebd3fa2b6003c8c7ac4321eb509b2b737f58051919669465a063c044e5cde187fb18c0de8af506d4a048
7
- data.tar.gz: 28f2eb73223629254c8329799d90abf012d18c67c1ab5ff9627a1b2cf55fd75a92e98b402ac6438ea398fc88f61f328429727066446ff239ffdd5ed7d92cc042
6
+ metadata.gz: 518b0be6499f49df9ec63c334e4067f1b679d592eed92150b17057f71b4eaa54527339e30e2c48f1a7dbd2538cffc2b9c1df16de318857309b097be7ce475c4e
7
+ data.tar.gz: 3be2dfb70569649ae360192b0fca8f6beb923127a8020992881b158cc11fc2954848a424ba7ee8801410a4882c4b3246a9ec9716e31eaf482c1ba5b98d2b8326
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # L43Peg
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/l43_peg.svg)](https://badge.fury.io/rb/l43_peg)
4
+
3
5
  ## A Parse Expression Grammar library for Ruby
4
6
 
5
7
  ### This Version (v0.1.x) is Alpha Quality (many PEG features are missing, like recursion and even alternatives.
@@ -68,9 +70,36 @@ When we map the parser
68
70
  Then we can convert the string valus
69
71
 
70
72
  ```ruby
71
- assert_parse_success(parser, %w[--start=42 --end=44 --inc=2], ast: {start: 42, end: 44, inc: 2}, rest: [])
73
+ assert_parse_success(int_arg_parser, %w[--start=42 --end=44 --inc=2], ast: {start: 42, end: 44, inc: 2}, rest: [])
74
+ ```
75
+
76
+ #### Context: Knowing When To Stop
77
+
78
+ An argument parser that respects itself provides a means to _end_ argument parsing even if more matches follow.
79
+ An exmaple for that is the posix argument `--`
80
+
81
+ We can use whatever we want in `args_parser`, here is a variation:
82
+
83
+ Given the specification
84
+
85
+ ```ruby
86
+ let :args do
87
+ {
88
+ width: "w:(\\d+)",
89
+ height: "h:(\\d+)",
90
+ __stop: "(::)"
91
+ }
92
+ end
93
+ let(:wh_parser) {args_parser(args, stop: :__stop, &:to_i)}
72
94
  ```
73
95
 
96
+ Then parsing the following input
97
+
98
+ ```ruby
99
+ input = %w[h:42 w:73 :: w:74]
100
+ ast = {height: 42, width: 73}
101
+ assert_parse_success(wh_parser, input, ast:, rest: %w[w:74])
102
+ ```
74
103
 
75
104
  ## Author
76
105
 
@@ -24,9 +24,14 @@ module L43Peg
24
24
  count += 1
25
25
  in L43Peg::Failure
26
26
  if count < min
27
- return fail_parser("many #{name} should match at least #{min} times but did only #{count} times")
27
+ return fail_parser("many #{name} should match at least #{min} times but did only #{count} times", input:)
28
28
  end
29
29
  return succeed_parser(ast, curr_input, cache: curr_cache)
30
+ in L43Peg::Stop
31
+ if count < min
32
+ return fail_parser("many #{name} should match at least #{min} times but did only #{count} times", input:)
33
+ end
34
+ return succeed_parser(ast, curr_input.drop, cache: curr_cache)
30
35
  end
31
36
  end
32
37
  end
@@ -13,8 +13,8 @@ module L43Peg
13
13
  include L43Peg::Mappers
14
14
  include L43Peg::Parsers
15
15
 
16
- def args_parser(definitions, name: nil, &block)
17
- parser = tokens_parser(definitions, &block)
16
+ def args_parser(definitions, name: nil, stop: nil, &block)
17
+ parser = tokens_parser(definitions, stop:, &block)
18
18
  name = name || "args_parser(#{definitions.inspect})"
19
19
  inner = many(parser)
20
20
  map(inner, name:, fn: join_maps)
@@ -2,8 +2,8 @@
2
2
 
3
3
  module L43Peg
4
4
  module Helper
5
- def fail_parser(reason)
6
- L43Peg::Failure.new(reason:)
5
+ def fail_parser(reason, input: nil)
6
+ L43Peg::Failure.new(reason:, input:)
7
7
  end
8
8
  def succeed_parser(ast, input, cache: nil)
9
9
  L43Peg::Success.new(ast:, rest: input, cache: cache || input.cache)
@@ -6,13 +6,13 @@ module L43Peg
6
6
 
7
7
  private
8
8
 
9
- def initialize(map, name: nil, &blk)
9
+ def initialize(map, name: nil, stop: nil, &blk)
10
10
  map = _check_arg!(map)
11
11
  name = name || "tokens_parser(#{map.inspect})"
12
12
  super(name) do |tokens, cache, _name|
13
13
  case _find_matching(map, tokens)
14
14
  in [token, success]
15
- _succeed(tokens:, token:, success:, &blk)
15
+ _succeed(tokens:, token:, stop:, success:, &blk)
16
16
  else
17
17
  reason = "no token matches (in #{name})"
18
18
  L43Peg::Failure.new(cache:, input: tokens, parsed_by: self, reason:)
@@ -47,7 +47,9 @@ module L43Peg
47
47
  end.to_h
48
48
  end
49
49
 
50
- def _succeed(success:, token:, tokens:, &blk)
50
+ def _succeed(stop:, success:, token:, tokens:, &blk)
51
+ return L43Peg::Stop.new if token == stop
52
+
51
53
  result = if blk
52
54
  blk.(success.ast)
53
55
  else
@@ -12,7 +12,8 @@ module L43Peg
12
12
  def int_parser = L43Peg::Parsers::IntParser.instance
13
13
  def rgx_parser(rgx, name: nil, **o) = L43Peg::Parsers::RgxParser.new(rgx, name:, **o)
14
14
  def token_parser(spc, name: nil, **o) = L43Peg::Parsers::TokenParser.new(spc, name:, **o)
15
- def tokens_parser(map, name: nil, &b) = L43Peg::Parsers::TokensParser.new(map, name:, &b)
15
+ def tokens_parser(map, name: nil, stop: nil, &b) =
16
+ L43Peg::Parsers::TokensParser.new(map, name:, stop:, &b)
16
17
  def verb_parser(verb, name: nil) = L43Peg::Parsers::VerbParser.new(verb, name:)
17
18
  end
18
19
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43Peg
4
+ class Stop
5
+ extend L43::OpenObject
6
+ attributes
7
+ # A placeholder to halt many
8
+ end
9
+ end
10
+ # SPDX-License-Identifier: Apache-2.0
data/lib/l43_peg.rb CHANGED
@@ -5,6 +5,6 @@ require_relative 'l43/open_object'
5
5
  require_subdir {}
6
6
 
7
7
  module L43Peg
8
- VERSION = "0.1.4"
8
+ VERSION = "0.1.5"
9
9
  end
10
10
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l43_peg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
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-22 00:00:00.000000000 Z
11
+ date: 2024-04-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  A (still incomplete) PEG Library
@@ -43,9 +43,10 @@ files:
43
43
  - lib/l43_peg/parsers/token_parser.rb
44
44
  - lib/l43_peg/parsers/tokens_parser.rb
45
45
  - lib/l43_peg/parsers/verb_parser.rb
46
+ - lib/l43_peg/stop.rb
46
47
  - lib/l43_peg/success.rb
47
48
  - lib/l43_peg/tokens.rb
48
- homepage: https://gitlab.com/l43-gems/l43_peg
49
+ homepage: https://codeberg.org/lab419/l43_peg
49
50
  licenses:
50
51
  - AGPL-3.0-or-later
51
52
  metadata: {}