l43_peg 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43Peg
4
+ module Parsers
5
+ class TokensParser < L43Peg::Parser
6
+
7
+ private
8
+
9
+ def initialize(map, name: nil, &blk)
10
+ map = _check_arg!(map)
11
+ name = name || "tokens_parser(#{map.inspect})"
12
+ super(name) do |tokens, cache, _name|
13
+ case _find_matching(map, tokens)
14
+ in [token, success]
15
+ _succeed(tokens:, token:, success:, &blk)
16
+ else
17
+ reason = "no token matches (in #{name})"
18
+ L43Peg::Failure.new(cache:, input: tokens, parsed_by: self, reason:)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ def _check_arg!(map)
25
+ case map
26
+ when Hash
27
+ _mk_tokens(map)
28
+ else
29
+ raise ArgumentError, "#{map.inspect} must be a regular expression or a string"
30
+ end
31
+ end
32
+
33
+ def _find_matching(map, tokens)
34
+ map.each do |token, token_parser|
35
+ case token_parser.(tokens)
36
+ in L43Peg::Success => success
37
+ return [token, success]
38
+ else
39
+ end
40
+ end
41
+ nil
42
+ end
43
+
44
+ def _mk_tokens(map)
45
+ map.map do |token, spec|
46
+ [token, TokenParser.new(Regexp.compile(spec), capture: 1)]
47
+ end.to_h
48
+ end
49
+
50
+ def _succeed(success:, token:, tokens:, &blk)
51
+ result = if blk
52
+ blk.(success.ast)
53
+ else
54
+ success.ast
55
+ end
56
+ L43Peg::Success.new(ast: {token => result}, cache: success.cache, rest: success.rest, position: tokens.position)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43Peg
4
+ module Parsers
5
+ class VerbParser < L43Peg::Parser
6
+
7
+ private
8
+
9
+ def initialize(verb, name: nil)
10
+ name = name || "verb_parser(#{verb.inspect})"
11
+ super(name) do |input, cache, _name|
12
+ if input.input.start_with?(verb)
13
+ L43Peg::Success.new(ast: verb, cache:, rest: input.drop(verb), position: input.position)
14
+ else
15
+ L43Peg::Failure.new(cache:, input:, parsed_by: self, reason: "input does not start with #{verb.inspect} (in #{name})")
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'combinators'
4
+ require_subdir {}
5
+
6
+ module L43Peg
7
+ module Parsers extend self
8
+ def args_parser(args, name: nil) = L43Peg::Parsers::ArgsParser.new(args, name:)
9
+ def char_parser(charset = nil) = L43Peg::Parsers::CharParser.new(charset)
10
+ def end_parser = L43Peg::Parsers::EndParser.instance
11
+ def failure_parser = L43Peg::Parsers::FailureParser.instance
12
+ def int_parser = L43Peg::Parsers::IntParser.instance
13
+ def rgx_parser(rgx, name: nil, **o) = L43Peg::Parsers::RgxParser.new(rgx, name:, **o)
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)
16
+ def verb_parser(verb, name: nil) = L43Peg::Parsers::VerbParser.new(verb, name:)
17
+ end
18
+ end
19
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43Peg
4
+ class Success
5
+ def _init
6
+ return if @position
7
+ @position = _position
8
+ end
9
+ extend L43::OpenObject
10
+
11
+ attributes ast: nil, cache: nil, position: nil, rest: nil
12
+
13
+ def map(&mapper)
14
+ self.class.new(ast: mapper.(ast), cache:, position:, rest:)
15
+ end
16
+
17
+ private
18
+
19
+
20
+ def _position
21
+ case @rest
22
+ when Tokens
23
+ 1
24
+ else
25
+ [1, 1]
26
+ end
27
+ end
28
+ end
29
+ end
30
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43Peg
4
+ class Tokens
5
+ extend L43::OpenObject
6
+
7
+ attributes tnb: 1, tokens: [], context: {}
8
+
9
+ def drop(by=nil)
10
+ by = by || 1
11
+ return self if empty?
12
+ self.class.new(tokens: input.drop(by), context:, tnb: tnb + by)
13
+ end
14
+
15
+ def empty? = tokens.empty?
16
+ def head = tokens.first
17
+ def input = tokens
18
+
19
+ def match(str_or_rgx, option)
20
+ return if empty?
21
+
22
+ case str_or_rgx
23
+ when String
24
+ head == str_or_rgx && head
25
+ when Regexp
26
+ _match_rgx(str_or_rgx, option || 0)
27
+ end
28
+ end
29
+
30
+ def position = tnb
31
+
32
+ private
33
+
34
+ def _match_rgx(rgx, option)
35
+ md = rgx.match(head)
36
+ return unless md
37
+
38
+ if option == :all
39
+ md.to_a
40
+ else
41
+ md[option]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ # SPDX-License-Identifier: Apache-2.0
data/lib/l43_peg.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'l43/require_helper'
4
+ require_relative 'l43/open_object'
5
+ require_subdir {}
6
+
3
7
  module L43Peg
4
- VERSION = "0.0.1"
5
-
8
+ VERSION = "0.1.1"
6
9
  end
7
- # SPDX-License-Identifier: Apache-2.0
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.0.1
4
+ version: 0.1.1
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-03-10 00:00:00.000000000 Z
11
+ date: 2024-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  **PLACEHOLDER**
@@ -26,7 +26,30 @@ extra_rdoc_files: []
26
26
  files:
27
27
  - LICENSE
28
28
  - README.md
29
+ - lib/l43/match_data_extenstion.rb
30
+ - lib/l43/open_object.rb
31
+ - lib/l43/require_helper.rb
29
32
  - lib/l43_peg.rb
33
+ - lib/l43_peg/cache.rb
34
+ - lib/l43_peg/combinators.rb
35
+ - lib/l43_peg/combinators/many.rb
36
+ - lib/l43_peg/combinators/seq.rb
37
+ - lib/l43_peg/failure.rb
38
+ - lib/l43_peg/helper.rb
39
+ - lib/l43_peg/input.rb
40
+ - lib/l43_peg/mappers.rb
41
+ - lib/l43_peg/parser.rb
42
+ - lib/l43_peg/parsers.rb
43
+ - lib/l43_peg/parsers/char_parser.rb
44
+ - lib/l43_peg/parsers/end_parser.rb
45
+ - lib/l43_peg/parsers/failure_parser.rb
46
+ - lib/l43_peg/parsers/int_parser.rb
47
+ - lib/l43_peg/parsers/rgx_parser.rb
48
+ - lib/l43_peg/parsers/token_parser.rb
49
+ - lib/l43_peg/parsers/tokens_parser.rb
50
+ - lib/l43_peg/parsers/verb_parser.rb
51
+ - lib/l43_peg/success.rb
52
+ - lib/l43_peg/tokens.rb
30
53
  homepage: https://gitlab.com/l43-gems/l43_peg
31
54
  licenses:
32
55
  - Apache-2.0