template-ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +27 -54
  5. data/TODO +17 -0
  6. data/bin/code +56 -31
  7. data/bin/format +3 -0
  8. data/bin/template +62 -20
  9. data/bin/test +17 -0
  10. data/code-ruby.gemspec +1 -3
  11. data/docs/class.code +9 -0
  12. data/docs/euler/1.template +1 -5
  13. data/docs/euler/5.template +0 -1
  14. data/docs/meetup.code +12 -0
  15. data/docs/precedence.template +6 -39
  16. data/docs/rain.code +22 -0
  17. data/docs/slack.code +17 -0
  18. data/docs/stripe.code +7 -0
  19. data/docs/twitter.code +9 -0
  20. data/language-ruby.gemspec +18 -0
  21. data/lib/code/node/base_10.rb +29 -0
  22. data/lib/code/node/base_16.rb +13 -0
  23. data/lib/code/node/base_2.rb +13 -0
  24. data/lib/code/node/base_8.rb +13 -0
  25. data/lib/code/node/boolean.rb +7 -7
  26. data/lib/code/node/call.rb +32 -38
  27. data/lib/code/node/call_argument.rb +11 -27
  28. data/lib/code/node/chained_call.rb +10 -27
  29. data/lib/code/node/code.rb +4 -6
  30. data/lib/code/node/decimal.rb +26 -0
  31. data/lib/code/node/dictionnary.rb +20 -9
  32. data/lib/code/node/equal.rb +18 -20
  33. data/lib/code/node/function.rb +10 -7
  34. data/lib/code/node/function_parameter.rb +31 -0
  35. data/lib/code/node/if.rb +36 -32
  36. data/lib/code/node/if_modifier.rb +35 -36
  37. data/lib/code/node/list.rb +6 -8
  38. data/lib/code/node/negation.rb +5 -23
  39. data/lib/code/node/not.rb +15 -0
  40. data/lib/code/node/nothing.rb +1 -1
  41. data/lib/code/node/number.rb +14 -12
  42. data/lib/code/node/operation.rb +21 -16
  43. data/lib/code/node/power.rb +10 -6
  44. data/lib/code/node/rescue.rb +4 -3
  45. data/lib/code/node/splat.rb +15 -0
  46. data/lib/code/node/statement.rb +48 -70
  47. data/lib/code/node/string.rb +42 -16
  48. data/lib/code/node/ternary.rb +7 -9
  49. data/lib/code/node/unary_minus.rb +5 -12
  50. data/lib/code/node/while.rb +17 -24
  51. data/lib/code/node.rb +7 -8
  52. data/lib/code/object/argument.rb +2 -11
  53. data/lib/code/object/decimal.rb +45 -30
  54. data/lib/code/object/dictionnary.rb +6 -5
  55. data/lib/code/object/function.rb +20 -23
  56. data/lib/code/object/global.rb +11 -6
  57. data/lib/code/object/integer.rb +73 -30
  58. data/lib/code/object/list.rb +40 -34
  59. data/lib/code/object/range.rb +18 -17
  60. data/lib/code/object/ruby_function.rb +12 -6
  61. data/lib/code/object/string.rb +22 -12
  62. data/lib/code/object.rb +82 -24
  63. data/lib/code/parser/addition.rb +12 -20
  64. data/lib/code/parser/and_operator.rb +9 -20
  65. data/lib/code/parser/bitwise_and.rb +9 -20
  66. data/lib/code/parser/bitwise_or.rb +12 -20
  67. data/lib/code/parser/boolean.rb +10 -7
  68. data/lib/code/parser/call.rb +92 -60
  69. data/lib/code/parser/chained_call.rb +47 -0
  70. data/lib/code/parser/class.rb +45 -0
  71. data/lib/code/parser/code.rb +17 -10
  72. data/lib/code/parser/dictionnary.rb +56 -30
  73. data/lib/code/parser/equal.rb +87 -35
  74. data/lib/code/parser/equality.rb +23 -24
  75. data/lib/code/parser/equality_lower.rb +9 -0
  76. data/lib/code/parser/function.rb +67 -42
  77. data/lib/code/parser/greater.rb +25 -0
  78. data/lib/code/parser/group.rb +13 -8
  79. data/lib/code/parser/if.rb +51 -21
  80. data/lib/code/parser/if_modifier.rb +43 -16
  81. data/lib/code/parser/list.rb +32 -19
  82. data/lib/code/parser/multiplication.rb +15 -20
  83. data/lib/code/parser/name.rb +96 -84
  84. data/lib/code/parser/negation.rb +20 -9
  85. data/lib/code/parser/not_keyword.rb +14 -12
  86. data/lib/code/parser/nothing.rb +13 -8
  87. data/lib/code/parser/number.rb +124 -68
  88. data/lib/code/parser/operation.rb +35 -0
  89. data/lib/code/parser/or_keyword.rb +12 -20
  90. data/lib/code/parser/or_operator.rb +9 -20
  91. data/lib/code/parser/power.rb +32 -14
  92. data/lib/code/parser/range.rb +9 -17
  93. data/lib/code/parser/rescue.rb +29 -13
  94. data/lib/code/parser/shift.rb +11 -21
  95. data/lib/code/parser/splat.rb +31 -0
  96. data/lib/code/parser/statement.rb +4 -3
  97. data/lib/code/parser/string.rb +53 -62
  98. data/lib/code/parser/ternary.rb +36 -15
  99. data/lib/code/parser/unary_minus.rb +23 -5
  100. data/lib/code/parser/while.rb +26 -15
  101. data/lib/code/parser/whitespace.rb +49 -0
  102. data/lib/code/parser.rb +15 -0
  103. data/lib/code/ruby.rb +13 -12
  104. data/lib/code-ruby.rb +2 -11
  105. data/lib/code.rb +16 -13
  106. data/lib/language/atom.rb +343 -0
  107. data/lib/language/output.rb +130 -0
  108. data/lib/language/parser/absent/present.rb +8 -0
  109. data/lib/language/parser/absent.rb +6 -0
  110. data/lib/language/parser/end_of_input.rb +6 -0
  111. data/lib/language/parser/interuption.rb +38 -0
  112. data/lib/language/parser/not_end_of_input.rb +6 -0
  113. data/lib/language/parser/str/not_found.rb +16 -0
  114. data/lib/language/parser/str.rb +6 -0
  115. data/lib/language/parser.rb +53 -0
  116. data/lib/language-ruby.rb +10 -0
  117. data/lib/language.rb +80 -0
  118. data/lib/template/node/code_part.rb +1 -1
  119. data/lib/template/node/part.rb +1 -1
  120. data/lib/template/node/template.rb +1 -1
  121. data/lib/template/node/text_part.rb +1 -1
  122. data/lib/template/node.rb +1 -1
  123. data/lib/template/parser/template.rb +26 -17
  124. data/lib/template/parser.rb +15 -0
  125. data/lib/template/version.rb +1 -1
  126. data/lib/template-ruby.rb +2 -11
  127. data/lib/template.rb +6 -11
  128. data/spec/code/addition_spec.rb +13 -0
  129. data/spec/code/and_operator_spec.rb +13 -0
  130. data/spec/code/bitwise_and_spec.rb +13 -0
  131. data/spec/code/bitwise_or_spec.rb +13 -0
  132. data/spec/code/boolean_spec.rb +13 -0
  133. data/spec/code/call_spec.rb +21 -0
  134. data/spec/code/chained_call_spec.rb +16 -0
  135. data/spec/code/dictionnary_spec.rb +17 -0
  136. data/spec/code/equal_spec.rb +26 -0
  137. data/spec/code/equality_spec.rb +13 -0
  138. data/spec/code/function_spec.rb +18 -0
  139. data/spec/code/greater_spec.rb +18 -0
  140. data/spec/code/group_spec.rb +12 -0
  141. data/spec/code/if_modifier_spec.rb +20 -0
  142. data/spec/code/if_spec.rb +25 -0
  143. data/spec/code/list_spec.rb +17 -0
  144. data/spec/code/multiplication_spec.rb +18 -0
  145. data/spec/code/negation_spec.rb +20 -0
  146. data/spec/code/not_keyword_spec.rb +13 -0
  147. data/spec/code/nothing_spec.rb +17 -0
  148. data/spec/code/number_spec.rb +22 -0
  149. data/spec/code/or_keyword_spec.rb +17 -0
  150. data/spec/code/or_operator_spec.rb +16 -0
  151. data/spec/code/parser/boolean_spec.rb +5 -7
  152. data/spec/code/parser/call_spec.rb +16 -56
  153. data/spec/code/parser/chained_call.rb +17 -0
  154. data/spec/code/parser/dictionnary_spec.rb +8 -9
  155. data/spec/code/parser/function_spec.rb +5 -21
  156. data/spec/code/parser/group_spec.rb +18 -0
  157. data/spec/code/parser/list_spec.rb +9 -20
  158. data/spec/code/parser/number_spec.rb +4 -109
  159. data/spec/code/parser/string_spec.rb +9 -17
  160. data/spec/code/parser_spec.rb +23 -0
  161. data/spec/code/power_spec.rb +13 -0
  162. data/spec/code/range_spec.rb +16 -0
  163. data/spec/code/rescue_spec.rb +13 -0
  164. data/spec/code/shift_spec.rb +13 -0
  165. data/spec/code/splat_spec.rb +13 -0
  166. data/spec/code/string_spec.rb +25 -0
  167. data/spec/code/ternary_spec.rb +18 -0
  168. data/spec/code/unary_minus_spec.rb +13 -0
  169. data/spec/code/while_spec.rb +18 -0
  170. data/spec/spec_helper.rb +4 -1
  171. data/template-ruby.gemspec +2 -4
  172. metadata +112 -72
  173. data/lib/code/node/base_10_decimal.rb +0 -32
  174. data/lib/code/node/base_10_integer.rb +0 -32
  175. data/lib/code/node/base_10_number.rb +0 -19
  176. data/lib/code/node/base_16_number.rb +0 -19
  177. data/lib/code/node/base_2_number.rb +0 -19
  178. data/lib/code/node/base_8_number.rb +0 -19
  179. data/lib/code/node/block.rb +0 -17
  180. data/lib/code/node/defined.rb +0 -19
  181. data/lib/code/node/dictionnary_key_value.rb +0 -23
  182. data/lib/code/node/function_argument.rb +0 -45
  183. data/lib/code/node/group.rb +0 -13
  184. data/lib/code/node/keyword_call_argument.rb +0 -30
  185. data/lib/code/node/keyword_function_argument.rb +0 -33
  186. data/lib/code/node/name.rb +0 -28
  187. data/lib/code/node/not_keyword.rb +0 -13
  188. data/lib/code/node/or_keyword.rb +0 -34
  189. data/lib/code/node/range.rb +0 -31
  190. data/lib/code/node/regular_call_argument.rb +0 -34
  191. data/lib/code/node/regular_function_argument.rb +0 -36
  192. data/lib/code/node/string_characters.rb +0 -13
  193. data/lib/code/node/string_component.rb +0 -23
  194. data/lib/code/node/string_interpolation.rb +0 -13
  195. data/lib/code/parser/defined.rb +0 -20
  196. data/lib/code/parser/greater_than.rb +0 -33
  197. data/spec/call_spec.rb +0 -22
  198. data/spec/code/error/type_error_spec.rb +0 -63
  199. data/spec/code/parser/name_spec.rb +0 -15
  200. data/spec/code/parser/nothing_spec.rb +0 -19
  201. data/spec/code_spec.rb +0 -182
  202. data/spec/function_spec.rb +0 -26
  203. data/spec/template/parser/template_spec.rb +0 -19
  204. data/spec/template_spec.rb +0 -52
@@ -1,41 +1,67 @@
1
1
  class Code
2
2
  class Parser
3
- class Dictionnary < Parslet::Parser
4
- rule(:list) { ::Code::Parser::List.new }
5
- rule(:code) { ::Code::Parser::Code.new.present }
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)) |
3
+ class Dictionnary < Language
4
+ def code
5
+ ::Code::Parser::Code.new.present
6
+ end
7
+
8
+ def statement
9
+ ::Code::Parser::Statement
10
+ end
11
+
12
+ def name
13
+ ::Code::Parser::Name
14
+ end
15
+
16
+ def whitespace
17
+ ::Code::Parser::Whitespace
18
+ end
19
+
20
+ def whitespace?
21
+ whitespace.maybe
22
+ end
23
+
24
+ def opening_curly_bracket
25
+ str("{")
26
+ end
27
+
28
+ def closing_curly_bracket
29
+ str("}")
30
+ end
31
+
32
+ def comma
33
+ str(",")
34
+ end
35
+
36
+ def colon
37
+ str(":")
38
+ end
39
+
40
+ def equal
41
+ str("=")
42
+ end
43
+
44
+ def greater
45
+ str(">")
46
+ end
47
+
48
+ def key_value
49
+ (name.aka(:name) << colon << code.aka(:value)) |
50
+ (statement.aka(:statement) << colon << code.aka(:value)) |
22
51
  (
23
- code.as(:key) >> whitespace? >> equal >> right_caret >>
24
- whitespace? >> code.as(:value)
52
+ statement.aka(:statement) << whitespace? << equal << greater <<
53
+ code.aka(:value)
25
54
  )
26
55
  end
27
56
 
28
- rule(:dictionnary) do
57
+ def root
29
58
  (
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
59
+ opening_curly_bracket.ignore << whitespace? <<
60
+ key_value.repeat(0, 1) <<
61
+ (whitespace? << comma << whitespace? << key_value).repeat <<
62
+ (whitespace? << closing_curly_bracket.ignore).maybe
63
+ ).aka(:dictionnary) | ::Code::Parser::List
36
64
  end
37
-
38
- root(:dictionnary)
39
65
  end
40
66
  end
41
67
  end
@@ -1,42 +1,94 @@
1
1
  class Code
2
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
3
+ class Equal < Language
4
+ def statement
5
+ ::Code::Parser::Rescue
6
+ end
7
+
8
+ def name
9
+ ::Code::Parser::Name
10
+ end
11
+
12
+ def equal_class
13
+ ::Code::Parser::Equal
14
+ end
15
+
16
+ def whitespace
17
+ ::Code::Parser::Whitespace
18
+ end
19
+
20
+ def whitespace?
21
+ whitespace.maybe
22
+ end
23
+
24
+ def equal
25
+ str("=")
26
+ end
27
+
28
+ def plus
29
+ str("+")
30
+ end
31
+
32
+ def minus
33
+ str("-")
37
34
  end
38
35
 
39
- root(:equal_rule)
36
+ def asterisk
37
+ str("*")
38
+ end
39
+
40
+ def slash
41
+ str("/")
42
+ end
43
+
44
+ def percent
45
+ str("%")
46
+ end
47
+
48
+ def greater
49
+ str(">")
50
+ end
51
+
52
+ def lesser
53
+ str("<")
54
+ end
55
+
56
+ def ampersand
57
+ str("&")
58
+ end
59
+
60
+ def pipe
61
+ str("|")
62
+ end
63
+
64
+ def caret
65
+ str("^")
66
+ end
67
+
68
+ def dot
69
+ str(".")
70
+ end
71
+
72
+ def operator
73
+ equal.ignore | (plus << equal.ignore) | (minus << equal.ignore) |
74
+ (asterisk << equal.ignore) | (slash << equal.ignore) |
75
+ (percent << equal.ignore) | (greater << greater << equal.ignore) |
76
+ (lesser << lesser << equal.ignore) | (ampersand << equal.ignore) |
77
+ (pipe << equal.ignore) | (caret << equal.ignore) |
78
+ (pipe << pipe << equal.ignore) |
79
+ (ampersand << ampersand << equal.ignore)
80
+ end
81
+
82
+ def names
83
+ name << (dot << name).repeat
84
+ end
85
+
86
+ def root
87
+ (
88
+ names.aka(:left) << whitespace? << operator.aka(:operator) <<
89
+ whitespace? << equal_class.aka(:right)
90
+ ).aka(:equal) | statement
91
+ end
40
92
  end
41
93
  end
42
94
  end
@@ -1,36 +1,35 @@
1
1
  class Code
2
2
  class Parser
3
- class Equality < Parslet::Parser
4
- rule(:while_parser) { ::Code::Parser::While.new }
3
+ class Equality < Operation
4
+ def statement
5
+ ::Code::Parser::Splat
6
+ end
7
+
8
+ def equal
9
+ str("=")
10
+ end
5
11
 
6
- rule(:right_caret) { str(">") }
7
- rule(:left_caret) { str("<") }
8
- rule(:equal) { str("=") }
9
- rule(:exclamation_point) { str("!") }
10
- rule(:tilde) { str("~") }
12
+ def greater
13
+ str(">")
14
+ end
11
15
 
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
+ def lesser
17
+ str("<")
16
18
  end
17
19
 
18
- rule(:space) { str(" ") }
19
- rule(:newline) { str("\n") }
20
- rule(:whitespace) { (space | newline).repeat(1) }
21
- rule(:whitespace?) { whitespace.maybe }
20
+ def exclamation_point
21
+ str("!")
22
+ end
22
23
 
23
- rule(:equality) do
24
- (
25
- while_parser.as(:first) >>
26
- (
27
- whitespace? >> operator.as(:operator) >> whitespace? >>
28
- while_parser.as(:statement)
29
- ).repeat(1).as(:rest)
30
- ).as(:equality) | while_parser
24
+ def tilde
25
+ str("~")
31
26
  end
32
27
 
33
- root(:equality)
28
+ def operator
29
+ (equal << equal << equal) | (equal << equal) |
30
+ (lesser << equal << greater) | (exclamation_point << equal) |
31
+ (equal << tilde) | (tilde << equal) | (exclamation_point << tilde)
32
+ end
34
33
  end
35
34
  end
36
35
  end
@@ -0,0 +1,9 @@
1
+ class Code
2
+ class Parser
3
+ class EqualityLower < Equality
4
+ def statement
5
+ ::Code::Parser::Greater
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,60 +1,85 @@
1
1
  class Code
2
2
  class Parser
3
- class Function < Parslet::Parser
4
- rule(:call) { ::Code::Parser::Call.new }
5
- rule(:code_present) { ::Code::Parser::Code.new.present }
6
- rule(:code) { ::Code::Parser::Code.new }
7
- rule(:name) { ::Code::Parser::Name.new }
3
+ class Function < Language
4
+ def name
5
+ ::Code::Parser::Name
6
+ end
7
+
8
+ def code
9
+ ::Code::Parser::Code
10
+ end
8
11
 
9
- rule(:opening_parenthesis) { str("(") }
10
- rule(:closing_parenthesis) { str(")") }
11
- rule(:equal) { str("=") }
12
- rule(:right_caret) { str(">") }
13
- rule(:opening_curly_bracket) { str("{") }
14
- rule(:closing_curly_bracket) { str("}") }
15
- rule(:comma) { str(",") }
16
- rule(:colon) { str(":") }
17
- rule(:asterisk) { str("*") }
18
- rule(:ampersand) { str("&") }
12
+ def code_present
13
+ ::Code::Parser::Code.new.present
14
+ end
19
15
 
20
- rule(:space) { str(" ") }
21
- rule(:newline) { str("\n") }
22
- rule(:whitespace) { (space | newline).repeat(1) }
23
- rule(:whitespace?) { whitespace.maybe }
16
+ def whitespace
17
+ ::Code::Parser::Whitespace
18
+ end
24
19
 
25
- rule(:keyword_argument) do
26
- name >> whitespace? >> colon >> code_present.as(:default).maybe
20
+ def whitespace?
21
+ whitespace.maybe
27
22
  end
28
23
 
29
- rule(:regular_argument) do
30
- ampersand.as(:block).maybe >>
31
- (asterisk >> asterisk).as(:keyword_splat).maybe >>
32
- asterisk.as(:splat).maybe >> name >>
33
- (
34
- whitespace? >> equal >> whitespace? >> code_present.as(:default)
35
- ).maybe
24
+ def opening_parenthesis
25
+ str("(")
36
26
  end
37
27
 
38
- rule(:argument) do
39
- keyword_argument.as(:keyword) | regular_argument.as(:regular)
28
+ def closing_parenthesis
29
+ str(")")
40
30
  end
41
31
 
42
- rule(:arguments) do
43
- argument.repeat(1, 1) >>
44
- (whitespace? >> comma >> whitespace? >> argument).repeat
32
+ def colon
33
+ str(":")
45
34
  end
46
35
 
47
- rule(:function) do
48
- (
49
- opening_parenthesis >> whitespace? >>
50
- arguments.as(:arguments).maybe >> whitespace? >>
51
- closing_parenthesis >> whitespace? >> equal >> right_caret >>
52
- whitespace? >> opening_curly_bracket >> code.as(:body) >>
53
- closing_curly_bracket
54
- ).as(:function) | call
36
+ def comma
37
+ str(",")
38
+ end
39
+
40
+ def equal
41
+ str("=")
42
+ end
43
+
44
+ def greater
45
+ str(">")
46
+ end
47
+
48
+ def opening_curly_bracket
49
+ str("{")
55
50
  end
56
51
 
57
- root(:function)
52
+ def closing_curly_bracket
53
+ str("}")
54
+ end
55
+
56
+ def keyword_parameter
57
+ name.aka(:name) << whitespace? << colon.aka(:keyword) <<
58
+ code_present.aka(:default).maybe
59
+ end
60
+
61
+ def regular_parameter
62
+ name.aka(:name) << whitespace? <<
63
+ (equal << whitespace? << code_present.aka(:default)).maybe
64
+ end
65
+
66
+ def parameter
67
+ keyword_parameter | regular_parameter
68
+ end
69
+
70
+ def parameters
71
+ opening_parenthesis.ignore << whitespace? << parameter.repeat(0, 1) <<
72
+ (whitespace? << comma << whitespace? << parameter).repeat <<
73
+ whitespace? << closing_parenthesis.ignore.maybe
74
+ end
75
+
76
+ def root
77
+ (
78
+ parameters.aka(:parameters) << whitespace? << equal << greater <<
79
+ whitespace? << opening_curly_bracket << code.aka(:body) <<
80
+ closing_curly_bracket.maybe
81
+ ).aka(:function) | ::Code::Parser::ChainedCall
82
+ end
58
83
  end
59
84
  end
60
85
  end
@@ -0,0 +1,25 @@
1
+ class Code
2
+ class Parser
3
+ class Greater < Operation
4
+ def statement
5
+ ::Code::Parser::BitwiseOr
6
+ end
7
+
8
+ def greater
9
+ str(">")
10
+ end
11
+
12
+ def lesser
13
+ str("<")
14
+ end
15
+
16
+ def equal
17
+ str("=")
18
+ end
19
+
20
+ def operator
21
+ (greater << equal) | (lesser << equal) | greater | lesser
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,17 +1,22 @@
1
1
  class Code
2
2
  class Parser
3
- class Group < Parslet::Parser
4
- rule(:name) { ::Code::Parser::Name.new }
5
- rule(:code) { ::Code::Parser::Code.new.present }
3
+ class Group < Language
4
+ def code
5
+ ::Code::Parser::Code
6
+ end
6
7
 
7
- rule(:opening_parenthesis) { str("(") }
8
- rule(:closing_parenthesis) { str(")") }
8
+ def opening_parenthesis
9
+ str("(")
10
+ end
9
11
 
10
- rule(:group) do
11
- (opening_parenthesis >> code >> closing_parenthesis).as(:group) | name
12
+ def closing_parenthesis
13
+ str(")")
12
14
  end
13
15
 
14
- root(:group)
16
+ def root
17
+ (opening_parenthesis << code << closing_parenthesis.maybe).aka(:group) |
18
+ ::Code::Parser::Call
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -1,33 +1,63 @@
1
1
  class Code
2
2
  class Parser
3
- class If < Parslet::Parser
4
- rule(:if_modifier) { ::Code::Parser::IfModifier.new }
5
- rule(:code) { ::Code::Parser::Code.new.present }
3
+ class If < Language
4
+ def statement
5
+ ::Code::Parser::IfModifier
6
+ end
7
+
8
+ def if_class
9
+ ::Code::Parser::If
10
+ end
11
+
12
+ def whitespace
13
+ ::Code::Parser::Whitespace
14
+ end
15
+
16
+ def code
17
+ ::Code::Parser::Code
18
+ end
19
+
20
+ def whitespace?
21
+ whitespace.maybe
22
+ end
6
23
 
7
- rule(:if_keyword) { str("if") }
8
- rule(:else_keyword) { str("else") }
9
- rule(:unless_keyword) { str("unless") }
10
- rule(:end_keyword) { str("end") }
24
+ def if_keyword
25
+ str("if")
26
+ end
11
27
 
12
- rule(:space) { str(" ") }
13
- rule(:newline) { str("\n") }
14
- rule(:whitespace) { (space | newline).repeat(1) }
28
+ def unless_keyword
29
+ str("unless")
30
+ end
15
31
 
16
- rule(:if_rule) do
32
+ def elsif_keyword
33
+ str("elsif")
34
+ end
35
+
36
+ def else_keyword
37
+ str("else")
38
+ end
39
+
40
+ def end_keyword
41
+ str("end")
42
+ end
43
+
44
+ def root
17
45
  (
18
- (if_keyword | unless_keyword).as(:if_operator) >> whitespace >>
19
- if_modifier.as(:if_statement) >> code.as(:if_body).maybe >>
46
+ (if_keyword | unless_keyword).aka(:first_operator) << whitespace <<
47
+ statement.aka(:first_statement) << code.aka(:first_body) <<
20
48
  (
21
- else_keyword >>
49
+ (
50
+ elsif_keyword.aka(:operator) << whitespace <<
51
+ statement.aka(:statement) << code.aka(:body)
52
+ ) |
22
53
  (
23
- whitespace >> (if_keyword | unless_keyword).as(:operator) >>
24
- whitespace >> if_modifier.as(:statement)
25
- ).maybe >> code.as(:body).maybe
26
- ).repeat(1).as(:elses).maybe >> end_keyword
27
- ).as(:if) | if_modifier
54
+ else_keyword << whitespace <<
55
+ (if_keyword | unless_keyword).aka(:operator) <<
56
+ whitespace << statement.aka(:statement) << code.aka(:body)
57
+ ) | (else_keyword.aka(:operator) << code.aka(:body))
58
+ ).repeat(1).aka(:elses).maybe << end_keyword.maybe
59
+ ).aka(:if) | statement
28
60
  end
29
-
30
- root(:if_rule)
31
61
  end
32
62
  end
33
63
  end
@@ -1,28 +1,55 @@
1
1
  class Code
2
2
  class Parser
3
- class IfModifier < Parslet::Parser
4
- rule(:or_keyword) { ::Code::Parser::OrKeyword.new }
3
+ class IfModifier < Language
4
+ def statement
5
+ ::Code::Parser::OrKeyword
6
+ end
5
7
 
6
- rule(:if_keyword) { str("if") }
7
- rule(:unless_keyword) { str("unless") }
8
- rule(:while_keyword) { str("while") }
9
- rule(:until_keyword) { str("until") }
8
+ def if_modifier
9
+ ::Code::Parser::IfModifier
10
+ end
10
11
 
11
- rule(:operator) do
12
- if_keyword | unless_keyword | while_keyword | until_keyword
12
+ def whitespace
13
+ ::Code::Parser::Whitespace.new.without_newline
14
+ end
15
+
16
+ def whitespace?
17
+ whitespace.maybe
13
18
  end
14
19
 
15
- rule(:space) { str(" ") }
16
- rule(:whitespace) { space.repeat(1) }
20
+ def if_keyword
21
+ str("if")
22
+ end
17
23
 
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
24
+ def unless_keyword
25
+ str("unless")
26
+ end
27
+
28
+ def while_keyword
29
+ str("while")
30
+ end
31
+
32
+ def until_keyword
33
+ str("until")
34
+ end
35
+
36
+ def operator
37
+ if_keyword | unless_keyword | while_keyword | until_keyword
23
38
  end
24
39
 
25
- root(:if_modifier)
40
+ def root
41
+ (
42
+ statement.aka(:left) <<
43
+ (
44
+ whitespace? << operator.aka(:operator) << whitespace? <<
45
+ if_modifier.aka(:right)
46
+ ).maybe
47
+ )
48
+ .aka(:if_modifier)
49
+ .then do |output|
50
+ output[:if_modifier][:right] ? output : output[:if_modifier][:left]
51
+ end
52
+ end
26
53
  end
27
54
  end
28
55
  end