yaparc 0.2.3 → 0.4.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 (53) hide show
  1. checksums.yaml +7 -0
  2. data/.document +3 -0
  3. data/.envrc +1 -0
  4. data/.rdoc_options +1 -0
  5. data/CHANGELOG.md +19 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +20 -0
  8. data/README +106 -86
  9. data/Rakefile +27 -0
  10. data/TODO +11 -0
  11. data/lib/yaparc/abstract_parser.rb +16 -0
  12. data/lib/yaparc/alt.rb +22 -0
  13. data/lib/yaparc/apply.rb +18 -0
  14. data/lib/yaparc/char.rb +16 -0
  15. data/lib/yaparc/cr.rb +11 -0
  16. data/lib/yaparc/digit.rb +11 -0
  17. data/lib/yaparc/fail_parser.rb +11 -0
  18. data/lib/yaparc/ident.rb +18 -0
  19. data/lib/yaparc/identifier.rb +32 -0
  20. data/lib/yaparc/item.rb +17 -0
  21. data/lib/yaparc/literal.rb +13 -0
  22. data/lib/yaparc/many.rb +14 -0
  23. data/lib/yaparc/many_one.rb +27 -0
  24. data/lib/yaparc/nat.rb +11 -0
  25. data/lib/yaparc/natural.rb +12 -0
  26. data/lib/yaparc/no_fail.rb +20 -0
  27. data/lib/yaparc/parsable.rb +20 -0
  28. data/lib/yaparc/regex.rb +22 -0
  29. data/lib/yaparc/satisfy.rb +28 -0
  30. data/lib/yaparc/seq.rb +33 -0
  31. data/lib/yaparc/space.rb +11 -0
  32. data/lib/yaparc/string.rb +24 -0
  33. data/lib/yaparc/succeed.rb +14 -0
  34. data/lib/yaparc/symbol.rb +11 -0
  35. data/lib/yaparc/tokenize.rb +18 -0
  36. data/lib/yaparc/white_space.rb +11 -0
  37. data/lib/yaparc/zero_one.rb +20 -0
  38. data/lib/yaparc.rb +40 -605
  39. data/sig/yaparc.gen.rbs +217 -0
  40. data/sig/yaparc.rbs +4 -0
  41. data/yaparc.gemspec +36 -0
  42. metadata +115 -58
  43. data/test/n3-report.html +0 -169
  44. data/test/n3.bnf +0 -129
  45. data/test/test_abc.rb.bak +0 -112
  46. data/test/test_calc.rb +0 -112
  47. data/test/test_lambda.rb +0 -87
  48. data/test/test_metric.rb +0 -617
  49. data/test/test_owl.rb +0 -1039
  50. data/test/test_parser.rb +0 -460
  51. data/test/test_prolog.rb +0 -287
  52. data/test/test_sql.rb +0 -317
  53. data/test/test_uri.rb +0 -752
@@ -0,0 +1,20 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ # hutton92:_higher_order_funct_parsin,p.19
5
+ # https://www.cambridge.org/core/journals/journal-of-functional-programming/article/higherorder-functions-for-parsing/0490F2C8511F7625F9FC15BFFEDBB0AA
6
+ class NoFail
7
+ include Parsable
8
+
9
+ def initialize(parser)
10
+ @parser = lambda do |input|
11
+ result = parser.parse(input)
12
+ if result.instance_of?(Fail)
13
+ Error.new(value: result.value, input: result.input)
14
+ else
15
+ Succeed.new(result.value)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Yaparc
2
+ module Parsable
3
+ IS_LOWER = ->(c) { c >= 'a' and c <= 'z' }
4
+ IS_ALPHANUM = ->(c) { (c >= 'a' and c <= 'z') or (c >= '0' and c <= '9') }
5
+ IS_DIGIT = ->(i) { i >= '0' and i <= '9' }
6
+ IS_SPACE = ->(i) { i == ' ' }
7
+ IS_WHITESPACE = ->(i) { [' ', "\n", "\t"].include?(i) }
8
+ IS_CR = ->(i) { i == "\n" }
9
+
10
+ def parse(input)
11
+ result = @parser.call(input)
12
+
13
+ if result.respond_to?(:parse)
14
+ result.parse(input)
15
+ else
16
+ result
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Regex
5
+ include Parsable
6
+
7
+ def initialize(regex)
8
+ @regex = regex
9
+ @parser = lambda do |input|
10
+ if match = Regexp.new(regex).match(input)
11
+ if block_given?
12
+ Succeed.new(yield(*match.to_a[1..])).parse(match.post_match)
13
+ else
14
+ OK.new(value: match[0], input: match.post_match)
15
+ end
16
+ else
17
+ Fail.new(input:)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Satisfy
5
+ include Parsable
6
+
7
+ def initialize(predicate)
8
+ @parser = lambda do |input|
9
+ result = Item.new.parse(input)
10
+
11
+ if result.instance_of?(OK) && predicate.call(result.value)
12
+ Succeed.new(result.value, result.input)
13
+ else
14
+ FailParser.new
15
+ end
16
+ end
17
+ end
18
+
19
+ def parse(input)
20
+ case parser = @parser.call(input)
21
+ in Succeed
22
+ parser.parse(parser.remaining)
23
+ in FailParser
24
+ parser.parse(input)
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/yaparc/seq.rb ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Seq
5
+ include Parsable
6
+
7
+ def initialize(*parsers)
8
+ @parser = lambda do |input|
9
+ args = []
10
+ initial_result = OK.new(input:)
11
+ final_result = parsers.inject(initial_result) do |subsequent, parser|
12
+ result = parser.parse(subsequent.input)
13
+ break Fail.new(input: subsequent.input) if result.instance_of?(Fail)
14
+
15
+ args << result.value
16
+ result
17
+ end
18
+
19
+ case final_result
20
+ in Fail
21
+ Fail.new(input: final_result.input)
22
+ in OK
23
+ final_value = if block_given?
24
+ yield(*args)
25
+ else
26
+ args.last
27
+ end
28
+ OK.new(value: final_value, input: final_result.input)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Space
5
+ include Parsable
6
+
7
+ def initialize
8
+ @parser = proc { Regex.new(/\A */) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class String
5
+ include Parsable
6
+
7
+ def initialize(string, case_sensitive = true)
8
+ @parser = lambda do |_input|
9
+ result = Item.new.parse(string)
10
+ if result.instance_of?(OK)
11
+ Seq.new(
12
+ Char.new(result.value, case_sensitive),
13
+ Yaparc::String.new(result.input, case_sensitive),
14
+ Succeed.new(result.value + result.input)
15
+ ) do |_, _, succeed_result|
16
+ succeed_result
17
+ end
18
+ else
19
+ Succeed.new(result)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Succeed
5
+ include Parsable
6
+
7
+ attr_reader :remaining
8
+
9
+ def initialize(value, remaining = nil)
10
+ @parser = ->(input) { OK.new(value:, input:) }
11
+ @remaining = remaining
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Symbol
5
+ include Parsable
6
+
7
+ def initialize(literal)
8
+ @parser = proc { Literal.new(literal) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class Tokenize
5
+ include Parsable
6
+
7
+ attr_writer :prefix, :postfix
8
+
9
+ def initialize(parser, prefix: nil, postfix: nil)
10
+ @parser = lambda do |_input|
11
+ @prefix = prefix || WhiteSpace.new
12
+ @postfix = postfix || WhiteSpace.new
13
+ block_given? and yield self
14
+ Seq.new(@prefix, parser, @postfix) { |_, vs, _| vs }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class WhiteSpace
5
+ include Parsable
6
+
7
+ def initialize
8
+ @parser = proc { Regex.new(/\A[\t\n ]*/) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'parsable'
2
+
3
+ module Yaparc
4
+ class ZeroOne
5
+ include Parsable
6
+
7
+ def initialize(parser, identity = [])
8
+ @parser = lambda do |input|
9
+ case (result = parser.parse(input))
10
+ in Fail
11
+ OK.new(value: identity, input:)
12
+ in Error
13
+ Error.new(value: result.value, input: result.input)
14
+ in OK
15
+ result
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end