code-ruby 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +28 -55
  6. data/TODO +17 -0
  7. data/bin/code +56 -31
  8. data/bin/format +3 -0
  9. data/bin/template +62 -20
  10. data/bin/test +17 -0
  11. data/code-ruby.gemspec +1 -6
  12. data/docs/class.code +9 -0
  13. data/docs/euler/1.template +1 -5
  14. data/docs/euler/5.template +0 -1
  15. data/docs/meetup.code +12 -0
  16. data/docs/precedence.template +6 -39
  17. data/docs/rain.code +22 -0
  18. data/docs/slack.code +17 -0
  19. data/docs/stripe.code +7 -0
  20. data/docs/twitter.code +9 -0
  21. data/language-ruby.gemspec +18 -0
  22. data/lib/code/error.rb +3 -0
  23. data/lib/code/node/base_10.rb +29 -0
  24. data/lib/code/node/base_16.rb +13 -0
  25. data/lib/code/node/base_2.rb +13 -0
  26. data/lib/code/node/base_8.rb +13 -0
  27. data/lib/code/node/boolean.rb +7 -7
  28. data/lib/code/node/call.rb +32 -37
  29. data/lib/code/node/call_argument.rb +11 -27
  30. data/lib/code/node/chained_call.rb +10 -25
  31. data/lib/code/node/code.rb +4 -6
  32. data/lib/code/node/decimal.rb +26 -0
  33. data/lib/code/node/dictionnary.rb +20 -9
  34. data/lib/code/node/equal.rb +18 -20
  35. data/lib/code/node/function.rb +10 -7
  36. data/lib/code/node/function_parameter.rb +31 -0
  37. data/lib/code/node/if.rb +36 -32
  38. data/lib/code/node/if_modifier.rb +35 -36
  39. data/lib/code/node/list.rb +6 -9
  40. data/lib/code/node/negation.rb +5 -23
  41. data/lib/code/node/not.rb +15 -0
  42. data/lib/code/node/nothing.rb +1 -1
  43. data/lib/code/node/number.rb +14 -12
  44. data/lib/code/node/operation.rb +21 -16
  45. data/lib/code/node/power.rb +10 -6
  46. data/lib/code/node/rescue.rb +4 -3
  47. data/lib/code/node/splat.rb +15 -0
  48. data/lib/code/node/statement.rb +47 -69
  49. data/lib/code/node/string.rb +41 -10
  50. data/lib/code/node/ternary.rb +7 -9
  51. data/lib/code/node/unary_minus.rb +5 -12
  52. data/lib/code/node/while.rb +17 -24
  53. data/lib/code/node.rb +7 -8
  54. data/lib/code/object/argument.rb +3 -12
  55. data/lib/code/object/decimal.rb +114 -27
  56. data/lib/code/object/dictionnary.rb +29 -12
  57. data/lib/code/object/function.rb +23 -24
  58. data/lib/code/object/global.rb +42 -0
  59. data/lib/code/object/integer.rb +142 -35
  60. data/lib/code/object/list.rb +74 -95
  61. data/lib/code/object/range.rb +38 -50
  62. data/lib/code/object/ruby_function.rb +31 -0
  63. data/lib/code/object/string.rb +36 -16
  64. data/lib/code/object.rb +114 -54
  65. data/lib/code/parser/addition.rb +12 -20
  66. data/lib/code/parser/and_operator.rb +9 -20
  67. data/lib/code/parser/bitwise_and.rb +9 -20
  68. data/lib/code/parser/bitwise_or.rb +12 -20
  69. data/lib/code/parser/boolean.rb +10 -7
  70. data/lib/code/parser/call.rb +92 -60
  71. data/lib/code/parser/chained_call.rb +47 -0
  72. data/lib/code/parser/class.rb +45 -0
  73. data/lib/code/parser/code.rb +17 -10
  74. data/lib/code/parser/dictionnary.rb +56 -30
  75. data/lib/code/parser/equal.rb +87 -35
  76. data/lib/code/parser/equality.rb +23 -24
  77. data/lib/code/parser/equality_lower.rb +9 -0
  78. data/lib/code/parser/function.rb +67 -40
  79. data/lib/code/parser/greater.rb +25 -0
  80. data/lib/code/parser/group.rb +13 -8
  81. data/lib/code/parser/if.rb +51 -21
  82. data/lib/code/parser/if_modifier.rb +43 -16
  83. data/lib/code/parser/list.rb +32 -19
  84. data/lib/code/parser/multiplication.rb +15 -20
  85. data/lib/code/parser/name.rb +96 -84
  86. data/lib/code/parser/negation.rb +20 -9
  87. data/lib/code/parser/not_keyword.rb +14 -12
  88. data/lib/code/parser/nothing.rb +13 -8
  89. data/lib/code/parser/number.rb +124 -68
  90. data/lib/code/parser/operation.rb +35 -0
  91. data/lib/code/parser/or_keyword.rb +12 -20
  92. data/lib/code/parser/or_operator.rb +9 -20
  93. data/lib/code/parser/power.rb +32 -14
  94. data/lib/code/parser/range.rb +9 -17
  95. data/lib/code/parser/rescue.rb +29 -13
  96. data/lib/code/parser/shift.rb +11 -21
  97. data/lib/code/parser/splat.rb +31 -0
  98. data/lib/code/parser/statement.rb +4 -3
  99. data/lib/code/parser/string.rb +53 -62
  100. data/lib/code/parser/ternary.rb +36 -15
  101. data/lib/code/parser/unary_minus.rb +23 -5
  102. data/lib/code/parser/while.rb +26 -15
  103. data/lib/code/parser/whitespace.rb +49 -0
  104. data/lib/code/parser.rb +15 -0
  105. data/lib/code/ruby.rb +162 -0
  106. data/lib/code-ruby.rb +2 -5
  107. data/lib/code.rb +24 -7
  108. data/lib/language/atom.rb +343 -0
  109. data/lib/language/output.rb +130 -0
  110. data/lib/language/parser/absent/present.rb +8 -0
  111. data/lib/language/parser/absent.rb +6 -0
  112. data/lib/language/parser/end_of_input.rb +6 -0
  113. data/lib/language/parser/interuption.rb +38 -0
  114. data/lib/language/parser/not_end_of_input.rb +6 -0
  115. data/lib/language/parser/str/not_found.rb +16 -0
  116. data/lib/language/parser/str.rb +6 -0
  117. data/lib/language/parser.rb +53 -0
  118. data/lib/language-ruby.rb +10 -0
  119. data/lib/language.rb +80 -0
  120. data/lib/template/node/code_part.rb +1 -1
  121. data/lib/template/node/part.rb +1 -1
  122. data/lib/template/node/template.rb +1 -1
  123. data/lib/template/node/text_part.rb +1 -1
  124. data/lib/template/node.rb +1 -1
  125. data/lib/template/parser/template.rb +26 -17
  126. data/lib/template/parser.rb +15 -0
  127. data/lib/template/version.rb +1 -1
  128. data/lib/template-ruby.rb +2 -5
  129. data/lib/template.rb +23 -13
  130. data/spec/code/addition_spec.rb +13 -0
  131. data/spec/code/and_operator_spec.rb +13 -0
  132. data/spec/code/bitwise_and_spec.rb +13 -0
  133. data/spec/code/bitwise_or_spec.rb +13 -0
  134. data/spec/code/boolean_spec.rb +13 -0
  135. data/spec/code/call_spec.rb +21 -0
  136. data/spec/code/chained_call_spec.rb +16 -0
  137. data/spec/code/dictionnary_spec.rb +17 -0
  138. data/spec/code/equal_spec.rb +26 -0
  139. data/spec/code/equality_spec.rb +13 -0
  140. data/spec/code/function_spec.rb +18 -0
  141. data/spec/code/greater_spec.rb +18 -0
  142. data/spec/code/group_spec.rb +12 -0
  143. data/spec/code/if_modifier_spec.rb +20 -0
  144. data/spec/code/if_spec.rb +25 -0
  145. data/spec/code/list_spec.rb +17 -0
  146. data/spec/code/multiplication_spec.rb +18 -0
  147. data/spec/code/negation_spec.rb +20 -0
  148. data/spec/code/not_keyword_spec.rb +13 -0
  149. data/spec/code/nothing_spec.rb +17 -0
  150. data/spec/code/number_spec.rb +22 -0
  151. data/spec/code/or_keyword_spec.rb +17 -0
  152. data/spec/code/or_operator_spec.rb +16 -0
  153. data/spec/code/parser/boolean_spec.rb +5 -7
  154. data/spec/code/parser/call_spec.rb +16 -56
  155. data/spec/code/parser/chained_call.rb +17 -0
  156. data/spec/code/parser/dictionnary_spec.rb +8 -9
  157. data/spec/code/parser/function_spec.rb +5 -21
  158. data/spec/code/parser/group_spec.rb +18 -0
  159. data/spec/code/parser/list_spec.rb +9 -20
  160. data/spec/code/parser/number_spec.rb +4 -109
  161. data/spec/code/parser/string_spec.rb +9 -17
  162. data/spec/code/parser_spec.rb +23 -0
  163. data/spec/code/power_spec.rb +13 -0
  164. data/spec/code/range_spec.rb +16 -0
  165. data/spec/code/rescue_spec.rb +13 -0
  166. data/spec/code/shift_spec.rb +13 -0
  167. data/spec/code/splat_spec.rb +13 -0
  168. data/spec/code/string_spec.rb +25 -0
  169. data/spec/code/ternary_spec.rb +18 -0
  170. data/spec/code/unary_minus_spec.rb +13 -0
  171. data/spec/code/while_spec.rb +18 -0
  172. data/spec/spec_helper.rb +4 -1
  173. data/template-ruby.gemspec +2 -7
  174. metadata +113 -99
  175. data/lib/code/node/base_10_decimal.rb +0 -32
  176. data/lib/code/node/base_10_integer.rb +0 -32
  177. data/lib/code/node/base_10_number.rb +0 -19
  178. data/lib/code/node/base_16_number.rb +0 -19
  179. data/lib/code/node/base_2_number.rb +0 -19
  180. data/lib/code/node/base_8_number.rb +0 -19
  181. data/lib/code/node/block.rb +0 -17
  182. data/lib/code/node/defined.rb +0 -19
  183. data/lib/code/node/dictionnary_key_value.rb +0 -23
  184. data/lib/code/node/function_argument.rb +0 -45
  185. data/lib/code/node/group.rb +0 -13
  186. data/lib/code/node/keyword_call_argument.rb +0 -30
  187. data/lib/code/node/keyword_function_argument.rb +0 -33
  188. data/lib/code/node/name.rb +0 -55
  189. data/lib/code/node/not_keyword.rb +0 -13
  190. data/lib/code/node/or_keyword.rb +0 -34
  191. data/lib/code/node/range.rb +0 -31
  192. data/lib/code/node/regular_call_argument.rb +0 -34
  193. data/lib/code/node/regular_function_argument.rb +0 -36
  194. data/lib/code/node/string_characters.rb +0 -13
  195. data/lib/code/node/string_component.rb +0 -23
  196. data/lib/code/node/string_interpolation.rb +0 -13
  197. data/lib/code/parser/defined.rb +0 -20
  198. data/lib/code/parser/greater_than.rb +0 -33
  199. data/spec/call_spec.rb +0 -22
  200. data/spec/code/error/type_error_spec.rb +0 -63
  201. data/spec/code/parser/name_spec.rb +0 -15
  202. data/spec/code/parser/nothing_spec.rb +0 -19
  203. data/spec/code_spec.rb +0 -120
  204. data/spec/function_spec.rb +0 -26
  205. data/spec/template/parser/template_spec.rb +0 -19
  206. data/spec/template_spec.rb +0 -33
data/lib/code/ruby.rb ADDED
@@ -0,0 +1,162 @@
1
+ class Code
2
+ class Ruby
3
+ def initialize(raw = {})
4
+ @raw = raw
5
+ end
6
+
7
+ def self.to_code(raw)
8
+ new(raw).to_code
9
+ end
10
+
11
+ def self.from_code(raw)
12
+ new(raw).from_code
13
+ end
14
+
15
+ def to_code
16
+ if code?
17
+ raw
18
+ elsif true?
19
+ ::Code::Object::Boolean.new(raw)
20
+ elsif false?
21
+ ::Code::Object::Boolean.new(raw)
22
+ elsif string?
23
+ ::Code::Object::String.new(raw)
24
+ elsif symbol?
25
+ ::Code::Object::String.new(raw.to_s)
26
+ elsif integer?
27
+ ::Code::Object::Integer.new(raw)
28
+ elsif float?
29
+ ::Code::Object::Decimal.new(raw.to_s)
30
+ elsif big_decimal?
31
+ ::Code::Object::Decimal.new(raw)
32
+ elsif hash?
33
+ ::Code::Object::Dictionnary.new(
34
+ raw
35
+ .map do |key, value|
36
+ [::Code::Ruby.to_code(key), ::Code::Ruby.to_code(value)]
37
+ end
38
+ .to_h
39
+ )
40
+ elsif array?
41
+ ::Code::Object::List.new(
42
+ raw.map { |element| ::Code::Ruby.to_code(key) }
43
+ )
44
+ elsif proc?
45
+ ::Code::Object::RubyFunction.new(raw)
46
+ else
47
+ raise "Unsupported class #{raw.class} for Ruby to Code conversion"
48
+ end
49
+ end
50
+
51
+ def from_code
52
+ if code?
53
+ if code_boolean?
54
+ raw.raw
55
+ elsif code_decimal?
56
+ raw.raw
57
+ elsif code_integer?
58
+ raw.raw
59
+ elsif code_nothing?
60
+ raw.raw
61
+ elsif code_range?
62
+ raw.raw
63
+ elsif code_string?
64
+ raw.raw
65
+ elsif code_dictionnary?
66
+ raw
67
+ .raw
68
+ .map do |key, value|
69
+ [::Code::Ruby.to_code(key), ::Code::Ruby.to_code(value)]
70
+ end
71
+ .to_h
72
+ elsif code_list?
73
+ raw.raw.map { |element| ::Code::Ruby.to_code(element) }
74
+ else
75
+ raise "Unsupported class #{raw.class} for Code to Ruby conversion"
76
+ end
77
+ else
78
+ raw
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ attr_reader :raw
85
+
86
+ def code?
87
+ raw.is_a?(::Code::Object)
88
+ end
89
+
90
+ def true?
91
+ raw.is_a?(::TrueClass)
92
+ end
93
+
94
+ def false?
95
+ raw.is_a?(::FalseClass)
96
+ end
97
+
98
+ def hash?
99
+ raw.is_a?(::Hash)
100
+ end
101
+
102
+ def array?
103
+ raw.is_a?(::Array)
104
+ end
105
+
106
+ def string?
107
+ raw.is_a?(::String)
108
+ end
109
+
110
+ def symbol?
111
+ raw.is_a?(::Symbol)
112
+ end
113
+
114
+ def integer?
115
+ raw.is_a?(::Integer)
116
+ end
117
+
118
+ def float?
119
+ raw.is_a?(::Float)
120
+ end
121
+
122
+ def big_decimal?
123
+ raw.is_a?(::BigDecimal)
124
+ end
125
+
126
+ def proc?
127
+ raw.is_a?(::Proc)
128
+ end
129
+
130
+ def code_boolean?
131
+ raw.is_a?(::Code::Object::Boolean)
132
+ end
133
+
134
+ def code_decimal?
135
+ raw.is_a?(::Code::Object::Decimal)
136
+ end
137
+
138
+ def code_integer?
139
+ raw.is_a?(::Code::Object::Integer)
140
+ end
141
+
142
+ def code_nothing?
143
+ raw.is_a?(::Code::Object::Nothing)
144
+ end
145
+
146
+ def code_range?
147
+ raw.is_a?(::Code::Object::Range)
148
+ end
149
+
150
+ def code_string?
151
+ raw.is_a?(::Code::Object::String)
152
+ end
153
+
154
+ def code_dictionnary?
155
+ raw.is_a?(::Code::Object::Dictionnary)
156
+ end
157
+
158
+ def code_list?
159
+ raw.is_a?(::Code::Object::List)
160
+ end
161
+ end
162
+ end
data/lib/code-ruby.rb CHANGED
@@ -1,13 +1,10 @@
1
- require "parslet"
2
- require "zeitwerk"
3
1
  require "bigdecimal"
4
- require "active_support"
5
- require "active_support/core_ext/object/blank"
6
- require "active_support/core_ext/object/deep_dup"
7
2
  require "stringio"
8
3
  require "timeout"
4
+ require "zeitwerk"
9
5
 
10
6
  loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
11
7
  loader.ignore("#{__dir__}/template-ruby.rb")
12
8
  loader.ignore("#{__dir__}/code-ruby.rb")
9
+ loader.ignore("#{__dir__}/language-ruby.rb")
13
10
  loader.setup
data/lib/code.rb CHANGED
@@ -1,30 +1,47 @@
1
1
  class Code
2
+ EMPTY_STRING = ""
3
+ GLOBALS = %i[io context object]
2
4
  DEFAULT_TIMEOUT = Template::DEFAULT_TIMEOUT
3
5
 
4
- def initialize(input, io: $stdout, timeout: DEFAULT_TIMEOUT)
6
+ def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
5
7
  @input = input
6
- @parsed = Timeout.timeout(timeout) { ::Code::Parser::Code.new.parse(@input) }
8
+ @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(@input).to_raw }
7
9
  @io = io
8
10
  @timeout = timeout || DEFAULT_TIMEOUT
11
+ @ruby = ::Code::Ruby.to_code(ruby || {})
9
12
  end
10
13
 
11
- def self.evaluate(input, context = "", io: $stdout, timeout: DEFAULT_TIMEOUT)
12
- new(input, io: io, timeout: timeout).evaluate(context)
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)
13
22
  end
14
23
 
15
24
  def evaluate(context = "")
16
25
  Timeout.timeout(timeout) do
17
- if context.present?
18
- context = ::Code.evaluate(context, timeout: timeout)
26
+ if context != EMPTY_STRING
27
+ context = ::Code.evaluate(context, timeout: timeout, io: io, ruby: ruby)
19
28
  else
20
29
  context = ::Code::Object::Dictionnary.new
21
30
  end
22
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
+
23
40
  ::Code::Node::Code.new(parsed).evaluate(context: context, io: io)
24
41
  end
25
42
  end
26
43
 
27
44
  private
28
45
 
29
- attr_reader :input, :parsed, :timeout, :io
46
+ attr_reader :input, :parsed, :timeout, :io, :ruby
30
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