koi-reference-parser 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.rdoc +4 -0
  2. data/VERSION +1 -1
  3. data/lib/parser/koi-reference-parser.treetop +58 -45
  4. data/lib/parser/parser.rb +2 -17
  5. data/lib/parser/syntax_nodes.rb +0 -9
  6. data/test/{parser/functional → functional}/function_call_as_argument.rb +1 -1
  7. data/test/{parser/functional → functional}/simple_program_test.rb +1 -1
  8. data/test/helpers/assert_assigns.rb +9 -0
  9. data/test/helpers/assert_assigns_expression.rb +10 -0
  10. data/test/helpers/assert_expression.rb +11 -0
  11. data/test/helpers/assert_identifier.rb +6 -0
  12. data/test/test_helper.rb +1 -0
  13. data/test/unit/assignment/assignment_of_hash_value_test.rb +19 -0
  14. data/test/{parser/unit/hash_accessor_test.rb → unit/assignment/assignment_to_hash_test.rb} +2 -14
  15. data/test/unit/assignment/boolean_assignment_test.rb +17 -0
  16. data/test/unit/assignment/float_assignment_test.rb +22 -0
  17. data/test/unit/assignment/function_call_assignment_test.rb +12 -0
  18. data/test/unit/assignment/function_definition_test.rb +12 -0
  19. data/test/unit/assignment/hash_literal_assignment_test.rb +27 -0
  20. data/test/unit/assignment/integer_assignment_test.rb +22 -0
  21. data/test/unit/assignment/nil_assignment_test.rb +12 -0
  22. data/test/unit/assignment/string_assignment_test.rb +17 -0
  23. data/test/unit/expressions/addition_expression_test.rb +27 -0
  24. data/test/{parser/unit → unit/expressions}/compound_expression_test.rb +0 -0
  25. data/test/unit/expressions/division_expression_test.rb +27 -0
  26. data/test/unit/expressions/equality_expression_test.rb +27 -0
  27. data/test/unit/expressions/greater_than_expression_test.rb +27 -0
  28. data/test/unit/expressions/inequality_expression_test.rb +27 -0
  29. data/test/unit/expressions/less_than_expression_test.rb +27 -0
  30. data/test/unit/expressions/multiplication_expression_test.rb +27 -0
  31. data/test/unit/expressions/subtraction_expression_test.rb +27 -0
  32. data/test/{parser/unit → unit/flow_control}/if_test.rb +0 -0
  33. data/test/{parser/unit → unit/flow_control}/unless_test.rb +0 -0
  34. data/test/{parser/unit → unit/functions}/function_call_test.rb +0 -0
  35. data/test/{parser/unit → unit/functions}/function_definition_test.rb +0 -0
  36. data/test/unit/identifier_test.rb +37 -0
  37. data/test/{parser/unit → unit}/statement_test.rb +1 -1
  38. data/test/{parser/unit → unit}/syntax_node_to_hash_test.rb +1 -1
  39. metadata +67 -29
  40. data/test/parser/unit/assignment_test.rb +0 -115
  41. data/test/parser/unit/identifier_test.rb +0 -55
  42. data/test/parser/unit/simple_expression_test.rb +0 -212
@@ -1,115 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class AssignmentTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- test "assignment of integer" do
8
- tree = Parser.parse('test = 1')
9
- assert_assigns_expression(IntegerLiteral, '1', tree)
10
- end
11
-
12
- test "assignment of explicitly positive integer" do
13
- tree = Parser.parse('test = +1')
14
- assert_assigns_expression(IntegerLiteral, '+1', tree)
15
- end
16
-
17
- test "assignment of explicitly negative integer" do
18
- tree = Parser.parse('test = -1')
19
- assert_assigns_expression(IntegerLiteral, '-1', tree)
20
- end
21
-
22
- test "assignment of float" do
23
- tree = Parser.parse('test = 99.0009')
24
- assert_assigns_expression(FloatLiteral, '99.0009', tree)
25
- end
26
-
27
- test "assignment of explicitly positive float" do
28
- tree = Parser.parse('test = +99.0009')
29
- assert_assigns_expression(FloatLiteral, '+99.0009', tree)
30
- end
31
-
32
- test "assignment of explicitly negative float" do
33
- tree = Parser.parse('test = -99.0009')
34
- assert_assigns_expression(FloatLiteral, '-99.0009', tree)
35
- end
36
-
37
- test "assignment of string" do
38
- tree = Parser.parse('test = "test_string"')
39
- assert_assigns_expression(StringLiteral, '"test_string"', tree)
40
- end
41
-
42
- test "assignment of string containing escaped quote" do
43
- tree = Parser.parse('test = "test_\"string"')
44
- assert_assigns_expression(StringLiteral, '"test_\"string"', tree)
45
- end
46
-
47
- test "assignment of function definition" do
48
- tree = Parser.parse('test = function(args) blah = 1 end')
49
- assert_assigns(FunctionDefinition, nil, tree)
50
- end
51
-
52
- test "assignment of result of function call" do
53
- tree = Parser.parse('test = function(args)')
54
- assert_assigns_expression(FunctionCall, nil, tree)
55
- end
56
-
57
- test "assignment of nil" do
58
- tree = Parser.parse('test = nil')
59
- assert_assigns_expression(NilLiteral, nil, tree)
60
- end
61
-
62
- test "assignment of boolean true" do
63
- tree = Parser.parse('test = true')
64
- assert_assigns_expression(TrueLiteral, nil, tree)
65
- end
66
-
67
- test "assignment of boolean false" do
68
- tree = Parser.parse('test = false')
69
- assert_assigns_expression(FalseLiteral, nil, tree)
70
- end
71
-
72
- test "assignment of empty hash literal" do
73
- tree = Parser.parse('test = {}')
74
- assert_assigns(HashLiteral, nil, tree)
75
- end
76
-
77
- test "assignment of hash literal with single key-value pair" do
78
- tree = Parser.parse('test = { 1 => 2 }')
79
- assert_assigns(HashLiteral, nil, tree)
80
- end
81
-
82
- test "assignment of hash literal with 2 key-value pairs" do
83
- tree = Parser.parse('test = { 1 => 2, 2 => 2 }')
84
- assert_assigns(HashLiteral, nil, tree)
85
- end
86
-
87
- test "assignment of hash literal with 3 key-value pairs" do
88
- tree = Parser.parse('test = { 1 => 2, 2 => 2, 3 => 2 }')
89
- assert_assigns(HashLiteral, nil, tree)
90
- end
91
-
92
- private
93
-
94
- def assert_assigns(node_type, value, tree)
95
- assert_kind_of Block, tree
96
- assert_kind_of Statement, tree.elements.last
97
- assert_kind_of Assignment, tree.elements.last.elements.last
98
- assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
99
- assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
100
- assert_kind_of node_type, tree.elements.last.elements.last.elements[2]
101
- assert_equal value, tree.elements.last.elements.last.elements[2].text_value unless(value.nil?)
102
- end
103
-
104
- def assert_assigns_expression(node_type, value, tree)
105
- assert_kind_of Block, tree
106
- assert_kind_of Statement, tree.elements.last
107
- assert_kind_of Assignment, tree.elements.last.elements.last
108
- assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
109
- assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
110
- assert_kind_of Expression, tree.elements.last.elements.last.elements[2]
111
- assert_kind_of node_type, tree.elements.last.elements.last.elements[2].elements.first
112
- assert_equal value, tree.elements.last.elements.last.elements[2].elements.first.text_value unless(value.nil?)
113
- end
114
-
115
- end
@@ -1,55 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class FunctionCallTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- test "should parse all lowercase identifier" do
8
- tree = Parser.parse('test = "test"')
9
- assert_kind_of Block, tree
10
- assert_kind_of Statement, tree.elements.first
11
- assert_kind_of Assignment, tree.elements.first.elements.last
12
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
13
- end
14
-
15
- test "should parse identifier with trailing numeral" do
16
- tree = Parser.parse('test1 = "test"')
17
- assert_kind_of Block, tree
18
- assert_kind_of Statement, tree.elements.first
19
- assert_kind_of Assignment, tree.elements.first.elements.last
20
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
21
- end
22
-
23
- test "should parse identifier with leading capital letter" do
24
- tree = Parser.parse('Test = "test"')
25
- assert_kind_of Block, tree
26
- assert_kind_of Statement, tree.elements.first
27
- assert_kind_of Assignment, tree.elements.first.elements.last
28
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
29
- end
30
-
31
- test "should parse identifier that contains an underscore" do
32
- tree = Parser.parse('test_test = "test"')
33
- assert_kind_of Block, tree
34
- assert_kind_of Statement, tree.elements.first
35
- assert_kind_of Assignment, tree.elements.first.elements.last
36
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
37
- end
38
-
39
- test "should parse single character identifier" do
40
- tree = Parser.parse('x = "test"')
41
- assert_kind_of Block, tree
42
- assert_kind_of Statement, tree.elements.first
43
- assert_kind_of Assignment, tree.elements.first.elements.last
44
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
45
- end
46
-
47
- test "should parse identifier with leading $" do
48
- tree = Parser.parse('$test = "test"')
49
- assert_kind_of Block, tree
50
- assert_kind_of Statement, tree.elements.first
51
- assert_kind_of Assignment, tree.elements.first.elements.last
52
- assert_kind_of Identifier, tree.elements.first.elements.last.elements.first
53
- end
54
-
55
- end
@@ -1,212 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class SimpleExpressionTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- ###############
8
- # ADDITION
9
-
10
- test "should parse simple addition expression with integers" do
11
- tree = Parser.parse('test = 1 + 1')
12
- assert_expression(tree, AdditionOperator)
13
- end
14
-
15
- test "should parse simple addition expression with floats" do
16
- tree = Parser.parse('test = 1.0 + 1.0')
17
- assert_expression(tree, AdditionOperator)
18
- end
19
-
20
- test "should parse simple addition expression with strings" do
21
- tree = Parser.parse('test = "1" + "1"')
22
- assert_expression(tree, AdditionOperator)
23
- end
24
-
25
- test "should parse simple addition expression with identifiers" do
26
- tree = Parser.parse('test = test1 + test2')
27
- assert_expression(tree, AdditionOperator)
28
- end
29
-
30
-
31
- ###############
32
- # SUBTRACTION
33
-
34
- test "should parse simple subtraction expression with integers" do
35
- tree = Parser.parse('test = 1 - 1')
36
- assert_expression(tree, SubtractionOperator)
37
- end
38
-
39
- test "should parse simple subtraction expression with floats" do
40
- tree = Parser.parse('test = 1.0 - 1.0')
41
- assert_expression(tree, SubtractionOperator)
42
- end
43
-
44
- test "should parse simple subtraction expression with strings" do
45
- tree = Parser.parse('test = "1" - "1"')
46
- assert_expression(tree, SubtractionOperator)
47
- end
48
-
49
- test "should parse simple subtraction expression with identifiers" do
50
- tree = Parser.parse('test = test1 - test2')
51
- assert_expression(tree, SubtractionOperator)
52
- end
53
-
54
-
55
- ###############
56
- # MULTIPLICATION
57
-
58
- test "should parse simple multiplication expression with integers" do
59
- tree = Parser.parse('test = 1 * 1')
60
- assert_expression(tree, MultiplicationOperator)
61
- end
62
-
63
- test "should parse simple multiplication expression with floats" do
64
- tree = Parser.parse('test = 1.0 * 1.0')
65
- assert_expression(tree, MultiplicationOperator)
66
- end
67
-
68
- test "should parse simple multiplication expression with strings" do
69
- tree = Parser.parse('test = "1" * "1"')
70
- assert_expression(tree, MultiplicationOperator)
71
- end
72
-
73
- test "should parse simple multiplication expression with identifiers" do
74
- tree = Parser.parse('test = test1 * test2')
75
- assert_expression(tree, MultiplicationOperator)
76
- end
77
-
78
-
79
- ###############
80
- # DIVISION
81
-
82
- test "should parse simple division expression with integers" do
83
- tree = Parser.parse('test = 1 / 1')
84
- assert_expression(tree, DivisionOperator)
85
- end
86
-
87
- test "should parse simple division expression with floats" do
88
- tree = Parser.parse('test = 1.0 / 1.0')
89
- assert_expression(tree, DivisionOperator)
90
- end
91
-
92
- test "should parse simple division expression with strings" do
93
- tree = Parser.parse('test = "1" / "1"')
94
- assert_expression(tree, DivisionOperator)
95
- end
96
-
97
- test "should parse simple division expression with identifiers" do
98
- tree = Parser.parse('test = test1 / test2')
99
- assert_expression(tree, DivisionOperator)
100
- end
101
-
102
-
103
- ###############
104
- # EQUALITY
105
-
106
- test "should parse simple equality expression with integers" do
107
- tree = Parser.parse('test = 1 == 1')
108
- assert_expression(tree, EqualityOperator)
109
- end
110
-
111
- test "should parse simple equality expression with floats" do
112
- tree = Parser.parse('test = 1.0 == 1.0')
113
- assert_expression(tree, EqualityOperator)
114
- end
115
-
116
- test "should parse simple equality expression with strings" do
117
- tree = Parser.parse('test = "1" == "1"')
118
- assert_expression(tree, EqualityOperator)
119
- end
120
-
121
- test "should parse simple equality expression with identifiers" do
122
- tree = Parser.parse('test = test1 == test2')
123
- assert_expression(tree, EqualityOperator)
124
- end
125
-
126
-
127
- ###############
128
- # INEQUALITY
129
-
130
- test "should parse simple inequality expression with integers" do
131
- tree = Parser.parse('test = 1 != 1')
132
- assert_expression(tree, InequalityOperator)
133
- end
134
-
135
- test "should parse simple inequality expression with floats" do
136
- tree = Parser.parse('test = 1.0 != 1.0')
137
- assert_expression(tree, InequalityOperator)
138
- end
139
-
140
- test "should parse simple inequality expression with strings" do
141
- tree = Parser.parse('test = "1" != "1"')
142
- assert_expression(tree, InequalityOperator)
143
- end
144
-
145
- test "should parse simple inequality expression with identifiers" do
146
- tree = Parser.parse('test = test1 != test2')
147
- assert_expression(tree, InequalityOperator)
148
- end
149
-
150
-
151
- ###############
152
- # GREATER THAN
153
-
154
- test "should parse simple 'greater than' expression with integers" do
155
- tree = Parser.parse('test = 1 > 1')
156
- assert_expression(tree, GreaterThanOperator)
157
- end
158
-
159
- test "should parse simple 'greater than' expression with floats" do
160
- tree = Parser.parse('test = 1.0 > 1.0')
161
- assert_expression(tree, GreaterThanOperator)
162
- end
163
-
164
- test "should parse simple 'greater than' expression with strings" do
165
- tree = Parser.parse('test = "1" > "1"')
166
- assert_expression(tree, GreaterThanOperator)
167
- end
168
-
169
- test "should parse simple 'greater than' expression with identifiers" do
170
- tree = Parser.parse('test = test1 > test2')
171
- assert_expression(tree, GreaterThanOperator)
172
- end
173
-
174
-
175
- ###############
176
- # LESS THAN
177
-
178
- test "should parse simple 'less than' expression with integers" do
179
- tree = Parser.parse('test = 1 < 1')
180
- assert_expression(tree, LessThanOperator)
181
- end
182
-
183
- test "should parse simple 'less than' expression with floats" do
184
- tree = Parser.parse('test = 1.0 < 1.0')
185
- assert_expression(tree, LessThanOperator)
186
- end
187
-
188
- test "should parse simple 'less than' expression with strings" do
189
- tree = Parser.parse('test = "1" < "1"')
190
- assert_expression(tree, LessThanOperator)
191
- end
192
-
193
- test "should parse simple 'less than' expression with identifiers" do
194
- tree = Parser.parse('test = test1 < test2')
195
- assert_expression(tree, LessThanOperator)
196
- end
197
-
198
-
199
- private
200
-
201
- def assert_expression(tree, operator = nil)
202
- assert_kind_of Block, tree
203
- assert_kind_of Statement, tree.elements.first
204
- assert_kind_of Assignment, tree.elements.first.elements.last
205
- assert_kind_of Identifier, tree.elements.first.elements.last.elements[0]
206
- assert_kind_of AssignmentOperator, tree.elements.first.elements.last.elements[1]
207
- assert_kind_of Expression, tree.elements.first.elements.last.elements[2]
208
- assert_equal 3, tree.elements.first.elements.last.elements[2].elements.first.elements.size
209
- assert_kind_of operator, tree.elements.first.elements.last.elements[2].elements.first.elements[1] unless(operator.nil?)
210
- end
211
-
212
- end