code-ruby 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +28 -55
  6. data/TODO +17 -0
  7. data/bin/code +56 -31
  8. data/bin/format +3 -0
  9. data/bin/template +62 -20
  10. data/bin/test +17 -0
  11. data/code-ruby.gemspec +1 -6
  12. data/docs/class.code +9 -0
  13. data/docs/euler/1.template +1 -5
  14. data/docs/euler/5.template +0 -1
  15. data/docs/meetup.code +12 -0
  16. data/docs/precedence.template +6 -39
  17. data/docs/rain.code +22 -0
  18. data/docs/slack.code +17 -0
  19. data/docs/stripe.code +7 -0
  20. data/docs/twitter.code +9 -0
  21. data/language-ruby.gemspec +18 -0
  22. data/lib/code/error.rb +3 -0
  23. data/lib/code/node/base_10.rb +29 -0
  24. data/lib/code/node/base_16.rb +13 -0
  25. data/lib/code/node/base_2.rb +13 -0
  26. data/lib/code/node/base_8.rb +13 -0
  27. data/lib/code/node/boolean.rb +7 -7
  28. data/lib/code/node/call.rb +32 -37
  29. data/lib/code/node/call_argument.rb +11 -27
  30. data/lib/code/node/chained_call.rb +10 -25
  31. data/lib/code/node/code.rb +4 -6
  32. data/lib/code/node/decimal.rb +26 -0
  33. data/lib/code/node/dictionnary.rb +20 -9
  34. data/lib/code/node/equal.rb +18 -20
  35. data/lib/code/node/function.rb +10 -7
  36. data/lib/code/node/function_parameter.rb +31 -0
  37. data/lib/code/node/if.rb +36 -32
  38. data/lib/code/node/if_modifier.rb +35 -36
  39. data/lib/code/node/list.rb +6 -9
  40. data/lib/code/node/negation.rb +5 -23
  41. data/lib/code/node/not.rb +15 -0
  42. data/lib/code/node/nothing.rb +1 -1
  43. data/lib/code/node/number.rb +14 -12
  44. data/lib/code/node/operation.rb +21 -16
  45. data/lib/code/node/power.rb +10 -6
  46. data/lib/code/node/rescue.rb +4 -3
  47. data/lib/code/node/splat.rb +15 -0
  48. data/lib/code/node/statement.rb +47 -69
  49. data/lib/code/node/string.rb +41 -10
  50. data/lib/code/node/ternary.rb +7 -9
  51. data/lib/code/node/unary_minus.rb +5 -12
  52. data/lib/code/node/while.rb +17 -24
  53. data/lib/code/node.rb +7 -8
  54. data/lib/code/object/argument.rb +3 -12
  55. data/lib/code/object/decimal.rb +114 -27
  56. data/lib/code/object/dictionnary.rb +29 -12
  57. data/lib/code/object/function.rb +23 -24
  58. data/lib/code/object/global.rb +42 -0
  59. data/lib/code/object/integer.rb +142 -35
  60. data/lib/code/object/list.rb +74 -95
  61. data/lib/code/object/range.rb +38 -50
  62. data/lib/code/object/ruby_function.rb +31 -0
  63. data/lib/code/object/string.rb +36 -16
  64. data/lib/code/object.rb +114 -54
  65. data/lib/code/parser/addition.rb +12 -20
  66. data/lib/code/parser/and_operator.rb +9 -20
  67. data/lib/code/parser/bitwise_and.rb +9 -20
  68. data/lib/code/parser/bitwise_or.rb +12 -20
  69. data/lib/code/parser/boolean.rb +10 -7
  70. data/lib/code/parser/call.rb +92 -60
  71. data/lib/code/parser/chained_call.rb +47 -0
  72. data/lib/code/parser/class.rb +45 -0
  73. data/lib/code/parser/code.rb +17 -10
  74. data/lib/code/parser/dictionnary.rb +56 -30
  75. data/lib/code/parser/equal.rb +87 -35
  76. data/lib/code/parser/equality.rb +23 -24
  77. data/lib/code/parser/equality_lower.rb +9 -0
  78. data/lib/code/parser/function.rb +67 -40
  79. data/lib/code/parser/greater.rb +25 -0
  80. data/lib/code/parser/group.rb +13 -8
  81. data/lib/code/parser/if.rb +51 -21
  82. data/lib/code/parser/if_modifier.rb +43 -16
  83. data/lib/code/parser/list.rb +32 -19
  84. data/lib/code/parser/multiplication.rb +15 -20
  85. data/lib/code/parser/name.rb +96 -84
  86. data/lib/code/parser/negation.rb +20 -9
  87. data/lib/code/parser/not_keyword.rb +14 -12
  88. data/lib/code/parser/nothing.rb +13 -8
  89. data/lib/code/parser/number.rb +124 -68
  90. data/lib/code/parser/operation.rb +35 -0
  91. data/lib/code/parser/or_keyword.rb +12 -20
  92. data/lib/code/parser/or_operator.rb +9 -20
  93. data/lib/code/parser/power.rb +32 -14
  94. data/lib/code/parser/range.rb +9 -17
  95. data/lib/code/parser/rescue.rb +29 -13
  96. data/lib/code/parser/shift.rb +11 -21
  97. data/lib/code/parser/splat.rb +31 -0
  98. data/lib/code/parser/statement.rb +4 -3
  99. data/lib/code/parser/string.rb +53 -62
  100. data/lib/code/parser/ternary.rb +36 -15
  101. data/lib/code/parser/unary_minus.rb +23 -5
  102. data/lib/code/parser/while.rb +26 -15
  103. data/lib/code/parser/whitespace.rb +49 -0
  104. data/lib/code/parser.rb +15 -0
  105. data/lib/code/ruby.rb +162 -0
  106. data/lib/code-ruby.rb +2 -5
  107. data/lib/code.rb +24 -7
  108. data/lib/language/atom.rb +343 -0
  109. data/lib/language/output.rb +130 -0
  110. data/lib/language/parser/absent/present.rb +8 -0
  111. data/lib/language/parser/absent.rb +6 -0
  112. data/lib/language/parser/end_of_input.rb +6 -0
  113. data/lib/language/parser/interuption.rb +38 -0
  114. data/lib/language/parser/not_end_of_input.rb +6 -0
  115. data/lib/language/parser/str/not_found.rb +16 -0
  116. data/lib/language/parser/str.rb +6 -0
  117. data/lib/language/parser.rb +53 -0
  118. data/lib/language-ruby.rb +10 -0
  119. data/lib/language.rb +80 -0
  120. data/lib/template/node/code_part.rb +1 -1
  121. data/lib/template/node/part.rb +1 -1
  122. data/lib/template/node/template.rb +1 -1
  123. data/lib/template/node/text_part.rb +1 -1
  124. data/lib/template/node.rb +1 -1
  125. data/lib/template/parser/template.rb +26 -17
  126. data/lib/template/parser.rb +15 -0
  127. data/lib/template/version.rb +1 -1
  128. data/lib/template-ruby.rb +2 -5
  129. data/lib/template.rb +23 -13
  130. data/spec/code/addition_spec.rb +13 -0
  131. data/spec/code/and_operator_spec.rb +13 -0
  132. data/spec/code/bitwise_and_spec.rb +13 -0
  133. data/spec/code/bitwise_or_spec.rb +13 -0
  134. data/spec/code/boolean_spec.rb +13 -0
  135. data/spec/code/call_spec.rb +21 -0
  136. data/spec/code/chained_call_spec.rb +16 -0
  137. data/spec/code/dictionnary_spec.rb +17 -0
  138. data/spec/code/equal_spec.rb +26 -0
  139. data/spec/code/equality_spec.rb +13 -0
  140. data/spec/code/function_spec.rb +18 -0
  141. data/spec/code/greater_spec.rb +18 -0
  142. data/spec/code/group_spec.rb +12 -0
  143. data/spec/code/if_modifier_spec.rb +20 -0
  144. data/spec/code/if_spec.rb +25 -0
  145. data/spec/code/list_spec.rb +17 -0
  146. data/spec/code/multiplication_spec.rb +18 -0
  147. data/spec/code/negation_spec.rb +20 -0
  148. data/spec/code/not_keyword_spec.rb +13 -0
  149. data/spec/code/nothing_spec.rb +17 -0
  150. data/spec/code/number_spec.rb +22 -0
  151. data/spec/code/or_keyword_spec.rb +17 -0
  152. data/spec/code/or_operator_spec.rb +16 -0
  153. data/spec/code/parser/boolean_spec.rb +5 -7
  154. data/spec/code/parser/call_spec.rb +16 -56
  155. data/spec/code/parser/chained_call.rb +17 -0
  156. data/spec/code/parser/dictionnary_spec.rb +8 -9
  157. data/spec/code/parser/function_spec.rb +5 -21
  158. data/spec/code/parser/group_spec.rb +18 -0
  159. data/spec/code/parser/list_spec.rb +9 -20
  160. data/spec/code/parser/number_spec.rb +4 -109
  161. data/spec/code/parser/string_spec.rb +9 -17
  162. data/spec/code/parser_spec.rb +23 -0
  163. data/spec/code/power_spec.rb +13 -0
  164. data/spec/code/range_spec.rb +16 -0
  165. data/spec/code/rescue_spec.rb +13 -0
  166. data/spec/code/shift_spec.rb +13 -0
  167. data/spec/code/splat_spec.rb +13 -0
  168. data/spec/code/string_spec.rb +25 -0
  169. data/spec/code/ternary_spec.rb +18 -0
  170. data/spec/code/unary_minus_spec.rb +13 -0
  171. data/spec/code/while_spec.rb +18 -0
  172. data/spec/spec_helper.rb +4 -1
  173. data/template-ruby.gemspec +2 -7
  174. metadata +113 -99
  175. data/lib/code/node/base_10_decimal.rb +0 -32
  176. data/lib/code/node/base_10_integer.rb +0 -32
  177. data/lib/code/node/base_10_number.rb +0 -19
  178. data/lib/code/node/base_16_number.rb +0 -19
  179. data/lib/code/node/base_2_number.rb +0 -19
  180. data/lib/code/node/base_8_number.rb +0 -19
  181. data/lib/code/node/block.rb +0 -17
  182. data/lib/code/node/defined.rb +0 -19
  183. data/lib/code/node/dictionnary_key_value.rb +0 -23
  184. data/lib/code/node/function_argument.rb +0 -45
  185. data/lib/code/node/group.rb +0 -13
  186. data/lib/code/node/keyword_call_argument.rb +0 -30
  187. data/lib/code/node/keyword_function_argument.rb +0 -33
  188. data/lib/code/node/name.rb +0 -55
  189. data/lib/code/node/not_keyword.rb +0 -13
  190. data/lib/code/node/or_keyword.rb +0 -34
  191. data/lib/code/node/range.rb +0 -31
  192. data/lib/code/node/regular_call_argument.rb +0 -34
  193. data/lib/code/node/regular_function_argument.rb +0 -36
  194. data/lib/code/node/string_characters.rb +0 -13
  195. data/lib/code/node/string_component.rb +0 -23
  196. data/lib/code/node/string_interpolation.rb +0 -13
  197. data/lib/code/parser/defined.rb +0 -20
  198. data/lib/code/parser/greater_than.rb +0 -33
  199. data/spec/call_spec.rb +0 -22
  200. data/spec/code/error/type_error_spec.rb +0 -63
  201. data/spec/code/parser/name_spec.rb +0 -15
  202. data/spec/code/parser/nothing_spec.rb +0 -19
  203. data/spec/code_spec.rb +0 -120
  204. data/spec/function_spec.rb +0 -26
  205. data/spec/template/parser/template_spec.rb +0 -19
  206. data/spec/template_spec.rb +0 -33
@@ -1,29 +1,42 @@
1
1
  class Code
2
2
  class Parser
3
- class List < Parslet::Parser
4
- rule(:string) { ::Code::Parser::String.new }
5
- rule(:code) { ::Code::Parser::Code.new.present }
3
+ class List < Language
4
+ def code
5
+ ::Code::Parser::Code.new.present
6
+ end
7
+
8
+ def whitespace
9
+ ::Code::Parser::Whitespace
10
+ end
6
11
 
7
- rule(:opening_square_bracket) { str("[") }
8
- rule(:closing_square_bracket) { str("]") }
9
- rule(:comma) { str(",") }
12
+ def whitespace?
13
+ whitespace.maybe
14
+ end
10
15
 
11
- rule(:space) { str(" ") }
12
- rule(:newline) { str("\n") }
13
- rule(:whitespace) { (space | newline).repeat(1) }
14
- rule(:whitespace?) { whitespace.maybe }
16
+ def opening_square_bracket
17
+ str("[")
18
+ end
15
19
 
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
20
+ def closing_square_bracket
21
+ str("]")
24
22
  end
25
23
 
26
- root(:list)
24
+ def comma
25
+ str(",")
26
+ end
27
+
28
+ def element
29
+ code
30
+ end
31
+
32
+ def root
33
+ (
34
+ opening_square_bracket.ignore << whitespace? <<
35
+ element.repeat(0, 1) <<
36
+ (whitespace? << comma << whitespace? << element).repeat <<
37
+ (whitespace? << closing_square_bracket.ignore).maybe
38
+ ).aka(:list) | ::Code::Parser::String
39
+ end
27
40
  end
28
41
  end
29
42
  end
@@ -1,30 +1,25 @@
1
1
  class Code
2
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("%") }
3
+ class Multiplication < Operation
4
+ def statement
5
+ ::Code::Parser::UnaryMinus
6
+ end
9
7
 
10
- rule(:operator) { asterisk | slash | percent }
8
+ def asterisk
9
+ str("*")
10
+ end
11
11
 
12
- rule(:space) { str(" ") }
13
- rule(:newline) { str("\n") }
14
- rule(:whitespace) { (space | newline).repeat(1) }
15
- rule(:whitespace?) { whitespace.maybe }
12
+ def slash
13
+ str("/")
14
+ end
16
15
 
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
16
+ def percent
17
+ str("%")
25
18
  end
26
19
 
27
- root(:multiplication)
20
+ def operator
21
+ asterisk | slash | percent
22
+ end
28
23
  end
29
24
  end
30
25
  end
@@ -1,89 +1,101 @@
1
1
  class Code
2
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)
3
+ class Name < Language
4
+ def space
5
+ str(" ")
6
+ end
7
+
8
+ def newline
9
+ str("\n")
10
+ end
11
+
12
+ def comma
13
+ str(",")
14
+ end
15
+
16
+ def equal
17
+ str("=")
18
+ end
19
+
20
+ def colon
21
+ str(":")
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 opening_square_bracket
33
+ str("[")
34
+ end
35
+
36
+ def closing_square_bracket
37
+ str("]")
38
+ end
39
+
40
+ def opening_parenthesis
41
+ str("(")
42
+ end
43
+
44
+ def closing_parenthesis
45
+ str(")")
46
+ end
47
+
48
+ def single_quote
49
+ str("'")
50
+ end
51
+
52
+ def double_quote
53
+ str('"')
54
+ end
55
+
56
+ def dot
57
+ str(".")
58
+ end
59
+
60
+ def pipe
61
+ str("|")
62
+ end
63
+
64
+ def ampersand
65
+ str("&")
66
+ end
67
+
68
+ def do_keyword
69
+ str("do")
70
+ end
71
+
72
+ def end_keyword
73
+ str("end")
74
+ end
75
+
76
+ def elsif_keyword
77
+ str("elsif")
78
+ end
79
+
80
+ def else_keyword
81
+ str("else")
82
+ end
83
+
84
+ def special_character
85
+ ampersand | equal | pipe | dot | colon | comma | space | newline |
86
+ opening_curly_bracket | closing_curly_bracket | opening_parenthesis |
87
+ closing_parenthesis | opening_square_bracket |
88
+ closing_square_bracket | single_quote | double_quote
89
+ end
90
+
91
+ def character
92
+ special_character.absent << any
93
+ end
94
+
95
+ def root
96
+ do_keyword.absent << end_keyword.absent << elsif_keyword.absent <<
97
+ else_keyword.absent << character.repeat(1)
98
+ end
87
99
  end
88
100
  end
89
101
  end
@@ -1,19 +1,30 @@
1
1
  class Code
2
2
  class Parser
3
- class Negation < Parslet::Parser
4
- rule(:function) { ::Code::Parser::Function.new }
3
+ class Negation < Language
4
+ def exclamation_point
5
+ str("!")
6
+ end
7
+
8
+ def tilde
9
+ str("~")
10
+ end
5
11
 
6
- rule(:exclamation_point) { str("!") }
7
- rule(:plus) { str("+") }
12
+ def plus
13
+ str("+")
14
+ end
8
15
 
9
- rule(:operator) { exclamation_point | plus }
16
+ def operator
17
+ exclamation_point | tilde | plus
18
+ end
10
19
 
11
- rule(:negation) do
12
- (operator.as(:operator) >> negation.as(:statement)).as(:negation) |
13
- function
20
+ def negation
21
+ ::Code::Parser::Negation
14
22
  end
15
23
 
16
- root(:negation)
24
+ def root
25
+ (operator.aka(:operator) << negation.aka(:right)).aka(:negation) |
26
+ ::Code::Parser::Function
27
+ end
17
28
  end
18
29
  end
19
30
  end
@@ -1,21 +1,23 @@
1
1
  class Code
2
2
  class Parser
3
- class NotKeyword < Parslet::Parser
4
- rule(:equal) { ::Code::Parser::Equal.new }
5
-
6
- rule(:not_keyword) { str("not") }
7
-
8
- rule(:operator) { not_keyword }
3
+ class NotKeyword < Language
4
+ def not_class
5
+ ::Code::Parser::NotKeyword
6
+ end
9
7
 
10
- rule(:space) { str(" ") }
11
- rule(:newline) { str("\n") }
12
- rule(:whitespace) { (space | newline).repeat(1) }
8
+ def whitespace
9
+ ::Code::Parser::Whitespace
10
+ end
13
11
 
14
- rule(:not_rule) do
15
- (not_keyword >> whitespace >> not_rule).as(:not_keyword) | equal
12
+ def not_keyword
13
+ str("not")
16
14
  end
17
15
 
18
- root(:not_rule)
16
+ def root
17
+ (not_keyword.aka(:operator) << whitespace << not_class.aka(:right)).aka(
18
+ :not
19
+ ) | ::Code::Parser::Equal
20
+ end
19
21
  end
20
22
  end
21
23
  end
@@ -1,17 +1,22 @@
1
1
  class Code
2
2
  class Parser
3
- class Nothing < Parslet::Parser
4
- rule(:group) { ::Code::Parser::Group.new }
3
+ class Nothing < Language
4
+ def nothing_keyword
5
+ str("nothing")
6
+ end
5
7
 
6
- rule(:nothing_keyword) { str("nothing") }
7
- rule(:null_keyword) { str("null") }
8
- rule(:nil_keyword) { str("nil") }
8
+ def null_keyword
9
+ str("null")
10
+ end
9
11
 
10
- rule(:nothing) do
11
- (nothing_keyword | null_keyword | nil_keyword).as(:nothing) | group
12
+ def nil_keyword
13
+ str("nil")
12
14
  end
13
15
 
14
- root(:nothing)
16
+ def root
17
+ (nothing_keyword | null_keyword | nil_keyword).aka(:nothing) |
18
+ ::Code::Parser::Group
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -1,98 +1,154 @@
1
1
  class Code
2
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
3
+ class Number < Language
4
+ def number
5
+ ::Code::Parser::Number
44
6
  end
45
7
 
46
- rule(:base_8_number) { zero.ignore >> o.ignore >> base_8_digit.repeat(1) }
8
+ def zero
9
+ str("0")
10
+ end
47
11
 
48
- # base 10
12
+ def one
13
+ str("1")
14
+ end
49
15
 
50
- rule(:base_10_digit) do
51
- zero | one | two | three | four | five | six | seven | eight | nine
16
+ def two
17
+ str("2")
18
+ end
19
+
20
+ def three
21
+ str("3")
22
+ end
23
+
24
+ def four
25
+ str("4")
26
+ end
27
+
28
+ def five
29
+ str("5")
52
30
  end
53
31
 
54
- rule(:base_10_whole) { base_10_digit.repeat(1) }
32
+ def six
33
+ str("6")
34
+ end
55
35
 
56
- rule(:base_10_exponent) { e.ignore >> base_10_number }
36
+ def seven
37
+ str("7")
38
+ end
39
+
40
+ def eight
41
+ str("8")
42
+ end
57
43
 
58
- rule(:base_10_integer) do
59
- sign.as(:sign).maybe >> base_10_whole.as(:whole) >>
60
- base_10_exponent.as(:exponent).maybe
44
+ def nine
45
+ str("9")
61
46
  end
62
47
 
63
- rule(:base_10_decimal_decimal) { dot.ignore >> base_10_digit.repeat(1) }
48
+ def dot
49
+ str(".")
50
+ end
51
+
52
+ def a
53
+ str("a") | str("A")
54
+ end
55
+
56
+ def b
57
+ str("b") | str("B")
58
+ end
59
+
60
+ def c
61
+ str("c") | str("C")
62
+ end
63
+
64
+ def d
65
+ str("d") | str("D")
66
+ end
67
+
68
+ def e
69
+ str("e") | str("E")
70
+ end
71
+
72
+ def f
73
+ str("f") | str("F")
74
+ end
64
75
 
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
76
+ def o
77
+ str("o") | str("O")
69
78
  end
70
79
 
71
- rule(:base_10_number) do
72
- base_10_decimal.as(:decimal) | base_10_integer.as(:integer)
80
+ def x
81
+ str("x") | str("X")
73
82
  end
74
83
 
75
- # base 16
84
+ def underscore
85
+ str("_")
86
+ end
76
87
 
77
- rule(:base_16_digit) do
88
+ def base_16_digit
78
89
  zero | one | two | three | four | five | six | seven | eight | nine |
79
90
  a | b | c | d | e | f
80
91
  end
81
92
 
82
- rule(:base_16_number) do
83
- zero.ignore >> x.ignore >> base_16_digit.repeat(1)
93
+ def base_10_digit
94
+ zero | one | two | three | four | five | six | seven | eight | nine
84
95
  end
85
96
 
86
- # number
97
+ def base_8_digit
98
+ zero | one | two | three | four | five | six | seven
99
+ end
87
100
 
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
101
+ def base_2_digit
102
+ zero | one
103
+ end
104
+
105
+ def base_16_whole
106
+ base_16_digit << (underscore.ignore | base_16_digit).repeat
93
107
  end
94
108
 
95
- root(:number)
109
+ def base_10_whole
110
+ base_10_digit << (underscore.ignore | base_10_digit).repeat
111
+ end
112
+
113
+ def base_8_whole
114
+ base_8_digit << (underscore.ignore | base_8_digit).repeat
115
+ end
116
+
117
+ def base_2_whole
118
+ base_2_digit << (underscore.ignore | base_2_digit).repeat
119
+ end
120
+
121
+ def exponent
122
+ (e << number).aka(:exponent)
123
+ end
124
+
125
+ def decimal
126
+ (base_10_whole << dot << base_10_whole).aka(:decimal) << exponent.maybe
127
+ end
128
+
129
+ def base_16_number
130
+ zero.ignore << x.ignore << base_16_whole
131
+ end
132
+
133
+ def base_10_number
134
+ base_10_whole.aka(:whole) << exponent.maybe
135
+ end
136
+
137
+ def base_8_number
138
+ zero.ignore << o.ignore << base_8_whole
139
+ end
140
+
141
+ def base_2_number
142
+ zero.ignore << b.ignore << base_2_whole
143
+ end
144
+
145
+ def root
146
+ (
147
+ decimal.aka(:decimal) | base_16_number.aka(:base_16) |
148
+ base_8_number.aka(:base_8) | base_2_number.aka(:base_2) |
149
+ base_10_number.aka(:base_10)
150
+ ).aka(:number) | ::Code::Parser::Boolean
151
+ end
96
152
  end
97
153
  end
98
154
  end
@@ -0,0 +1,35 @@
1
+ class Code
2
+ class Parser
3
+ class Operation < Language
4
+ def statement
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def whitespace
9
+ ::Code::Parser::Whitespace
10
+ end
11
+
12
+ def whitespace?
13
+ whitespace.maybe
14
+ end
15
+
16
+ def operator
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def root
21
+ (
22
+ statement.aka(:first) <<
23
+ (
24
+ whitespace? << operator.aka(:operator) << whitespace? <<
25
+ statement.aka(:statement)
26
+ ).repeat(1).aka(:others).maybe
27
+ )
28
+ .aka(:operation)
29
+ .then do |output|
30
+ output[:operation][:others] ? output : output[:operation][:first]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end