opulent 1.5.5 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CODE_OF_CONDUCT.md +13 -0
  3. data/{LICENSE → LICENSE.md} +0 -0
  4. data/bin/opulent +0 -0
  5. data/lib/opulent.rb +10 -10
  6. data/lib/opulent/compiler.rb +15 -9
  7. data/lib/opulent/compiler/buffer.rb +123 -62
  8. data/lib/opulent/compiler/comment.rb +3 -4
  9. data/lib/opulent/compiler/control.rb +20 -26
  10. data/lib/opulent/compiler/define.rb +88 -36
  11. data/lib/opulent/compiler/doctype.rb +20 -22
  12. data/lib/opulent/compiler/eval.rb +23 -2
  13. data/lib/opulent/compiler/filter.rb +4 -5
  14. data/lib/opulent/compiler/node.rb +18 -12
  15. data/lib/opulent/compiler/root.rb +4 -5
  16. data/lib/opulent/compiler/text.rb +7 -2
  17. data/lib/opulent/compiler/yield.rb +2 -3
  18. data/lib/opulent/engine.rb +21 -20
  19. data/lib/opulent/exec.rb +21 -63
  20. data/lib/opulent/logger.rb +230 -3
  21. data/lib/opulent/parser.rb +14 -76
  22. data/lib/opulent/parser/comment.rb +45 -34
  23. data/lib/opulent/parser/control.rb +132 -111
  24. data/lib/opulent/parser/define.rb +15 -12
  25. data/lib/opulent/parser/doctype.rb +15 -15
  26. data/lib/opulent/parser/eval.rb +16 -6
  27. data/lib/opulent/parser/expression.rb +87 -84
  28. data/lib/opulent/parser/filter.rb +31 -25
  29. data/lib/opulent/parser/include.rb +38 -42
  30. data/lib/opulent/parser/node.rb +136 -118
  31. data/lib/opulent/parser/root.rb +24 -18
  32. data/lib/opulent/parser/text.rb +150 -123
  33. data/lib/opulent/parser/yield.rb +23 -23
  34. data/lib/opulent/settings.rb +70 -51
  35. data/lib/opulent/tokens.rb +17 -15
  36. data/lib/opulent/utils.rb +5 -4
  37. data/lib/opulent/version.rb +1 -1
  38. metadata +4 -43
  39. data/.libold/opulent.rb +0 -96
  40. data/.libold/opulent/context.rb +0 -80
  41. data/.libold/opulent/engine.rb +0 -88
  42. data/.libold/opulent/filter.rb +0 -101
  43. data/.libold/opulent/logger.rb +0 -67
  44. data/.libold/opulent/nodes.rb +0 -13
  45. data/.libold/opulent/nodes/block.rb +0 -29
  46. data/.libold/opulent/nodes/comment.rb +0 -35
  47. data/.libold/opulent/nodes/control.rb +0 -230
  48. data/.libold/opulent/nodes/define.rb +0 -42
  49. data/.libold/opulent/nodes/eval.rb +0 -41
  50. data/.libold/opulent/nodes/expression.rb +0 -28
  51. data/.libold/opulent/nodes/filter.rb +0 -70
  52. data/.libold/opulent/nodes/helper.rb +0 -69
  53. data/.libold/opulent/nodes/node.rb +0 -101
  54. data/.libold/opulent/nodes/root.rb +0 -62
  55. data/.libold/opulent/nodes/text.rb +0 -54
  56. data/.libold/opulent/nodes/theme.rb +0 -36
  57. data/.libold/opulent/parser.rb +0 -252
  58. data/.libold/opulent/parser/block.rb +0 -70
  59. data/.libold/opulent/parser/comment.rb +0 -32
  60. data/.libold/opulent/parser/control.rb +0 -83
  61. data/.libold/opulent/parser/define.rb +0 -39
  62. data/.libold/opulent/parser/eval.rb +0 -33
  63. data/.libold/opulent/parser/expression.rb +0 -350
  64. data/.libold/opulent/parser/filter.rb +0 -41
  65. data/.libold/opulent/parser/node.rb +0 -232
  66. data/.libold/opulent/parser/root.rb +0 -96
  67. data/.libold/opulent/parser/text.rb +0 -114
  68. data/.libold/opulent/parser/theme.rb +0 -36
  69. data/.libold/opulent/preprocessor.rb +0 -102
  70. data/.libold/opulent/runtime.rb +0 -144
  71. data/.libold/opulent/template.rb +0 -43
  72. data/.libold/opulent/tokens.rb +0 -276
  73. data/.libold/opulent/version.rb +0 -5
  74. data/.travis.yml +0 -4
  75. data/benchmark/benchmark.rb +0 -57
  76. data/benchmark/cases/node/node.haml +0 -7
  77. data/benchmark/cases/node/node.op +0 -7
  78. data/benchmark/cases/node/node.slim +0 -7
@@ -14,21 +14,24 @@ module Opulent
14
14
  # @param nodes [Array] Parent node to which we append to
15
15
  #
16
16
  def define(parent, indent)
17
- if(match = accept :def)
18
- # Process data
19
- name = accept(:node, :*).to_sym
17
+ return unless accept(:def)
20
18
 
21
- # Create node
22
- definition = [:def, name, {parameters: attributes}, [], indent]
19
+ # Definition parent check
20
+ Logger.error :parse, @code, @i, @j, :definition if parent[@type] != :root
23
21
 
24
- # Set definition as root node and let the parser know that we're inside
25
- # a definition. This is used because inside definitions we do not process
26
- # nodes (we do not check if they are have a definition or not).
27
- root definition, indent
22
+ # Process data
23
+ name = accept(:node, :*).to_sym
28
24
 
29
- # Add to parent
30
- @definitions[name] = definition
31
- end
25
+ # Create node
26
+ definition = [:def, name, { parameters: attributes }, [], indent]
27
+
28
+ # Set definition as root node and let the parser know that we're inside
29
+ # a definition. This is used because inside definitions we do not
30
+ # process nodes (we do not check if they are have a definition or not).
31
+ root definition, indent
32
+
33
+ # Add to parent
34
+ @definitions[name] = definition
32
35
  end
33
36
  end
34
37
  end
@@ -1,15 +1,15 @@
1
- # @Opulent
2
- module Opulent
3
- # @Parser
4
- class Parser
5
- # Match one line or multiline comments
6
- #
7
- def doctype(parent, indent)
8
- if (accept :doctype)
9
- buffer = accept(:line_feed)
10
-
11
- parent[@children] << [:doctype, buffer.strip.to_sym, {}, nil, indent]
12
- end
13
- end
14
- end
15
- end
1
+ # @Opulent
2
+ module Opulent
3
+ # @Parser
4
+ class Parser
5
+ # Match one line or multiline comments
6
+ #
7
+ def doctype(parent, indent)
8
+ return unless accept :doctype
9
+
10
+ buffer = accept(:line_feed)
11
+
12
+ parent[@children] << [:doctype, buffer.strip.to_sym, {}, nil, indent]
13
+ end
14
+ end
15
+ end
@@ -6,16 +6,26 @@ module Opulent
6
6
  #
7
7
  def evaluate(parent, indent)
8
8
  # Accept eval or multiline eval syntax and return a new node,
9
- if (evaluate = accept(:eval))
10
- eval_node = [:evaluate, evaluate[1..-1].strip, {}, nil, indent]
11
- elsif (evaluate = accept(:eval_multiline))
9
+ return unless accept :eval
10
+
11
+ multiline = accept(:text)
12
+
13
+ if multiline
14
+ # Get first evaluation line
15
+ evaluate_code = accept(:line_feed) || ''
16
+
12
17
  # Get all the lines which are more indented than the current one
13
- eval_node = [:evaluate, evaluate[1..-1].strip, {}, nil, indent]
14
- eval_node[@value] += accept(:newline) || ""
18
+ eval_node = [:evaluate, evaluate_code.strip, {}, nil, indent]
19
+ eval_node[@value] += accept(:newline) || ''
15
20
  eval_node[@value] += get_indented_lines(indent)
21
+ else
22
+ evaluate_code = accept(:line_feed) || ''
23
+ eval_node = [:evaluate, evaluate_code.strip, {}, [], indent]
24
+
25
+ root eval_node, indent
16
26
  end
17
27
 
18
- parent[@children] << eval_node if eval_node
28
+ parent[@children] << eval_node
19
29
  end
20
30
  end
21
31
  end
@@ -5,46 +5,50 @@ module Opulent
5
5
  # Check if the parser matches an expression node
6
6
  #
7
7
  def expression(allow_assignments = true, wrapped = true, whitespace = true)
8
- buffer = ""
8
+ buffer = ''
9
9
 
10
10
  # Build a ruby expression out of accepted literals
11
- while (term = (whitespace ? accept(:whitespace) : nil) ||
12
- modifier ||
13
- identifier ||
14
- method_call ||
15
- paranthesis ||
16
- array ||
17
- hash ||
18
- symbol ||
19
- percent ||
11
+ while (term = (whitespace ? accept(:whitespace) : nil) ||
12
+ modifier ||
13
+ identifier ||
14
+ method_call ||
15
+ paranthesis ||
16
+ array ||
17
+ hash ||
18
+ symbol ||
19
+ percent ||
20
20
  primary_term)
21
21
  buffer += term
22
22
 
23
23
  # Accept operations which have a right term and raise an error if
24
24
  # we have an unfinished expression such as "a +", "b - 1 >" and other
25
25
  # expressions following the same pattern
26
- if wrapped && (op = operation || (allow_assignments ? accept_stripped(:exp_assignment) : nil))
26
+ if wrapped && (op = operation ||
27
+ (allow_assignments ? accept_stripped(:exp_assignment) : nil))
28
+
27
29
  buffer += op
28
30
  if (right_term = expression(allow_assignments, wrapped)).nil?
29
- error :expression
31
+ Logger.error :parse, @code, @i, @j, :expression
30
32
  else
31
33
  buffer += right_term[@value]
32
34
  end
33
- elsif (conditional = ternary_operator allow_assignments, wrapped)
34
- buffer += conditional
35
+ elsif (op = array || op = method_call || op = ternary_operator(allow_assignments, wrapped))
36
+ p buffer
37
+ p op
38
+ buffer += op
35
39
  end
36
40
 
37
41
  # Do not continue if the expression has whitespace method calls in
38
42
  # an unwrapped context because this will confuse the parser
39
43
  unless buffer.strip.empty?
40
- break unless wrapped || lookahead(:exp_identifier_lookahead).nil?
44
+ break if lookahead(:exp_identifier_lookahead).nil?
41
45
  end
42
46
  end
43
47
 
44
48
  if buffer.strip.empty?
45
- return undo buffer
49
+ undo buffer
46
50
  else
47
- return [:expression, buffer.strip, {}]
51
+ [:expression, buffer.strip, {}]
48
52
  end
49
53
  end
50
54
 
@@ -55,12 +59,12 @@ module Opulent
55
59
  # [array_elements]
56
60
  #
57
61
  def array
58
- if (buffer = accept :square_bracket)
59
- accept_newline
60
- buffer += array_elements
61
- accept_newline
62
- buffer += accept :'[', :*
63
- end
62
+ return unless (buffer = accept :square_bracket)
63
+ accept_newline
64
+ buffer += array_elements
65
+ accept_newline
66
+ buffer += accept :'[', :*
67
+ buffer
64
68
  end
65
69
 
66
70
  # Recursively gather expressions separated by a comma and add them to the
@@ -75,7 +79,7 @@ module Opulent
75
79
  buffer += term[@value]
76
80
  # If there is an array_terminator ",", recursively gather the next
77
81
  # array element into the buffer
78
- if (terminator = accept_stripped :comma) then
82
+ if (terminator = accept_stripped :comma)
79
83
  accept_newline
80
84
  buffer += array_elements terminator
81
85
  end
@@ -83,11 +87,9 @@ module Opulent
83
87
 
84
88
  # Array ended prematurely with a trailing comma, therefore the current
85
89
  # parsing process will stop
86
- if buffer.strip[-1] == ','
87
- error :array_elements_terminator
88
- end
90
+ error :array_elements_terminator if buffer.strip[-1] == ','
89
91
 
90
- return buffer
92
+ buffer
91
93
  end
92
94
 
93
95
  # Check if it's possible to parse a ruby hash literal. First, try to see
@@ -97,12 +99,12 @@ module Opulent
97
99
  # { hash_elements }
98
100
  #
99
101
  def hash
100
- if (buffer = accept :curly_bracket)
101
- accept_newline
102
- buffer += hash_elements
103
- accept_newline
104
- buffer += accept :'{', :*
105
- end
102
+ return unless (buffer = accept :curly_bracket)
103
+ accept_newline
104
+ buffer += hash_elements
105
+ accept_newline
106
+ buffer += accept :'{', :*
107
+ buffer
106
108
  end
107
109
 
108
110
  # Recursively gather expression attributions separated by a comma and add
@@ -113,7 +115,7 @@ module Opulent
113
115
  # @param buffer [String] Accumulator for the hash elements
114
116
  #
115
117
  def hash_elements(buffer = '')
116
- value = Proc.new do
118
+ value = proc do
117
119
  # Get the value associated to the current hash key
118
120
  if (exp = expression)
119
121
  buffer += exp[@value]
@@ -123,7 +125,7 @@ module Opulent
123
125
 
124
126
  # If there is an hash_terminator ",", recursively gather the next
125
127
  # array element into the buffer
126
- if (terminator = accept_stripped :comma) then
128
+ if (terminator = accept_stripped :comma)
127
129
  accept_newline
128
130
  buffer += hash_elements terminator
129
131
  end
@@ -139,7 +141,7 @@ module Opulent
139
141
  value[]
140
142
  elsif (exp = expression false)
141
143
  buffer += exp[@value]
142
- if(assign = accept_stripped :hash_assignment)
144
+ if (assign = accept_stripped :hash_assignment)
143
145
  buffer += assign
144
146
  value[]
145
147
  else
@@ -149,31 +151,28 @@ module Opulent
149
151
 
150
152
  # Array ended prematurely with a trailing comma, therefore the current
151
153
  # parsing process will stop
152
- if buffer.strip[-1] == ','
153
- error :hash_elements_terminator
154
- end
154
+ error :hash_elements_terminator if buffer.strip[-1] == ','
155
155
 
156
- return buffer
156
+ buffer
157
157
  end
158
158
 
159
159
  # Accept a ruby identifier such as a class, module, method or variable
160
160
  #
161
161
  def identifier
162
- if (buffer = accept :exp_identifier)
163
- if (args = call)
164
- buffer += args
165
- end
166
- return buffer
162
+ return unless (buffer = accept :exp_identifier)
163
+ if (args = call)
164
+ buffer += args
167
165
  end
166
+ buffer
168
167
  end
169
168
 
170
169
  # Check if it's possible to parse a ruby paranthesis expression wrapper.
171
170
  #
172
171
  def paranthesis
173
- if (buffer = accept :round_bracket)
174
- buffer += expression[@value]
175
- buffer += accept_stripped :'(', :*
176
- end
172
+ return unless (buffer = accept :round_bracket)
173
+ buffer += expression[@value]
174
+ buffer += accept_stripped :'(', :*
175
+ buffer
177
176
  end
178
177
 
179
178
  # Check if it's possible to parse a ruby call literal. First, try to see
@@ -183,10 +182,11 @@ module Opulent
183
182
  # ( call_elements )
184
183
  #
185
184
  def call
186
- if (buffer = accept :round_bracket)
187
- buffer += call_elements
188
- buffer += accept_stripped :'(', :*
189
- end
185
+ return unless (buffer = accept :round_bracket)
186
+
187
+ buffer += call_elements
188
+ buffer += accept_stripped :'(', :*
189
+ buffer
190
190
  end
191
191
 
192
192
  # Recursively gather expression attributes separated by a comma and add
@@ -215,13 +215,13 @@ module Opulent
215
215
 
216
216
  # If there is an comma ",", recursively gather the next
217
217
  # array element into the buffer
218
- if (terminator = accept_stripped :comma) then
218
+ if (terminator = accept_stripped :comma)
219
219
  buffer += call_elements terminator
220
220
  end
221
221
  elsif (exp = expression(true))
222
222
  buffer += exp[@value]
223
223
 
224
- if(assign = accept_stripped :hash_assignment)
224
+ if (assign = accept_stripped :hash_assignment)
225
225
  buffer += assign
226
226
 
227
227
  # Get the value associated to the current hash key
@@ -234,7 +234,7 @@ module Opulent
234
234
 
235
235
  # If there is an comma ",", recursively gather the next
236
236
  # array element into the buffer
237
- if (terminator = accept_stripped :comma) then
237
+ if (terminator = accept_stripped :comma)
238
238
  buffer += call_elements terminator
239
239
  end
240
240
  end
@@ -248,14 +248,13 @@ module Opulent
248
248
  # :symbol
249
249
  #
250
250
  def symbol
251
- if (colon = accept :colon)
252
- return undo colon if lookahead(:whitespace)
251
+ return unless (colon = accept :colon)
252
+ return undo colon if lookahead(:whitespace)
253
253
 
254
- if (exp = expression).nil?
255
- error :symbol
256
- else
257
- colon + exp[@value]
258
- end
254
+ if (exp = expression).nil?
255
+ error :symbol
256
+ else
257
+ colon + exp[@value]
259
258
  end
260
259
  end
261
260
 
@@ -268,30 +267,29 @@ module Opulent
268
267
  accept(:exp_context) || accept(:exp_module)
269
268
  end
270
269
 
271
-
272
270
  # Accept a ruby percentage operator for arrays of strings, symbols and
273
271
  # simple escaped strings
274
272
  #
275
273
  # %w(word1 word2 word3)
276
274
  #
277
275
  def percent
278
- if (buffer = accept_stripped :exp_percent)
279
- match_start = buffer[-1]
280
- match_name = :"percent#{match_start}"
281
-
282
- unless Tokens[match_name]
283
- match_end = Tokens.bracket(match_start) || match_start
276
+ return unless (buffer = accept_stripped :exp_percent)
277
+ match_start = buffer[-1]
278
+ match_name = :"percent#{match_start}"
284
279
 
285
- match_inner = "\\#{match_start}"
286
- match_inner += "\\#{match_end}" unless match_end == match_start
280
+ unless Tokens[match_name]
281
+ match_end = Tokens.bracket(match_start) || match_start
287
282
 
288
- pattern = /(((?:[^#{match_inner}\\]|\\.)*?)#{'\\' + match_end})/
283
+ match_inner = "\\#{match_start}"
284
+ match_inner += "\\#{match_end}" unless match_end == match_start
289
285
 
290
- Tokens[match_name] = pattern
291
- end
286
+ pattern = /(((?:[^#{match_inner}\\]|\\.)*?)#{'\\' + match_end})/
292
287
 
293
- buffer += accept match_name
288
+ Tokens[match_name] = pattern
294
289
  end
290
+
291
+ buffer += accept match_name
292
+ buffer
295
293
  end
296
294
 
297
295
  # Accept any primary term and return it without the leading whitespace to
@@ -306,12 +304,12 @@ module Opulent
306
304
  # /.*/
307
305
  #
308
306
  def primary_term
309
- accept_stripped(:exp_string) ||
310
- accept_stripped(:exp_fixnum) ||
311
- accept_stripped(:exp_double) ||
312
- accept_stripped(:exp_nil) ||
313
- accept_stripped(:exp_regex) ||
314
- accept_stripped(:exp_boolean)
307
+ accept_stripped(:exp_string) ||
308
+ accept_stripped(:exp_fixnum) ||
309
+ accept_stripped(:exp_double) ||
310
+ accept_stripped(:exp_nil) ||
311
+ accept_stripped(:exp_regex) ||
312
+ accept_stripped(:exp_boolean)
315
313
  end
316
314
 
317
315
  # Accept an operation between two or more expressions
@@ -323,7 +321,12 @@ module Opulent
323
321
  # Accept a ruby method call modifier
324
322
  #
325
323
  def method_call
326
- accept(:exp_method_call)
324
+ return unless (method_code = accept(:exp_method_call))
325
+
326
+ argument = call
327
+ method_code += argument if argument
328
+
329
+ method_code
327
330
  end
328
331
 
329
332
  # Accept ternary operator syntax
@@ -1,25 +1,31 @@
1
- # @Opulent
2
- module Opulent
3
- # @Parser
4
- class Parser
5
- # Check if we match an compile time filter
6
- #
7
- # :filter
8
- #
9
- # @param parent [Node] Parent node to which we append the element
10
- #
11
- def filter(parent, indent)
12
- if (filter_name = accept :filter)
13
- # Get element attributes
14
- atts = attributes(shorthand_attributes) || {}
15
-
16
- # Accept inline text or multiline text feed as first child
17
- error :fiter unless accept(:line_feed).strip.empty?
18
-
19
- # Get everything under the filter and set it as the node value
20
- # and create a new node and set its extension
21
- parent[@children] << [:filter, filter_name[1..-1].to_sym, atts, get_indented_lines(indent), indent]
22
- end
23
- end
24
- end
25
- end
1
+ # @Opulent
2
+ module Opulent
3
+ # @Parser
4
+ class Parser
5
+ # Check if we match an compile time filter
6
+ #
7
+ # :filter
8
+ #
9
+ # @param parent [Node] Parent node to which we append the element
10
+ #
11
+ def filter(parent, indent)
12
+ return unless (filter_name = accept :filter)
13
+
14
+ # Get element attributes
15
+ atts = attributes(shorthand_attributes) || {}
16
+
17
+ # Accept inline text or multiline text feed as first child
18
+ error :fiter unless accept(:line_feed).strip.empty?
19
+
20
+ # Get everything under the filter and set it as the node value
21
+ # and create a new node and set its extension
22
+ parent[@children] << [
23
+ :filter,
24
+ filter_name[1..-1].to_sym,
25
+ atts,
26
+ get_indented_lines(indent),
27
+ indent
28
+ ]
29
+ end
30
+ end
31
+ end