code-ruby 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) 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 +31 -0
  8. data/Gemfile +3 -0
  9. data/Gemfile.lock +70 -0
  10. data/LICENSE +7 -0
  11. data/README.md +103 -0
  12. data/TODO.md +1 -0
  13. data/bin/template +39 -0
  14. data/code-ruby.gemspec +22 -0
  15. data/docs/euler/1.template +14 -0
  16. data/docs/euler/2.template +16 -0
  17. data/docs/euler/3.template +16 -0
  18. data/docs/euler/4.template +11 -0
  19. data/docs/euler/5.template +14 -0
  20. data/docs/precedence.template +94 -0
  21. data/lib/code/error.rb +15 -0
  22. data/lib/code/node/base_10_decimal.rb +32 -0
  23. data/lib/code/node/base_10_integer.rb +32 -0
  24. data/lib/code/node/base_10_number.rb +19 -0
  25. data/lib/code/node/base_16_number.rb +19 -0
  26. data/lib/code/node/base_2_number.rb +19 -0
  27. data/lib/code/node/base_8_number.rb +19 -0
  28. data/lib/code/node/block.rb +17 -0
  29. data/lib/code/node/boolean.rb +22 -0
  30. data/lib/code/node/call.rb +52 -0
  31. data/lib/code/node/call_argument.rb +37 -0
  32. data/lib/code/node/chained_call.rb +38 -0
  33. data/lib/code/node/code.rb +16 -0
  34. data/lib/code/node/defined.rb +19 -0
  35. data/lib/code/node/dictionnary.rb +22 -0
  36. data/lib/code/node/dictionnary_key_value.rb +23 -0
  37. data/lib/code/node/equal.rb +36 -0
  38. data/lib/code/node/function.rb +17 -0
  39. data/lib/code/node/function_argument.rb +45 -0
  40. data/lib/code/node/group.rb +13 -0
  41. data/lib/code/node/if.rb +55 -0
  42. data/lib/code/node/if_modifier.rb +48 -0
  43. data/lib/code/node/keyword_call_argument.rb +30 -0
  44. data/lib/code/node/keyword_function_argument.rb +33 -0
  45. data/lib/code/node/list.rb +19 -0
  46. data/lib/code/node/name.rb +50 -0
  47. data/lib/code/node/negation.rb +33 -0
  48. data/lib/code/node/not_keyword.rb +13 -0
  49. data/lib/code/node/nothing.rb +12 -0
  50. data/lib/code/node/number.rb +23 -0
  51. data/lib/code/node/operation.rb +33 -0
  52. data/lib/code/node/or_keyword.rb +34 -0
  53. data/lib/code/node/power.rb +16 -0
  54. data/lib/code/node/range.rb +31 -0
  55. data/lib/code/node/regular_call_argument.rb +34 -0
  56. data/lib/code/node/regular_function_argument.rb +36 -0
  57. data/lib/code/node/rescue.rb +16 -0
  58. data/lib/code/node/statement.rb +81 -0
  59. data/lib/code/node/string.rb +17 -0
  60. data/lib/code/node/ternary.rb +26 -0
  61. data/lib/code/node/unary_minus.rb +22 -0
  62. data/lib/code/node/while.rb +42 -0
  63. data/lib/code/node.rb +14 -0
  64. data/lib/code/object/argument.rb +41 -0
  65. data/lib/code/object/boolean.rb +27 -0
  66. data/lib/code/object/decimal.rb +54 -0
  67. data/lib/code/object/dictionnary.rb +55 -0
  68. data/lib/code/object/function.rb +64 -0
  69. data/lib/code/object/integer.rb +116 -0
  70. data/lib/code/object/list.rb +217 -0
  71. data/lib/code/object/nothing.rb +23 -0
  72. data/lib/code/object/number.rb +6 -0
  73. data/lib/code/object/range.rb +158 -0
  74. data/lib/code/object/string.rb +68 -0
  75. data/lib/code/object.rb +130 -0
  76. data/lib/code/parser/addition.rb +29 -0
  77. data/lib/code/parser/and_operator.rb +28 -0
  78. data/lib/code/parser/bitwise_and.rb +28 -0
  79. data/lib/code/parser/bitwise_or.rb +29 -0
  80. data/lib/code/parser/boolean.rb +14 -0
  81. data/lib/code/parser/call.rb +90 -0
  82. data/lib/code/parser/code.rb +19 -0
  83. data/lib/code/parser/defined.rb +20 -0
  84. data/lib/code/parser/dictionnary.rb +41 -0
  85. data/lib/code/parser/equal.rb +42 -0
  86. data/lib/code/parser/equality.rb +36 -0
  87. data/lib/code/parser/function.rb +57 -0
  88. data/lib/code/parser/greater_than.rb +33 -0
  89. data/lib/code/parser/group.rb +17 -0
  90. data/lib/code/parser/if.rb +33 -0
  91. data/lib/code/parser/if_modifier.rb +28 -0
  92. data/lib/code/parser/list.rb +29 -0
  93. data/lib/code/parser/multiplication.rb +30 -0
  94. data/lib/code/parser/name.rb +89 -0
  95. data/lib/code/parser/negation.rb +19 -0
  96. data/lib/code/parser/not_keyword.rb +21 -0
  97. data/lib/code/parser/nothing.rb +17 -0
  98. data/lib/code/parser/number.rb +98 -0
  99. data/lib/code/parser/or_keyword.rb +29 -0
  100. data/lib/code/parser/or_operator.rb +28 -0
  101. data/lib/code/parser/power.rb +25 -0
  102. data/lib/code/parser/range.rb +25 -0
  103. data/lib/code/parser/rescue.rb +23 -0
  104. data/lib/code/parser/shift.rb +31 -0
  105. data/lib/code/parser/statement.rb +8 -0
  106. data/lib/code/parser/string.rb +72 -0
  107. data/lib/code/parser/ternary.rb +25 -0
  108. data/lib/code/parser/unary_minus.rb +13 -0
  109. data/lib/code/parser/while.rb +25 -0
  110. data/lib/code/parser.rb +4 -0
  111. data/lib/code-ruby.rb +11 -0
  112. data/lib/code.rb +29 -0
  113. data/lib/template/node/code_part.rb +13 -0
  114. data/lib/template/node/part.rb +19 -0
  115. data/lib/template/node/template.rb +15 -0
  116. data/lib/template/node/text_part.rb +13 -0
  117. data/lib/template/node.rb +4 -0
  118. data/lib/template/parser/template.rb +30 -0
  119. data/lib/template/parser.rb +4 -0
  120. data/lib/template/version.rb +3 -0
  121. data/lib/template-ruby.rb +11 -0
  122. data/lib/template.rb +34 -0
  123. data/spec/call_spec.rb +22 -0
  124. data/spec/code/error/type_error_spec.rb +65 -0
  125. data/spec/code/parser/boolean_spec.rb +18 -0
  126. data/spec/code/parser/call_spec.rb +66 -0
  127. data/spec/code/parser/dictionnary_spec.rb +46 -0
  128. data/spec/code/parser/function_spec.rb +32 -0
  129. data/spec/code/parser/list_spec.rb +29 -0
  130. data/spec/code/parser/name_spec.rb +15 -0
  131. data/spec/code/parser/nothing_spec.rb +19 -0
  132. data/spec/code/parser/number_spec.rb +117 -0
  133. data/spec/code/parser/string_spec.rb +30 -0
  134. data/spec/code_spec.rb +108 -0
  135. data/spec/function_spec.rb +26 -0
  136. data/spec/spec_helper.rb +3 -0
  137. data/spec/template/parser/template_spec.rb +19 -0
  138. data/spec/template_spec.rb +27 -0
  139. data/template-ruby.gemspec +24 -0
  140. metadata +266 -0
@@ -0,0 +1,29 @@
1
+ class Code
2
+ class Parser
3
+ class Addition < Parslet::Parser
4
+ rule(:multiplication) { ::Code::Parser::Multiplication.new }
5
+
6
+ rule(:plus) { str("+") }
7
+ rule(:minus) { str("-") }
8
+
9
+ rule(:operator) { plus | minus }
10
+
11
+ rule(:space) { str(" ") }
12
+ rule(:newline) { str("\n") }
13
+ rule(:whitespace) { (space | newline).repeat(1) }
14
+ rule(:whitespace?) { whitespace.maybe }
15
+
16
+ rule(:addition) do
17
+ (
18
+ multiplication.as(:first) >>
19
+ (
20
+ whitespace? >> operator.as(:operator) >> whitespace? >>
21
+ multiplication.as(:statement)
22
+ ).repeat(1).as(:rest)
23
+ ).as(:addition) | multiplication
24
+ end
25
+
26
+ root(:addition)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ class Code
2
+ class Parser
3
+ class AndOperator < Parslet::Parser
4
+ rule(:equality) { ::Code::Parser::Equality.new }
5
+
6
+ rule(:ampersand) { str("&") }
7
+
8
+ rule(:operator) { ampersand >> ampersand }
9
+
10
+ rule(:space) { str(" ") }
11
+ rule(:newline) { str("\n") }
12
+ rule(:whitespace) { (space | newline).repeat(1) }
13
+ rule(:whitespace?) { whitespace.maybe }
14
+
15
+ rule(:and_operator) do
16
+ (
17
+ equality.as(:first) >>
18
+ (
19
+ whitespace? >> operator.as(:operator) >> whitespace? >>
20
+ equality.as(:statement)
21
+ ).repeat(1).as(:rest)
22
+ ).as(:and_operator) | equality
23
+ end
24
+
25
+ root(:and_operator)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ class Code
2
+ class Parser
3
+ class BitwiseAnd < Parslet::Parser
4
+ rule(:shift) { ::Code::Parser::Shift.new }
5
+
6
+ rule(:ampersand) { str("&") }
7
+
8
+ rule(:operator) { ampersand }
9
+
10
+ rule(:space) { str(" ") }
11
+ rule(:newline) { str("\n") }
12
+ rule(:whitespace) { (space | newline).repeat(1) }
13
+ rule(:whitespace?) { whitespace.maybe }
14
+
15
+ rule(:bitwise_and) do
16
+ (
17
+ shift.as(:first) >>
18
+ (
19
+ whitespace? >> operator.as(:operator) >> whitespace? >>
20
+ shift.as(:statement)
21
+ ).repeat(1).as(:rest)
22
+ ).as(:bitwise_and) | shift
23
+ end
24
+
25
+ root(:bitwise_and)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ class Code
2
+ class Parser
3
+ class BitwiseOr < Parslet::Parser
4
+ rule(:bitwise_and) { ::Code::Parser::BitwiseAnd.new }
5
+
6
+ rule(:pipe) { str("|") }
7
+ rule(:caret) { str("^") }
8
+
9
+ rule(:operator) { pipe | caret }
10
+
11
+ rule(:space) { str(" ") }
12
+ rule(:newline) { str("\n") }
13
+ rule(:whitespace) { (space | newline).repeat(1) }
14
+ rule(:whitespace?) { whitespace.maybe }
15
+
16
+ rule(:bitwise_or) do
17
+ (
18
+ bitwise_and.as(:first) >>
19
+ (
20
+ whitespace? >> operator.as(:operator) >> whitespace? >>
21
+ bitwise_and.as(:statement)
22
+ ).repeat(1).as(:rest)
23
+ ).as(:bitwise_or) | bitwise_and
24
+ end
25
+
26
+ root(:bitwise_or)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ class Code
2
+ class Parser
3
+ class Boolean < Parslet::Parser
4
+ rule(:nothing) { ::Code::Parser::Nothing.new }
5
+
6
+ rule(:true_keyword) { str("true") }
7
+ rule(:false_keyword) { str("false") }
8
+
9
+ rule(:boolean) { (true_keyword | false_keyword).as(:boolean) | nothing }
10
+
11
+ root(:boolean)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,90 @@
1
+ class Code
2
+ class Parser
3
+ class Call < Parslet::Parser
4
+ rule(:dictionnary) { ::Code::Parser::Dictionnary.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+ rule(:name) { ::Code::Parser::Name.new }
7
+ rule(:function_arguments) { ::Code::Parser::Function.new.arguments }
8
+
9
+ rule(:dot) { str(".") }
10
+ rule(:opening_parenthesis) { str("(") }
11
+ rule(:closing_parenthesis) { str(")") }
12
+ rule(:opening_curly_bracket) { str("{") }
13
+ rule(:closing_curly_bracket) { str("}") }
14
+ rule(:comma) { str(",") }
15
+ rule(:colon) { str(":") }
16
+ rule(:ampersand) { str("&") }
17
+ rule(:asterisk) { str("*") }
18
+ rule(:pipe) { str("|") }
19
+ rule(:do_keyword) { str("do") }
20
+ rule(:end_keyword) { str("end") }
21
+
22
+ rule(:space) { str(" ") }
23
+ rule(:newline) { str("\n") }
24
+ rule(:whitespace) { (space | newline).repeat(1) }
25
+ rule(:whitespace?) { whitespace.maybe }
26
+
27
+ rule(:keyword_argument) do
28
+ name >> whitespace? >> colon >> whitespace? >> code.as(:value)
29
+ end
30
+
31
+ rule(:regular_argument) do
32
+ ampersand.as(:block).maybe >>
33
+ (asterisk >> asterisk).as(:keyword_splat).maybe >>
34
+ asterisk.as(:splat).maybe >> code.as(:value)
35
+ end
36
+
37
+ rule(:argument) do
38
+ keyword_argument.as(:keyword) | regular_argument.as(:regular)
39
+ end
40
+
41
+ rule(:arguments) do
42
+ argument.repeat(1, 1) >>
43
+ (whitespace? >> comma >> whitespace? >> argument).repeat
44
+ end
45
+
46
+ rule(:single_call) do
47
+ dictionnary.as(:left) >>
48
+ (
49
+ opening_parenthesis >> whitespace? >>
50
+ arguments.as(:arguments).maybe >> whitespace? >>
51
+ closing_parenthesis
52
+ ) >> block.as(:block).maybe
53
+ end
54
+
55
+ rule(:chained_single_call) do
56
+ dot >> name >>
57
+ (
58
+ opening_parenthesis >> whitespace? >>
59
+ arguments.as(:arguments).maybe >> whitespace? >>
60
+ closing_parenthesis
61
+ ).maybe >> block.as(:block).maybe
62
+ end
63
+
64
+ rule(:chained_call) do
65
+ dictionnary.as(:left) >> chained_single_call.repeat(1).as(:right)
66
+ end
67
+
68
+ rule(:block_arguments) do
69
+ pipe >> whitespace? >> function_arguments >> whitespace? >> pipe
70
+ end
71
+
72
+ rule(:block) do
73
+ (
74
+ whitespace >> do_keyword >> whitespace >>
75
+ block_arguments.as(:arguments).maybe >> code.as(:body) >>
76
+ end_keyword
77
+ ) |
78
+ (
79
+ whitespace? >> opening_curly_bracket >> whitespace >>
80
+ block_arguments.as(:arguments).maybe >> code.as(:body) >>
81
+ closing_curly_bracket
82
+ )
83
+ end
84
+
85
+ rule(:call) { (single_call | chained_call).as(:call) | dictionnary }
86
+
87
+ root(:call)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Parser
3
+ class Code < Parslet::Parser
4
+ rule(:statement) { ::Code::Parser::Statement.new }
5
+
6
+ rule(:space) { str(" ") }
7
+ rule(:newline) { str("\n") }
8
+ rule(:whitespace) { (space | newline).repeat(1) }
9
+ rule(:whitespace?) { whitespace.maybe }
10
+
11
+ rule(:code) do
12
+ (whitespace?.ignore >> statement >> whitespace?.ignore).repeat(1) |
13
+ whitespace?.ignore
14
+ end
15
+
16
+ root(:code)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ class Code
2
+ class Parser
3
+ class Defined < Parslet::Parser
4
+ rule(:equal) { ::Code::Parser::Equal.new }
5
+ rule(:name) { ::Code::Parser::Name.new }
6
+
7
+ rule(:defined_keyword) { str("defined?") }
8
+ rule(:opening_parenthesis) { str("(") }
9
+ rule(:closing_parenthesis) { str(")") }
10
+
11
+ rule(:defined) do
12
+ (
13
+ defined_keyword >> opening_parenthesis >> name >> closing_parenthesis
14
+ ).as(:defined) | equal
15
+ end
16
+
17
+ root(:defined)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ class Code
2
+ class Parser
3
+ class Dictionnary < Parslet::Parser
4
+ rule(:list) { ::Code::Parser::List.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+ rule(:string) { ::Code::Parser::String.new }
7
+
8
+ rule(:opening_curly_bracket) { str("{") }
9
+ rule(:closing_curly_bracket) { str("}") }
10
+ rule(:colon) { str(":") }
11
+ rule(:equal) { str("=") }
12
+ rule(:right_caret) { str(">") }
13
+ rule(:comma) { str(",") }
14
+
15
+ rule(:space) { str(" ") }
16
+ rule(:newline) { str("\n") }
17
+ rule(:whitespace) { (space | newline).repeat(1) }
18
+ rule(:whitespace?) { whitespace.maybe }
19
+
20
+ rule(:key_value) do
21
+ (string.as(:key) >> colon >> whitespace? >> code.as(:value)) |
22
+ (
23
+ code.as(:key) >> whitespace? >> equal >> right_caret >>
24
+ whitespace? >> code.as(:value)
25
+ )
26
+ end
27
+
28
+ rule(:dictionnary) do
29
+ (
30
+ opening_curly_bracket.ignore >> whitespace?.ignore >>
31
+ key_value.repeat(0, 1) >>
32
+ (whitespace? >> comma >> whitespace? >> key_value).repeat >>
33
+ whitespace?.ignore >> comma.maybe.ignore >> whitespace?.ignore >>
34
+ closing_curly_bracket.ignore
35
+ ).as(:dictionnary) | list
36
+ end
37
+
38
+ root(:dictionnary)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,42 @@
1
+ class Code
2
+ class Parser
3
+ class Equal < Parslet::Parser
4
+ rule(:rescue_rule) { ::Code::Parser::Rescue.new }
5
+ rule(:name) { ::Code::Parser::Name.new }
6
+
7
+ rule(:equal) { str("=") }
8
+ rule(:plus) { str("+") }
9
+ rule(:minus) { str("-") }
10
+ rule(:asterisk) { str("*") }
11
+ rule(:slash) { str("/") }
12
+ rule(:percent) { str("%") }
13
+ rule(:left_caret) { str("<") }
14
+ rule(:right_caret) { str(">") }
15
+ rule(:ampersand) { str("&") }
16
+ rule(:pipe) { str("|") }
17
+ rule(:caret) { str("^") }
18
+
19
+ rule(:operator) do
20
+ equal | (plus >> equal) | (minus >> equal) | (asterisk >> equal) |
21
+ (slash >> equal) | (percent >> equal) |
22
+ (left_caret >> left_caret >> equal) |
23
+ (right_caret >> right_caret >> equal) | (ampersand >> equal) |
24
+ (pipe >> equal) | (caret >> equal)
25
+ end
26
+
27
+ rule(:space) { str(" ") }
28
+ rule(:newline) { str("\n") }
29
+ rule(:whitespace) { (space | newline).repeat(1) }
30
+ rule(:whitespace?) { whitespace.maybe }
31
+
32
+ rule(:equal_rule) do
33
+ (
34
+ name.as(:left) >> whitespace? >> operator.as(:operator) >>
35
+ whitespace? >> rescue_rule.as(:right)
36
+ ).as(:equal) | rescue_rule
37
+ end
38
+
39
+ root(:equal_rule)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,36 @@
1
+ class Code
2
+ class Parser
3
+ class Equality < Parslet::Parser
4
+ rule(:greater_than) { ::Code::Parser::GreaterThan.new }
5
+
6
+ rule(:right_caret) { str(">") }
7
+ rule(:left_caret) { str("<") }
8
+ rule(:equal) { str("=") }
9
+ rule(:exclamation_point) { str("!") }
10
+ rule(:tilde) { str("~") }
11
+
12
+ rule(:operator) do
13
+ (left_caret >> equal >> right_caret) | (equal >> equal >> equal) |
14
+ (equal >> equal) | (exclamation_point >> equal) | (equal >> tilde) |
15
+ (exclamation_point >> tilde)
16
+ end
17
+
18
+ rule(:space) { str(" ") }
19
+ rule(:newline) { str("\n") }
20
+ rule(:whitespace) { (space | newline).repeat(1) }
21
+ rule(:whitespace?) { whitespace.maybe }
22
+
23
+ rule(:equality) do
24
+ (
25
+ greater_than.as(:first) >>
26
+ (
27
+ whitespace? >> operator.as(:operator) >> whitespace? >>
28
+ greater_than.as(:statement)
29
+ ).repeat(1).as(:rest)
30
+ ).as(:equality) | greater_than
31
+ end
32
+
33
+ root(:equality)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ class Code
2
+ class Parser
3
+ class Function < Parslet::Parser
4
+ rule(:call) { ::Code::Parser::Call.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+ rule(:name) { ::Code::Parser::Name.new }
7
+
8
+ rule(:opening_parenthesis) { str("(") }
9
+ rule(:closing_parenthesis) { str(")") }
10
+ rule(:equal) { str("=") }
11
+ rule(:right_caret) { str(">") }
12
+ rule(:opening_curly_bracket) { str("{") }
13
+ rule(:closing_curly_bracket) { str("}") }
14
+ rule(:comma) { str(",") }
15
+ rule(:colon) { str(":") }
16
+ rule(:asterisk) { str("*") }
17
+ rule(:ampersand) { str("&") }
18
+
19
+ rule(:space) { str(" ") }
20
+ rule(:newline) { str("\n") }
21
+ rule(:whitespace) { (space | newline).repeat(1) }
22
+ rule(:whitespace?) { whitespace.maybe }
23
+
24
+ rule(:keyword_argument) do
25
+ name >> whitespace? >> colon >> (whitespace? >> code.as(:default)).maybe
26
+ end
27
+
28
+ rule(:regular_argument) do
29
+ ampersand.as(:block).maybe >>
30
+ (asterisk >> asterisk).as(:keyword_splat).maybe >>
31
+ asterisk.as(:splat).maybe >> name >>
32
+ (whitespace? >> equal >> whitespace? >> code.as(:default)).maybe
33
+ end
34
+
35
+ rule(:argument) do
36
+ keyword_argument.as(:keyword) | regular_argument.as(:regular)
37
+ end
38
+
39
+ rule(:arguments) do
40
+ argument.repeat(1, 1) >>
41
+ (whitespace? >> comma >> whitespace? >> argument).repeat
42
+ end
43
+
44
+ rule(:function) do
45
+ (
46
+ opening_parenthesis >> whitespace? >>
47
+ arguments.as(:arguments).maybe >> whitespace? >>
48
+ closing_parenthesis >> whitespace? >> equal >> right_caret >>
49
+ whitespace? >> opening_curly_bracket >> code.as(:body) >>
50
+ closing_curly_bracket
51
+ ).as(:function) | call
52
+ end
53
+
54
+ root(:function)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ class Code
2
+ class Parser
3
+ class GreaterThan < Parslet::Parser
4
+ rule(:bitwise_or) { ::Code::Parser::BitwiseOr.new }
5
+
6
+ rule(:right_caret) { str(">") }
7
+ rule(:left_caret) { str("<") }
8
+ rule(:equal) { str("=") }
9
+
10
+ rule(:operator) do
11
+ (right_caret >> equal) | (left_caret >> equal) | right_caret |
12
+ left_caret
13
+ end
14
+
15
+ rule(:space) { str(" ") }
16
+ rule(:newline) { str("\n") }
17
+ rule(:whitespace) { (space | newline).repeat(1) }
18
+ rule(:whitespace?) { whitespace.maybe }
19
+
20
+ rule(:greater_than) do
21
+ (
22
+ bitwise_or.as(:first) >>
23
+ (
24
+ whitespace? >> operator.as(:operator) >> whitespace? >>
25
+ bitwise_or.as(:statement)
26
+ ).repeat(1).as(:rest)
27
+ ).as(:greater_than) | bitwise_or
28
+ end
29
+
30
+ root(:greater_than)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ class Code
2
+ class Parser
3
+ class Group < Parslet::Parser
4
+ rule(:name) { ::Code::Parser::Name.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+
7
+ rule(:opening_parenthesis) { str("(") }
8
+ rule(:closing_parenthesis) { str(")") }
9
+
10
+ rule(:group) do
11
+ (opening_parenthesis >> code >> closing_parenthesis).as(:group) | name
12
+ end
13
+
14
+ root(:group)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ class Code
2
+ class Parser
3
+ class If < Parslet::Parser
4
+ rule(:if_modifier) { ::Code::Parser::IfModifier.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+
7
+ rule(:if_keyword) { str("if") }
8
+ rule(:else_keyword) { str("else") }
9
+ rule(:unless_keyword) { str("unless") }
10
+ rule(:end_keyword) { str("end") }
11
+
12
+ rule(:space) { str(" ") }
13
+ rule(:newline) { str("\n") }
14
+ rule(:whitespace) { (space | newline).repeat(1) }
15
+
16
+ rule(:if_rule) do
17
+ (
18
+ (if_keyword | unless_keyword).as(:if_operator) >> whitespace >>
19
+ if_modifier.as(:if_statement) >> code.as(:if_body) >>
20
+ (
21
+ else_keyword >>
22
+ (
23
+ whitespace >> (if_keyword | unless_keyword).as(:operator) >>
24
+ whitespace >> if_modifier.as(:statement)
25
+ ).maybe >> code.as(:body)
26
+ ).repeat(1).as(:elses).maybe >> end_keyword
27
+ ).as(:if) | if_modifier
28
+ end
29
+
30
+ root(:if_rule)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ class Code
2
+ class Parser
3
+ class IfModifier < Parslet::Parser
4
+ rule(:or_keyword) { ::Code::Parser::OrKeyword.new }
5
+
6
+ rule(:if_keyword) { str("if") }
7
+ rule(:unless_keyword) { str("unless") }
8
+ rule(:while_keyword) { str("while") }
9
+ rule(:until_keyword) { str("until") }
10
+
11
+ rule(:operator) do
12
+ if_keyword | unless_keyword | while_keyword | until_keyword
13
+ end
14
+
15
+ rule(:space) { str(" ") }
16
+ rule(:whitespace) { space.repeat(1) }
17
+
18
+ rule(:if_modifier) do
19
+ (
20
+ or_keyword.as(:left) >> whitespace >> operator.as(:operator) >>
21
+ whitespace >> if_modifier.as(:right)
22
+ ).as(:if_modifier) | or_keyword
23
+ end
24
+
25
+ root(:if_modifier)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ class Code
2
+ class Parser
3
+ class List < Parslet::Parser
4
+ rule(:string) { ::Code::Parser::String.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+
7
+ rule(:opening_square_bracket) { str("[") }
8
+ rule(:closing_square_bracket) { str("]") }
9
+ rule(:comma) { str(",") }
10
+
11
+ rule(:space) { str(" ") }
12
+ rule(:newline) { str("\n") }
13
+ rule(:whitespace) { (space | newline).repeat(1) }
14
+ rule(:whitespace?) { whitespace.maybe }
15
+
16
+ rule(:list) do
17
+ (
18
+ opening_square_bracket.ignore >> whitespace?.ignore >>
19
+ code.as(:code).repeat(0, 1) >>
20
+ (whitespace? >> comma >> whitespace? >> code.as(:code)).repeat >>
21
+ whitespace?.ignore >> comma.maybe.ignore >> whitespace?.ignore >>
22
+ closing_square_bracket.ignore
23
+ ).as(:list) | string
24
+ end
25
+
26
+ root(:list)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ class Code
2
+ class Parser
3
+ class Multiplication < Parslet::Parser
4
+ rule(:unary_minus) { ::Code::Parser::UnaryMinus.new }
5
+
6
+ rule(:asterisk) { str("*") }
7
+ rule(:slash) { str("/") }
8
+ rule(:percent) { str("%") }
9
+
10
+ rule(:operator) { asterisk | slash | percent }
11
+
12
+ rule(:space) { str(" ") }
13
+ rule(:newline) { str("\n") }
14
+ rule(:whitespace) { (space | newline).repeat(1) }
15
+ rule(:whitespace?) { whitespace.maybe }
16
+
17
+ rule(:multiplication) do
18
+ (
19
+ unary_minus.as(:first) >>
20
+ (
21
+ whitespace? >> operator.as(:operator) >> whitespace? >>
22
+ unary_minus.as(:statement)
23
+ ).repeat(1).as(:rest)
24
+ ).as(:multiplication) | unary_minus
25
+ end
26
+
27
+ root(:multiplication)
28
+ end
29
+ end
30
+ end