language-ruby 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +9 -0
  3. data/.github/workflows/rspec.yml +14 -0
  4. data/.gitignore +2 -0
  5. data/.prettierrc +3 -0
  6. data/.rspec +1 -0
  7. data/CHANGELOG.md +55 -0
  8. data/Gemfile +8 -0
  9. data/Gemfile.lock +48 -0
  10. data/LICENSE +7 -0
  11. data/README.md +103 -0
  12. data/TODO +17 -0
  13. data/bin/code +76 -0
  14. data/bin/format +3 -0
  15. data/bin/template +85 -0
  16. data/bin/test +17 -0
  17. data/code-ruby.gemspec +17 -0
  18. data/docs/class.code +9 -0
  19. data/docs/euler/1.template +10 -0
  20. data/docs/euler/2.template +16 -0
  21. data/docs/euler/3.template +16 -0
  22. data/docs/euler/4.template +10 -0
  23. data/docs/euler/5.template +13 -0
  24. data/docs/fibonnaci.template +14 -0
  25. data/docs/meetup.code +12 -0
  26. data/docs/precedence.template +36 -0
  27. data/docs/rain.code +22 -0
  28. data/docs/slack.code +17 -0
  29. data/docs/stripe.code +7 -0
  30. data/docs/twitter.code +9 -0
  31. data/language-ruby.gemspec +18 -0
  32. data/lib/code/error.rb +18 -0
  33. data/lib/code/node/base_10.rb +29 -0
  34. data/lib/code/node/base_16.rb +13 -0
  35. data/lib/code/node/base_2.rb +13 -0
  36. data/lib/code/node/base_8.rb +13 -0
  37. data/lib/code/node/boolean.rb +22 -0
  38. data/lib/code/node/call.rb +47 -0
  39. data/lib/code/node/call_argument.rb +21 -0
  40. data/lib/code/node/chained_call.rb +23 -0
  41. data/lib/code/node/code.rb +20 -0
  42. data/lib/code/node/decimal.rb +26 -0
  43. data/lib/code/node/dictionnary.rb +33 -0
  44. data/lib/code/node/equal.rb +34 -0
  45. data/lib/code/node/function.rb +20 -0
  46. data/lib/code/node/function_parameter.rb +31 -0
  47. data/lib/code/node/if.rb +59 -0
  48. data/lib/code/node/if_modifier.rb +47 -0
  49. data/lib/code/node/list.rb +16 -0
  50. data/lib/code/node/negation.rb +15 -0
  51. data/lib/code/node/not.rb +15 -0
  52. data/lib/code/node/nothing.rb +12 -0
  53. data/lib/code/node/number.rb +25 -0
  54. data/lib/code/node/operation.rb +38 -0
  55. data/lib/code/node/power.rb +20 -0
  56. data/lib/code/node/rescue.rb +17 -0
  57. data/lib/code/node/splat.rb +15 -0
  58. data/lib/code/node/statement.rb +59 -0
  59. data/lib/code/node/string.rb +53 -0
  60. data/lib/code/node/ternary.rb +24 -0
  61. data/lib/code/node/unary_minus.rb +15 -0
  62. data/lib/code/node/while.rb +35 -0
  63. data/lib/code/node.rb +13 -0
  64. data/lib/code/object/argument.rb +32 -0
  65. data/lib/code/object/boolean.rb +27 -0
  66. data/lib/code/object/decimal.rb +162 -0
  67. data/lib/code/object/dictionnary.rb +96 -0
  68. data/lib/code/object/function.rb +64 -0
  69. data/lib/code/object/global.rb +42 -0
  70. data/lib/code/object/integer.rb +221 -0
  71. data/lib/code/object/list.rb +200 -0
  72. data/lib/code/object/nothing.rb +23 -0
  73. data/lib/code/object/number.rb +6 -0
  74. data/lib/code/object/range.rb +146 -0
  75. data/lib/code/object/ruby_function.rb +31 -0
  76. data/lib/code/object/string.rb +88 -0
  77. data/lib/code/object.rb +197 -0
  78. data/lib/code/parser/addition.rb +21 -0
  79. data/lib/code/parser/and_operator.rb +17 -0
  80. data/lib/code/parser/bitwise_and.rb +17 -0
  81. data/lib/code/parser/bitwise_or.rb +21 -0
  82. data/lib/code/parser/boolean.rb +17 -0
  83. data/lib/code/parser/call.rb +122 -0
  84. data/lib/code/parser/chained_call.rb +47 -0
  85. data/lib/code/parser/class.rb +45 -0
  86. data/lib/code/parser/code.rb +25 -0
  87. data/lib/code/parser/dictionnary.rb +67 -0
  88. data/lib/code/parser/equal.rb +94 -0
  89. data/lib/code/parser/equality.rb +35 -0
  90. data/lib/code/parser/equality_lower.rb +9 -0
  91. data/lib/code/parser/function.rb +85 -0
  92. data/lib/code/parser/greater.rb +25 -0
  93. data/lib/code/parser/group.rb +22 -0
  94. data/lib/code/parser/if.rb +63 -0
  95. data/lib/code/parser/if_modifier.rb +55 -0
  96. data/lib/code/parser/list.rb +42 -0
  97. data/lib/code/parser/multiplication.rb +25 -0
  98. data/lib/code/parser/name.rb +101 -0
  99. data/lib/code/parser/negation.rb +30 -0
  100. data/lib/code/parser/not_keyword.rb +23 -0
  101. data/lib/code/parser/nothing.rb +22 -0
  102. data/lib/code/parser/number.rb +154 -0
  103. data/lib/code/parser/operation.rb +35 -0
  104. data/lib/code/parser/or_keyword.rb +21 -0
  105. data/lib/code/parser/or_operator.rb +17 -0
  106. data/lib/code/parser/power.rb +43 -0
  107. data/lib/code/parser/range.rb +17 -0
  108. data/lib/code/parser/rescue.rb +39 -0
  109. data/lib/code/parser/shift.rb +21 -0
  110. data/lib/code/parser/splat.rb +31 -0
  111. data/lib/code/parser/statement.rb +9 -0
  112. data/lib/code/parser/string.rb +78 -0
  113. data/lib/code/parser/ternary.rb +46 -0
  114. data/lib/code/parser/unary_minus.rb +31 -0
  115. data/lib/code/parser/while.rb +36 -0
  116. data/lib/code/parser/whitespace.rb +49 -0
  117. data/lib/code/parser.rb +19 -0
  118. data/lib/code/ruby.rb +162 -0
  119. data/lib/code-ruby.rb +10 -0
  120. data/lib/code.rb +47 -0
  121. data/lib/language/atom.rb +343 -0
  122. data/lib/language/output.rb +130 -0
  123. data/lib/language/parser/absent/present.rb +8 -0
  124. data/lib/language/parser/absent.rb +6 -0
  125. data/lib/language/parser/end_of_input.rb +6 -0
  126. data/lib/language/parser/interuption.rb +38 -0
  127. data/lib/language/parser/not_end_of_input.rb +6 -0
  128. data/lib/language/parser/str/not_found.rb +16 -0
  129. data/lib/language/parser/str.rb +6 -0
  130. data/lib/language/parser.rb +53 -0
  131. data/lib/language-ruby.rb +10 -0
  132. data/lib/language.rb +80 -0
  133. data/lib/template/node/code_part.rb +13 -0
  134. data/lib/template/node/part.rb +19 -0
  135. data/lib/template/node/template.rb +15 -0
  136. data/lib/template/node/text_part.rb +13 -0
  137. data/lib/template/node.rb +4 -0
  138. data/lib/template/parser/template.rb +39 -0
  139. data/lib/template/parser.rb +19 -0
  140. data/lib/template/version.rb +3 -0
  141. data/lib/template-ruby.rb +10 -0
  142. data/lib/template.rb +50 -0
  143. data/spec/code/addition_spec.rb +13 -0
  144. data/spec/code/and_operator_spec.rb +13 -0
  145. data/spec/code/bitwise_and_spec.rb +13 -0
  146. data/spec/code/bitwise_or_spec.rb +13 -0
  147. data/spec/code/boolean_spec.rb +13 -0
  148. data/spec/code/call_spec.rb +21 -0
  149. data/spec/code/chained_call_spec.rb +16 -0
  150. data/spec/code/dictionnary_spec.rb +17 -0
  151. data/spec/code/equal_spec.rb +26 -0
  152. data/spec/code/equality_spec.rb +13 -0
  153. data/spec/code/function_spec.rb +18 -0
  154. data/spec/code/greater_spec.rb +18 -0
  155. data/spec/code/group_spec.rb +12 -0
  156. data/spec/code/if_modifier_spec.rb +20 -0
  157. data/spec/code/if_spec.rb +25 -0
  158. data/spec/code/list_spec.rb +17 -0
  159. data/spec/code/multiplication_spec.rb +18 -0
  160. data/spec/code/negation_spec.rb +20 -0
  161. data/spec/code/not_keyword_spec.rb +13 -0
  162. data/spec/code/nothing_spec.rb +17 -0
  163. data/spec/code/number_spec.rb +22 -0
  164. data/spec/code/or_keyword_spec.rb +17 -0
  165. data/spec/code/or_operator_spec.rb +16 -0
  166. data/spec/code/parser/boolean_spec.rb +16 -0
  167. data/spec/code/parser/call_spec.rb +26 -0
  168. data/spec/code/parser/chained_call.rb +17 -0
  169. data/spec/code/parser/dictionnary_spec.rb +18 -0
  170. data/spec/code/parser/function_spec.rb +16 -0
  171. data/spec/code/parser/group_spec.rb +18 -0
  172. data/spec/code/parser/list_spec.rb +18 -0
  173. data/spec/code/parser/number_spec.rb +12 -0
  174. data/spec/code/parser/string_spec.rb +21 -0
  175. data/spec/code/parser_spec.rb +23 -0
  176. data/spec/code/power_spec.rb +13 -0
  177. data/spec/code/range_spec.rb +16 -0
  178. data/spec/code/rescue_spec.rb +13 -0
  179. data/spec/code/shift_spec.rb +13 -0
  180. data/spec/code/splat_spec.rb +13 -0
  181. data/spec/code/string_spec.rb +25 -0
  182. data/spec/code/ternary_spec.rb +18 -0
  183. data/spec/code/unary_minus_spec.rb +13 -0
  184. data/spec/code/while_spec.rb +18 -0
  185. data/spec/spec_helper.rb +6 -0
  186. data/template-ruby.gemspec +19 -0
  187. metadata +284 -0
data/lib/code.rb ADDED
@@ -0,0 +1,47 @@
1
+ class Code
2
+ EMPTY_STRING = ""
3
+ GLOBALS = %i[io context object]
4
+ DEFAULT_TIMEOUT = Template::DEFAULT_TIMEOUT
5
+
6
+ def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
7
+ @input = input
8
+ @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(@input).to_raw }
9
+ @io = io
10
+ @timeout = timeout || DEFAULT_TIMEOUT
11
+ @ruby = ::Code::Ruby.to_code(ruby || {})
12
+ end
13
+
14
+ def self.evaluate(
15
+ input,
16
+ context = "",
17
+ io: StringIO.new,
18
+ timeout: DEFAULT_TIMEOUT,
19
+ ruby: {}
20
+ )
21
+ new(input, io: io, timeout: timeout, ruby: ruby).evaluate(context)
22
+ end
23
+
24
+ def evaluate(context = "")
25
+ Timeout.timeout(timeout) do
26
+ if context != EMPTY_STRING
27
+ context = ::Code.evaluate(context, timeout: timeout, io: io, ruby: ruby)
28
+ else
29
+ context = ::Code::Object::Dictionnary.new
30
+ end
31
+
32
+ if !context.is_a?(::Code::Object::Dictionnary)
33
+ raise ::Code::Error::IncompatibleContext.new(
34
+ "context must be a dictionnary"
35
+ )
36
+ end
37
+
38
+ context = context.merge(ruby)
39
+
40
+ ::Code::Node::Code.new(parsed).evaluate(context: context, io: io)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :input, :parsed, :timeout, :io, :ruby
47
+ end
@@ -0,0 +1,343 @@
1
+ class Language
2
+ class Atom
3
+ class Rule < Atom
4
+ def initialize(name:)
5
+ @name = name
6
+ end
7
+
8
+ def parse(parser)
9
+ parser.find_rule!(@name).parse(parser)
10
+ end
11
+
12
+ def to_s
13
+ "rule(#{@name.inspect})"
14
+ end
15
+ end
16
+
17
+ class Any < Atom
18
+ def parse(parser)
19
+ parser.consume(1)
20
+ end
21
+
22
+ def to_s
23
+ "any"
24
+ end
25
+ end
26
+
27
+ class Then < Atom
28
+ def initialize(parent:, block:)
29
+ @parent = parent
30
+ @block = block
31
+ end
32
+
33
+ def parse(parser)
34
+ @parent.parse(parser)
35
+ parser.output = Output.from_raw(@block.call(parser.output.to_raw))
36
+ end
37
+
38
+ def to_s
39
+ "(#{@parent}).then {}"
40
+ end
41
+ end
42
+
43
+ class Repeat < Atom
44
+ def initialize(parent: nil, min: 0, max: nil)
45
+ @parent = parent
46
+ @min = min
47
+ @max = max
48
+ end
49
+
50
+ def parse(parser)
51
+ return unless @parent
52
+
53
+ if @max.nil?
54
+ @min.times { match(parser) }
55
+
56
+ begin
57
+ loop { match(parser) }
58
+ rescue Parser::Interuption
59
+ end
60
+ else
61
+ @min.times { match(parser) }
62
+
63
+ begin
64
+ (@max - @min).times { match(parser) }
65
+ rescue Parser::Interuption
66
+ end
67
+ end
68
+ end
69
+
70
+ def to_s
71
+ min = @min.zero? ? "" : @min.to_s
72
+ max = @max.nil? ? "" : ", #{@max}"
73
+ parenthesis = min.empty? && max.empty? ? "" : "(#{min}#{max})"
74
+
75
+ @parent ? "(#{@parent}).repeat#{parenthesis}" : "repeat#{parenthesis}"
76
+ end
77
+
78
+ private
79
+
80
+ def match(parser)
81
+ clone =
82
+ Parser.new(
83
+ root: self,
84
+ input: parser.input,
85
+ cursor: parser.cursor,
86
+ buffer: parser.buffer
87
+ )
88
+
89
+ @parent.parse(clone)
90
+
91
+ parser.cursor = clone.cursor
92
+ parser.buffer = clone.buffer
93
+ parser.output << clone.output
94
+ end
95
+ end
96
+
97
+ class Str < Atom
98
+ def initialize(string:)
99
+ @string = string
100
+ end
101
+
102
+ def parse(parser)
103
+ if parser.next?(@string)
104
+ parser.consume(@string.size)
105
+ else
106
+ raise Parser::Str::NotFound.new(parser, @string)
107
+ end
108
+ end
109
+
110
+ def to_s
111
+ "str(#{@string.inspect})"
112
+ end
113
+ end
114
+
115
+ class Absent < Atom
116
+ def initialize(parent: nil)
117
+ @parent = parent
118
+ end
119
+
120
+ def parse(parser)
121
+ clone =
122
+ Parser.new(
123
+ root: self,
124
+ input: parser.input,
125
+ cursor: parser.cursor,
126
+ buffer: parser.buffer
127
+ )
128
+ @parent.parse(clone) if @parent
129
+ rescue Parser::Interuption
130
+ else
131
+ raise Parser::Interuption.new(parser, self)
132
+ end
133
+
134
+ def to_s
135
+ @parent ? "(#{@parent}).absent" : "absent"
136
+ end
137
+ end
138
+
139
+ class Ignore < Atom
140
+ def initialize(parent: nil)
141
+ @parent = parent
142
+ end
143
+
144
+ def parse(parser)
145
+ clone =
146
+ Parser.new(
147
+ root: self,
148
+ input: parser.input,
149
+ cursor: parser.cursor,
150
+ buffer: parser.buffer
151
+ )
152
+ @parent.parse(clone) if @parent
153
+ parser.cursor = clone.cursor
154
+ end
155
+
156
+ def to_s
157
+ @parent ? "#{@parent}.ignore" : "ignore"
158
+ end
159
+ end
160
+
161
+ class Maybe < Atom
162
+ def initialize(parent:)
163
+ @parent = parent
164
+ end
165
+
166
+ def parse(parser)
167
+ clone =
168
+ Parser.new(
169
+ root: self,
170
+ input: parser.input,
171
+ cursor: parser.cursor,
172
+ buffer: parser.buffer
173
+ )
174
+
175
+ @parent.parse(clone)
176
+ rescue Parser::Interuption
177
+ else
178
+ parser.cursor = clone.cursor
179
+ parser.output = clone.output
180
+ end
181
+
182
+ def to_s
183
+ @parent ? "#{@parent}.maybe" : "maybe"
184
+ end
185
+ end
186
+
187
+ class Aka < Atom
188
+ def initialize(name:, parent:)
189
+ @name = name
190
+ @parent = parent
191
+ end
192
+
193
+ def parse(parser)
194
+ clone =
195
+ Parser.new(root: self, input: parser.input, cursor: parser.cursor)
196
+
197
+ require "colorize"
198
+ @parent.parse(clone)
199
+
200
+ if clone.output?
201
+ parser.output = Output.new(@name => clone.output)
202
+ else
203
+ parser.output[@name] = Output.new(clone.buffer)
204
+ parser.buffer = ""
205
+ end
206
+
207
+ parser.cursor = clone.cursor
208
+ end
209
+
210
+ def to_s
211
+ @parent ? "#{@parent}.aka(#{@name.inspect})" : "aka(#{@name.inspect})"
212
+ end
213
+ end
214
+
215
+ class Or < Atom
216
+ def initialize(left:, right:)
217
+ @left = left
218
+ @right = right
219
+ end
220
+
221
+ def parse(parser)
222
+ left_clone =
223
+ Parser.new(
224
+ root: self,
225
+ input: parser.input,
226
+ cursor: parser.cursor,
227
+ buffer: parser.buffer
228
+ )
229
+
230
+ right_clone =
231
+ Parser.new(
232
+ root: self,
233
+ input: parser.input,
234
+ cursor: parser.cursor,
235
+ buffer: parser.buffer
236
+ )
237
+
238
+ begin
239
+ @left.parse(left_clone)
240
+ parser.cursor = left_clone.cursor
241
+ parser.buffer = left_clone.buffer
242
+ parser.output.merge(left_clone.output)
243
+ rescue Parser::Interuption
244
+ @right.parse(right_clone)
245
+ parser.cursor = right_clone.cursor
246
+ parser.buffer = right_clone.buffer
247
+ parser.output.merge(right_clone.output)
248
+ end
249
+ end
250
+
251
+ def to_s
252
+ "((#{@left}) | (#{@right}))"
253
+ end
254
+ end
255
+
256
+ class And < Atom
257
+ def initialize(left:, right:)
258
+ @left = left
259
+ @right = right
260
+ end
261
+
262
+ def parse(parser)
263
+ @left.parse(parser)
264
+ right_clone =
265
+ Parser.new(
266
+ root: self,
267
+ input: parser.input,
268
+ cursor: parser.cursor,
269
+ buffer: parser.buffer
270
+ )
271
+ @right.parse(right_clone)
272
+ parser.cursor = right_clone.cursor
273
+ parser.buffer = right_clone.buffer
274
+
275
+ parser.output.merge(right_clone.output)
276
+ end
277
+
278
+ def to_s
279
+ "#{@left} >> #{@right}".to_s
280
+ end
281
+ end
282
+
283
+ def any
284
+ Any.new
285
+ end
286
+
287
+ def str(string)
288
+ Str.new(string: string)
289
+ end
290
+
291
+ def absent
292
+ Absent.new(parent: self)
293
+ end
294
+
295
+ def ignore
296
+ Ignore.new(parent: self)
297
+ end
298
+
299
+ def maybe
300
+ Maybe.new(parent: self)
301
+ end
302
+
303
+ def repeat(min = 0, max = nil)
304
+ Repeat.new(parent: self, min: min, max: max)
305
+ end
306
+
307
+ def aka(name)
308
+ Aka.new(parent: self, name: name)
309
+ end
310
+
311
+ def |(other)
312
+ Or.new(left: self, right: other)
313
+ end
314
+
315
+ def >>(other)
316
+ And.new(left: self, right: other)
317
+ end
318
+
319
+ def <<(other)
320
+ And.new(left: self, right: other)
321
+ end
322
+
323
+ def then(&block)
324
+ Then.new(parent: self, block: block)
325
+ end
326
+
327
+ def rule(name)
328
+ Rule.new(name: name)
329
+ end
330
+
331
+ def parse(parser)
332
+ raise NotImplementedError.new("#{self.class}#parse")
333
+ end
334
+
335
+ def to_s
336
+ ""
337
+ end
338
+
339
+ def inspect
340
+ to_s
341
+ end
342
+ end
343
+ end
@@ -0,0 +1,130 @@
1
+ class Language
2
+ class Output
3
+ attr_reader :raw
4
+
5
+ def initialize(raw = nil)
6
+ @raw = raw
7
+ end
8
+
9
+ def self.from_raw(raw)
10
+ if raw.is_a?(Array)
11
+ new(raw.map { |element| from_raw(element) })
12
+ elsif raw.is_a?(Hash)
13
+ new(raw.map { |key, value| [key, from_raw(value)] }.to_h)
14
+ else
15
+ new(raw)
16
+ end
17
+ end
18
+
19
+ def to_raw
20
+ if raw.is_a?(Array)
21
+ raw.map(&:to_raw)
22
+ elsif raw.is_a?(Hash)
23
+ raw.map { |key, value| [key, value.to_raw] }.to_h
24
+ else
25
+ raw
26
+ end
27
+ end
28
+
29
+ def clone
30
+ Output.new(@raw.clone)
31
+ end
32
+
33
+ def present?
34
+ @raw != nil
35
+ end
36
+
37
+ def ==(other)
38
+ other.is_a?(Output) ? raw == other.raw : raw == other
39
+ end
40
+
41
+ def to_s
42
+ raw.to_s
43
+ end
44
+
45
+ def inspect
46
+ raw.inspect
47
+ end
48
+
49
+ def []=(key, value)
50
+ case @raw
51
+ when NilClass
52
+ @raw = { key => value }
53
+ when String
54
+ @raw = { key => value }
55
+ when Array
56
+ @raw << Output.new({ key => value })
57
+ when Hash
58
+ @raw[key] = value
59
+ end
60
+ end
61
+
62
+ def merge(other)
63
+ case @raw
64
+ when NilClass
65
+ @raw = other.raw
66
+ when String
67
+ case other.raw
68
+ when String
69
+ @raw = @raw + other.raw
70
+ when Array
71
+ @raw = other.raw
72
+ when Hash
73
+ @raw = other.raw
74
+ end
75
+ when Array
76
+ case other.raw
77
+ when String
78
+ return
79
+ when Array
80
+ @raw = @raw + other.raw
81
+ when Hash
82
+ @raw << other
83
+ end
84
+ when Hash
85
+ case other.raw
86
+ when String
87
+ return
88
+ when Array
89
+ return
90
+ when Hash
91
+ @raw.merge!(other.raw)
92
+ end
93
+ end
94
+ end
95
+
96
+ def <<(other)
97
+ case @raw
98
+ when NilClass
99
+ case other.raw
100
+ when String
101
+ @raw = [other]
102
+ when Array
103
+ @raw = [other]
104
+ when Hash
105
+ @raw = [other]
106
+ end
107
+ when String
108
+ case other.raw
109
+ when String
110
+ @raw = @raw + other.raw
111
+ when Array
112
+ @raw = [other]
113
+ when Hash
114
+ @raw = [other]
115
+ end
116
+ when Array
117
+ @raw << other
118
+ when Hash
119
+ case other.raw
120
+ when String
121
+ return
122
+ when Array
123
+ return
124
+ when Hash
125
+ @raw.merge!(other.raw)
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,8 @@
1
+ class Language
2
+ class Parser
3
+ class Absent
4
+ class Present < Interuption
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ class Language
2
+ class Parser
3
+ class Absent
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class Language
2
+ class Parser
3
+ class EndOfInput < Interuption
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,38 @@
1
+ class Language
2
+ class Parser
3
+ class Interuption < StandardError
4
+ def initialize(parser, atom = Atom.new)
5
+ @parser = parser
6
+ @atom = atom
7
+ end
8
+
9
+ def message
10
+ "\n#{line}#{" " * column_index}^\n"
11
+ end
12
+
13
+ def line
14
+ l = input.lines[line_index]
15
+ l += "\n" if l[-1] != "\n"
16
+ l
17
+ end
18
+
19
+ def line_index
20
+ input[...cursor].count("\n")
21
+ end
22
+
23
+ def column_index
24
+ cursor - input.lines[...line_index].map(&:size).sum
25
+ end
26
+
27
+ private
28
+
29
+ def cursor
30
+ @parser.cursor
31
+ end
32
+
33
+ def input
34
+ @parser.input
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,6 @@
1
+ class Language
2
+ class Parser
3
+ class NotEndOfInput < Interuption
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ class Language
2
+ class Parser
3
+ class Str
4
+ class NotFound < Interuption
5
+ def initialize(parser, string)
6
+ @parser = parser
7
+ @string = string
8
+ end
9
+
10
+ def message
11
+ "#{@string} not found\n#{super}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ class Language
2
+ class Parser
3
+ class Str
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,53 @@
1
+ class Language
2
+ class Parser
3
+ attr_accessor :input, :buffer, :output, :root, :cursor
4
+
5
+ def initialize(root:, input:, cursor: 0, buffer: "", output: Output.new)
6
+ @root = root
7
+ @input = input
8
+ @cursor = cursor
9
+ @buffer = buffer
10
+ @output = output
11
+ end
12
+
13
+ def parse(check_end_of_input: true)
14
+ @root.parse(self)
15
+
16
+ if @cursor == @input.size || !check_end_of_input
17
+ @output.present? ? @output : Output.new(@buffer)
18
+ else
19
+ raise NotEndOfInput.new(self)
20
+ end
21
+ end
22
+
23
+ def consume(n)
24
+ if @cursor + n <= @input.size
25
+ @buffer += @input[@cursor, n]
26
+ @cursor += n
27
+ else
28
+ raise EndOfInput.new(self)
29
+ end
30
+ end
31
+
32
+ def aka(name)
33
+ if @buffer.empty?
34
+ @output = Output.new({ name => @output })
35
+ else
36
+ @output[name] = Output.new(@buffer)
37
+ @buffer = ""
38
+ end
39
+ end
40
+
41
+ def next?(string)
42
+ @input[@cursor...(@cursor + string.size)] == string
43
+ end
44
+
45
+ def buffer?
46
+ @buffer != ""
47
+ end
48
+
49
+ def output?
50
+ @output.present?
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ require "bigdecimal"
2
+ require "stringio"
3
+ require "timeout"
4
+ require "zeitwerk"
5
+
6
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
7
+ loader.ignore("#{__dir__}/template-ruby.rb")
8
+ loader.ignore("#{__dir__}/code-ruby.rb")
9
+ loader.ignore("#{__dir__}/language-ruby.rb")
10
+ loader.setup