curlybars 1.16.1.pre.3 → 1.17.0.pre
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.
- checksums.yaml +4 -4
- data/exe/curlybars-indent-lint +2 -0
- data/lib/curlybars/configuration.rb +5 -1
- data/lib/curlybars/dependency_tracker.rb +2 -0
- data/lib/curlybars/error/base.rb +2 -0
- data/lib/curlybars/error/compile.rb +2 -0
- data/lib/curlybars/error/lex.rb +2 -0
- data/lib/curlybars/error/parse.rb +4 -0
- data/lib/curlybars/error/presenter/not_found.rb +2 -0
- data/lib/curlybars/error/render.rb +2 -0
- data/lib/curlybars/error/validate.rb +2 -0
- data/lib/curlybars/generic.rb +2 -0
- data/lib/curlybars/lexer.rb +159 -53
- data/lib/curlybars/method_whitelist.rb +2 -0
- data/lib/curlybars/node/block_helper_else.rb +2 -0
- data/lib/curlybars/node/each_else.rb +3 -0
- data/lib/curlybars/node/if_else.rb +2 -0
- data/lib/curlybars/node/item.rb +2 -0
- data/lib/curlybars/node/literal.rb +2 -0
- data/lib/curlybars/node/option.rb +2 -0
- data/lib/curlybars/node/output.rb +2 -0
- data/lib/curlybars/node/partial.rb +2 -0
- data/lib/curlybars/node/path.rb +2 -5
- data/lib/curlybars/node/root.rb +2 -0
- data/lib/curlybars/node/sub_expression.rb +10 -11
- data/lib/curlybars/node/template.rb +2 -5
- data/lib/curlybars/node/text.rb +2 -0
- data/lib/curlybars/node/unless_else.rb +2 -0
- data/lib/curlybars/node/variable.rb +2 -0
- data/lib/curlybars/node/with_else.rb +3 -0
- data/lib/curlybars/parser.rb +247 -146
- data/lib/curlybars/partial_presenter.rb +2 -0
- data/lib/curlybars/path_finder.rb +3 -2
- data/lib/curlybars/position.rb +2 -0
- data/lib/curlybars/presenter.rb +2 -0
- data/lib/curlybars/processor/token_factory.rb +3 -1
- data/lib/curlybars/railtie.rb +2 -0
- data/lib/curlybars/rendering_support.rb +16 -1
- data/lib/curlybars/safe_buffer.rb +2 -0
- data/lib/curlybars/serialization_guard.rb +2 -0
- data/lib/curlybars/template_handler.rb +2 -0
- data/lib/curlybars/token.rb +28 -0
- data/lib/curlybars/validation_context.rb +2 -0
- data/lib/curlybars/version.rb +1 -1
- data/lib/curlybars/visitor.rb +2 -0
- data/lib/curlybars.rb +16 -10
- metadata +3 -33
- data/lib/curlybars/node/boolean.rb +0 -24
- data/lib/curlybars/node/string.rb +0 -24
- data/lib/curlybars/processor/tilde.rb +0 -34
data/lib/curlybars/parser.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'curlybars/token'
|
|
2
4
|
|
|
3
5
|
require 'curlybars/node/root'
|
|
4
6
|
require 'curlybars/node/template'
|
|
@@ -18,192 +20,290 @@ require 'curlybars/node/output'
|
|
|
18
20
|
require 'curlybars/node/sub_expression'
|
|
19
21
|
|
|
20
22
|
module Curlybars
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
# Recursive descent parser turning a token stream from Curlybars::Lexer into
|
|
24
|
+
# an AST of Curlybars::Node objects.
|
|
25
|
+
#
|
|
26
|
+
# Grammar (tokens in upper case, `?` optional, `*` zero or more):
|
|
27
|
+
#
|
|
28
|
+
# root := template? EOS
|
|
29
|
+
# template := item+
|
|
30
|
+
# item := TEXT
|
|
31
|
+
# | START HASH block
|
|
32
|
+
# | START GT path option* END
|
|
33
|
+
# | START path expression* option* END
|
|
34
|
+
# | START value END
|
|
35
|
+
# block := (IF | UNLESS) expression END body(IF | UNLESS)
|
|
36
|
+
# | (EACH | WITH) (path | subexpression) END body(EACH | WITH)
|
|
37
|
+
# | path expression* option* END body(path)
|
|
38
|
+
# body(close) := template? (START ELSE END template?)? START SLASH close END
|
|
39
|
+
# option := KEY expression
|
|
40
|
+
# expression := value | path | subexpression
|
|
41
|
+
# value := LITERAL | VARIABLE
|
|
42
|
+
# path := PATH
|
|
43
|
+
# subexpression := LPAREN path expression* option* RPAREN
|
|
44
|
+
#
|
|
45
|
+
# Inside a block body, a template ends at `{{/` or `{{else`, which is why the
|
|
46
|
+
# parser looks one token past START before deciding to parse an item.
|
|
47
|
+
class Parser
|
|
48
|
+
class Error < StandardError
|
|
49
|
+
# The token that the parser could not accept.
|
|
50
|
+
attr_reader :current
|
|
51
|
+
|
|
52
|
+
def initialize(current)
|
|
53
|
+
@current = current
|
|
54
|
+
super("Unexpected token #{current.type}")
|
|
55
|
+
end
|
|
30
56
|
end
|
|
31
57
|
|
|
32
|
-
|
|
33
|
-
|
|
58
|
+
class ParseDepthExceeded < Error
|
|
59
|
+
# The depth limit that the template exceeded.
|
|
60
|
+
attr_reader :limit
|
|
34
61
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
Node::BlockHelperElse.new(
|
|
39
|
-
helper,
|
|
40
|
-
arguments || [],
|
|
41
|
-
options || [],
|
|
42
|
-
template || VOID,
|
|
43
|
-
VOID,
|
|
44
|
-
helperclose,
|
|
45
|
-
pos(0)
|
|
46
|
-
)
|
|
62
|
+
def initialize(current, limit)
|
|
63
|
+
@limit = limit
|
|
64
|
+
super(current)
|
|
47
65
|
end
|
|
66
|
+
end
|
|
48
67
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
helper_template || VOID,
|
|
59
|
-
else_template || VOID,
|
|
60
|
-
helperclose,
|
|
61
|
-
pos(0)
|
|
62
|
-
)
|
|
63
|
-
end
|
|
68
|
+
# Blocks and subexpressions nested deeper than the depth limit are rejected,
|
|
69
|
+
# so that recursion in the parser, in Node#validate and Node#compile, and in
|
|
70
|
+
# Ruby compiling the generated code cannot exhaust the stack. The last one
|
|
71
|
+
# is the tightest, and it is bounded by the machine stack rather than the VM
|
|
72
|
+
# stack: with a 1 MB machine stack, about 72 nested #each blocks are enough
|
|
73
|
+
# to raise SystemStackError, and each extra MB allows roughly 70 more
|
|
74
|
+
# levels. Threads get 1 MB by default (RUBY_THREAD_MACHINE_STACK_SIZE),
|
|
75
|
+
# while the main thread gets the process stack size limit (`ulimit -s`).
|
|
76
|
+
DEFAULT_PARSE_DEPTH_LIMIT = 50
|
|
64
77
|
|
|
65
|
-
|
|
66
|
-
Node::BlockHelperElse.new(
|
|
67
|
-
helper,
|
|
68
|
-
arguments || [],
|
|
69
|
-
options || [],
|
|
70
|
-
VOID,
|
|
71
|
-
VOID,
|
|
72
|
-
helper,
|
|
73
|
-
pos(0)
|
|
74
|
-
)
|
|
75
|
-
end
|
|
78
|
+
EXPRESSION_START = [:LITERAL, :VARIABLE, :PATH, :LPAREN].freeze
|
|
76
79
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
def self.parse(tokens, depth_limit: DEFAULT_PARSE_DEPTH_LIMIT)
|
|
81
|
+
new(tokens, depth_limit: depth_limit).parse
|
|
82
|
+
end
|
|
80
83
|
|
|
81
|
-
|
|
82
|
-
.template?
|
|
83
|
-
START SLASH IF END') do |expression, if_template|
|
|
84
|
-
Node::IfElse.new(expression, if_template || VOID, VOID)
|
|
85
|
-
end
|
|
84
|
+
attr_reader :depth_limit
|
|
86
85
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
.template?
|
|
91
|
-
START SLASH IF END') do |expression, if_template, else_template|
|
|
92
|
-
Node::IfElse.new(expression, if_template || VOID, else_template || VOID)
|
|
93
|
-
end
|
|
86
|
+
# The deepest nesting of blocks and subexpressions seen so far, which is
|
|
87
|
+
# what the depth limit applies to.
|
|
88
|
+
attr_reader :max_depth
|
|
94
89
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
90
|
+
def initialize(tokens, depth_limit: DEFAULT_PARSE_DEPTH_LIMIT)
|
|
91
|
+
@tokens = tokens
|
|
92
|
+
@depth_limit = depth_limit
|
|
93
|
+
@index = 0
|
|
94
|
+
@depth = 0
|
|
95
|
+
@max_depth = 0
|
|
96
|
+
end
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
def parse
|
|
99
|
+
template = parse_template(in_block: false)
|
|
100
|
+
expect(:EOS)
|
|
101
|
+
if template
|
|
102
|
+
Node::Root.new(template, template.position)
|
|
103
|
+
else
|
|
104
|
+
Node::Root.new(VOID, StreamPosition::NULL)
|
|
107
105
|
end
|
|
106
|
+
end
|
|
108
107
|
|
|
109
|
-
|
|
110
|
-
.template?
|
|
111
|
-
START SLASH EACH END') do |path, each_template|
|
|
112
|
-
Node::EachElse.new(path, each_template || VOID, VOID, pos(0))
|
|
113
|
-
end
|
|
108
|
+
private
|
|
114
109
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
110
|
+
def parse_template(in_block:)
|
|
111
|
+
first_index = @index
|
|
112
|
+
items = []
|
|
113
|
+
items << Node::Item.new(parse_item) while item_start?(in_block)
|
|
114
|
+
Node::Template.new(items, span(first_index)) unless items.empty?
|
|
115
|
+
end
|
|
120
116
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
Node::EachElse.new(path, each_template || VOID, else_template || VOID, pos(0))
|
|
117
|
+
def item_start?(in_block)
|
|
118
|
+
case peek.type
|
|
119
|
+
when :TEXT then true
|
|
120
|
+
when :START then !(in_block && [:SLASH, :ELSE].include?(peek(1).type))
|
|
121
|
+
else false
|
|
127
122
|
end
|
|
123
|
+
end
|
|
128
124
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
125
|
+
def parse_item
|
|
126
|
+
return Node::Text.new(advance.value) if peek.type == :TEXT
|
|
127
|
+
|
|
128
|
+
first_index = @index
|
|
129
|
+
expect(:START)
|
|
130
|
+
|
|
131
|
+
case peek.type
|
|
132
|
+
when :HASH
|
|
133
|
+
advance
|
|
134
|
+
parse_block(first_index)
|
|
135
|
+
when :GT
|
|
136
|
+
advance
|
|
137
|
+
path = parse_path
|
|
138
|
+
options = parse_options
|
|
139
|
+
expect(:END)
|
|
140
|
+
Node::Partial.new(path, options, span(first_index))
|
|
141
|
+
when :PATH
|
|
142
|
+
helper = parse_path
|
|
143
|
+
arguments = parse_expressions
|
|
144
|
+
options = parse_options
|
|
145
|
+
expect(:END)
|
|
146
|
+
Node::BlockHelperElse.new(helper, arguments, options, VOID, VOID, helper, span(first_index))
|
|
147
|
+
when :LITERAL
|
|
148
|
+
literal = parse_literal
|
|
149
|
+
expect(:END)
|
|
150
|
+
Node::Output.new(literal)
|
|
151
|
+
when :VARIABLE
|
|
152
|
+
variable = parse_variable
|
|
153
|
+
expect(:END)
|
|
154
|
+
Node::Output.new(variable)
|
|
155
|
+
else
|
|
156
|
+
raise Error, peek
|
|
135
157
|
end
|
|
158
|
+
end
|
|
136
159
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
160
|
+
def parse_block(first_index)
|
|
161
|
+
case peek.type
|
|
162
|
+
when :IF, :UNLESS
|
|
163
|
+
keyword = advance.type
|
|
164
|
+
expression = parse_expression
|
|
165
|
+
expect(:END)
|
|
166
|
+
template, else_template = parse_body(first_index) { expect(keyword) }
|
|
167
|
+
node_class = keyword == :IF ? Node::IfElse : Node::UnlessElse
|
|
168
|
+
node_class.new(expression, template || VOID, else_template || VOID)
|
|
169
|
+
when :EACH, :WITH
|
|
170
|
+
keyword = advance.type
|
|
171
|
+
path = peek.type == :LPAREN ? parse_subexpression : parse_path
|
|
172
|
+
expect(:END)
|
|
173
|
+
template, else_template = parse_body(first_index) { expect(keyword) }
|
|
174
|
+
node_class = keyword == :EACH ? Node::EachElse : Node::WithElse
|
|
175
|
+
node_class.new(path, template || VOID, else_template || VOID, span(first_index))
|
|
176
|
+
when :PATH
|
|
177
|
+
helper = parse_path
|
|
178
|
+
arguments = parse_expressions
|
|
179
|
+
options = parse_options
|
|
180
|
+
expect(:END)
|
|
181
|
+
template, else_template, helperclose = parse_body(first_index) { parse_path }
|
|
182
|
+
Node::BlockHelperElse.new(
|
|
183
|
+
helper,
|
|
184
|
+
arguments,
|
|
185
|
+
options,
|
|
186
|
+
template || VOID,
|
|
187
|
+
else_template || VOID,
|
|
188
|
+
helperclose,
|
|
189
|
+
span(first_index)
|
|
190
|
+
)
|
|
191
|
+
else
|
|
192
|
+
raise Error, peek
|
|
141
193
|
end
|
|
194
|
+
end
|
|
142
195
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
196
|
+
# Parses a block body up to and including its closing tag. The block
|
|
197
|
+
# parses the name that follows `{{/`, which is returned as the third
|
|
198
|
+
# element.
|
|
199
|
+
def parse_body(first_index)
|
|
200
|
+
nested(first_index) do
|
|
201
|
+
template = parse_template(in_block: true)
|
|
202
|
+
expect(:START)
|
|
203
|
+
|
|
204
|
+
if peek.type == :ELSE
|
|
205
|
+
advance
|
|
206
|
+
expect(:END)
|
|
207
|
+
else_template = parse_template(in_block: true)
|
|
208
|
+
expect(:START)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
expect(:SLASH)
|
|
212
|
+
closing_name = yield
|
|
213
|
+
expect(:END)
|
|
214
|
+
|
|
215
|
+
[template, else_template, closing_name]
|
|
147
216
|
end
|
|
217
|
+
end
|
|
148
218
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
Node::WithElse.new(path, with_template || VOID, else_template || VOID, pos(0))
|
|
155
|
-
end
|
|
219
|
+
def parse_options
|
|
220
|
+
options = []
|
|
221
|
+
options << Node::Option.new(advance.value, parse_expression) while peek.type == :KEY
|
|
222
|
+
options
|
|
223
|
+
end
|
|
156
224
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
Node::WithElse.new(subexpression, with_template || VOID, else_template || VOID, pos(0))
|
|
163
|
-
end
|
|
225
|
+
def parse_expressions
|
|
226
|
+
expressions = []
|
|
227
|
+
expressions << parse_expression while EXPRESSION_START.include?(peek.type)
|
|
228
|
+
expressions
|
|
229
|
+
end
|
|
164
230
|
|
|
165
|
-
|
|
166
|
-
|
|
231
|
+
def parse_expression
|
|
232
|
+
case peek.type
|
|
233
|
+
when :LITERAL then parse_literal
|
|
234
|
+
when :VARIABLE then parse_variable
|
|
235
|
+
when :PATH then parse_path
|
|
236
|
+
when :LPAREN then parse_subexpression
|
|
237
|
+
else raise Error, peek
|
|
167
238
|
end
|
|
168
239
|
end
|
|
169
240
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
241
|
+
def parse_literal
|
|
242
|
+
token = expect(:LITERAL)
|
|
243
|
+
Node::Literal.new(token.value)
|
|
173
244
|
end
|
|
174
245
|
|
|
175
|
-
|
|
176
|
-
|
|
246
|
+
def parse_variable
|
|
247
|
+
token = expect(:VARIABLE)
|
|
248
|
+
Node::Variable.new(token.value, token.position)
|
|
177
249
|
end
|
|
178
250
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
251
|
+
def parse_path
|
|
252
|
+
token = expect(:PATH)
|
|
253
|
+
Node::Path.new(token.value, token.position)
|
|
182
254
|
end
|
|
183
255
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
256
|
+
def parse_subexpression
|
|
257
|
+
first_index = @index
|
|
258
|
+
expect(:LPAREN)
|
|
259
|
+
nested(first_index) do
|
|
260
|
+
helper = parse_path
|
|
261
|
+
arguments = parse_expressions
|
|
262
|
+
options = parse_options
|
|
263
|
+
expect(:RPAREN)
|
|
264
|
+
Node::SubExpression.new(helper, arguments, options, span(first_index))
|
|
265
|
+
end
|
|
188
266
|
end
|
|
189
267
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
clause('VARIABLE') { |variable| Node::Variable.new(variable, pos(0)) }
|
|
193
|
-
end
|
|
268
|
+
def nested(first_index)
|
|
269
|
+
raise ParseDepthExceeded.new(@tokens[first_index], depth_limit) if @depth >= depth_limit
|
|
194
270
|
|
|
195
|
-
|
|
271
|
+
@depth += 1
|
|
272
|
+
@max_depth = @depth if @depth > @max_depth
|
|
273
|
+
result = yield
|
|
274
|
+
@depth -= 1
|
|
275
|
+
result
|
|
276
|
+
end
|
|
196
277
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
278
|
+
# The position spanning from the token at `first_index` through the last
|
|
279
|
+
# token consumed.
|
|
280
|
+
def span(first_index)
|
|
281
|
+
start = @tokens[first_index].position
|
|
282
|
+
finish = @tokens[@index - 1].position
|
|
283
|
+
StreamPosition.new(
|
|
284
|
+
start.stream_offset,
|
|
285
|
+
start.line_number,
|
|
286
|
+
start.line_offset,
|
|
287
|
+
finish.stream_offset + finish.length - start.stream_offset,
|
|
288
|
+
start.file_name
|
|
203
289
|
)
|
|
204
290
|
end
|
|
205
291
|
|
|
206
|
-
|
|
292
|
+
def expect(type)
|
|
293
|
+
raise Error, peek unless peek.type == type
|
|
294
|
+
|
|
295
|
+
advance
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def advance
|
|
299
|
+
token = peek
|
|
300
|
+
@index += 1
|
|
301
|
+
token
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def peek(offset = 0)
|
|
305
|
+
@tokens[@index + offset] || Token::EOS
|
|
306
|
+
end
|
|
207
307
|
|
|
208
308
|
VOID = Class.new do
|
|
209
309
|
def compile
|
|
@@ -218,5 +318,6 @@ module Curlybars
|
|
|
218
318
|
# Empty cache key.
|
|
219
319
|
end
|
|
220
320
|
end.new
|
|
321
|
+
private_constant :VOID
|
|
221
322
|
end
|
|
222
323
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Curlybars
|
|
2
4
|
class PathFinder
|
|
3
5
|
def initialize(ast)
|
|
@@ -82,8 +84,7 @@ module Curlybars
|
|
|
82
84
|
stack.pop unless stack.empty?
|
|
83
85
|
end
|
|
84
86
|
|
|
85
|
-
|
|
86
|
-
stack + remaining_segments
|
|
87
|
+
stack + full_path.split('.')
|
|
87
88
|
end
|
|
88
89
|
|
|
89
90
|
def handle_block_helper(node, context_stack)
|
data/lib/curlybars/position.rb
CHANGED
data/lib/curlybars/presenter.rb
CHANGED
data/lib/curlybars/railtie.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Curlybars
|
|
2
4
|
class RenderingSupport
|
|
3
5
|
def initialize(timeout, contexts, variables, file_name, global_helpers_providers = [], cache = ->(key, &block) { block.call }, start_time: nil, depth: 0, partial_provider: nil)
|
|
@@ -31,6 +33,16 @@ module Curlybars
|
|
|
31
33
|
raise ::Curlybars::Error::Render.new('timeout', message, nil)
|
|
32
34
|
end
|
|
33
35
|
|
|
36
|
+
# Called before a #with or #each block pushes its context. The contexts
|
|
37
|
+
# stack starts with the root presenter, so its length is one more than
|
|
38
|
+
# the number of enclosing #with and #each blocks.
|
|
39
|
+
def check_nesting_not_too_deep(position)
|
|
40
|
+
return if contexts.length <= Curlybars.configuration.nesting_limit
|
|
41
|
+
|
|
42
|
+
message = "Nesting too deep (> #{Curlybars.configuration.nesting_limit} levels)"
|
|
43
|
+
raise Curlybars::Error::Render.new('nesting_too_deep', message, position)
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
def check_context_is_presenter(context, path, position)
|
|
35
47
|
return if presenter?(context)
|
|
36
48
|
|
|
@@ -257,8 +269,11 @@ module Curlybars
|
|
|
257
269
|
raise Curlybars::Error::Render.new('unallowed_path', message, position)
|
|
258
270
|
end
|
|
259
271
|
|
|
272
|
+
# Counts the dots after the final `/` in the path. Leading `../` segments
|
|
273
|
+
# move up the contexts stack rather than traversing, so they don't count.
|
|
260
274
|
def check_traverse_not_too_deep(traverse, position)
|
|
261
|
-
|
|
275
|
+
dots = traverse.split('/').last.count('.')
|
|
276
|
+
return unless dots > Curlybars.configuration.traversing_limit
|
|
262
277
|
|
|
263
278
|
message = "`#{traverse}` too deep"
|
|
264
279
|
raise Curlybars::Error::Render.new('traverse_too_deep', message, position)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Curlybars
|
|
4
|
+
# A token produced by Curlybars::Lexer and consumed by Curlybars::Parser.
|
|
5
|
+
# Custom processors receive and may replace these (see Processor::TokenFactory).
|
|
6
|
+
Token = Struct.new(:type, :value, :position) do
|
|
7
|
+
# Tokens are equal when type and value match, regardless of position.
|
|
8
|
+
def ==(other)
|
|
9
|
+
type == other.type && value == other.value
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Matches RLTK::Token#to_s, e.g. `PATH(a)`, or `END` without a value.
|
|
13
|
+
def to_s
|
|
14
|
+
value ? "#{type}(#{value})" : type.to_s
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
Token::EOS = Token.new(:EOS, nil, nil).freeze
|
|
19
|
+
|
|
20
|
+
# Where a token, or a node built from a run of tokens, appears in the source.
|
|
21
|
+
StreamPosition = Struct.new(:stream_offset, :line_number, :line_offset, :length, :file_name) do
|
|
22
|
+
# Kept from RLTK::StreamPosition.
|
|
23
|
+
alias_method :start, :line_offset
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A shared position for tokens that have no source, e.g. in an empty template.
|
|
27
|
+
StreamPosition::NULL = StreamPosition.new(0, 0, 0, 0, nil).freeze
|
|
28
|
+
end
|
data/lib/curlybars/version.rb
CHANGED