cadenza 0.7.0.rc1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,54 @@
1
1
  module Cadenza
2
+ # The {OperationNode} is a node which contains an evaluatable {#left} and
3
+ # {#right} subtree and an {#operator} with which to evaluate it.
2
4
  class OperationNode
3
- attr_accessor :left, :right, :operator
5
+ # @return [VariableNode|ConstantNode|OperationNode|BooleanInverseNode] the
6
+ # root of the left subtree to evaluate for this operation.
7
+ attr_accessor :left
4
8
 
9
+ # @return [VariableNode|ConstantNode|OperationNode|BooleanInverseNode] the
10
+ # root of the right subtree to evaluate for this operation.
11
+ attr_accessor :right
12
+
13
+ # The operation of this node to do when evaluating. The operator should be
14
+ # one of the following values:
15
+ # - "==" for equal
16
+ # - "!=" for not equal
17
+ # - ">=" for greater than or equal to
18
+ # - "<=" for less than or equal to
19
+ # - ">" for greater than
20
+ # - "<" for less than
21
+ # - "and" for the boolean 'and' conjunction
22
+ # - "or" for the boolean 'or' conjunction
23
+ # - "+" for the arithmetic addition operation
24
+ # - "-" for the arithmetic subtraction operation
25
+ # - "*" for the arithmetic multiplication operation
26
+ # - "/" for the arithmetic division operation
27
+ # @return [String] the operation for this node
28
+ attr_accessor :operator
29
+
30
+ # creates a new {OperationNode} with the given left and right subtrees and
31
+ # the given operator.
32
+ # @param [VariableNode|ConstantNode|OperationNode|BooleanInverseNode] left see {#left}
33
+ # @param [VariableNode|ConstantNode|OperationNode|BooleanInverseNode] right see {#right}
34
+ # @param [String] operator see {#operator}
5
35
  def initialize(left, operator, right)
6
36
  @left = left
7
37
  @right = right
8
38
  @operator = operator
9
39
  end
10
40
 
41
+ # @return [Array] a list of variable names implied to be global in this node
11
42
  def implied_globals
12
43
  @left.implied_globals | @right.implied_globals
13
44
  end
14
45
 
46
+ # Evalutes the left and right subtree in the given context and uses the
47
+ # {#operator} to manipulate the two values into a single output value
48
+ # which is then returned.
49
+ # @param [Context] context
50
+ # @return [Object] the result of performing the operation on the evaluated
51
+ # left and right subtrees in the given context.
15
52
  def eval(context)
16
53
  l = @left.eval(context)
17
54
  r = @right.eval(context)
@@ -57,6 +94,9 @@ module Cadenza
57
94
  end
58
95
  end
59
96
 
97
+ # @param [OperationNode] rhs
98
+ # @return [Boolean] true if the given {OperationNode} is equivalent by value
99
+ # to this node.
60
100
  def ==(rhs)
61
101
  @operator == rhs.operator and
62
102
  @left == rhs.left and
@@ -1,15 +1,24 @@
1
1
  module Cadenza
2
+ # The {TextNode} is a very simple container for static text defined in your
3
+ # template.
2
4
  class TextNode
5
+ # @return [String] the content of the text in this node
3
6
  attr_accessor :text
4
7
 
8
+ # Creates a new {TextNode} with the given text
9
+ # @param [String] text see {#text}
5
10
  def initialize(text)
6
11
  @text = text
7
12
  end
8
13
 
14
+ # @return [Array] a list of global variable names implied by this node (none).
9
15
  def implied_globals
10
16
  []
11
17
  end
12
18
 
19
+ # @param [TextNode] rhs
20
+ # @return [Boolean] true if the given {TextNode} is equivalent by value to
21
+ # this node.
13
22
  def ==(rhs)
14
23
  @text == rhs.text
15
24
  end
@@ -1,19 +1,32 @@
1
1
  module Cadenza
2
+ # The {VariableNode} holds a variable name (identifier) which it can render
3
+ # the value of given a {Context} with the name defined in it's variable stack.
2
4
  class VariableNode
5
+ # @return [String] the name given to this variable
3
6
  attr_accessor :identifier
4
7
 
8
+ # creates a new {VariableNode} with the name given.
9
+ # @param [String] identifier see {#identifier}
5
10
  def initialize(identifier)
6
11
  @identifier = identifier
7
12
  end
8
13
 
14
+ # @return [Array] a list of names which are implied to be global variables
15
+ # from this node.
9
16
  def implied_globals
10
17
  [self.identifier]
11
18
  end
12
19
 
20
+ # @param [Context] context
21
+ # @return [Object] looks up and returns the value of this variable in the
22
+ # given {Context}
13
23
  def eval(context)
14
24
  context.lookup(@identifier)
15
25
  end
16
26
 
27
+ # @param [VariableNode] rhs
28
+ # @return [Boolean] if the given {VariableNode} is equivalent by value to
29
+ # this node.
17
30
  def ==(rhs)
18
31
  self.identifier == rhs.identifier
19
32
  end
@@ -1,942 +1,84 @@
1
- #
2
- # DO NOT MODIFY!!!!
3
- # This file is automatically generated by Racc 1.4.7
4
- # from Racc grammer file "".
5
- #
6
1
 
7
- require 'racc/parser.rb'
8
-
9
- # parser.rb : generated by racc
10
-
11
2
  module Cadenza
12
- class Parser < Racc::Parser
13
-
14
- module_eval(<<'...end cadenza.y/module_eval...', 'cadenza.y', 229)
15
- attr_reader :lexer
16
-
17
- def initialize(options={})
18
- @lexer = options.fetch(:lexer, Cadenza::Lexer.new)
19
-
20
- raise "Lexers passed to the parser must define next_token" unless @lexer.respond_to?(:next_token)
21
-
22
- raise "Lexers passed to the parser must define source=" unless @lexer.respond_to?(:source=)
23
- end
24
-
25
- def parse(source)
26
- @lexer.source = source
27
-
28
- @stack = [DocumentNode.new]
29
-
30
- do_parse
31
-
32
- @stack.first
33
- end
34
-
35
- def push_child(node)
36
- @stack.last.children.push(node)
37
- end
38
-
39
- def push_block(block_node)
40
- @stack.first.add_block(block_node)
41
- push_child(block_node)
42
- end
43
-
44
- def next_token
45
- @lexer.next_token
46
- end
47
- ...end cadenza.y/module_eval...
48
- ##### State transition tables begin ###
49
-
50
- racc_action_table = [
51
- 38, 3, 111, 4, 3, 38, 4, 55, 23, 24,
52
- 25, 26, 3, 38, 154, 70, 71, 9, 38, 95,
53
- 9, 70, 71, 31, 115, 34, 118, 35, 9, 38,
54
- 34, 36, 35, 79, 37, 93, 36, 39, 34, 37,
55
- 35, 157, 39, 34, 36, 35, 82, 37, 87, 36,
56
- 39, 38, 37, 93, 34, 39, 35, 82, 83, 3,
57
- 36, 155, 38, 37, 136, 3, 39, 129, 70, 71,
58
- 38, 81, 70, 71, 80, 9, 34, 114, 35, 59,
59
- 60, 9, 36, 38, 91, 37, 141, 34, 39, 35,
60
- 61, 62, 87, 36, 133, 34, 37, 35, 144, 39,
61
- 3, 36, 44, 3, 37, 124, 156, 39, 34, 95,
62
- 35, 97, 83, 96, 36, 91, 9, 37, 128, 9,
63
- 39, 55, 23, 24, 25, 26, 55, 23, 24, 25,
64
- 26, 55, 23, 24, 25, 26, 142, 31, 143, 142,
65
- 77, 149, 31, 3, 3, 40, 40, 31, 55, 23,
66
- 24, 25, 26, 55, 23, 24, 25, 26, 76, 9,
67
- 9, 3, 123, 4, 31, 3, 3, 4, 4, 31,
68
- 22, 23, 24, 25, 26, 122, 3, 9, 4, 61,
69
- 62, 9, 9, 3, 121, 4, 31, 55, 23, 24,
70
- 25, 26, 9, 55, 23, 24, 25, 26, 3, 9,
71
- 4, 120, 3, 31, 137, 3, 117, 134, 116, 31,
72
- 111, 3, 152, 129, 9, 3, 111, 129, 9, 61,
73
- 62, 9, 55, 23, 24, 25, 26, 9, 3, 98,
74
- 124, 9, 3, 3, 124, 44, 61, 62, 31, 70,
75
- 71, 70, 71, 73, 9, 72, 59, 60, 9, 9,
76
- 63, 64, 65, 66, 67, 68, 63, 64, 65, 66,
77
- 67, 68, 55, 23, 24, 25, 26, 55, 23, 24,
78
- 25, 26, 55, 23, 24, 25, 26, 55, 23, 24,
79
- 25, 26, 55, 23, 24, 25, 26, 55, 23, 24,
80
- 25, 26, 55, 23, 24, 25, 26, 55, 23, 24,
81
- 25, 26, 55, 23, 24, 25, 26, 55, 23, 24,
82
- 25, 26, 55, 23, 24, 25, 26, 59, 60, 61,
83
- 62, 61, 62, 61, 62, 54, 95, 51, 48, 18,
84
- 158, 159 ]
85
-
86
- racc_action_check = [
87
- 137, 0, 97, 0, 145, 155, 145, 38, 38, 38,
88
- 38, 38, 146, 4, 146, 75, 75, 0, 44, 78,
89
- 145, 56, 56, 38, 75, 137, 78, 137, 146, 40,
90
- 155, 137, 155, 38, 137, 137, 155, 137, 4, 155,
91
- 4, 155, 155, 44, 4, 44, 44, 4, 44, 44,
92
- 4, 134, 44, 51, 40, 44, 40, 40, 40, 153,
93
- 40, 153, 129, 40, 93, 131, 40, 131, 74, 74,
94
- 154, 39, 139, 139, 39, 153, 134, 74, 134, 28,
95
- 28, 131, 134, 124, 134, 134, 111, 129, 134, 129,
96
- 108, 108, 129, 129, 91, 154, 129, 154, 116, 129,
97
- 6, 154, 6, 126, 154, 126, 154, 154, 124, 57,
98
- 124, 57, 124, 57, 124, 48, 6, 124, 87, 126,
99
- 124, 26, 26, 26, 26, 26, 141, 141, 141, 141,
100
- 141, 34, 34, 34, 34, 34, 113, 26, 113, 140,
101
- 37, 140, 141, 5, 42, 5, 42, 34, 35, 35,
102
- 35, 35, 35, 22, 22, 22, 22, 22, 36, 5,
103
- 42, 43, 83, 43, 35, 2, 47, 2, 47, 22,
104
- 3, 3, 3, 3, 3, 82, 119, 43, 119, 29,
105
- 29, 2, 47, 50, 81, 50, 3, 70, 70, 70,
106
- 70, 70, 119, 95, 95, 95, 95, 95, 53, 50,
107
- 53, 80, 94, 70, 94, 92, 77, 92, 76, 95,
108
- 142, 90, 144, 90, 53, 89, 73, 89, 94, 107,
109
- 107, 92, 71, 71, 71, 71, 71, 90, 86, 58,
110
- 86, 89, 85, 46, 85, 46, 106, 106, 71, 33,
111
- 33, 58, 58, 33, 86, 33, 101, 101, 85, 46,
112
- 30, 30, 30, 30, 30, 30, 69, 69, 69, 69,
113
- 69, 69, 63, 63, 63, 63, 63, 64, 64, 64,
114
- 64, 64, 65, 65, 65, 65, 65, 59, 59, 59,
115
- 59, 59, 60, 60, 60, 60, 60, 66, 66, 66,
116
- 66, 66, 61, 61, 61, 61, 61, 68, 68, 68,
117
- 68, 68, 31, 31, 31, 31, 31, 67, 67, 67,
118
- 67, 67, 62, 62, 62, 62, 62, 102, 102, 105,
119
- 105, 104, 104, 103, 103, 18, 150, 8, 7, 1,
120
- 156, 157 ]
121
-
122
- racc_action_pointer = [
123
- -23, 329, 141, 168, 11, 119, 76, 302, 301, nil,
124
- nil, nil, nil, nil, nil, nil, nil, nil, 325, nil,
125
- nil, nil, 151, nil, nil, nil, 119, nil, 71, 169,
126
- 238, 300, nil, 220, 129, 146, 156, 138, 5, 69,
127
- 27, nil, 120, 137, 16, nil, 209, 142, 80, nil,
128
- 159, 16, nil, 174, nil, nil, 2, 88, 222, 275,
129
- 280, 290, 310, 260, 265, 270, 285, 305, 295, 244,
130
- 185, 220, nil, 214, 49, -4, 174, 178, -2, nil,
131
- 173, 156, 147, 134, nil, 208, 204, 90, nil, 191,
132
- 187, 66, 181, 36, 178, 191, nil, 0, nil, nil,
133
- nil, 238, 309, 313, 311, 309, 226, 209, 80, nil,
134
- nil, 64, nil, 113, nil, nil, 96, nil, nil, 152,
135
- nil, nil, nil, nil, 81, nil, 79, nil, nil, 60,
136
- nil, 41, nil, nil, 49, nil, nil, -2, nil, 53,
137
- 116, 124, 208, nil, 184, -20, -12, nil, nil, nil,
138
- 305, nil, nil, 35, 68, 3, 302, 303, nil, nil ]
139
-
140
- racc_action_default = [
141
- -2, -79, -1, -79, -79, -79, -79, -54, -59, -67,
142
- -68, -69, -70, -71, -72, -73, -75, -77, -79, -74,
143
- -76, -78, -3, -4, -5, -6, -79, -8, -11, -14,
144
- -21, -79, -23, -79, -79, -79, -79, -79, -79, -79,
145
- -79, -41, -79, -79, -79, -46, -79, -79, -79, -53,
146
- -79, -79, -58, -79, 160, -3, -26, -79, -79, -79,
147
- -79, -79, -79, -79, -79, -79, -79, -79, -79, -22,
148
- -79, -79, -32, -79, -79, -79, -79, -79, -79, -61,
149
- -79, -79, -79, -79, -42, -79, -79, -79, -47, -79,
150
- -79, -79, -79, -79, -79, -79, -34, -79, -7, -9,
151
- -10, -12, -13, -15, -16, -17, -18, -19, -20, -24,
152
- -25, -28, -30, -79, -36, -37, -79, -56, -63, -79,
153
- -65, -66, -38, -39, -79, -44, -79, -43, -40, -79,
154
- -49, -79, -48, -52, -79, -55, -57, -79, -60, -27,
155
- -79, -79, -79, -33, -79, -79, -79, -45, -50, -35,
156
- -29, -31, -51, -79, -79, -79, -79, -79, -62, -64 ]
157
-
158
- racc_goto_table = [
159
- 19, 21, 41, 45, 33, 43, 47, 57, 113, 52,
160
- 20, 50, 2, 69, 49, 99, 100, 42, 46, 109,
161
- 110, 101, 102, 78, 53, 119, 145, 58, 151, 1,
162
- nil, nil, 140, nil, nil, 74, 75, nil, nil, 84,
163
- 19, 21, 85, 88, 19, 21, 89, nil, nil, nil,
164
- 20, nil, nil, nil, 20, 86, nil, nil, nil, 90,
165
- nil, nil, 92, nil, nil, 94, 103, 104, 105, 106,
166
- 107, 108, nil, nil, nil, nil, nil, nil, nil, nil,
167
- nil, nil, 125, 127, 19, 21, 130, 132, 19, 21,
168
- 19, 21, 19, 21, 20, 138, 139, 126, 20, 135,
169
- 20, 131, 20, nil, nil, nil, nil, nil, nil, nil,
170
- nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
171
- nil, nil, nil, 147, 19, 21, 150, nil, 148, 19,
172
- 21, 146, nil, nil, 20, nil, nil, nil, nil, 20,
173
- nil, nil, nil, nil, 19, 21, nil, nil, nil, nil,
174
- nil, 19, 21, nil, 20, nil, nil, 153, nil, nil,
175
- nil, 20 ]
176
-
177
- racc_goto_check = [
178
- 32, 26, 16, 17, 4, 15, 15, 9, 11, 25,
179
- 31, 23, 2, 7, 21, 3, 3, 2, 2, 8,
180
- 8, 5, 5, 9, 27, 29, 30, 4, 10, 1,
181
- nil, nil, 11, nil, nil, 4, 4, nil, nil, 16,
182
- 32, 26, 15, 17, 32, 26, 15, nil, nil, nil,
183
- 31, nil, nil, nil, 31, 2, nil, nil, nil, 2,
184
- nil, nil, 2, nil, nil, 2, 6, 6, 6, 6,
185
- 6, 6, nil, nil, nil, nil, nil, nil, nil, nil,
186
- nil, nil, 16, 16, 32, 26, 17, 17, 32, 26,
187
- 32, 26, 32, 26, 31, 25, 4, 2, 31, 21,
188
- 31, 2, 31, nil, nil, nil, nil, nil, nil, nil,
189
- nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
190
- nil, nil, nil, 16, 32, 26, 9, nil, 17, 32,
191
- 26, 2, nil, nil, 31, nil, nil, nil, nil, 31,
192
- nil, nil, nil, nil, 32, 26, nil, nil, nil, nil,
193
- nil, 32, 26, nil, 31, nil, nil, 2, nil, nil,
194
- nil, 31 ]
195
-
196
- racc_goto_pointer = [
197
- nil, 29, 12, -44, 1, -40, 3, -18, -51, -15,
198
- -114, -65, nil, nil, nil, 0, -3, -3, nil, nil,
199
- nil, 7, nil, 4, nil, 1, -1, 16, nil, -54,
200
- -92, 8, -2 ]
201
-
202
- racc_goto_default = [
203
- nil, nil, nil, 27, 56, 28, 29, 30, 32, nil,
204
- 112, nil, 10, 5, 6, nil, nil, nil, 11, 12,
205
- 7, nil, 13, nil, 8, nil, 17, nil, 14, nil,
206
- nil, 16, 15 ]
207
-
208
- racc_reduce_table = [
209
- 0, 0, :racc_error,
210
- 1, 42, :_reduce_none,
211
- 0, 42, :_reduce_2,
212
- 1, 44, :_reduce_3,
213
- 1, 44, :_reduce_4,
214
- 1, 44, :_reduce_5,
215
- 1, 44, :_reduce_6,
216
- 3, 44, :_reduce_7,
217
- 1, 46, :_reduce_none,
218
- 3, 46, :_reduce_9,
219
- 3, 46, :_reduce_10,
220
- 1, 47, :_reduce_none,
221
- 3, 47, :_reduce_12,
222
- 3, 47, :_reduce_13,
223
- 1, 48, :_reduce_none,
224
- 3, 48, :_reduce_15,
225
- 3, 48, :_reduce_16,
226
- 3, 48, :_reduce_17,
227
- 3, 48, :_reduce_18,
228
- 3, 48, :_reduce_19,
229
- 3, 48, :_reduce_20,
230
- 1, 49, :_reduce_none,
231
- 2, 49, :_reduce_22,
232
- 1, 45, :_reduce_none,
233
- 3, 45, :_reduce_24,
234
- 3, 45, :_reduce_25,
235
- 1, 50, :_reduce_26,
236
- 3, 50, :_reduce_27,
237
- 1, 51, :_reduce_28,
238
- 3, 51, :_reduce_29,
239
- 1, 52, :_reduce_30,
240
- 3, 52, :_reduce_31,
241
- 3, 53, :_reduce_32,
242
- 5, 53, :_reduce_33,
243
- 4, 53, :_reduce_34,
244
- 6, 53, :_reduce_35,
245
- 4, 54, :_reduce_36,
246
- 4, 55, :_reduce_37,
247
- 3, 56, :_reduce_38,
248
- 3, 57, :_reduce_none,
249
- 3, 58, :_reduce_none,
250
- 2, 59, :_reduce_41,
251
- 3, 59, :_reduce_42,
252
- 4, 59, :_reduce_43,
253
- 4, 59, :_reduce_44,
254
- 5, 59, :_reduce_45,
255
- 2, 60, :_reduce_46,
256
- 3, 60, :_reduce_47,
257
- 4, 60, :_reduce_48,
258
- 4, 60, :_reduce_49,
259
- 5, 60, :_reduce_50,
260
- 6, 61, :_reduce_51,
261
- 3, 62, :_reduce_none,
262
- 2, 63, :_reduce_53,
263
- 0, 64, :_reduce_54,
264
- 4, 63, :_reduce_55,
265
- 4, 65, :_reduce_56,
266
- 3, 66, :_reduce_none,
267
- 2, 67, :_reduce_58,
268
- 0, 68, :_reduce_59,
269
- 4, 67, :_reduce_60,
270
- 0, 70, :_reduce_61,
271
- 8, 69, :_reduce_62,
272
- 0, 71, :_reduce_63,
273
- 9, 69, :_reduce_64,
274
- 4, 72, :_reduce_65,
275
- 4, 72, :_reduce_66,
276
- 1, 73, :_reduce_67,
277
- 1, 73, :_reduce_none,
278
- 1, 73, :_reduce_none,
279
- 1, 73, :_reduce_none,
280
- 1, 73, :_reduce_none,
281
- 1, 73, :_reduce_none,
282
- 1, 43, :_reduce_73,
283
- 2, 43, :_reduce_74,
284
- 1, 43, :_reduce_75,
285
- 2, 43, :_reduce_76,
286
- 1, 43, :_reduce_77,
287
- 2, 43, :_reduce_78 ]
288
-
289
- racc_reduce_n = 79
290
-
291
- racc_shift_n = 160
292
-
293
- racc_token_table = {
294
- false => 0,
295
- :error => 1,
296
- :IDENTIFIER => 2,
297
- :INTEGER => 3,
298
- :REAL => 4,
299
- :STRING => 5,
300
- "(" => 6,
301
- ")" => 7,
302
- "*" => 8,
303
- "/" => 9,
304
- "+" => 10,
305
- "-" => 11,
306
- :OP_EQ => 12,
307
- :OP_NEQ => 13,
308
- :OP_LEQ => 14,
309
- :OP_GEQ => 15,
310
- ">" => 16,
311
- "<" => 17,
312
- :NOT => 18,
313
- :AND => 19,
314
- :OR => 20,
315
- "," => 21,
316
- ":" => 22,
317
- "|" => 23,
318
- :VAR_OPEN => 24,
319
- :VAR_CLOSE => 25,
320
- :STMT_OPEN => 26,
321
- :IF => 27,
322
- :STMT_CLOSE => 28,
323
- :UNLESS => 29,
324
- :ELSE => 30,
325
- :ENDIF => 31,
326
- :ENDUNLESS => 32,
327
- :FOR => 33,
328
- :IN => 34,
329
- :ENDFOR => 35,
330
- :BLOCK => 36,
331
- :ENDBLOCK => 37,
332
- :END => 38,
333
- :EXTENDS => 39,
334
- :TEXT_BLOCK => 40 }
335
-
336
- racc_nt_base = 41
337
-
338
- racc_use_result_var = true
339
-
340
- Racc_arg = [
341
- racc_action_table,
342
- racc_action_check,
343
- racc_action_default,
344
- racc_action_pointer,
345
- racc_goto_table,
346
- racc_goto_check,
347
- racc_goto_default,
348
- racc_goto_pointer,
349
- racc_nt_base,
350
- racc_reduce_table,
351
- racc_token_table,
352
- racc_shift_n,
353
- racc_reduce_n,
354
- racc_use_result_var ]
355
-
356
- Racc_token_to_s_table = [
357
- "$end",
358
- "error",
359
- "IDENTIFIER",
360
- "INTEGER",
361
- "REAL",
362
- "STRING",
363
- "\"(\"",
364
- "\")\"",
365
- "\"*\"",
366
- "\"/\"",
367
- "\"+\"",
368
- "\"-\"",
369
- "OP_EQ",
370
- "OP_NEQ",
371
- "OP_LEQ",
372
- "OP_GEQ",
373
- "\">\"",
374
- "\"<\"",
375
- "NOT",
376
- "AND",
377
- "OR",
378
- "\",\"",
379
- "\":\"",
380
- "\"|\"",
381
- "VAR_OPEN",
382
- "VAR_CLOSE",
383
- "STMT_OPEN",
384
- "IF",
385
- "STMT_CLOSE",
386
- "UNLESS",
387
- "ELSE",
388
- "ENDIF",
389
- "ENDUNLESS",
390
- "FOR",
391
- "IN",
392
- "ENDFOR",
393
- "BLOCK",
394
- "ENDBLOCK",
395
- "END",
396
- "EXTENDS",
397
- "TEXT_BLOCK",
398
- "$start",
399
- "target",
400
- "document",
401
- "primary_expression",
402
- "logical_expression",
403
- "multiplicative_expression",
404
- "additive_expression",
405
- "boolean_expression",
406
- "inverse_expression",
407
- "parameter_list",
408
- "filter",
409
- "filter_list",
410
- "inject_statement",
411
- "if_tag",
412
- "unless_tag",
413
- "else_tag",
414
- "end_if_tag",
415
- "end_unless_tag",
416
- "if_block",
417
- "unless_block",
418
- "for_tag",
419
- "end_for_tag",
420
- "for_block",
421
- "@1",
422
- "block_tag",
423
- "end_block_tag",
424
- "block_block",
425
- "@2",
426
- "generic_block",
427
- "@3",
428
- "@4",
429
- "extends_statement",
430
- "document_component" ]
431
-
432
- Racc_debug_parser = false
433
-
434
- ##### State transition tables end #####
435
-
436
- # reduce 0 omitted
437
-
438
- # reduce 1 omitted
439
-
440
- module_eval(<<'.,.,', 'cadenza.y', 8)
441
- def _reduce_2(val, _values, result)
442
- result = nil
443
- result
444
- end
445
- .,.,
446
-
447
- module_eval(<<'.,.,', 'cadenza.y', 12)
448
- def _reduce_3(val, _values, result)
449
- result = VariableNode.new(val[0].value)
450
- result
451
- end
452
- .,.,
453
-
454
- module_eval(<<'.,.,', 'cadenza.y', 13)
455
- def _reduce_4(val, _values, result)
456
- result = ConstantNode.new(val[0].value)
457
- result
458
- end
459
- .,.,
460
-
461
- module_eval(<<'.,.,', 'cadenza.y', 14)
462
- def _reduce_5(val, _values, result)
463
- result = ConstantNode.new(val[0].value)
464
- result
465
- end
466
- .,.,
467
-
468
- module_eval(<<'.,.,', 'cadenza.y', 15)
469
- def _reduce_6(val, _values, result)
470
- result = ConstantNode.new(val[0].value)
471
- result
472
- end
473
- .,.,
474
-
475
- module_eval(<<'.,.,', 'cadenza.y', 16)
476
- def _reduce_7(val, _values, result)
477
- result = val[1]
478
- result
479
- end
480
- .,.,
481
-
482
- # reduce 8 omitted
483
-
484
- module_eval(<<'.,.,', 'cadenza.y', 21)
485
- def _reduce_9(val, _values, result)
486
- result = OperationNode.new(val[0], "*", val[2])
487
- result
488
- end
489
- .,.,
490
-
491
- module_eval(<<'.,.,', 'cadenza.y', 22)
492
- def _reduce_10(val, _values, result)
493
- result = OperationNode.new(val[0], "/", val[2])
494
- result
495
- end
496
- .,.,
497
-
498
- # reduce 11 omitted
499
-
500
- module_eval(<<'.,.,', 'cadenza.y', 27)
501
- def _reduce_12(val, _values, result)
502
- result = OperationNode.new(val[0], "+", val[2])
503
- result
504
- end
505
- .,.,
506
-
507
- module_eval(<<'.,.,', 'cadenza.y', 28)
508
- def _reduce_13(val, _values, result)
509
- result = OperationNode.new(val[0], "-", val[2])
510
- result
511
- end
512
- .,.,
513
-
514
- # reduce 14 omitted
515
-
516
- module_eval(<<'.,.,', 'cadenza.y', 33)
517
- def _reduce_15(val, _values, result)
518
- result = OperationNode.new(val[0], "==", val[2])
519
- result
520
- end
521
- .,.,
522
-
523
- module_eval(<<'.,.,', 'cadenza.y', 34)
524
- def _reduce_16(val, _values, result)
525
- result = OperationNode.new(val[0], "!=", val[2])
526
- result
527
- end
528
- .,.,
529
-
530
- module_eval(<<'.,.,', 'cadenza.y', 35)
531
- def _reduce_17(val, _values, result)
532
- result = OperationNode.new(val[0], "<=", val[2])
533
- result
534
- end
535
- .,.,
536
-
537
- module_eval(<<'.,.,', 'cadenza.y', 36)
538
- def _reduce_18(val, _values, result)
539
- result = OperationNode.new(val[0], ">=", val[2])
540
- result
541
- end
542
- .,.,
543
-
544
- module_eval(<<'.,.,', 'cadenza.y', 37)
545
- def _reduce_19(val, _values, result)
546
- result = OperationNode.new(val[0], ">", val[2])
547
- result
548
- end
549
- .,.,
550
-
551
- module_eval(<<'.,.,', 'cadenza.y', 38)
552
- def _reduce_20(val, _values, result)
553
- result = OperationNode.new(val[0], "<", val[2])
554
- result
555
- end
556
- .,.,
557
-
558
- # reduce 21 omitted
559
-
560
- module_eval(<<'.,.,', 'cadenza.y', 43)
561
- def _reduce_22(val, _values, result)
562
- result = BooleanInverseNode.new(val[1])
563
- result
564
- end
565
- .,.,
566
-
567
- # reduce 23 omitted
568
-
569
- module_eval(<<'.,.,', 'cadenza.y', 48)
570
- def _reduce_24(val, _values, result)
571
- result = OperationNode.new(val[0], "and", val[2])
572
- result
573
- end
574
- .,.,
575
-
576
- module_eval(<<'.,.,', 'cadenza.y', 49)
577
- def _reduce_25(val, _values, result)
578
- result = OperationNode.new(val[0], "or", val[2])
579
- result
580
- end
581
- .,.,
582
-
583
- module_eval(<<'.,.,', 'cadenza.y', 53)
584
- def _reduce_26(val, _values, result)
585
- result = [val[0]]
586
- result
587
- end
588
- .,.,
589
-
590
- module_eval(<<'.,.,', 'cadenza.y', 54)
591
- def _reduce_27(val, _values, result)
592
- result = val[0].push(val[2])
593
- result
594
- end
595
- .,.,
596
-
597
- module_eval(<<'.,.,', 'cadenza.y', 58)
598
- def _reduce_28(val, _values, result)
599
- result = FilterNode.new(val[0].value)
600
- result
601
- end
602
- .,.,
603
-
604
- module_eval(<<'.,.,', 'cadenza.y', 59)
605
- def _reduce_29(val, _values, result)
606
- result = FilterNode.new(val[0].value, val[2])
607
- result
608
- end
609
- .,.,
610
-
611
- module_eval(<<'.,.,', 'cadenza.y', 63)
612
- def _reduce_30(val, _values, result)
613
- result = [val[0]]
614
- result
615
- end
616
- .,.,
617
-
618
- module_eval(<<'.,.,', 'cadenza.y', 64)
619
- def _reduce_31(val, _values, result)
620
- result = val[0].push(val[2])
621
- result
622
- end
623
- .,.,
624
-
625
- module_eval(<<'.,.,', 'cadenza.y', 69)
626
- def _reduce_32(val, _values, result)
627
- result = InjectNode.new(val[1])
628
- result
629
- end
630
- .,.,
631
-
632
- module_eval(<<'.,.,', 'cadenza.y', 71)
633
- def _reduce_33(val, _values, result)
634
- result = InjectNode.new(val[1], val[3])
635
- result
636
- end
637
- .,.,
638
-
639
- module_eval(<<'.,.,', 'cadenza.y', 74)
640
- def _reduce_34(val, _values, result)
641
- variable = VariableNode.new(val[1].value)
642
- result = InjectNode.new(variable, [], val[2])
643
-
644
- result
645
- end
646
- .,.,
647
-
648
- module_eval(<<'.,.,', 'cadenza.y', 79)
649
- def _reduce_35(val, _values, result)
650
- variable = VariableNode.new(val[1].value)
651
- result = InjectNode.new(variable, val[4], val[2])
652
-
653
- result
654
- end
655
- .,.,
656
-
657
- module_eval(<<'.,.,', 'cadenza.y', 87)
658
- def _reduce_36(val, _values, result)
659
- @stack.push DocumentNode.new
660
- result = val[2]
661
-
662
- result
663
- end
664
- .,.,
665
-
666
- module_eval(<<'.,.,', 'cadenza.y', 95)
667
- def _reduce_37(val, _values, result)
668
- @stack.push DocumentNode.new
669
- result = BooleanInverseNode.new(val[2])
670
-
671
- result
672
- end
673
- .,.,
674
-
675
- module_eval(<<'.,.,', 'cadenza.y', 101)
676
- def _reduce_38(val, _values, result)
677
- @stack.push DocumentNode.new
678
- result
679
- end
680
- .,.,
681
-
682
- # reduce 39 omitted
683
-
684
- # reduce 40 omitted
685
-
686
- module_eval(<<'.,.,', 'cadenza.y', 113)
687
- def _reduce_41(val, _values, result)
688
- @stack.pop; result = IfNode.new(val[0])
689
- result
690
- end
691
- .,.,
692
-
693
- module_eval(<<'.,.,', 'cadenza.y', 114)
694
- def _reduce_42(val, _values, result)
695
- result = IfNode.new(val[0], @stack.pop.children)
696
- result
697
- end
698
- .,.,
699
-
700
- module_eval(<<'.,.,', 'cadenza.y', 117)
701
- def _reduce_43(val, _values, result)
702
- false_children, true_children = @stack.pop.children, @stack.pop.children
703
- result = IfNode.new(val[0], true_children, false_children)
704
-
705
- result
706
- end
707
- .,.,
708
-
709
- module_eval(<<'.,.,', 'cadenza.y', 122)
710
- def _reduce_44(val, _values, result)
711
- false_children, true_children = @stack.pop.children, @stack.pop.children
712
- result = IfNode.new(val[0], true_children, false_children)
713
-
714
- result
715
- end
716
- .,.,
717
-
718
- module_eval(<<'.,.,', 'cadenza.y', 127)
719
- def _reduce_45(val, _values, result)
720
- false_children, true_children = @stack.pop.children, @stack.pop.children
721
- result = IfNode.new(val[0], true_children, false_children)
722
-
723
- result
724
- end
725
- .,.,
726
-
727
- module_eval(<<'.,.,', 'cadenza.y', 133)
728
- def _reduce_46(val, _values, result)
729
- @stack.pop; result = IfNode.new(val[0])
730
- result
731
- end
732
- .,.,
733
-
734
- module_eval(<<'.,.,', 'cadenza.y', 134)
735
- def _reduce_47(val, _values, result)
736
- result = IfNode.new(val[0], @stack.pop.children)
737
- result
738
- end
739
- .,.,
740
-
741
- module_eval(<<'.,.,', 'cadenza.y', 137)
742
- def _reduce_48(val, _values, result)
743
- false_children, true_children = @stack.pop.children, @stack.pop.children
744
- result = IfNode.new(val[0], true_children, false_children)
745
-
746
- result
747
- end
748
- .,.,
749
-
750
- module_eval(<<'.,.,', 'cadenza.y', 142)
751
- def _reduce_49(val, _values, result)
752
- false_children, true_children = @stack.pop.children, @stack.pop.children
753
- result = IfNode.new(val[0], true_children, false_children)
754
-
755
- result
756
- end
757
- .,.,
758
-
759
- module_eval(<<'.,.,', 'cadenza.y', 147)
760
- def _reduce_50(val, _values, result)
761
- false_children, true_children = @stack.pop.children, @stack.pop.children
762
- result = IfNode.new(val[0], true_children, false_children)
763
-
764
- result
765
- end
766
- .,.,
767
-
768
- module_eval(<<'.,.,', 'cadenza.y', 153)
769
- def _reduce_51(val, _values, result)
770
- result = [val[2].value, val[4].value]
771
- result
772
- end
773
- .,.,
774
-
775
- # reduce 52 omitted
776
-
777
- module_eval(<<'.,.,', 'cadenza.y', 164)
778
- def _reduce_53(val, _values, result)
779
- iterator = VariableNode.new(val[0][0])
780
- iterable = VariableNode.new(val[0][1])
781
-
782
- result = ForNode.new(iterator, iterable, [])
783
-
784
- result
785
- end
786
- .,.,
787
-
788
- module_eval(<<'.,.,', 'cadenza.y', 169)
789
- def _reduce_54(val, _values, result)
790
- @stack.push DocumentNode.new
791
- result
792
- end
793
- .,.,
794
-
795
- module_eval(<<'.,.,', 'cadenza.y', 171)
796
- def _reduce_55(val, _values, result)
797
- iterator = VariableNode.new(val[0][0])
798
- iterable = VariableNode.new(val[0][1])
799
-
800
- result = ForNode.new(iterator, iterable, @stack.pop.children)
801
-
802
- result
803
- end
804
- .,.,
805
-
806
- module_eval(<<'.,.,', 'cadenza.y', 179)
807
- def _reduce_56(val, _values, result)
808
- result = val[2].value
809
- result
810
- end
811
- .,.,
812
-
813
- # reduce 57 omitted
814
-
815
- module_eval(<<'.,.,', 'cadenza.y', 188)
816
- def _reduce_58(val, _values, result)
817
- result = BlockNode.new(val[0], [])
818
- result
819
- end
820
- .,.,
821
-
822
- module_eval(<<'.,.,', 'cadenza.y', 189)
823
- def _reduce_59(val, _values, result)
824
- @stack.push DocumentNode.new
825
- result
826
- end
827
- .,.,
828
-
829
- module_eval(<<'.,.,', 'cadenza.y', 189)
830
- def _reduce_60(val, _values, result)
831
- result = BlockNode.new(val[0], @stack.pop.children)
832
- result
833
- end
834
- .,.,
835
-
836
- module_eval(<<'.,.,', 'cadenza.y', 193)
837
- def _reduce_61(val, _values, result)
838
- @stack.push DocumentNode.new
839
- result
840
- end
841
- .,.,
842
-
843
- module_eval(<<'.,.,', 'cadenza.y', 195)
844
- def _reduce_62(val, _values, result)
845
- result = GenericBlockNode.new(val[1].value, @stack.pop.children)
846
- result
847
- end
848
- .,.,
849
-
850
- module_eval(<<'.,.,', 'cadenza.y', 196)
851
- def _reduce_63(val, _values, result)
852
- @stack.push DocumentNode.new
853
- result
854
- end
855
- .,.,
856
-
857
- module_eval(<<'.,.,', 'cadenza.y', 198)
858
- def _reduce_64(val, _values, result)
859
- result = GenericBlockNode.new(val[1].value, @stack.pop.children, val[2])
860
- result
861
- end
862
- .,.,
863
-
864
- module_eval(<<'.,.,', 'cadenza.y', 202)
865
- def _reduce_65(val, _values, result)
866
- result = val[2].value
867
- result
868
- end
869
- .,.,
870
-
871
- module_eval(<<'.,.,', 'cadenza.y', 203)
872
- def _reduce_66(val, _values, result)
873
- result = VariableNode.new(val[2].value)
874
- result
875
- end
876
- .,.,
877
-
878
- module_eval(<<'.,.,', 'cadenza.y', 207)
879
- def _reduce_67(val, _values, result)
880
- result = TextNode.new(val[0].value)
881
- result
882
- end
883
- .,.,
884
-
885
- # reduce 68 omitted
886
-
887
- # reduce 69 omitted
888
-
889
- # reduce 70 omitted
890
-
891
- # reduce 71 omitted
892
-
893
- # reduce 72 omitted
894
-
895
- module_eval(<<'.,.,', 'cadenza.y', 216)
896
- def _reduce_73(val, _values, result)
897
- push_child val[0]
898
- result
899
- end
900
- .,.,
901
-
902
- module_eval(<<'.,.,', 'cadenza.y', 217)
903
- def _reduce_74(val, _values, result)
904
- push_child val[1]
905
- result
906
- end
907
- .,.,
908
-
909
- module_eval(<<'.,.,', 'cadenza.y', 218)
910
- def _reduce_75(val, _values, result)
911
- @stack.first.extends = val[0]
912
- result
913
- end
914
- .,.,
915
-
916
- module_eval(<<'.,.,', 'cadenza.y', 219)
917
- def _reduce_76(val, _values, result)
918
- @stack.first.extends = val[1]
919
- result
920
- end
921
- .,.,
922
-
923
- module_eval(<<'.,.,', 'cadenza.y', 220)
924
- def _reduce_77(val, _values, result)
925
- push_block(val[0])
926
- result
927
- end
928
- .,.,
929
-
930
- module_eval(<<'.,.,', 'cadenza.y', 221)
931
- def _reduce_78(val, _values, result)
932
- push_block(val[1])
933
- result
934
- end
935
- .,.,
936
-
937
- def _reduce_none(val, _values, result)
938
- val[0]
939
- end
940
-
941
- end # class Parser
942
- end # module Cadenza
3
+ class ParseError < Cadenza::Error
4
+ end
5
+
6
+ # The {Parser} class takes all tokens retrieved from it's lexer and forms them
7
+ # into an abstract syntax tree (AST) with a {DocumentNode} at it's root.
8
+ #
9
+ # {Parser} and {RaccParser} are tightly coupled to each other but were separated
10
+ # since Racc tends to generate undocumentable parser classes.
11
+ class Parser < RaccParser
12
+ # @return [Lexer] the lexer object this parser is using to retrieve tokens from
13
+ attr_reader :lexer
14
+
15
+ # creates a new {Parser} with the given options
16
+ # @param [Hash] options
17
+ # @option options [Lexer] :lexer (Cadenza::Lexer.new) the lexer you want this
18
+ # parser to retrieve tokens from.
19
+ # @raise [RuntimeError] if the given lexer doesn't respond to :next_token and :source=
20
+ def initialize(options={})
21
+ @lexer = options.fetch(:lexer, Cadenza::Lexer.new)
22
+
23
+ raise "Lexers passed to the parser must define next_token" unless @lexer.respond_to?(:next_token)
24
+
25
+ raise "Lexers passed to the parser must define source=" unless @lexer.respond_to?(:source=)
26
+ end
27
+
28
+ # takes the given source object and parses tokens from it, the tokens are
29
+ # then constructed into an abstract syntax tree (AST) with a {DocumentNode}
30
+ # at the root. The root node is then returned.
31
+ #
32
+ # @param [String] source the template text to parse
33
+ # @return [DocumentNode] the root node of the parsed AST
34
+ # @raise [ParseError] if the given template does not have a valid syntax
35
+ def parse(source)
36
+ @lexer.source = source
37
+
38
+ @stack = [DocumentNode.new]
39
+
40
+ do_parse
41
+
42
+ @stack.first
43
+ end
44
+
45
+ private
46
+
47
+ # this is a handy method to add a node to the AST properly, it's used in
48
+ # the cadenza.y file
49
+ def push_child(node)
50
+ @stack.last.children.push(node)
51
+ end
52
+
53
+ # this is a handy method to add a block to the AST properly, it's used in
54
+ # the cadenza.y file
55
+ def push_block(block_node)
56
+ @stack.first.add_block(block_node)
57
+ push_child(block_node)
58
+ end
59
+
60
+ # this is the method Racc will call to get the next token in the stream
61
+ def next_token
62
+ @lexer.next_token
63
+ end
64
+
65
+ # this is Racc's callback for a parse error
66
+ def on_error(error_token_id, error_value, value_stack)
67
+ token = token_to_str(error_token_id)
68
+ value = error_value
69
+
70
+ line, column = value ? [value.line, value.column] : [nil, nil]
71
+
72
+ # use the stringified token to try to get as informative and human an error
73
+ # message as possible for us to raise.
74
+ #
75
+ # To contributors: if you get an uninformative error message please let me know so I can improve this!
76
+ msg = case token
77
+ when "$end" then "unexpected end of input"
78
+ else "unexpected token #{value.source.inspect} at line #{line.inspect}, column #{column.inspect}"
79
+ end
80
+
81
+ raise ParseError, msg
82
+ end
83
+ end
84
+ end