rparsec2 1.2.1 → 1.3.0

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rparsec/alt_parser.rb +38 -0
  3. data/lib/rparsec/any_parser.rb +12 -0
  4. data/lib/rparsec/are_parser.rb +20 -0
  5. data/lib/rparsec/atom_parser.rb +16 -0
  6. data/lib/rparsec/best_parser.rb +44 -0
  7. data/lib/rparsec/bound_parser.rb +11 -0
  8. data/lib/rparsec/boundn_parser.rb +11 -0
  9. data/lib/rparsec/catch_parser.rb +17 -0
  10. data/lib/rparsec/context.rb +5 -3
  11. data/lib/rparsec/{misc.rb → def_helper.rb} +5 -8
  12. data/lib/rparsec/eof_parser.rb +11 -0
  13. data/lib/rparsec/error.rb +8 -12
  14. data/lib/rparsec/expect_parser.rb +18 -0
  15. data/lib/rparsec/expressions.rb +5 -19
  16. data/lib/rparsec/failure_parser.rb +10 -0
  17. data/lib/rparsec/failures.rb +31 -0
  18. data/lib/rparsec/followed_parser.rb +13 -0
  19. data/lib/rparsec/fragment_parser.rb +12 -0
  20. data/lib/rparsec/functor_mixin.rb +10 -31
  21. data/lib/rparsec/get_index_parser.rb +9 -0
  22. data/lib/rparsec/keywords.rb +9 -9
  23. data/lib/rparsec/lazy_parser.rb +10 -0
  24. data/lib/rparsec/locator.rb +9 -5
  25. data/lib/rparsec/look_ahead_sensitive_parser.rb +46 -0
  26. data/lib/rparsec/many__parser.rb +20 -0
  27. data/lib/rparsec/many_parser.rb +27 -0
  28. data/lib/rparsec/map_current_parser.rb +11 -0
  29. data/lib/rparsec/map_parser.rb +12 -0
  30. data/lib/rparsec/mapn_current_parser.rb +11 -0
  31. data/lib/rparsec/mapn_parser.rb +12 -0
  32. data/lib/rparsec/nested_parser.rb +47 -0
  33. data/lib/rparsec/not_parser.rb +27 -0
  34. data/lib/rparsec/one_parser.rb +9 -0
  35. data/lib/rparsec/operator_table.rb +29 -63
  36. data/lib/rparsec/parser.rb +14 -20
  37. data/lib/rparsec/parser_monad.rb +4 -17
  38. data/lib/rparsec/parsers.rb +52 -626
  39. data/lib/rparsec/peek_parser.rb +16 -0
  40. data/lib/rparsec/plus_parser.rb +32 -0
  41. data/lib/rparsec/regexp_parser.rb +17 -0
  42. data/lib/rparsec/repeat__parser.rb +13 -0
  43. data/lib/rparsec/repeat_parser.rb +15 -0
  44. data/lib/rparsec/satisfies_parser.rb +15 -0
  45. data/lib/rparsec/sequence_parser.rb +25 -0
  46. data/lib/rparsec/set_index_parser.rb +10 -0
  47. data/lib/rparsec/some__parser.rb +19 -0
  48. data/lib/rparsec/some_parser.rb +28 -0
  49. data/lib/rparsec/string_case_insensitive_parser.rb +26 -0
  50. data/lib/rparsec/throw_parser.rb +10 -0
  51. data/lib/rparsec/token.rb +9 -28
  52. data/lib/rparsec/token_parser.rb +14 -0
  53. data/lib/rparsec/value_parser.rb +10 -0
  54. data/lib/rparsec/watch_parser.rb +11 -0
  55. data/lib/rparsec/watchn_parser.rb +11 -0
  56. data/lib/rparsec/zero_parser.rb +9 -0
  57. data/lib/rparsec.rb +1 -1
  58. metadata +52 -7
  59. data/lib/rparsec/id_monad.rb +0 -19
@@ -0,0 +1,16 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class PeekParser < Parser # :nodoc:
5
+ init :parser
6
+ def _parse ctxt
7
+ ind = ctxt.index
8
+ return false unless @parser._parse ctxt
9
+ ctxt.index = ind
10
+ return true
11
+ end
12
+ def peek
13
+ self
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require "rparsec/look_ahead_sensitive_parser"
2
+ require "rparsec/failures"
3
+
4
+ module RParsec
5
+ class PlusParser < LookAheadSensitiveParser # :nodoc:
6
+ def initialize(alts, la = 1)
7
+ super(la)
8
+ @alts = alts
9
+ end
10
+ def _parse ctxt
11
+ ind = ctxt.index
12
+ result = ctxt.result
13
+ err = ctxt.error
14
+ for p in @alts
15
+ ctxt.reset_error
16
+ ctxt.index = ind
17
+ ctxt.result = result
18
+ return true if p._parse(ctxt)
19
+ return false unless visible(ctxt, ind)
20
+ err = Failures.add_error(err, ctxt.error)
21
+ end
22
+ ctxt.error = err
23
+ return false
24
+ end
25
+ def withLookahead(n)
26
+ PlusParser.new(@alts, n)
27
+ end
28
+ def plus other
29
+ PlusParser.new(@alts.dup << other, @lookahead).tap { |p| p.name = name }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class RegexpParser < Parser # :nodoc:
5
+ init :ptn, :msg
6
+ def _parse ctxt
7
+ scanner = ctxt.scanner
8
+ result = scanner.check @ptn
9
+ if result.nil?
10
+ ctxt.expecting(@msg)
11
+ else
12
+ ctxt.advance(scanner.matched_size)
13
+ ctxt.retn(result)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class Repeat_Parser < Parser # :nodoc:
5
+ init :parser, :times
6
+ def _parse ctxt
7
+ @times.times do
8
+ return false unless @parser._parse ctxt
9
+ end
10
+ return true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class RepeatParser < Parser # :nodoc:
5
+ init :parser, :times
6
+ def _parse ctxt
7
+ result = []
8
+ @times.times do
9
+ return false unless @parser._parse ctxt
10
+ result << ctxt.result
11
+ end
12
+ return ctxt.retn(result)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class SatisfiesParser < Parser # :nodoc:
5
+ init :pred, :expected
6
+ def _parse ctxt
7
+ elem = nil
8
+ if ctxt.eof || !@pred.call(elem = ctxt.current)
9
+ return ctxt.expecting(@expected)
10
+ end
11
+ ctxt.next
12
+ ctxt.retn elem
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class SequenceParser < Parser # :nodoc:
5
+ init :parsers, :proc
6
+ def _parse ctxt
7
+ if @proc.nil?
8
+ for p in @parsers
9
+ return false unless p._parse(ctxt)
10
+ end
11
+ else
12
+ results = []
13
+ for p in @parsers
14
+ return false unless p._parse(ctxt)
15
+ results << ctxt.result
16
+ end
17
+ ctxt.retn(@proc.call(*results))
18
+ end
19
+ return true
20
+ end
21
+ def seq(other, &block)
22
+ SequenceParser.new(@parsers.dup << other, &block)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ require 'rparsec/parser'
2
+
3
+ module RParsec
4
+ class SetIndexParser < Parser # :nodoc:
5
+ init :index
6
+ def _parse ctxt
7
+ ctxt.index = @index
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class Some_Parser < Parser # :nodoc:
5
+ init :parser, :least, :max
6
+ def _parse ctxt
7
+ @least.times { return false unless @parser._parse ctxt }
8
+ (@least...@max).each do
9
+ ind = ctxt.index
10
+ if @parser._parse ctxt
11
+ return true if ind == ctxt.index # infinite loop
12
+ next
13
+ end
14
+ return ind == ctxt.index
15
+ end
16
+ return true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class SomeParser < Parser # :nodoc:
5
+ init :parser, :least, :max
6
+ def _parse ctxt
7
+ result = []
8
+ @least.times do
9
+ return false unless @parser._parse ctxt
10
+ result << ctxt.result
11
+ end
12
+ (@least...@max).each do
13
+ ind = ctxt.index
14
+ if @parser._parse ctxt
15
+ result << ctxt.result
16
+ return ctxt.retn(result) if ind == ctxt.index # infinite loop
17
+ next
18
+ end
19
+ if ind == ctxt.index
20
+ return ctxt.retn(result)
21
+ else
22
+ return false
23
+ end
24
+ end
25
+ return ctxt.retn(result)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class StringCaseInsensitiveParser < Parser # :nodoc:
5
+ init :str, :msg
6
+ def _downcase c
7
+ case when c.ord >= ?A.ord && c.ord <= ?Z.ord then (c.ord + (?a.ord - ?A.ord)).chr else c end
8
+ end
9
+ private :_downcase
10
+
11
+ def _parse ctxt
12
+ if @str.length > ctxt.available
13
+ return ctxt.expecting(@msg)
14
+ end
15
+ cur = 0
16
+ for cur in (0...@str.length)
17
+ if _downcase(@str[cur]) != _downcase(ctxt.peek(cur))
18
+ return ctxt.expecting(@msg)
19
+ end
20
+ end
21
+ result = ctxt.src[ctxt.index, @str.length]
22
+ ctxt.advance(@str.length)
23
+ ctxt.retn result
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class ThrowParser < Parser # :nodoc:
5
+ init :symbol
6
+ def _parse _ctxt
7
+ throw @symbol
8
+ end
9
+ end
10
+ end
data/lib/rparsec/token.rb CHANGED
@@ -1,45 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rparsec/misc'
4
-
5
3
  module RParsec
6
4
 
7
5
  #
8
6
  # Represents a token during lexical analysis.
9
7
  #
10
- class Token
11
- extend DefHelper
12
-
13
- def_ctor :kind, :text, :index
14
-
15
- #
16
- # The type of the token
17
- #
18
- attr_reader :kind
19
-
20
- #
21
- # The text of the matched range
22
- #
23
- attr_reader :text
24
-
25
- #
26
- # The starting index of the matched range
27
- #
28
- attr_reader :index
8
+ # kind :: The type of the token
9
+ # text :: The text of the matched range
10
+ # index :: The starting index of the matched range
11
+ #
12
+ Token = Data.define(:kind, :text, :index)
29
13
 
14
+ class Token
30
15
  #
31
16
  # The length of the token.
32
17
  #
33
- def length
34
- @text.length
35
- end
18
+ def length = @text.length
36
19
 
37
20
  #
38
21
  # String representation of the token.
39
22
  #
40
- def to_s
41
- "#{@kind}: #{@text}"
42
- end
23
+ def to_s = "#{@kind}: #{@text}"
43
24
  end
44
25
 
45
- end # module
26
+ end # module
@@ -0,0 +1,14 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class TokenParser < Parser # :nodoc:
5
+ init :symbol, :parser
6
+ def _parse ctxt
7
+ ind = ctxt.index
8
+ return false unless @parser._parse ctxt
9
+ raw = ctxt.result
10
+ raw = ctxt.src[ind, ctxt.index - ind] unless raw.kind_of? String
11
+ ctxt.retn(Token.new(@symbol, raw, ind))
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class ValueParser < Parser # :nodoc:
5
+ init :value
6
+ def _parse ctxt
7
+ ctxt.retn @value
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class WatchParser < Parser # :nodoc:
5
+ init :proc
6
+ def _parse ctxt
7
+ @proc.call(ctxt.result)
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class WatchnParser < Parser # :nodoc:
5
+ init :proc
6
+ def _parse ctxt
7
+ @proc.call(*ctxt.result)
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require "rparsec/parser"
2
+
3
+ module RParsec
4
+ class ZeroParser < Parser # :nodoc:
5
+ def _parse ctxt
6
+ return ctxt.failure
7
+ end
8
+ end
9
+ end
data/lib/rparsec.rb CHANGED
@@ -6,5 +6,5 @@ require "rparsec/keywords"
6
6
  require "rparsec/expressions"
7
7
 
8
8
  module RParsec
9
- VERSION = "1.2.1"
9
+ VERSION = "1.3.0"
10
10
  end
metadata CHANGED
@@ -1,39 +1,84 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rparsec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Yu
8
+ - gemmaro
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2024-11-17 00:00:00.000000000 Z
12
+ date: 2025-01-20 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description: rparsec is a recursive descent parser combinator framework. Declarative
14
15
  API allows creating parser intuitively and dynamically.
15
- email: ajoo.email@gmail.com
16
+ email:
17
+ - ajoo.email@gmail.com
18
+ - gemmaro.dev@gmail.com
16
19
  executables: []
17
20
  extensions: []
18
21
  extra_rdoc_files: []
19
22
  files:
20
23
  - lib/rparsec.rb
24
+ - lib/rparsec/alt_parser.rb
25
+ - lib/rparsec/any_parser.rb
26
+ - lib/rparsec/are_parser.rb
27
+ - lib/rparsec/atom_parser.rb
28
+ - lib/rparsec/best_parser.rb
29
+ - lib/rparsec/bound_parser.rb
30
+ - lib/rparsec/boundn_parser.rb
31
+ - lib/rparsec/catch_parser.rb
21
32
  - lib/rparsec/context.rb
33
+ - lib/rparsec/def_helper.rb
34
+ - lib/rparsec/eof_parser.rb
22
35
  - lib/rparsec/error.rb
36
+ - lib/rparsec/expect_parser.rb
23
37
  - lib/rparsec/expressions.rb
38
+ - lib/rparsec/failure_parser.rb
39
+ - lib/rparsec/failures.rb
40
+ - lib/rparsec/followed_parser.rb
41
+ - lib/rparsec/fragment_parser.rb
24
42
  - lib/rparsec/functor_mixin.rb
25
43
  - lib/rparsec/functors.rb
26
- - lib/rparsec/id_monad.rb
44
+ - lib/rparsec/get_index_parser.rb
27
45
  - lib/rparsec/keywords.rb
46
+ - lib/rparsec/lazy_parser.rb
28
47
  - lib/rparsec/locator.rb
29
- - lib/rparsec/misc.rb
48
+ - lib/rparsec/look_ahead_sensitive_parser.rb
49
+ - lib/rparsec/many__parser.rb
50
+ - lib/rparsec/many_parser.rb
51
+ - lib/rparsec/map_current_parser.rb
52
+ - lib/rparsec/map_parser.rb
53
+ - lib/rparsec/mapn_current_parser.rb
54
+ - lib/rparsec/mapn_parser.rb
30
55
  - lib/rparsec/monad.rb
56
+ - lib/rparsec/nested_parser.rb
57
+ - lib/rparsec/not_parser.rb
58
+ - lib/rparsec/one_parser.rb
31
59
  - lib/rparsec/operator_table.rb
32
60
  - lib/rparsec/operators.rb
33
61
  - lib/rparsec/parser.rb
34
62
  - lib/rparsec/parser_monad.rb
35
63
  - lib/rparsec/parsers.rb
64
+ - lib/rparsec/peek_parser.rb
65
+ - lib/rparsec/plus_parser.rb
66
+ - lib/rparsec/regexp_parser.rb
67
+ - lib/rparsec/repeat__parser.rb
68
+ - lib/rparsec/repeat_parser.rb
69
+ - lib/rparsec/satisfies_parser.rb
70
+ - lib/rparsec/sequence_parser.rb
71
+ - lib/rparsec/set_index_parser.rb
72
+ - lib/rparsec/some__parser.rb
73
+ - lib/rparsec/some_parser.rb
74
+ - lib/rparsec/string_case_insensitive_parser.rb
75
+ - lib/rparsec/throw_parser.rb
36
76
  - lib/rparsec/token.rb
77
+ - lib/rparsec/token_parser.rb
78
+ - lib/rparsec/value_parser.rb
79
+ - lib/rparsec/watch_parser.rb
80
+ - lib/rparsec/watchn_parser.rb
81
+ - lib/rparsec/zero_parser.rb
37
82
  homepage: https://git.disroot.org/gemmaro/rparsec2
38
83
  licenses:
39
84
  - BSD-3-Clause
@@ -53,14 +98,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
98
  requirements:
54
99
  - - ">="
55
100
  - !ruby/object:Gem::Version
56
- version: '3.1'
101
+ version: '3.2'
57
102
  required_rubygems_version: !ruby/object:Gem::Requirement
58
103
  requirements:
59
104
  - - ">="
60
105
  - !ruby/object:Gem::Version
61
106
  version: '0'
62
107
  requirements: []
63
- rubygems_version: 3.3.26
108
+ rubygems_version: 3.4.19
64
109
  signing_key:
65
110
  specification_version: 4
66
111
  summary: A Ruby Parser Combinator Framework
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RParsec
4
-
5
- class IdMonad # :nodoc:
6
- def value v
7
- v
8
- end
9
-
10
- def bind prev
11
- yield prev
12
- end
13
-
14
- def mplus a, _b
15
- a
16
- end
17
- end
18
-
19
- end # module