language-ruby 0.5.6 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.ruby-version +1 -0
  4. data/.tool-versions +1 -1
  5. data/Gemfile +5 -4
  6. data/Gemfile.lock +12 -23
  7. data/README.md +2 -102
  8. data/language-ruby.gemspec +7 -5
  9. data/lib/language/atom/absent.rb +29 -0
  10. data/lib/language/atom/aka.rb +32 -0
  11. data/lib/language/atom/and.rb +32 -0
  12. data/lib/language/atom/any.rb +15 -0
  13. data/lib/language/atom/ignore.rb +27 -0
  14. data/lib/language/atom/maybe.rb +32 -0
  15. data/lib/language/atom/or.rb +46 -0
  16. data/lib/language/atom/repeat.rb +57 -0
  17. data/lib/language/atom/str.rb +21 -0
  18. data/lib/language/atom/then.rb +21 -0
  19. data/lib/language/atom.rb +10 -291
  20. data/lib/language/output.rb +13 -11
  21. data/lib/language/parser/absent/present.rb +2 -0
  22. data/lib/language/parser/absent.rb +2 -0
  23. data/lib/language/parser/end_of_input.rb +2 -0
  24. data/lib/language/parser/interuption.rb +2 -0
  25. data/lib/language/parser/not_end_of_input.rb +2 -0
  26. data/lib/language/parser/str/not_found.rb +2 -0
  27. data/lib/language/parser/str.rb +2 -0
  28. data/lib/language/parser.rb +10 -10
  29. data/lib/language/version.rb +5 -0
  30. data/lib/language-ruby.rb +2 -5
  31. data/lib/language.rb +9 -7
  32. data/spec/language_spec.rb +21 -0
  33. data/spec/spec_helper.rb +2 -5
  34. metadata +18 -218
  35. data/.cherry.js +0 -21
  36. data/.editorconfig +0 -9
  37. data/.github/workflows/rspec.yml +0 -14
  38. data/.prettierrc +0 -3
  39. data/CHANGELOG.md +0 -55
  40. data/LICENSE +0 -7
  41. data/TODO +0 -17
  42. data/bin/code +0 -76
  43. data/bin/format +0 -3
  44. data/bin/publish +0 -19
  45. data/bin/template +0 -85
  46. data/bin/test +0 -17
  47. data/code-ruby.gemspec +0 -17
  48. data/docs/class.code +0 -9
  49. data/docs/euler/1.template +0 -10
  50. data/docs/euler/2.template +0 -16
  51. data/docs/euler/3.template +0 -16
  52. data/docs/euler/4.template +0 -10
  53. data/docs/euler/5.template +0 -13
  54. data/docs/fibonnaci.template +0 -14
  55. data/docs/meetup.code +0 -12
  56. data/docs/precedence.template +0 -36
  57. data/docs/rain.code +0 -22
  58. data/docs/slack.code +0 -17
  59. data/docs/stripe.code +0 -7
  60. data/docs/twitter.code +0 -9
  61. data/lib/code/error.rb +0 -18
  62. data/lib/code/node/base_10.rb +0 -29
  63. data/lib/code/node/base_16.rb +0 -13
  64. data/lib/code/node/base_2.rb +0 -13
  65. data/lib/code/node/base_8.rb +0 -13
  66. data/lib/code/node/boolean.rb +0 -22
  67. data/lib/code/node/call.rb +0 -47
  68. data/lib/code/node/call_argument.rb +0 -21
  69. data/lib/code/node/chained_call.rb +0 -23
  70. data/lib/code/node/code.rb +0 -20
  71. data/lib/code/node/decimal.rb +0 -26
  72. data/lib/code/node/dictionnary.rb +0 -33
  73. data/lib/code/node/equal.rb +0 -34
  74. data/lib/code/node/function.rb +0 -20
  75. data/lib/code/node/function_parameter.rb +0 -31
  76. data/lib/code/node/if.rb +0 -59
  77. data/lib/code/node/if_modifier.rb +0 -47
  78. data/lib/code/node/list.rb +0 -16
  79. data/lib/code/node/negation.rb +0 -15
  80. data/lib/code/node/not.rb +0 -15
  81. data/lib/code/node/nothing.rb +0 -12
  82. data/lib/code/node/number.rb +0 -25
  83. data/lib/code/node/operation.rb +0 -38
  84. data/lib/code/node/power.rb +0 -20
  85. data/lib/code/node/rescue.rb +0 -17
  86. data/lib/code/node/splat.rb +0 -15
  87. data/lib/code/node/statement.rb +0 -59
  88. data/lib/code/node/string.rb +0 -53
  89. data/lib/code/node/ternary.rb +0 -24
  90. data/lib/code/node/unary_minus.rb +0 -15
  91. data/lib/code/node/while.rb +0 -35
  92. data/lib/code/node.rb +0 -13
  93. data/lib/code/object/argument.rb +0 -32
  94. data/lib/code/object/boolean.rb +0 -27
  95. data/lib/code/object/decimal.rb +0 -162
  96. data/lib/code/object/dictionnary.rb +0 -96
  97. data/lib/code/object/function.rb +0 -64
  98. data/lib/code/object/global.rb +0 -42
  99. data/lib/code/object/integer.rb +0 -221
  100. data/lib/code/object/list.rb +0 -207
  101. data/lib/code/object/nothing.rb +0 -23
  102. data/lib/code/object/number.rb +0 -6
  103. data/lib/code/object/range.rb +0 -146
  104. data/lib/code/object/ruby_function.rb +0 -31
  105. data/lib/code/object/string.rb +0 -102
  106. data/lib/code/object.rb +0 -197
  107. data/lib/code/parser/addition.rb +0 -21
  108. data/lib/code/parser/and_operator.rb +0 -17
  109. data/lib/code/parser/bitwise_and.rb +0 -17
  110. data/lib/code/parser/bitwise_or.rb +0 -21
  111. data/lib/code/parser/boolean.rb +0 -17
  112. data/lib/code/parser/call.rb +0 -122
  113. data/lib/code/parser/chained_call.rb +0 -47
  114. data/lib/code/parser/class.rb +0 -45
  115. data/lib/code/parser/code.rb +0 -25
  116. data/lib/code/parser/dictionnary.rb +0 -67
  117. data/lib/code/parser/equal.rb +0 -94
  118. data/lib/code/parser/equality.rb +0 -35
  119. data/lib/code/parser/equality_lower.rb +0 -9
  120. data/lib/code/parser/function.rb +0 -85
  121. data/lib/code/parser/greater.rb +0 -25
  122. data/lib/code/parser/group.rb +0 -22
  123. data/lib/code/parser/if.rb +0 -63
  124. data/lib/code/parser/if_modifier.rb +0 -55
  125. data/lib/code/parser/list.rb +0 -42
  126. data/lib/code/parser/multiplication.rb +0 -25
  127. data/lib/code/parser/name.rb +0 -107
  128. data/lib/code/parser/negation.rb +0 -30
  129. data/lib/code/parser/not_keyword.rb +0 -23
  130. data/lib/code/parser/nothing.rb +0 -22
  131. data/lib/code/parser/number.rb +0 -154
  132. data/lib/code/parser/operation.rb +0 -35
  133. data/lib/code/parser/or_keyword.rb +0 -21
  134. data/lib/code/parser/or_operator.rb +0 -17
  135. data/lib/code/parser/power.rb +0 -43
  136. data/lib/code/parser/range.rb +0 -17
  137. data/lib/code/parser/rescue.rb +0 -39
  138. data/lib/code/parser/shift.rb +0 -21
  139. data/lib/code/parser/splat.rb +0 -31
  140. data/lib/code/parser/statement.rb +0 -9
  141. data/lib/code/parser/string.rb +0 -78
  142. data/lib/code/parser/ternary.rb +0 -46
  143. data/lib/code/parser/unary_minus.rb +0 -31
  144. data/lib/code/parser/while.rb +0 -36
  145. data/lib/code/parser/whitespace.rb +0 -49
  146. data/lib/code/parser.rb +0 -19
  147. data/lib/code/ruby.rb +0 -174
  148. data/lib/code-ruby.rb +0 -10
  149. data/lib/code.rb +0 -47
  150. data/lib/template/node/code_part.rb +0 -13
  151. data/lib/template/node/part.rb +0 -19
  152. data/lib/template/node/template.rb +0 -15
  153. data/lib/template/node/text_part.rb +0 -13
  154. data/lib/template/node.rb +0 -4
  155. data/lib/template/parser/template.rb +0 -39
  156. data/lib/template/parser.rb +0 -19
  157. data/lib/template/version.rb +0 -3
  158. data/lib/template-ruby.rb +0 -10
  159. data/lib/template.rb +0 -50
  160. data/spec/code/addition_spec.rb +0 -13
  161. data/spec/code/and_operator_spec.rb +0 -13
  162. data/spec/code/bitwise_and_spec.rb +0 -13
  163. data/spec/code/bitwise_or_spec.rb +0 -13
  164. data/spec/code/boolean_spec.rb +0 -13
  165. data/spec/code/call_spec.rb +0 -21
  166. data/spec/code/chained_call_spec.rb +0 -16
  167. data/spec/code/code_spec.rb +0 -29
  168. data/spec/code/dictionnary_spec.rb +0 -17
  169. data/spec/code/equal_spec.rb +0 -26
  170. data/spec/code/equality_spec.rb +0 -13
  171. data/spec/code/function_spec.rb +0 -18
  172. data/spec/code/greater_spec.rb +0 -18
  173. data/spec/code/group_spec.rb +0 -12
  174. data/spec/code/if_modifier_spec.rb +0 -20
  175. data/spec/code/if_spec.rb +0 -25
  176. data/spec/code/list_spec.rb +0 -19
  177. data/spec/code/multiplication_spec.rb +0 -18
  178. data/spec/code/negation_spec.rb +0 -20
  179. data/spec/code/not_keyword_spec.rb +0 -13
  180. data/spec/code/nothing_spec.rb +0 -17
  181. data/spec/code/number_spec.rb +0 -22
  182. data/spec/code/or_keyword_spec.rb +0 -17
  183. data/spec/code/or_operator_spec.rb +0 -16
  184. data/spec/code/parser/boolean_spec.rb +0 -16
  185. data/spec/code/parser/call_spec.rb +0 -26
  186. data/spec/code/parser/chained_call.rb +0 -17
  187. data/spec/code/parser/dictionnary_spec.rb +0 -18
  188. data/spec/code/parser/function_spec.rb +0 -16
  189. data/spec/code/parser/group_spec.rb +0 -18
  190. data/spec/code/parser/list_spec.rb +0 -18
  191. data/spec/code/parser/number_spec.rb +0 -12
  192. data/spec/code/parser/string_spec.rb +0 -21
  193. data/spec/code/parser_spec.rb +0 -23
  194. data/spec/code/power_spec.rb +0 -13
  195. data/spec/code/range_spec.rb +0 -16
  196. data/spec/code/rescue_spec.rb +0 -13
  197. data/spec/code/shift_spec.rb +0 -13
  198. data/spec/code/splat_spec.rb +0 -13
  199. data/spec/code/string_spec.rb +0 -27
  200. data/spec/code/ternary_spec.rb +0 -18
  201. data/spec/code/unary_minus_spec.rb +0 -13
  202. data/spec/code/while_spec.rb +0 -18
  203. data/template-ruby.gemspec +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ffefd2b6b98738fdb6cca491d3e66d49954089665ee0c549c9fbaa72c44c6de
4
- data.tar.gz: 3bebdecdd1017fb28756407d7c759739a92c7367d2da1c0184b2c992412b065f
3
+ metadata.gz: fe75dad140254848b0688e73fc0361003867abf259e3efd57eb8709072808461
4
+ data.tar.gz: 592c2cf305d38613461981802bb63a9288b2a7bac862220cb818c4e1b9e1922c
5
5
  SHA512:
6
- metadata.gz: 838a0152426d4e052b4a2349dbafded5dbb38f003a1854920a99180a8b215234e2b460d40623d9c02a929ffc3b61b8d42363fa4db81eb418cc073ac26ddfb83e
7
- data.tar.gz: 28ed842cf8c9cbfbe624b2f4bfadaa256cbf3472210949d023a7e915a73e1ace1447d67d1c117ef403b94f5c0d76b24569105dc27753ac4777c79ed40d7a863a
6
+ metadata.gz: b61958d200d6d2e2f1a0ed41e93a992320e3b224c8e65f0cfb3e3ff35ad78f07b930234d2ec190cad4384b1127a347d7651597831033bf1b14bda18a488f6f41
7
+ data.tar.gz: eb44f9c8522a461b4b40e7b369a9ac0653ff30c710c2c5395840e3e5b8181596ce4553fa7033370636e6974dd79e2e326e0e7bf6d0f2061be84468d30b670b49
data/.gitignore CHANGED
@@ -1,2 +1 @@
1
1
  *.gem
2
- tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 3.1.3
1
+ ruby 3.3.0
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
- gemspec name: "template-ruby"
4
- gemspec name: "code-ruby"
5
+ gemspec
6
+
7
+ ruby "3.3.0"
5
8
 
6
9
  gem "rspec"
7
- gem "syntax_tree", github: "dorianmariefr/syntax_tree"
8
- gem "ruby-prof"
data/Gemfile.lock CHANGED
@@ -1,49 +1,38 @@
1
- GIT
2
- remote: https://github.com/dorianmariefr/syntax_tree.git
3
- revision: f7c9dc36a3d60c90a8d2411f123913b37319dc11
4
- specs:
5
- syntax_tree (5.0.1)
6
- prettier_print (>= 1.1.0)
7
-
8
1
  PATH
9
2
  remote: .
10
3
  specs:
11
- code-ruby (0.5.6)
12
- zeitwerk (~> 2)
13
- template-ruby (0.5.6)
4
+ language-ruby (0.6.0)
14
5
  zeitwerk (~> 2)
15
6
 
16
7
  GEM
17
8
  remote: https://rubygems.org/
18
9
  specs:
19
10
  diff-lcs (1.5.0)
20
- prettier_print (1.1.0)
21
11
  rspec (3.12.0)
22
12
  rspec-core (~> 3.12.0)
23
13
  rspec-expectations (~> 3.12.0)
24
14
  rspec-mocks (~> 3.12.0)
25
- rspec-core (3.12.0)
15
+ rspec-core (3.12.2)
26
16
  rspec-support (~> 3.12.0)
27
- rspec-expectations (3.12.0)
17
+ rspec-expectations (3.12.3)
28
18
  diff-lcs (>= 1.2.0, < 2.0)
29
19
  rspec-support (~> 3.12.0)
30
- rspec-mocks (3.12.0)
20
+ rspec-mocks (3.12.6)
31
21
  diff-lcs (>= 1.2.0, < 2.0)
32
22
  rspec-support (~> 3.12.0)
33
- rspec-support (3.12.0)
34
- ruby-prof (1.4.3)
35
- zeitwerk (2.6.6)
23
+ rspec-support (3.12.1)
24
+ zeitwerk (2.6.12)
36
25
 
37
26
  PLATFORMS
38
- arm64-darwin-21
39
27
  arm64-darwin-22
28
+ arm64-darwin-23
40
29
 
41
30
  DEPENDENCIES
42
- code-ruby!
31
+ language-ruby!
43
32
  rspec
44
- ruby-prof
45
- syntax_tree!
46
- template-ruby!
33
+
34
+ RUBY VERSION
35
+ ruby 3.3.0p0
47
36
 
48
37
  BUNDLED WITH
49
- 2.3.18
38
+ 2.4.21
data/README.md CHANGED
@@ -1,103 +1,3 @@
1
- # Template
1
+ # language-ruby
2
2
 
3
- [![RSpec](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml/badge.svg)](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml)
4
-
5
- See [templatelang.com](https://templatelang.com) for the full documentation and
6
- live code editing.
7
-
8
- ## The programming language
9
-
10
- Hi, I'm [Dorian Marié](https://dorianmarie.fr), I created Template to let users of my websites provide templates to customize their experience.
11
-
12
- Template is meant to be:
13
-
14
- - **Simple**: `Hello` and `Hello {name}`
15
- - **Safe**: Can be provided user input
16
- - **Powerful**: Functions, object-oriented, built-in methods
17
-
18
- Template is currently written in Ruby and embeddable as a Ruby gem.
19
-
20
- ## Install
21
-
22
- ### As a command line tool:
23
-
24
- ```bash
25
- $ gem install template-ruby
26
- $ template --input "Hello {name}" --context '{ name: "Dorian" }'
27
- Hello Dorian
28
- $ template --input "1 + 2 = {1 + 2}"
29
- 1 + 2 = 3
30
- ```
31
-
32
- ### As a Ruby gem:
33
-
34
- In a `Gemfile`:
35
-
36
- ```ruby
37
- gem "template-ruby"
38
- ```
39
-
40
- Then `$ bundle install`
41
-
42
- Then you can use Template like:
43
-
44
- ```ruby
45
- Template.render("Hello {name}", '{ name: "Dorian" }')
46
- # => "Hello Dorian"
47
- Template.render("1 + 2 = {1 + 2}")
48
- # => "1 + 2 = 3"
49
- Template.render(input, context, io: StringIO.new, timeout: 10)
50
- ```
51
-
52
- The context is a sub-language called Code, you can use it like:
53
-
54
- ```ruby
55
- Code.evaluate("1 + 2") # => 3
56
- ```
57
-
58
- ## Future work
59
-
60
- - Extend standard library
61
- - Global methods from Ruby, e.g. `{markdown "**bold**"}`
62
- - Object methods from Ruby, e.g. `{"**bold**".markdown}`
63
- - Classes, e.g. `{class User end}`
64
- - Write JavaScript version
65
- - Write Crystal version
66
-
67
- ## Contributing
68
-
69
- Feel free to open [issues](https://github.com/dorianmariefr/template-ruby/issues),
70
- and [pull requests](https://github.com/dorianmariefr/template-ruby/pulls).
71
-
72
- To develop locally:
73
-
74
- ```text
75
- $ git clone https://github.com/dorianmariefr/template-ruby
76
- $ cd template-ruby
77
- $ bundle
78
- $ rspec
79
- $ bin/template -i docs/...
80
- ```
81
-
82
- ## Credits
83
-
84
- Thanks to [thoughtbot](https://thoughtbot.com) who let me work on this programming
85
- language as a Friday project.
86
-
87
- Thanks to [Kaspar Schiess](https://github.com/kschiess) who made
88
- [Parslet](https://kschiess.github.io/parslet/), the gem that helped me write the parser.
89
-
90
- Inspiration from [Ruby](https://www.ruby-lang.org/en/) and
91
- [Liquid](https://shopify.github.io/liquid/).
92
-
93
- ## License
94
-
95
- MIT
96
-
97
- Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
98
-
99
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
100
-
101
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
102
-
103
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3
+ A Parsing Expression Grammar (PEG) for making parsers
@@ -1,16 +1,18 @@
1
- require_relative "lib/template/version"
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require_relative "lib/language/version"
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = "language-ruby"
5
- s.version = ::Template::Version
8
+ s.version = ::Language::Version
6
9
  s.summary = "A Parsing Expression Grammar (PEG) for making parsers"
7
10
  s.description = s.summary
8
11
  s.authors = ["Dorian Marié"]
9
12
  s.email = "dorian@dorianmarie.fr"
10
- s.files = `git ls-files`.split($/)
11
- s.test_files = s.files.grep(%r{^(test|spec|features)/})
13
+ s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
12
14
  s.require_paths = ["lib"]
13
- s.homepage = "https://github.com/dorianmariefr/template-ruby"
15
+ s.homepage = "https://github.com/dorianmariefr/language-ruby"
14
16
  s.license = "MIT"
15
17
 
16
18
  s.add_dependency "zeitwerk", "~> 2"
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Absent < Atom
6
+ def initialize(parent: nil)
7
+ @parent = parent
8
+ end
9
+
10
+ def parse(parser)
11
+ clone =
12
+ Parser.new(
13
+ root: self,
14
+ input: parser.input,
15
+ cursor: parser.cursor,
16
+ buffer: parser.buffer
17
+ )
18
+ @parent&.parse(clone)
19
+ rescue Parser::Interuption
20
+ else
21
+ raise Parser::Interuption.new(parser, self)
22
+ end
23
+
24
+ def to_s
25
+ @parent ? "(#{@parent}).absent" : "absent"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Aka < Atom
6
+ def initialize(name:, parent:)
7
+ @name = name
8
+ @parent = parent
9
+ end
10
+
11
+ def parse(parser)
12
+ clone =
13
+ Parser.new(root: self, input: parser.input, cursor: parser.cursor)
14
+
15
+ @parent.parse(clone)
16
+
17
+ if clone.output?
18
+ parser.output = Output.new(@name => clone.output)
19
+ else
20
+ parser.output[@name] = Output.new(clone.buffer)
21
+ parser.buffer = ""
22
+ end
23
+
24
+ parser.cursor = clone.cursor
25
+ end
26
+
27
+ def to_s
28
+ @parent ? "#{@parent}.aka(#{@name.inspect})" : "aka(#{@name.inspect})"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class And < Atom
6
+ def initialize(left:, right:)
7
+ @left = left
8
+ @right = right
9
+ end
10
+
11
+ def parse(parser)
12
+ @left.parse(parser)
13
+ right_clone =
14
+ Parser.new(
15
+ root: self,
16
+ input: parser.input,
17
+ cursor: parser.cursor,
18
+ buffer: parser.buffer
19
+ )
20
+ @right.parse(right_clone)
21
+ parser.cursor = right_clone.cursor
22
+ parser.buffer = right_clone.buffer
23
+
24
+ parser.output.merge(right_clone.output)
25
+ end
26
+
27
+ def to_s
28
+ "#{@left} >> #{@right}".to_s
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Any < Atom
6
+ def parse(parser)
7
+ parser.consume(1)
8
+ end
9
+
10
+ def to_s
11
+ "any"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Ignore < Atom
6
+ def initialize(parent:)
7
+ @parent = parent
8
+ end
9
+
10
+ def parse(parser)
11
+ clone =
12
+ Parser.new(
13
+ root: self,
14
+ input: parser.input,
15
+ cursor: parser.cursor,
16
+ buffer: parser.buffer
17
+ )
18
+ @parent.parse(clone)
19
+ parser.cursor = clone.cursor
20
+ end
21
+
22
+ def to_s
23
+ "#{@parent}.ignore"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Maybe < Atom
6
+ def initialize(parent:)
7
+ @parent = parent
8
+ end
9
+
10
+ def parse(parser)
11
+ clone =
12
+ Parser.new(
13
+ root: self,
14
+ input: parser.input,
15
+ cursor: parser.cursor,
16
+ buffer: parser.buffer
17
+ )
18
+
19
+ @parent.parse(clone)
20
+ rescue Parser::Interuption
21
+ else
22
+ parser.buffer = clone.buffer
23
+ parser.cursor = clone.cursor
24
+ parser.output = clone.output
25
+ end
26
+
27
+ def to_s
28
+ @parent ? "#{@parent}.maybe" : "maybe"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Or < Atom
6
+ def initialize(left:, right:)
7
+ @left = left
8
+ @right = right
9
+ end
10
+
11
+ def parse(parser)
12
+ left_clone =
13
+ Parser.new(
14
+ root: self,
15
+ input: parser.input,
16
+ cursor: parser.cursor,
17
+ buffer: parser.buffer
18
+ )
19
+
20
+ right_clone =
21
+ Parser.new(
22
+ root: self,
23
+ input: parser.input,
24
+ cursor: parser.cursor,
25
+ buffer: parser.buffer
26
+ )
27
+
28
+ begin
29
+ @left.parse(left_clone)
30
+ parser.cursor = left_clone.cursor
31
+ parser.buffer = left_clone.buffer
32
+ parser.output.merge(left_clone.output)
33
+ rescue Parser::Interuption
34
+ @right.parse(right_clone)
35
+ parser.cursor = right_clone.cursor
36
+ parser.buffer = right_clone.buffer
37
+ parser.output.merge(right_clone.output)
38
+ end
39
+ end
40
+
41
+ def to_s
42
+ "((#{@left}) | (#{@right}))"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Repeat < Atom
6
+ def initialize(parent: nil, minimum: 0, maximum: nil)
7
+ @parent = parent
8
+ @minimum = minimum
9
+ @maximum = maximum
10
+ end
11
+
12
+ def parse(parser)
13
+ return unless @parent
14
+
15
+ @minimum.times { match(parser) }
16
+ if @maximum.nil?
17
+ begin
18
+ loop { match(parser) }
19
+ rescue Parser::Interuption
20
+ end
21
+ else
22
+ begin
23
+ (@maximum - @minimum).times { match(parser) }
24
+ rescue Parser::Interuption
25
+ end
26
+ end
27
+ end
28
+
29
+ def to_s
30
+ minimum = @minimum.zero? ? "" : @minimum.to_s
31
+ maximum = @maximum.nil? ? "" : ", #{@maximum}"
32
+ parenthesis =
33
+ (minimum.empty? && maximum.empty?) ? "" : "(#{minimum}#{maximum})"
34
+
35
+ @parent ? "(#{@parent}).repeat#{parenthesis}" : "repeat#{parenthesis}"
36
+ end
37
+
38
+ private
39
+
40
+ def match(parser)
41
+ clone =
42
+ Parser.new(
43
+ root: self,
44
+ input: parser.input,
45
+ cursor: parser.cursor,
46
+ buffer: parser.buffer
47
+ )
48
+
49
+ @parent.parse(clone)
50
+
51
+ parser.cursor = clone.cursor
52
+ parser.buffer = clone.buffer
53
+ parser.output << clone.output
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Str < Atom
6
+ def initialize(string:)
7
+ @string = string
8
+ end
9
+
10
+ def parse(parser)
11
+ raise Parser::Str::NotFound.new(parser, @string) unless parser.next?(@string)
12
+
13
+ parser.consume(@string.size)
14
+ end
15
+
16
+ def to_s
17
+ "str(#{@string.inspect})"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Language
4
+ class Atom
5
+ class Then < Atom
6
+ def initialize(parent:, block:)
7
+ @parent = parent
8
+ @block = block
9
+ end
10
+
11
+ def parse(parser)
12
+ @parent.parse(parser)
13
+ parser.output = Output.from_raw(@block.call(parser.output.to_raw))
14
+ end
15
+
16
+ def to_s
17
+ "(#{@parent}).then {}"
18
+ end
19
+ end
20
+ end
21
+ end