code-ruby 2.0.1 → 3.0.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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/VERSION +1 -1
  4. data/lib/code/format.rb +4 -2
  5. data/lib/code/parser.rb +1113 -6
  6. data/lib/code-ruby.rb +0 -1
  7. data/spec/code/node/call_spec.rb +10 -0
  8. data/spec/code/parser/boolean_spec.rb +1 -1
  9. data/spec/code/parser/chained_call_spec.rb +1 -1
  10. data/spec/code/parser/dictionary_spec.rb +3 -2
  11. data/spec/code/parser/function_spec.rb +3 -2
  12. data/spec/code/parser/group_spec.rb +1 -1
  13. data/spec/code/parser/if_modifier_spec.rb +1 -1
  14. data/spec/code/parser/list_spec.rb +1 -1
  15. data/spec/code/parser/number_spec.rb +1 -1
  16. data/spec/code/parser/string_spec.rb +1 -1
  17. data/spec/code_spec.rb +2 -1
  18. metadata +1 -41
  19. data/lib/code/parser/addition.rb +0 -23
  20. data/lib/code/parser/and_operator.rb +0 -19
  21. data/lib/code/parser/bitwise_and.rb +0 -23
  22. data/lib/code/parser/bitwise_or.rb +0 -23
  23. data/lib/code/parser/boolean.rb +0 -23
  24. data/lib/code/parser/call.rb +0 -161
  25. data/lib/code/parser/chained_call.rb +0 -31
  26. data/lib/code/parser/class.rb +0 -15
  27. data/lib/code/parser/code.rb +0 -27
  28. data/lib/code/parser/dictionary.rb +0 -72
  29. data/lib/code/parser/equal.rb +0 -67
  30. data/lib/code/parser/equality.rb +0 -42
  31. data/lib/code/parser/function.rb +0 -131
  32. data/lib/code/parser/greater.rb +0 -32
  33. data/lib/code/parser/group.rb +0 -58
  34. data/lib/code/parser/if.rb +0 -101
  35. data/lib/code/parser/if_modifier.rb +0 -39
  36. data/lib/code/parser/left_operation.rb +0 -44
  37. data/lib/code/parser/list.rb +0 -47
  38. data/lib/code/parser/multiplication.rb +0 -39
  39. data/lib/code/parser/name.rb +0 -188
  40. data/lib/code/parser/negation.rb +0 -32
  41. data/lib/code/parser/not_keyword.rb +0 -29
  42. data/lib/code/parser/nothing.rb +0 -19
  43. data/lib/code/parser/number.rb +0 -156
  44. data/lib/code/parser/or_keyword.rb +0 -27
  45. data/lib/code/parser/or_operator.rb +0 -19
  46. data/lib/code/parser/power.rb +0 -19
  47. data/lib/code/parser/range.rb +0 -19
  48. data/lib/code/parser/rescue.rb +0 -19
  49. data/lib/code/parser/right_operation.rb +0 -44
  50. data/lib/code/parser/shift.rb +0 -23
  51. data/lib/code/parser/splat.rb +0 -33
  52. data/lib/code/parser/square_bracket.rb +0 -44
  53. data/lib/code/parser/statement.rb +0 -11
  54. data/lib/code/parser/string.rb +0 -81
  55. data/lib/code/parser/ternary.rb +0 -45
  56. data/lib/code/parser/unary_minus.rb +0 -33
  57. data/lib/code/parser/while.rb +0 -81
  58. data/lib/code/parser/whitespace.rb +0 -51
data/lib/code-ruby.rb CHANGED
@@ -8,7 +8,6 @@ require "date"
8
8
  require "did_you_mean"
9
9
  require "digest"
10
10
  require "json"
11
- require "language-ruby"
12
11
  require "mail"
13
12
  require "net/http"
14
13
  require "nokogiri"
@@ -1,3 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "spec_helper"
4
+
5
+ RSpec.describe "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
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::Boolean do
5
+ RSpec.describe "parser boolean" do
6
6
  [%w[true !false], %w[false !true]].each do |input, expected|
7
7
  it "#{input} == #{expected}" do
8
8
  expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::ChainedCall do
5
+ RSpec.describe "parser chained call" do
6
6
  [
7
7
  "a.b",
8
8
  "user.first_name",
@@ -2,13 +2,14 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::Dictionary do
5
+ RSpec.describe "parser dictionary" do
6
6
  [
7
7
  "{}",
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)
@@ -2,11 +2,12 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::Function do
5
+ RSpec.describe "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)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::Group do
5
+ RSpec.describe "parser group" do
6
6
  ["(true (nothing))"].each do |input|
7
7
  it "parses #{input}" do
8
8
  Code::Parser.parse(input)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::IfModifier do
5
+ RSpec.describe "parser if modifier" do
6
6
  [
7
7
  ["1 if true", "1"],
8
8
  ["1 if false", "nothing"],
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::List do
5
+ RSpec.describe "parser list" do
6
6
  [
7
7
  "[]",
8
8
  "[ ]",
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::Number do
5
+ RSpec.describe "parser number" do
6
6
  %w[0 1 1.1 123.123 1_000.12_244 0xf0 0o70 0b10].each do |input|
7
7
  it "parses #{input}" do
8
8
  Code::Parser.parse(input)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- RSpec.describe Code::Parser::String do
5
+ RSpec.describe "parser string" do
6
6
  [
7
7
  "''",
8
8
  '""',
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],
@@ -343,6 +344,7 @@ RSpec.describe Code do
343
344
  ["a = 1 a += 1 a", "2"],
344
345
  ["a = 1 a -= 1 a", "0"],
345
346
  ["a = 3 a -= 1 if false a", "3"],
347
+ ["a = 1 a = - 1 if false a", "1"],
346
348
  ["a = 3\n(a -= 1) if false\na", "3"],
347
349
  ["a = 1 a /= 2 a", "0.5"],
348
350
  ["a = 1 a <<= 1 a", "2"],
@@ -484,7 +486,6 @@ RSpec.describe Code do
484
486
  ["", ""]
485
487
  ].each do |input, expected|
486
488
  it "#{input} == #{expected}" do
487
- puts input
488
489
  formatted = format_input(input)
489
490
 
490
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.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -289,46 +289,6 @@ files:
289
289
  - lib/code/object/time.rb
290
290
  - lib/code/object/url.rb
291
291
  - lib/code/parser.rb
292
- - lib/code/parser/addition.rb
293
- - lib/code/parser/and_operator.rb
294
- - lib/code/parser/bitwise_and.rb
295
- - lib/code/parser/bitwise_or.rb
296
- - lib/code/parser/boolean.rb
297
- - lib/code/parser/call.rb
298
- - lib/code/parser/chained_call.rb
299
- - lib/code/parser/class.rb
300
- - lib/code/parser/code.rb
301
- - lib/code/parser/dictionary.rb
302
- - lib/code/parser/equal.rb
303
- - lib/code/parser/equality.rb
304
- - lib/code/parser/function.rb
305
- - lib/code/parser/greater.rb
306
- - lib/code/parser/group.rb
307
- - lib/code/parser/if.rb
308
- - lib/code/parser/if_modifier.rb
309
- - lib/code/parser/left_operation.rb
310
- - lib/code/parser/list.rb
311
- - lib/code/parser/multiplication.rb
312
- - lib/code/parser/name.rb
313
- - lib/code/parser/negation.rb
314
- - lib/code/parser/not_keyword.rb
315
- - lib/code/parser/nothing.rb
316
- - lib/code/parser/number.rb
317
- - lib/code/parser/or_keyword.rb
318
- - lib/code/parser/or_operator.rb
319
- - lib/code/parser/power.rb
320
- - lib/code/parser/range.rb
321
- - lib/code/parser/rescue.rb
322
- - lib/code/parser/right_operation.rb
323
- - lib/code/parser/shift.rb
324
- - lib/code/parser/splat.rb
325
- - lib/code/parser/square_bracket.rb
326
- - lib/code/parser/statement.rb
327
- - lib/code/parser/string.rb
328
- - lib/code/parser/ternary.rb
329
- - lib/code/parser/unary_minus.rb
330
- - lib/code/parser/while.rb
331
- - lib/code/parser/whitespace.rb
332
292
  - lib/code/type.rb
333
293
  - lib/code/type/hash.rb
334
294
  - lib/code/type/maybe.rb
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Addition < LeftOperation
6
- def statement
7
- Multiplication
8
- end
9
-
10
- def plus
11
- str("+")
12
- end
13
-
14
- def minus
15
- str("-")
16
- end
17
-
18
- def operator
19
- plus | minus
20
- end
21
- end
22
- end
23
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class AndOperator < LeftOperation
6
- def statement
7
- Equality
8
- end
9
-
10
- def ampersand
11
- str("&")
12
- end
13
-
14
- def operator
15
- ampersand << ampersand
16
- end
17
- end
18
- end
19
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class BitwiseAnd < LeftOperation
6
- def statement
7
- Shift
8
- end
9
-
10
- def whitespace?
11
- whitespace
12
- end
13
-
14
- def ampersand
15
- str("&")
16
- end
17
-
18
- def operator
19
- ampersand
20
- end
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class BitwiseOr < LeftOperation
6
- def statement
7
- BitwiseAnd
8
- end
9
-
10
- def pipe
11
- str("|")
12
- end
13
-
14
- def caret
15
- str("^")
16
- end
17
-
18
- def operator
19
- pipe | caret
20
- end
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Boolean < Language
6
- def separator
7
- Name.new.separator
8
- end
9
-
10
- def true_keyword
11
- str("true") << separator.present
12
- end
13
-
14
- def false_keyword
15
- str("false") << separator.present
16
- end
17
-
18
- def root
19
- (true_keyword | false_keyword).aka(:boolean) | Nothing
20
- end
21
- end
22
- end
23
- end
@@ -1,161 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Call < Language
6
- def name
7
- Name
8
- end
9
-
10
- def whitespace
11
- Whitespace
12
- end
13
-
14
- def whiltespace_without_newline?
15
- Whitespace.new.without_newline.maybe
16
- end
17
-
18
- def whitespace?
19
- whitespace.maybe
20
- end
21
-
22
- def code
23
- Code
24
- end
25
-
26
- def code_present
27
- Code.new.present
28
- end
29
-
30
- def colon
31
- str(":")
32
- end
33
-
34
- def comma
35
- str(",")
36
- end
37
-
38
- def pipe
39
- str("|")
40
- end
41
-
42
- def equal
43
- str("=")
44
- end
45
-
46
- def opening_curly_bracket
47
- str("{")
48
- end
49
-
50
- def closing_curly_bracket
51
- str("}")
52
- end
53
-
54
- def opening_parenthesis
55
- str("(")
56
- end
57
-
58
- def closing_parenthesis
59
- str(")")
60
- end
61
-
62
- def separator
63
- Name.new.separator
64
- end
65
-
66
- def do_keyword
67
- str("do") << separator.present
68
- end
69
-
70
- def begin_keyword
71
- str("do") << separator.present
72
- end
73
-
74
- def end_keyword
75
- str("end") << separator.present
76
- end
77
-
78
- def asterisk
79
- str("*")
80
- end
81
-
82
- def ampersand
83
- str("&")
84
- end
85
-
86
- def spread_operator
87
- str("...") | str("..") | str(".")
88
- end
89
-
90
- def keyword_argument
91
- name.aka(:name) << whitespace? << colon << code.aka(:value)
92
- end
93
-
94
- def regular_argument
95
- code_present.aka(:value)
96
- end
97
-
98
- def argument
99
- keyword_argument | regular_argument
100
- end
101
-
102
- def arguments
103
- opening_parenthesis.ignore << whitespace? <<
104
- (
105
- whitespace? << argument <<
106
- (whitespace? << comma << whitespace?).maybe
107
- ).repeat << whitespace? << closing_parenthesis.ignore.maybe
108
- end
109
-
110
- def prefix
111
- (asterisk << asterisk).aka(:keyword_splat) |
112
- asterisk.aka(:regular_splat) | ampersand.aka(:block) |
113
- spread_operator.aka(:spread)
114
- end
115
-
116
- def keyword_parameter
117
- name.aka(:name) << whitespace? << colon.aka(:keyword) <<
118
- code_present.aka(:default).maybe
119
- end
120
-
121
- def regular_parameter
122
- ((prefix.maybe << name.aka(:name)) | prefix) << whitespace? <<
123
- (equal << whitespace? << code_present.aka(:default)).maybe
124
- end
125
-
126
- def parameter
127
- keyword_parameter | regular_parameter
128
- end
129
-
130
- def parameters
131
- pipe.ignore << whitespace? <<
132
- (
133
- whitespace? << parameter <<
134
- (whitespace? << comma << whitespace?).maybe
135
- ).repeat << (whitespace? << pipe.ignore).maybe
136
- end
137
-
138
- def block
139
- (
140
- (do_keyword | begin_keyword).ignore << whitespace <<
141
- parameters.aka(:parameters).maybe << whitespace? <<
142
- code.aka(:body) << (whitespace? << end_keyword).maybe.ignore
143
- ) |
144
- (
145
- opening_curly_bracket.ignore << whitespace? <<
146
- parameters.aka(:parameters).maybe << whitespace? <<
147
- code.aka(:body) <<
148
- (whitespace? << closing_curly_bracket).maybe.ignore
149
- )
150
- end
151
-
152
- def root
153
- (
154
- name.aka(:name) <<
155
- (whiltespace_without_newline? << arguments.aka(:arguments)).maybe <<
156
- (whiltespace_without_newline? << block.aka(:block)).maybe
157
- ).aka(:call)
158
- end
159
- end
160
- end
161
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class ChainedCall < LeftOperation
6
- def statement
7
- SquareBracket
8
- end
9
-
10
- def dot
11
- str(".")
12
- end
13
-
14
- def ampersand
15
- str("&")
16
- end
17
-
18
- def colon
19
- str(":")
20
- end
21
-
22
- def right_statement
23
- ChainedCall
24
- end
25
-
26
- def operator
27
- dot | (colon << colon) | (ampersand << dot)
28
- end
29
- end
30
- end
31
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Class < Language
6
- def statement
7
- While
8
- end
9
-
10
- def root
11
- statement
12
- end
13
- end
14
- end
15
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Code < Language
6
- def whitespace
7
- Whitespace
8
- end
9
-
10
- def whitespace?
11
- whitespace.maybe
12
- end
13
-
14
- def statement
15
- Statement
16
- end
17
-
18
- def present
19
- (whitespace? << statement << whitespace?).repeat(1)
20
- end
21
-
22
- def root
23
- present | whitespace?
24
- end
25
- end
26
- end
27
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Parser
5
- class Dictionary < Language
6
- def code_present
7
- Code.new.present
8
- end
9
-
10
- def statement
11
- Statement
12
- end
13
-
14
- def name
15
- Name
16
- end
17
-
18
- def whitespace
19
- Whitespace
20
- end
21
-
22
- def whitespace?
23
- whitespace.maybe
24
- end
25
-
26
- def opening_curly_bracket
27
- str("{")
28
- end
29
-
30
- def closing_curly_bracket
31
- str("}")
32
- end
33
-
34
- def comma
35
- str(",")
36
- end
37
-
38
- def colon
39
- str(":")
40
- end
41
-
42
- def equal
43
- str("=")
44
- end
45
-
46
- def greater
47
- str(">")
48
- end
49
-
50
- def key_value
51
- (name.aka(:name) << colon << code_present.aka(:code).maybe).aka(
52
- :name_code
53
- ) |
54
- (
55
- statement.aka(:statement) << colon << code_present.aka(:code).maybe
56
- ).aka(:statement_code) |
57
- (
58
- statement.aka(:statement) << whitespace? << equal << greater <<
59
- code_present.aka(:code).maybe
60
- ).aka(:statement_code) | code_present.aka(:code)
61
- end
62
-
63
- def root
64
- (
65
- opening_curly_bracket.ignore << whitespace? <<
66
- (whitespace? << key_value << (whitespace? << comma).maybe).repeat <<
67
- (whitespace? << closing_curly_bracket.ignore).maybe
68
- ).aka(:dictionnary) | List
69
- end
70
- end
71
- end
72
- end