template-ruby 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +9 -0
  3. data/.gitignore +1 -0
  4. data/.prettierrc +3 -0
  5. data/CHANGELOG.md +22 -0
  6. data/Gemfile.lock +5 -5
  7. data/README.md +10 -0
  8. data/bin/template +39 -0
  9. data/docs/euler/1.template +14 -0
  10. data/docs/euler/2.template +16 -0
  11. data/docs/euler/3.template +16 -0
  12. data/docs/euler/4.template +11 -0
  13. data/docs/euler/5.template +14 -0
  14. data/docs/precedence.template +94 -0
  15. data/lib/code/error.rb +15 -0
  16. data/lib/code/node/base_10_decimal.rb +12 -7
  17. data/lib/code/node/base_10_integer.rb +13 -4
  18. data/lib/code/node/base_10_number.rb +3 -3
  19. data/lib/code/node/base_16_number.rb +2 -2
  20. data/lib/code/node/base_2_number.rb +2 -2
  21. data/lib/code/node/base_8_number.rb +2 -2
  22. data/lib/code/node/block.rb +17 -0
  23. data/lib/code/node/boolean.rb +13 -4
  24. data/lib/code/node/call.rb +41 -8
  25. data/lib/code/node/call_argument.rb +37 -0
  26. data/lib/code/node/chained_call.rb +38 -0
  27. data/lib/code/node/code.rb +4 -7
  28. data/lib/code/node/defined.rb +19 -0
  29. data/lib/code/node/dictionnary.rb +11 -7
  30. data/lib/code/node/dictionnary_key_value.rb +3 -3
  31. data/lib/code/node/equal.rb +36 -0
  32. data/lib/code/node/function.rb +17 -0
  33. data/lib/code/node/function_argument.rb +45 -0
  34. data/lib/code/node/group.rb +13 -0
  35. data/lib/code/node/if.rb +55 -0
  36. data/lib/code/node/if_modifier.rb +48 -0
  37. data/lib/code/node/keyword_call_argument.rb +30 -0
  38. data/lib/code/node/keyword_function_argument.rb +33 -0
  39. data/lib/code/node/list.rb +10 -4
  40. data/lib/code/node/name.rb +37 -4
  41. data/lib/code/node/negation.rb +33 -0
  42. data/lib/code/node/not_keyword.rb +13 -0
  43. data/lib/code/node/nothing.rb +2 -2
  44. data/lib/code/node/number.rb +3 -3
  45. data/lib/code/node/operation.rb +33 -0
  46. data/lib/code/node/or_keyword.rb +34 -0
  47. data/lib/code/node/power.rb +16 -0
  48. data/lib/code/node/range.rb +31 -0
  49. data/lib/code/node/regular_call_argument.rb +34 -0
  50. data/lib/code/node/regular_function_argument.rb +36 -0
  51. data/lib/code/node/rescue.rb +16 -0
  52. data/lib/code/node/statement.rb +53 -3
  53. data/lib/code/node/string.rb +2 -2
  54. data/lib/code/node/ternary.rb +26 -0
  55. data/lib/code/node/unary_minus.rb +22 -0
  56. data/lib/code/node/while.rb +42 -0
  57. data/lib/code/node.rb +10 -0
  58. data/lib/code/object/argument.rb +41 -0
  59. data/lib/code/object/boolean.rb +8 -9
  60. data/lib/code/object/decimal.rb +32 -9
  61. data/lib/code/object/dictionnary.rb +33 -10
  62. data/lib/code/object/function.rb +64 -0
  63. data/lib/code/object/integer.rb +94 -7
  64. data/lib/code/object/list.rb +190 -10
  65. data/lib/code/object/nothing.rb +10 -9
  66. data/lib/code/object/number.rb +6 -0
  67. data/lib/code/object/range.rb +158 -0
  68. data/lib/code/object/string.rb +37 -7
  69. data/lib/code/object.rb +121 -2
  70. data/lib/code/parser/addition.rb +29 -0
  71. data/lib/code/parser/and_operator.rb +28 -0
  72. data/lib/code/parser/bitwise_and.rb +28 -0
  73. data/lib/code/parser/bitwise_or.rb +29 -0
  74. data/lib/code/parser/call.rb +77 -3
  75. data/lib/code/parser/code.rb +2 -1
  76. data/lib/code/parser/defined.rb +20 -0
  77. data/lib/code/parser/equal.rb +42 -0
  78. data/lib/code/parser/equality.rb +36 -0
  79. data/lib/code/parser/function.rb +57 -0
  80. data/lib/code/parser/greater_than.rb +33 -0
  81. data/lib/code/parser/group.rb +17 -0
  82. data/lib/code/parser/if.rb +33 -0
  83. data/lib/code/parser/if_modifier.rb +28 -0
  84. data/lib/code/parser/multiplication.rb +30 -0
  85. data/lib/code/parser/name.rb +44 -4
  86. data/lib/code/parser/negation.rb +19 -0
  87. data/lib/code/parser/not_keyword.rb +21 -0
  88. data/lib/code/parser/nothing.rb +2 -2
  89. data/lib/code/parser/or_keyword.rb +29 -0
  90. data/lib/code/parser/or_operator.rb +28 -0
  91. data/lib/code/parser/power.rb +25 -0
  92. data/lib/code/parser/range.rb +25 -0
  93. data/lib/code/parser/rescue.rb +23 -0
  94. data/lib/code/parser/shift.rb +31 -0
  95. data/lib/code/parser/statement.rb +1 -4
  96. data/lib/code/parser/string.rb +7 -1
  97. data/lib/code/parser/ternary.rb +25 -0
  98. data/lib/code/parser/unary_minus.rb +13 -0
  99. data/lib/code/parser/while.rb +25 -0
  100. data/lib/code.rb +5 -7
  101. data/lib/template/node/code_part.rb +2 -2
  102. data/lib/template/node/part.rb +2 -2
  103. data/lib/template/node/template.rb +4 -2
  104. data/lib/template/node/text_part.rb +1 -1
  105. data/lib/template/parser/template.rb +6 -2
  106. data/lib/template-ruby.rb +4 -0
  107. data/lib/template.rb +9 -4
  108. data/spec/call_spec.rb +22 -0
  109. data/spec/code/error/type_error_spec.rb +65 -0
  110. data/spec/code/parser/boolean_spec.rb +1 -1
  111. data/spec/code/parser/call_spec.rb +38 -11
  112. data/spec/code/parser/dictionnary_spec.rb +11 -11
  113. data/spec/code/parser/function_spec.rb +32 -0
  114. data/spec/code/parser/list_spec.rb +5 -5
  115. data/spec/code/parser/nothing_spec.rb +1 -1
  116. data/spec/code/parser/number_spec.rb +35 -35
  117. data/spec/code/parser/string_spec.rb +3 -2
  118. data/spec/code_spec.rb +75 -3
  119. data/spec/function_spec.rb +26 -0
  120. data/spec/spec_helper.rb +2 -0
  121. data/spec/template/parser/template_spec.rb +1 -1
  122. data/spec/template_spec.rb +3 -6
  123. data/template-ruby.gemspec +6 -3
  124. metadata +76 -4
@@ -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
@@ -2,14 +2,88 @@ class Code
2
2
  class Parser
3
3
  class Call < Parslet::Parser
4
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 }
5
8
 
6
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") }
7
21
 
8
- rule(:call) do
9
- (dictionnary.as(:left) >> dot >> call.as(:right)).as(:call) |
10
- dictionnary
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
11
53
  end
12
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
+
13
87
  root(:call)
14
88
  end
15
89
  end
@@ -9,7 +9,8 @@ class Code
9
9
  rule(:whitespace?) { whitespace.maybe }
10
10
 
11
11
  rule(:code) do
12
- (whitespace? >> statement >> whitespace?).repeat(1) | whitespace?
12
+ (whitespace?.ignore >> statement >> whitespace?.ignore).repeat(1) |
13
+ whitespace?.ignore
13
14
  end
14
15
 
15
16
  root(:code)
@@ -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,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,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
@@ -12,9 +12,36 @@ class Code
12
12
  rule(:closing_curly_bracket) { str("}") }
13
13
  rule(:opening_square_bracket) { str("[") }
14
14
  rule(:closing_square_bracket) { str("]") }
15
+ rule(:opening_parenthesis) { str("(") }
16
+ rule(:closing_parenthesis) { str(")") }
15
17
  rule(:equal) { str("=") }
16
18
  rule(:left_caret) { str("<") }
17
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") }
18
45
 
19
46
  rule(:zero) { str("0") }
20
47
  rule(:one) { str("1") }
@@ -32,18 +59,31 @@ class Code
32
59
  end
33
60
 
34
61
  rule(:name_character) do
35
- space.absent? >> newline.absent? >> comma.absent? >> colon.absent? >>
36
- dot.absent? >> single_quote.absent? >> double_quote.absent? >>
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? >>
37
69
  opening_curly_bracket.absent? >> closing_curly_bracket.absent? >>
38
70
  opening_square_bracket.absent? >> closing_square_bracket.absent? >>
39
71
  equal.absent? >> left_caret.absent? >> right_caret.absent? >> any
40
72
  end
41
73
 
42
74
  rule(:name) do
43
- (digit.absent? >> name_character >> name_character.repeat).as(:name)
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
44
82
  end
45
83
 
46
- root(:name)
84
+ rule(:name_rule) { name.as(:name) }
85
+
86
+ root(:name_rule)
47
87
  end
48
88
  end
49
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
@@ -1,14 +1,14 @@
1
1
  class Code
2
2
  class Parser
3
3
  class Nothing < Parslet::Parser
4
- rule(:name) { ::Code::Parser::Name.new }
4
+ rule(:group) { ::Code::Parser::Group.new }
5
5
 
6
6
  rule(:nothing_keyword) { str("nothing") }
7
7
  rule(:null_keyword) { str("null") }
8
8
  rule(:nil_keyword) { str("nil") }
9
9
 
10
10
  rule(:nothing) do
11
- (nothing_keyword | null_keyword | nil_keyword).as(:nothing) | name
11
+ (nothing_keyword | null_keyword | nil_keyword).as(:nothing) | group
12
12
  end
13
13
 
14
14
  root(:nothing)
@@ -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