code-ruby 2.0.0 → 2.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f52de5a641a1d879b4ac472d5f291a1c101a42797362019d6a2c3854e6bf90a2
4
- data.tar.gz: 32645dbb24c34523d7848852f59a2de98786de33b8f1782ad514c8e8a317ebb7
3
+ metadata.gz: 26d4b2a42933e4c3b408c6363ecbd343c106e117d469e196f5088c5db5ecfb8f
4
+ data.tar.gz: a888fd3dc5f2987dae06dffe0df43f00ca3c031404e48390d53d201078224808
5
5
  SHA512:
6
- metadata.gz: 1dc8fe275e70f5fdc948f082408ae64cb1ab8aaaa73d1f73fb9b8c9eccb180f987dc2d10d7ace3a94ecb85678a9c8ccd95b3c7737c95cd349d6ff3cdb35872a5
7
- data.tar.gz: 6317f857f1c7984212ca971650dda29e5d9b6fff92d677c4b6c069041cbfda0f92a2bb78d1345c5f11e5eb51d0f9a9d2285bfb60423ef0dd94fb21344e9bec12
6
+ metadata.gz: 1c3f838b4cdd9a17c18ad194dd035e281b64d8ff9c536e935f903dfbd8d6a82e64fcb04ae37d8ed0f49e764269f4d70d8f0ca44d3d6314c9bd5fe13864cb88df
7
+ data.tar.gz: 42435bd8fafc85bdfb3b4fd6af6b033942f7bb4e03e49e6ddc93079e2caeb6bbd08dc33214d79e2324abfb528be1debe6c82600ffebf84601a21e6a54c997e77
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (2.0.0)
4
+ code-ruby (2.0.2)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.0.2
@@ -6,6 +6,7 @@ class Code
6
6
  def initialize(parsed)
7
7
  return if parsed.blank?
8
8
 
9
+ @parsed = parsed.deep_dup
9
10
  @statements =
10
11
  (parsed.presence || []).map { |statement| Statement.new(statement) }
11
12
  end
@@ -57,6 +58,10 @@ class Code
57
58
  def to_code
58
59
  @statements.blank? ? Object::Nothing.new : Object::Code.new(self)
59
60
  end
61
+
62
+ def to_raw
63
+ @parsed.deep_dup
64
+ end
60
65
  end
61
66
  end
62
67
  end
@@ -19,6 +19,10 @@ class Code
19
19
  def call(...)
20
20
  raw.call(...)
21
21
  end
22
+
23
+ def code_to_string
24
+ String.new(raw.name.to_s.split("::")[2..].join("::"))
25
+ end
22
26
  end
23
27
  end
24
28
  end
@@ -152,7 +152,18 @@ class Code
152
152
  end
153
153
 
154
154
  def code_to_string
155
- String.new("<#{self.class.name} #{raw}>")
155
+ String.new(
156
+ Format.format(
157
+ [
158
+ {
159
+ function: {
160
+ parameters: code_parameters.raw.map { |parameter| parameter_to_raw(parameter) },
161
+ body: code_body.raw.to_raw
162
+ }
163
+ }
164
+ ]
165
+ )
166
+ )
156
167
  end
157
168
 
158
169
  def code_extend(function)
@@ -202,6 +213,29 @@ class Code
202
213
 
203
214
  nil
204
215
  end
216
+
217
+ def parameter_to_raw(parameter)
218
+ code_parameter = parameter.to_code
219
+ raw_parameter = { name: code_parameter.code_name.to_s }
220
+
221
+ if code_parameter.keyword?
222
+ raw_parameter[:keyword] = ":"
223
+ elsif code_parameter.keyword_splat?
224
+ raw_parameter[:keyword_splat] = "**"
225
+ elsif code_parameter.regular_splat?
226
+ raw_parameter[:regular_splat] = "*"
227
+ elsif code_parameter.block?
228
+ raw_parameter[:block] = "&"
229
+ elsif code_parameter.spread?
230
+ raw_parameter[:spread] = "..."
231
+ end
232
+
233
+ unless code_parameter.code_default.nothing?
234
+ raw_parameter[:default] = code_parameter.code_default.code_to_string.raw == "nothing" ? [] : Code.parse(code_parameter.code_default.to_s)
235
+ end
236
+
237
+ raw_parameter
238
+ end
205
239
  end
206
240
  end
207
241
  end
@@ -7,6 +7,10 @@ class Code
7
7
  Name
8
8
  end
9
9
 
10
+ def label_name
11
+ LabelName
12
+ end
13
+
10
14
  def whitespace
11
15
  Whitespace
12
16
  end
@@ -88,7 +92,7 @@ class Code
88
92
  end
89
93
 
90
94
  def keyword_argument
91
- name.aka(:name) << whitespace? << colon << code.aka(:value)
95
+ label_name.aka(:name) << whitespace? << colon << code.aka(:value)
92
96
  end
93
97
 
94
98
  def regular_argument
@@ -114,7 +118,7 @@ class Code
114
118
  end
115
119
 
116
120
  def keyword_parameter
117
- name.aka(:name) << whitespace? << colon.aka(:keyword) <<
121
+ label_name.aka(:name) << whitespace? << colon.aka(:keyword) <<
118
122
  code_present.aka(:default).maybe
119
123
  end
120
124
 
@@ -15,6 +15,10 @@ class Code
15
15
  Name
16
16
  end
17
17
 
18
+ def label_name
19
+ LabelName
20
+ end
21
+
18
22
  def whitespace
19
23
  Whitespace
20
24
  end
@@ -48,7 +52,7 @@ class Code
48
52
  end
49
53
 
50
54
  def key_value
51
- (name.aka(:name) << colon << code_present.aka(:code).maybe).aka(
55
+ (label_name.aka(:name) << colon << code_present.aka(:code).maybe).aka(
52
56
  :name_code
53
57
  ) |
54
58
  (
@@ -7,6 +7,10 @@ class Code
7
7
  Name
8
8
  end
9
9
 
10
+ def label_name
11
+ LabelName
12
+ end
13
+
10
14
  def code
11
15
  Code
12
16
  end
@@ -90,7 +94,7 @@ class Code
90
94
  end
91
95
 
92
96
  def keyword_parameter
93
- name.aka(:name) << whitespace? << colon.aka(:keyword) <<
97
+ label_name.aka(:name) << whitespace? << colon.aka(:keyword) <<
94
98
  code_present.aka(:default).maybe
95
99
  end
96
100
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Code
4
+ class Parser
5
+ class LabelName < Name
6
+ def root
7
+ (
8
+ (special_name << separator.ignore) |
9
+ (special_characters.absent << character.repeat(1))
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -11,6 +11,10 @@ class Code
11
11
  Name
12
12
  end
13
13
 
14
+ def label_name
15
+ LabelName
16
+ end
17
+
14
18
  def single_quote
15
19
  str("'")
16
20
  end
@@ -69,7 +73,7 @@ class Code
69
73
  end
70
74
 
71
75
  def symbol
72
- (colon.ignore << name).aka(:text).repeat(1, 1)
76
+ (colon.ignore << label_name).aka(:text).repeat(1, 1)
73
77
  end
74
78
 
75
79
  def root
@@ -1,3 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "spec_helper"
4
+
5
+ RSpec.describe Code::Parser::Call do
6
+ [
7
+ "f(end: 2)"
8
+ ].each do |input|
9
+ it "parses #{input}" do
10
+ Code::Parser.parse(input)
11
+ end
12
+ end
13
+ end
@@ -8,7 +8,8 @@ RSpec.describe Code::Parser::Dictionary do
8
8
  "{ }",
9
9
  "{/* comment */}",
10
10
  "{ a: 1, b: 2, c: 3 }",
11
- '{ "first_name": "Dorian" }'
11
+ '{ "first_name": "Dorian" }',
12
+ "{ end: 2 }"
12
13
  ].each do |input|
13
14
  it "parses #{input}" do
14
15
  Code::Parser.parse(input)
@@ -6,7 +6,8 @@ RSpec.describe Code::Parser::Function do
6
6
  [
7
7
  "() => {}",
8
8
  "(a, b) => { add(a, b) }",
9
- "(a:, b:) => { add(a, b) }"
9
+ "(a:, b:) => { add(a, b) }",
10
+ "(end:) => { nothing }"
10
11
  ].each do |input|
11
12
  it "parses #{input}" do
12
13
  Code::Parser.parse(input)
data/spec/code_spec.rb CHANGED
@@ -173,6 +173,7 @@ RSpec.describe Code do
173
173
  "(user = { name: :Dorian, age: 31 }).transform_values { user.name }.age",
174
174
  ":Dorian"
175
175
  ],
176
+ ["{ end: 2 }.keys.first", ":end"],
176
177
  %w[!!1 true],
177
178
  %w[!!true true],
178
179
  %w[!false true],
@@ -239,6 +240,9 @@ RSpec.describe Code do
239
240
  %w[Class(Time) Time],
240
241
  %w[Class(nothing) Nothing],
241
242
  %w[Class(true) Boolean],
243
+ %w[String String],
244
+ %w[Time Time],
245
+ %w[Class Class],
242
246
  %w[Class.new Nothing],
243
247
  %w[Class.new Nothing],
244
248
  %w[Class.new(2.days.ago) Time],
@@ -331,15 +335,9 @@ RSpec.describe Code do
331
335
  ["a = 0 [1, 2, 3].each { |i| next if i == 2 a += i } a", "4"],
332
336
  ["a = 0\nb = 0\nwhile a < 4\n a += 1\n continue if a == 2\n b += a\nend\nb", "8"],
333
337
  ["a = 0 loop a += 1 break end a", "1"],
334
- ["a = 1\nbegin\n a += 1\n break if a > 3\n retry\nend\na", "4"],
335
338
  ["x = loop break(42) end x", "42"],
336
- ["a = 0 a += 1 retry if a < 3 a", "3"],
337
339
  ["a = 0\nuntil a > 10 a += 1 end a", "11"],
338
340
  ["a = 0\nwhile a < 10 a += 1 end a", "10"],
339
- [
340
- "a = 0\nretried = false\nwhile a < 2\n a += 1\n retry if a == 1 && !retried && (retried = true)\nend\na",
341
- "2"
342
- ],
343
341
  ["a = 1 3.times { a += 1 } a", "4"],
344
342
  ["a = 1 a *= 2 a", "2"],
345
343
  ["a = 1 a += 1 a", "2"],
@@ -377,7 +375,6 @@ RSpec.describe Code do
377
375
  ["next(7)", "7"],
378
376
  ["not not false", "false"],
379
377
  ["not true", "false"],
380
- ["retry(8)", "8"],
381
378
  ["return(9)", "9"],
382
379
  ["f = () => { return(3) 4 } f()", "3"],
383
380
  ["a = 1 orirginal = 2 orirginal", "2"],
@@ -488,6 +485,7 @@ RSpec.describe Code do
488
485
  ["", ""]
489
486
  ].each do |input, expected|
490
487
  it "#{input} == #{expected}" do
488
+ puts input
491
489
  formatted = format_input(input)
492
490
 
493
491
  output = StringIO.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -306,6 +306,7 @@ files:
306
306
  - lib/code/parser/group.rb
307
307
  - lib/code/parser/if.rb
308
308
  - lib/code/parser/if_modifier.rb
309
+ - lib/code/parser/label_name.rb
309
310
  - lib/code/parser/left_operation.rb
310
311
  - lib/code/parser/list.rb
311
312
  - lib/code/parser/multiplication.rb
@@ -346,7 +347,6 @@ files:
346
347
  - spec/code/object/decimal_spec.rb
347
348
  - spec/code/object/dictionary_spec.rb
348
349
  - spec/code/object/function_spec.rb
349
- - spec/code/object/http_spec.rb
350
350
  - spec/code/object/identifier_list_spec.rb
351
351
  - spec/code/object/integer_spec.rb
352
352
  - spec/code/object/list_spec.rb
@@ -1,99 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Code::Object::Http do
6
- def self.verbs
7
- %w[get head post put delete options trace patch]
8
- end
9
-
10
- def self.response_codes
11
- {
12
- continue: 100,
13
- switching_protocols: 101,
14
- processing: 102,
15
- early_hints: 103,
16
- ok: 200,
17
- created: 201,
18
- accepted: 202,
19
- non_authoritative_information: 203,
20
- no_content: 204,
21
- reset_content: 205,
22
- partial_content: 206,
23
- multi_status: 207,
24
- already_reported: 208,
25
- im_used: 226,
26
- multiple_choices: 300,
27
- moved_permanently: 301,
28
- found: 302,
29
- see_other: 303,
30
- not_modified: 304,
31
- use_proxy: 305,
32
- reserved: 306,
33
- temporary_redirect: 307,
34
- permanent_redirect: 308,
35
- bad_request: 400,
36
- unauthorized: 401,
37
- payment_required: 402,
38
- forbidden: 403,
39
- not_found: 404,
40
- method_not_allowed: 405,
41
- not_acceptable: 406,
42
- proxy_authentication_required: 407,
43
- request_timeout: 408,
44
- conflict: 409,
45
- gone: 410,
46
- length_required: 411,
47
- precondition_failed: 412,
48
- request_entity_too_large: 413,
49
- request_uri_too_long: 414,
50
- unsupported_media_type: 415,
51
- requested_range_not_satisfiable: 416,
52
- expectation_failed: 417,
53
- misdirected_request: 421,
54
- unprocessable_entity: 422,
55
- locked: 423,
56
- failed_dependency: 424,
57
- too_early: 425,
58
- upgrade_required: 426,
59
- precondition_required: 428,
60
- too_many_requests: 429,
61
- request_header_fields_too_large: 431,
62
- unavailable_for_legal_reasons: 451,
63
- internal_server_error: 500,
64
- not_implemented: 501,
65
- bad_gateway: 502,
66
- service_unavailable: 503,
67
- gateway_timeout: 504,
68
- http_version_not_supported: 505,
69
- variant_also_negotiates: 506,
70
- insufficient_storage: 507,
71
- loop_detected: 508,
72
- bandwidth_limit_exceeded: 509,
73
- not_extended: 510,
74
- network_authentication_required: 511
75
- }
76
- end
77
-
78
- verbs.each do |verb|
79
- describe ".#{verb}" do
80
- response_codes.each do |status, code|
81
- it "returns #{code} as code and #{status} as status" do
82
- expect(Code.evaluate(<<~INPUT)).to eq(Code.evaluate(<<~OUTPUT))
83
- response = Http.#{verb}("https://httpbin.org/status/#{code}")
84
- [response.code, response.status]
85
- INPUT
86
- [#{code}, "#{status}"]
87
- OUTPUT
88
-
89
- expect(Code.evaluate(<<~INPUT)).to eq(Code.evaluate(<<~OUTPUT))
90
- response = Http.fetch("#{verb}", "https://httpbin.org/status/#{code}")
91
- [response.code, response.status]
92
- INPUT
93
- [#{code}, "#{status}"]
94
- OUTPUT
95
- end
96
- end
97
- end
98
- end
99
- end