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,89 @@
1
+ class Code
2
+ class Parser
3
+ class Name < Parslet::Parser
4
+ rule(:space) { str(" ") }
5
+ rule(:newline) { str("\n") }
6
+ rule(:comma) { str(",") }
7
+ rule(:colon) { str(":") }
8
+ rule(:dot) { str(".") }
9
+ rule(:single_quote) { str("'") }
10
+ rule(:double_quote) { str('"') }
11
+ rule(:opening_curly_bracket) { str("{") }
12
+ rule(:closing_curly_bracket) { str("}") }
13
+ rule(:opening_square_bracket) { str("[") }
14
+ rule(:closing_square_bracket) { str("]") }
15
+ rule(:opening_parenthesis) { str("(") }
16
+ rule(:closing_parenthesis) { str(")") }
17
+ rule(:equal) { str("=") }
18
+ rule(:left_caret) { str("<") }
19
+ rule(:right_caret) { str(">") }
20
+ rule(:tilde) { str("~") }
21
+ rule(:pipe) { str("|") }
22
+ rule(:ampersand) { str("&") }
23
+ rule(:asterisk) { str("*") }
24
+ rule(:slash) { str("/") }
25
+ rule(:antislash) { str("\\") }
26
+ rule(:percent) { str("%") }
27
+ rule(:plus) { str("+") }
28
+ rule(:minus) { str("-") }
29
+ rule(:equal) { str("=") }
30
+
31
+ rule(:exclamation_point) { str("!") }
32
+ rule(:question_mark) { str("?") }
33
+
34
+ rule(:rescue_keyword) { str("rescue") }
35
+ rule(:defined_keyword) { str("defined?") }
36
+ rule(:not_keyword) { str("not") }
37
+ rule(:or_keyword) { str("or") }
38
+ rule(:and_keyword) { str("and") }
39
+ rule(:if_keyword) { str("if") }
40
+ rule(:else_keyword) { str("else") }
41
+ rule(:unless_keyword) { str("unless") }
42
+ rule(:until_keyword) { str("until") }
43
+ rule(:while_keyword) { str("while") }
44
+ rule(:end_keyword) { str("end") }
45
+
46
+ rule(:zero) { str("0") }
47
+ rule(:one) { str("1") }
48
+ rule(:two) { str("2") }
49
+ rule(:three) { str("3") }
50
+ rule(:four) { str("4") }
51
+ rule(:five) { str("5") }
52
+ rule(:six) { str("6") }
53
+ rule(:seven) { str("7") }
54
+ rule(:eight) { str("8") }
55
+ rule(:nine) { str("9") }
56
+
57
+ rule(:digit) do
58
+ zero | one | two | three | four | five | six | seven | eight | nine
59
+ end
60
+
61
+ rule(:name_character) do
62
+ opening_parenthesis.absent? >> closing_parenthesis.absent? >>
63
+ exclamation_point.absent? >> question_mark.absent? >> tilde.absent? >>
64
+ pipe.absent? >> ampersand.absent? >> asterisk.absent? >>
65
+ slash.absent? >> antislash.absent? >> percent.absent? >>
66
+ plus.absent? >> minus.absent? >> equal.absent? >> space.absent? >>
67
+ newline.absent? >> comma.absent? >> colon.absent? >> dot.absent? >>
68
+ single_quote.absent? >> double_quote.absent? >>
69
+ opening_curly_bracket.absent? >> closing_curly_bracket.absent? >>
70
+ opening_square_bracket.absent? >> closing_square_bracket.absent? >>
71
+ equal.absent? >> left_caret.absent? >> right_caret.absent? >> any
72
+ end
73
+
74
+ rule(:name) do
75
+ rescue_keyword.absent? >> defined_keyword.absent? >>
76
+ not_keyword.absent? >> or_keyword.absent? >> and_keyword.absent? >>
77
+ if_keyword.absent? >> else_keyword.absent? >>
78
+ unless_keyword.absent? >> until_keyword.absent? >>
79
+ while_keyword.absent? >> digit.absent? >> end_keyword.absent? >>
80
+ name_character >> name_character.repeat >> question_mark.maybe >>
81
+ exclamation_point.maybe
82
+ end
83
+
84
+ rule(:name_rule) { name.as(:name) }
85
+
86
+ root(:name_rule)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Parser
3
+ class Negation < Parslet::Parser
4
+ rule(:function) { ::Code::Parser::Function.new }
5
+
6
+ rule(:exclamation_point) { str("!") }
7
+ rule(:plus) { str("+") }
8
+
9
+ rule(:operator) { exclamation_point | plus }
10
+
11
+ rule(:negation) do
12
+ (operator.as(:operator) >> negation.as(:statement)).as(:negation) |
13
+ function
14
+ end
15
+
16
+ root(:negation)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ class Code
2
+ class Parser
3
+ class NotKeyword < Parslet::Parser
4
+ rule(:defined) { ::Code::Parser::Defined.new }
5
+
6
+ rule(:not_keyword) { str("not") }
7
+
8
+ rule(:operator) { not_keyword }
9
+
10
+ rule(:space) { str(" ") }
11
+ rule(:newline) { str("\n") }
12
+ rule(:whitespace) { (space | newline).repeat(1) }
13
+
14
+ rule(:not_rule) do
15
+ (not_keyword >> whitespace >> not_rule).as(:not_keyword) | defined
16
+ end
17
+
18
+ root(:not_rule)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ class Code
2
+ class Parser
3
+ class Nothing < Parslet::Parser
4
+ rule(:group) { ::Code::Parser::Group.new }
5
+
6
+ rule(:nothing_keyword) { str("nothing") }
7
+ rule(:null_keyword) { str("null") }
8
+ rule(:nil_keyword) { str("nil") }
9
+
10
+ rule(:nothing) do
11
+ (nothing_keyword | null_keyword | nil_keyword).as(:nothing) | group
12
+ end
13
+
14
+ root(:nothing)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,98 @@
1
+ class Code
2
+ class Parser
3
+ class Number < Parslet::Parser
4
+ rule(:boolean) { ::Code::Parser::Boolean.new }
5
+
6
+ rule(:dot) { str(".") }
7
+ rule(:plus) { str("+") }
8
+ rule(:minus) { str("-") }
9
+
10
+ rule(:zero) { str("0") }
11
+ rule(:one) { str("1") }
12
+ rule(:two) { str("2") }
13
+ rule(:three) { str("3") }
14
+ rule(:four) { str("4") }
15
+ rule(:five) { str("5") }
16
+ rule(:six) { str("6") }
17
+ rule(:seven) { str("7") }
18
+ rule(:eight) { str("8") }
19
+ rule(:nine) { str("9") }
20
+ rule(:a) { str("a") | str("A") }
21
+ rule(:b) { str("b") | str("B") }
22
+ rule(:b) { str("b") | str("B") }
23
+ rule(:c) { str("c") | str("C") }
24
+ rule(:d) { str("d") | str("D") }
25
+ rule(:e) { str("e") | str("E") }
26
+ rule(:f) { str("f") | str("F") }
27
+ rule(:o) { str("o") | str("O") }
28
+ rule(:x) { str("x") | str("X") }
29
+
30
+ # sign
31
+
32
+ rule(:sign) { plus | minus }
33
+
34
+ # base 2
35
+
36
+ rule(:base_2_digit) { zero | one }
37
+
38
+ rule(:base_2_number) { zero.ignore >> b.ignore >> base_2_digit.repeat(1) }
39
+
40
+ # base 8
41
+
42
+ rule(:base_8_digit) do
43
+ zero | one | two | three | four | five | six | seven
44
+ end
45
+
46
+ rule(:base_8_number) { zero.ignore >> o.ignore >> base_8_digit.repeat(1) }
47
+
48
+ # base 10
49
+
50
+ rule(:base_10_digit) do
51
+ zero | one | two | three | four | five | six | seven | eight | nine
52
+ end
53
+
54
+ rule(:base_10_whole) { base_10_digit.repeat(1) }
55
+
56
+ rule(:base_10_exponent) { e.ignore >> base_10_number }
57
+
58
+ rule(:base_10_integer) do
59
+ sign.as(:sign).maybe >> base_10_whole.as(:whole) >>
60
+ base_10_exponent.as(:exponent).maybe
61
+ end
62
+
63
+ rule(:base_10_decimal_decimal) { dot.ignore >> base_10_digit.repeat(1) }
64
+
65
+ rule(:base_10_decimal) do
66
+ sign.as(:sign).maybe >> base_10_whole.as(:whole) >>
67
+ base_10_decimal_decimal.as(:decimal) >>
68
+ base_10_exponent.as(:exponent).maybe
69
+ end
70
+
71
+ rule(:base_10_number) do
72
+ base_10_decimal.as(:decimal) | base_10_integer.as(:integer)
73
+ end
74
+
75
+ # base 16
76
+
77
+ rule(:base_16_digit) do
78
+ zero | one | two | three | four | five | six | seven | eight | nine |
79
+ a | b | c | d | e | f
80
+ end
81
+
82
+ rule(:base_16_number) do
83
+ zero.ignore >> x.ignore >> base_16_digit.repeat(1)
84
+ end
85
+
86
+ # number
87
+
88
+ rule(:number) do
89
+ (
90
+ base_2_number.as(:base_2) | base_8_number.as(:base_8) |
91
+ base_16_number.as(:base_16) | base_10_number.as(:base_10)
92
+ ).as(:number) | boolean
93
+ end
94
+
95
+ root(:number)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,29 @@
1
+ class Code
2
+ class Parser
3
+ class OrKeyword < Parslet::Parser
4
+ rule(:not_keyword) { ::Code::Parser::NotKeyword.new }
5
+
6
+ rule(:or_keyword) { str("or") }
7
+ rule(:and_keyword) { str("and") }
8
+
9
+ rule(:operator) { or_keyword | and_keyword }
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(:or_rule) do
17
+ (
18
+ not_keyword.as(:first) >>
19
+ (
20
+ whitespace? >> operator.as(:operator) >> whitespace? >>
21
+ not_keyword.as(:statement)
22
+ ).repeat(1).as(:rest)
23
+ ).as(:or_keyword) | not_keyword
24
+ end
25
+
26
+ root(:or_rule)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ class Code
2
+ class Parser
3
+ class OrOperator < Parslet::Parser
4
+ rule(:and_operator) { ::Code::Parser::AndOperator.new }
5
+
6
+ rule(:pipe) { str("|") }
7
+
8
+ rule(:operator) { pipe >> pipe }
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(:or_operator) do
16
+ (
17
+ and_operator.as(:first) >>
18
+ (
19
+ whitespace? >> operator.as(:operator) >> whitespace? >>
20
+ and_operator.as(:statement)
21
+ ).repeat(1).as(:rest)
22
+ ).as(:or_operator) | and_operator
23
+ end
24
+
25
+ root(:or_operator)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ class Code
2
+ class Parser
3
+ class Power < Parslet::Parser
4
+ rule(:negation) { ::Code::Parser::Negation.new }
5
+
6
+ rule(:asterisk) { str("*") }
7
+
8
+ rule(:operator) { asterisk >> asterisk }
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(:power) do
16
+ (
17
+ negation.as(:left) >> whitespace? >> operator >> whitespace? >>
18
+ power.as(:right)
19
+ ).as(:power) | negation
20
+ end
21
+
22
+ root(:power)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ class Code
2
+ class Parser
3
+ class Range < Parslet::Parser
4
+ rule(:or_operator) { ::Code::Parser::OrOperator.new }
5
+
6
+ rule(:dot) { str(".") }
7
+
8
+ rule(:operator) { dot >> dot >> dot | dot >> dot }
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(:range) do
16
+ (
17
+ or_operator.as(:left) >> whitespace? >> operator.as(:operator) >>
18
+ whitespace? >> range.as(:right)
19
+ ).as(:range) | or_operator
20
+ end
21
+
22
+ root(:range)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ class Code
2
+ class Parser
3
+ class Rescue < Parslet::Parser
4
+ rule(:ternary) { ::Code::Parser::Ternary.new }
5
+
6
+ rule(:rescue_keyword) { str("rescue") }
7
+
8
+ rule(:space) { str(" ") }
9
+ rule(:newline) { str("\n") }
10
+ rule(:whitespace) { (space | newline).repeat(1) }
11
+ rule(:whitespace?) { whitespace.maybe }
12
+
13
+ rule(:rescue) do
14
+ (
15
+ ternary.as(:left) >> whitespace? >> rescue_keyword >> whitespace? >>
16
+ ternary.as(:right)
17
+ ).as(:rescue) | ternary
18
+ end
19
+
20
+ root(:rescue)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ class Code
2
+ class Parser
3
+ class Shift < Parslet::Parser
4
+ rule(:addition) { ::Code::Parser::Addition.new }
5
+
6
+ rule(:right_caret) { str(">") }
7
+ rule(:left_caret) { str("<") }
8
+
9
+ rule(:operator) do
10
+ (left_caret >> left_caret) | (right_caret >> right_caret)
11
+ end
12
+
13
+ rule(:space) { str(" ") }
14
+ rule(:newline) { str("\n") }
15
+ rule(:whitespace) { (space | newline).repeat(1) }
16
+ rule(:whitespace?) { whitespace.maybe }
17
+
18
+ rule(:shift) do
19
+ (
20
+ addition.as(:first) >>
21
+ (
22
+ whitespace? >> operator.as(:operator) >> whitespace? >>
23
+ addition.as(:statement)
24
+ ).repeat(1).as(:rest)
25
+ ).as(:shift) | addition
26
+ end
27
+
28
+ root(:shift)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ class Code
2
+ class Parser
3
+ class Statement < Parslet::Parser
4
+ rule(:statement) { ::Code::Parser::While.new }
5
+ root(:statement)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,72 @@
1
+ class Code
2
+ class Parser
3
+ class String < Parslet::Parser
4
+ rule(:number) { ::Code::Parser::Number.new }
5
+ rule(:name) { ::Code::Parser::Name.new.name }
6
+
7
+ rule(:single_quote) { str("'") }
8
+ rule(:double_quote) { str('"') }
9
+ rule(:backslash) { str("\\") }
10
+ rule(:colon) { str(":") }
11
+
12
+ rule(:zero) { str("0") }
13
+ rule(:one) { str("1") }
14
+ rule(:two) { str("2") }
15
+ rule(:three) { str("3") }
16
+ rule(:four) { str("4") }
17
+ rule(:five) { str("5") }
18
+ rule(:six) { str("6") }
19
+ rule(:seven) { str("7") }
20
+ rule(:eight) { str("8") }
21
+ rule(:nine) { str("9") }
22
+ rule(:a) { str("a") | str("A") }
23
+ rule(:b) { str("b") | str("B") }
24
+ rule(:b) { str("b") | str("B") }
25
+ rule(:c) { str("c") | str("C") }
26
+ rule(:d) { str("d") | str("D") }
27
+ rule(:e) { str("e") | str("E") }
28
+ rule(:f) { str("f") | str("F") }
29
+ rule(:n) { str("n") | str("N") }
30
+ rule(:r) { str("r") | str("R") }
31
+ rule(:t) { str("t") | str("T") }
32
+ rule(:u) { str("u") | str("U") }
33
+
34
+ rule(:base_16_digit) do
35
+ zero | one | two | three | four | five | six | seven | eight | nine |
36
+ a | b | c | d | e | f
37
+ end
38
+
39
+ rule(:escaped_character) do
40
+ (backslash >> u >> base_16_digit.repeat(4, 4)) |
41
+ (backslash >> (b | f | n | r | t)) | (backslash.ignore >> any)
42
+ end
43
+
44
+ rule(:single_quoted_character) do
45
+ escaped_character | (single_quote.absent? >> any)
46
+ end
47
+
48
+ rule(:double_quoted_character) do
49
+ escaped_character | (double_quote.absent? >> any)
50
+ end
51
+
52
+ rule(:single_quoted_string) do
53
+ single_quote.ignore >> single_quoted_character.repeat >>
54
+ single_quote.ignore
55
+ end
56
+
57
+ rule(:double_quoted_string) do
58
+ double_quote.ignore >> double_quoted_character.repeat >>
59
+ double_quote.ignore
60
+ end
61
+
62
+ rule(:symbol) { colon.ignore >> name }
63
+
64
+ rule(:string) do
65
+ (single_quoted_string | double_quoted_string | symbol).as(:string) |
66
+ number
67
+ end
68
+
69
+ root(:string)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,25 @@
1
+ class Code
2
+ class Parser
3
+ class Ternary < Parslet::Parser
4
+ rule(:range) { ::Code::Parser::Range.new }
5
+
6
+ rule(:question_mark) { str("?") }
7
+ rule(:colon) { str(":") }
8
+
9
+ rule(:space) { str(" ") }
10
+ rule(:newline) { str("\n") }
11
+ rule(:whitespace) { (space | newline).repeat(1) }
12
+ rule(:whitespace?) { whitespace.maybe }
13
+
14
+ rule(:ternary) do
15
+ (
16
+ range.as(:left) >> whitespace >> question_mark >> whitespace? >>
17
+ ternary.as(:middle) >>
18
+ (whitespace? >> colon >> whitespace? >> ternary.as(:right)).maybe
19
+ ).as(:ternary) | range
20
+ end
21
+
22
+ root(:ternary)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ class Code
2
+ class Parser
3
+ class UnaryMinus < Parslet::Parser
4
+ rule(:power) { ::Code::Parser::Power.new }
5
+
6
+ rule(:minus) { str("-") }
7
+
8
+ rule(:unary_minus) { (minus >> unary_minus).as(:unary_minus) | power }
9
+
10
+ root(:unary_minus)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ class Code
2
+ class Parser
3
+ class While < Parslet::Parser
4
+ rule(:if_rule) { ::Code::Parser::If.new }
5
+ rule(:code) { ::Code::Parser::Code.new }
6
+
7
+ rule(:while_keyword) { str("while") }
8
+ rule(:until_keyword) { str("until") }
9
+ rule(:end_keyword) { str("end") }
10
+
11
+ rule(:space) { str(" ") }
12
+ rule(:newline) { str("\n") }
13
+ rule(:whitespace) { (space | newline).repeat(1) }
14
+
15
+ rule(:while_rule) do
16
+ (
17
+ (while_keyword | until_keyword).as(:operator) >> whitespace >>
18
+ if_rule.as(:statement) >> code.as(:body) >> end_keyword
19
+ ).as(:while) | if_rule
20
+ end
21
+
22
+ root(:while_rule)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ class Code
2
+ class Parser
3
+ end
4
+ end
data/lib/code-ruby.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "parslet"
2
+ require "zeitwerk"
3
+ require "bigdecimal"
4
+ require "active_support"
5
+ require "active_support/core_ext/object/blank"
6
+ require "stringio"
7
+ require "timeout"
8
+
9
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
10
+ loader.ignore(__FILE__)
11
+ loader.setup
data/lib/code.rb ADDED
@@ -0,0 +1,29 @@
1
+ class Code
2
+ def initialize(input, io: $stdout, timeout: 10)
3
+ @input = input
4
+ @parsed =
5
+ Timeout.timeout(timeout) { ::Code::Parser::Code.new.parse(@input) }
6
+ @io = io
7
+ @timeout = timeout
8
+ end
9
+
10
+ def self.evaluate(input, context = "", io: $stdout, timeout: 10)
11
+ new(input, io: io, timeout: timeout).evaluate(context)
12
+ end
13
+
14
+ def evaluate(context = "")
15
+ Timeout.timeout(@timeout) do
16
+ if context.present?
17
+ context = ::Code.evaluate(context, timeout: @timeout)
18
+ else
19
+ context = ::Code::Object::Dictionnary.new
20
+ end
21
+
22
+ ::Code::Node::Code.new(parsed).evaluate(context: context, io: @io)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :input, :parsed
29
+ end
@@ -0,0 +1,13 @@
1
+ class Template
2
+ class Node
3
+ class CodePart
4
+ def initialize(code)
5
+ @code = ::Code::Node::Code.new(code)
6
+ end
7
+
8
+ def evaluate(**args)
9
+ @code.evaluate(**args)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ class Template
2
+ class Node
3
+ class Part
4
+ def initialize(part)
5
+ if part.key?(:text)
6
+ @part = ::Template::Node::TextPart.new(part[:text])
7
+ elsif part.key?(:code)
8
+ @part = ::Template::Node::CodePart.new(part[:code])
9
+ else
10
+ raise NotImplementedError.new(part.inspect)
11
+ end
12
+ end
13
+
14
+ def evaluate(**args)
15
+ @part.evaluate(**args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ class Template
2
+ class Node
3
+ class Template
4
+ def initialize(parts)
5
+ @parts = parts.map { |part| ::Template::Node::Part.new(part) }
6
+ end
7
+
8
+ def evaluate(**args)
9
+ io = args.fetch(:io)
10
+
11
+ @parts.each { |part| io.print(part.evaluate(**args)) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class Template
2
+ class Node
3
+ class TextPart
4
+ def initialize(text)
5
+ @text = text
6
+ end
7
+
8
+ def evaluate(**args)
9
+ ::Code::Object::String.new(@text.to_s)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ class Template
2
+ class Node
3
+ end
4
+ end