curlybars 1.16.1 → 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/lib/curlybars/configuration.rb +4 -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 +2 -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 -0
- data/lib/curlybars/node/root.rb +2 -0
- data/lib/curlybars/node/sub_expression.rb +2 -0
- data/lib/curlybars/node/template.rb +2 -0
- 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 +2 -0
- data/lib/curlybars/parser.rb +247 -146
- data/lib/curlybars/partial_presenter.rb +2 -0
- data/lib/curlybars/path_finder.rb +2 -0
- 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 +2 -0
- 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 +12 -6
- metadata +2 -30
- data/lib/curlybars/processor/tilde.rb +0 -34
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f6da5518b6b73d9f76f01132039228b8eeb4e7ca2e47fb6148dcdb05bbac949d
|
|
4
|
+
data.tar.gz: '0688645c6e1f342ca1529b64b7797e48f0ffc27f697a73b717bcb1e222bb0ba7'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bc3d992f6cc5d2f5a730efd765aae1d75ff2ec8665588d8387ff4faded3716f24f9b0b1680f8e9066cf4b05ebb3e008a6f91432a211af629434374398ceac49
|
|
7
|
+
data.tar.gz: c1a1f65604432f1156f34ec8d4ae3755f656a6647380bd068eda016fcaf5d79bbb5eabb6c5d63cb94a56be89ac6df007b96ab3b45351143a8513c34fd10ff241
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Curlybars
|
|
2
4
|
class << self
|
|
3
5
|
attr_writer :configuration
|
|
@@ -17,11 +19,12 @@ module Curlybars
|
|
|
17
19
|
end
|
|
18
20
|
|
|
19
21
|
class Configuration
|
|
20
|
-
attr_accessor :presenters_namespace, :nesting_limit, :traversing_limit, :output_limit, :rendering_timeout, :custom_processors, :compiler_transformers, :global_helpers_provider_classes, :cache, :partial_nesting_limit, :partial_provider_class
|
|
22
|
+
attr_accessor :presenters_namespace, :nesting_limit, :traversing_limit, :output_limit, :rendering_timeout, :custom_processors, :compiler_transformers, :global_helpers_provider_classes, :cache, :partial_nesting_limit, :partial_provider_class, :parse_depth_limit
|
|
21
23
|
|
|
22
24
|
def initialize
|
|
23
25
|
@presenters_namespace = ''
|
|
24
26
|
@nesting_limit = 10
|
|
27
|
+
@parse_depth_limit = Curlybars::Parser::DEFAULT_PARSE_DEPTH_LIMIT
|
|
25
28
|
@traversing_limit = 10
|
|
26
29
|
@output_limit = 1.megabyte
|
|
27
30
|
@rendering_timeout = 10.seconds
|
data/lib/curlybars/error/base.rb
CHANGED
data/lib/curlybars/error/lex.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'curlybars/error/base'
|
|
2
4
|
|
|
3
5
|
module Curlybars
|
|
@@ -9,6 +11,8 @@ module Curlybars
|
|
|
9
11
|
if exception.current.type == :EOS
|
|
10
12
|
message = "A block helper hasn't been closed properly"
|
|
11
13
|
position = EOSPosition.new(source)
|
|
14
|
+
elsif exception.is_a?(Curlybars::Parser::ParseDepthExceeded)
|
|
15
|
+
message = "Blocks and subexpressions cannot be nested more than %d levels deep" % exception.limit
|
|
12
16
|
else
|
|
13
17
|
line_number = position.line_number
|
|
14
18
|
line_offset = position.line_offset
|
data/lib/curlybars/generic.rb
CHANGED
data/lib/curlybars/lexer.rb
CHANGED
|
@@ -1,64 +1,170 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'strscan'
|
|
4
|
+
require 'curlybars/token'
|
|
2
5
|
|
|
3
6
|
# rubocop:disable Style/RegexpLiteral, Style/Semicolon
|
|
4
7
|
module Curlybars
|
|
5
|
-
class Lexer
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
class Lexer
|
|
9
|
+
class Error < StandardError
|
|
10
|
+
attr_reader :stream_offset, :line_number, :line_offset, :remainder
|
|
11
|
+
|
|
12
|
+
def initialize(stream_offset, line_number, line_offset, remainder)
|
|
13
|
+
@stream_offset = stream_offset
|
|
14
|
+
@line_number = line_number
|
|
15
|
+
@line_offset = line_offset
|
|
16
|
+
@remainder = remainder
|
|
17
|
+
super("Unable to match string with any of the given rules: #{remainder}")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Rules are tried in definition order and the first match wins. A rule
|
|
22
|
+
# can declare the bytes its matches start with (`first:`), and is then
|
|
23
|
+
# only tried when the next byte is one of them. Rules without `first:`
|
|
24
|
+
# are tried at every byte.
|
|
25
|
+
# Each rule belongs to a state; the current state is the top of a stack
|
|
26
|
+
# that rule actions manipulate with push_state and pop_state.
|
|
27
|
+
# Actions receive the matched text and can read capture groups with
|
|
28
|
+
# @scanner[n].
|
|
29
|
+
Rule = Data.define(:pattern, :action)
|
|
30
|
+
|
|
31
|
+
@rules = Hash.new { |hash, state| hash[state] = [] }
|
|
32
|
+
# For each state, the rules to try for each value of the next byte.
|
|
33
|
+
@dispatch = Hash.new { |hash, state| hash[state] = Array.new(256) { [] } }
|
|
34
|
+
|
|
35
|
+
class << self
|
|
36
|
+
attr_reader :rules, :dispatch
|
|
37
|
+
|
|
38
|
+
def r(pattern, state = :default, first: nil, &action)
|
|
39
|
+
rule = Rule.new(pattern, action || proc {})
|
|
40
|
+
@rules[state] << rule
|
|
41
|
+
(first ? first.bytes : 0..255).each { |byte| @dispatch[state][byte] << rule }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def lex(string, file_name = nil)
|
|
45
|
+
new(string, file_name).tokens
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The following is a Handlebars compliant identifier
|
|
50
|
+
# IDENTIFIER = '[A-Za-z_]\w*'
|
|
10
51
|
|
|
11
52
|
# This accomodates the edge case of identifiers containing dashes
|
|
12
|
-
# IDENTIFIER = '[A-Za-z_\-][
|
|
53
|
+
# IDENTIFIER = '[A-Za-z_\-][\w\-]*'
|
|
13
54
|
|
|
14
55
|
# This accomodates the edge case of identifiers containing all numbers
|
|
15
56
|
# and dashes
|
|
16
|
-
IDENTIFIER = '[
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
r(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
r(/{{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
r(/{{
|
|
30
|
-
r(/~}}/, :curly) { pop_state; :
|
|
31
|
-
|
|
32
|
-
r(/{{
|
|
33
|
-
r(/}}/, :curly) { pop_state; :END }
|
|
34
|
-
|
|
35
|
-
r(/\(/, :curly) { push_state :curly; :LPAREN }
|
|
36
|
-
r(/\)/, :curly) { pop_state; :RPAREN }
|
|
37
|
-
|
|
38
|
-
r(/#/, :curly) { :HASH }
|
|
39
|
-
r(/\//, :curly) { :SLASH }
|
|
40
|
-
r(/>/, :curly) { :GT }
|
|
41
|
-
|
|
42
|
-
r(/if\b/, :curly) { :IF }
|
|
43
|
-
r(/unless\b/, :curly) { :UNLESS }
|
|
44
|
-
r(/each\b/, :curly) { :EACH }
|
|
45
|
-
r(/with\b/, :curly) { :WITH }
|
|
46
|
-
r(/else\b/, :curly) { :ELSE }
|
|
47
|
-
|
|
48
|
-
r(/true/, :curly) { [:LITERAL, true] }
|
|
49
|
-
r(/false/, :curly) { [:LITERAL, false] }
|
|
50
|
-
r(/[-+]?\d+/, :curly) { |integer| [:LITERAL, integer.to_i] }
|
|
51
|
-
r(/'(.*?)'/, :curly) { [:LITERAL,
|
|
52
|
-
r(/"(.*?)"/, :curly) { [:LITERAL,
|
|
53
|
-
|
|
54
|
-
r(/@((?:\.\.\/)*#{IDENTIFIER})/o, :curly) {
|
|
55
|
-
|
|
56
|
-
r(/(#{IDENTIFIER})\s*=/o, :curly) { [:KEY,
|
|
57
|
-
r(/(?:\.\.\/)*(?:#{IDENTIFIER}\.)*#{IDENTIFIER}/o, :curly) { |name| [:PATH, name] }
|
|
58
|
-
|
|
59
|
-
r(/\s
|
|
60
|
-
|
|
61
|
-
r(/.*?(?=\\|{{|\z)/m) { |text| [:TEXT, text] }
|
|
57
|
+
IDENTIFIER = '[\w\-]+'
|
|
58
|
+
IDENTIFIER_START = [*'0'..'9', *'A'..'Z', *'a'..'z', '_', '-'].join.freeze
|
|
59
|
+
|
|
60
|
+
r(/\\{/, first: '\\') { [:TEXT, '{'] }
|
|
61
|
+
r(/\\/, first: '\\') { [:TEXT, '\\'] }
|
|
62
|
+
|
|
63
|
+
# Comments are skipped up to their closing delimiter, or to the end of
|
|
64
|
+
# the template when they are not closed.
|
|
65
|
+
r(/{{!--.*?(?:--}}|\z)/m, first: '{')
|
|
66
|
+
r(/{{!.*?(?:}}|\z)/m, first: '{')
|
|
67
|
+
|
|
68
|
+
# The tilde operator strips whitespace from the adjacent text:
|
|
69
|
+
# `{{~` from the previous token and `~}}` from the next one.
|
|
70
|
+
r(/{{~/, first: '{') { rstrip_previous_text; push_state :curly; :START }
|
|
71
|
+
r(/~}}/, :curly, first: '~') { pop_state; lstrip_next_text; :END }
|
|
72
|
+
|
|
73
|
+
r(/{{/, first: '{') { push_state :curly; :START }
|
|
74
|
+
r(/}}/, :curly, first: '}') { pop_state; :END }
|
|
75
|
+
|
|
76
|
+
r(/\(/, :curly, first: '(') { push_state :curly; :LPAREN }
|
|
77
|
+
r(/\)/, :curly, first: ')') { pop_state; :RPAREN }
|
|
78
|
+
|
|
79
|
+
r(/#/, :curly, first: '#') { :HASH }
|
|
80
|
+
r(/\//, :curly, first: '/') { :SLASH }
|
|
81
|
+
r(/>/, :curly, first: '>') { :GT }
|
|
82
|
+
|
|
83
|
+
r(/if\b/, :curly, first: 'i') { :IF }
|
|
84
|
+
r(/unless\b/, :curly, first: 'u') { :UNLESS }
|
|
85
|
+
r(/each\b/, :curly, first: 'e') { :EACH }
|
|
86
|
+
r(/with\b/, :curly, first: 'w') { :WITH }
|
|
87
|
+
r(/else\b/, :curly, first: 'e') { :ELSE }
|
|
88
|
+
|
|
89
|
+
r(/true/, :curly, first: 't') { [:LITERAL, true] }
|
|
90
|
+
r(/false/, :curly, first: 'f') { [:LITERAL, false] }
|
|
91
|
+
r(/[-+]?\d+/, :curly, first: '-+0123456789') { |integer| [:LITERAL, integer.to_i] }
|
|
92
|
+
r(/'(.*?)'/, :curly, first: "'") { [:LITERAL, @scanner[1].inspect] }
|
|
93
|
+
r(/"(.*?)"/, :curly, first: '"') { [:LITERAL, @scanner[1].inspect] }
|
|
94
|
+
|
|
95
|
+
r(/@((?:\.\.\/)*#{IDENTIFIER})/o, :curly, first: '@') { [:VARIABLE, @scanner[1]] }
|
|
96
|
+
|
|
97
|
+
r(/(#{IDENTIFIER})\s*=/o, :curly, first: IDENTIFIER_START) { [:KEY, @scanner[1]] }
|
|
98
|
+
r(/(?:\.\.\/)*(?:#{IDENTIFIER}\.)*#{IDENTIFIER}/o, :curly, first: ".#{IDENTIFIER_START}") { |name| [:PATH, name] }
|
|
99
|
+
|
|
100
|
+
r(/\s+/, :curly, first: " \t\n\v\f\r")
|
|
101
|
+
|
|
102
|
+
r(/.*?(?=\\|{{|\z)/m) { |text| [:TEXT, @tokens.length == @lstrip_index ? text.lstrip : text] }
|
|
103
|
+
|
|
104
|
+
def initialize(string, file_name)
|
|
105
|
+
@string = string
|
|
106
|
+
@scanner = StringScanner.new(string)
|
|
107
|
+
@file_name = file_name
|
|
108
|
+
@states = [:default]
|
|
109
|
+
@tokens = []
|
|
110
|
+
@lstrip_index = nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def tokens
|
|
114
|
+
stream_offset = 0
|
|
115
|
+
line_number = 1
|
|
116
|
+
line_offset = 0
|
|
117
|
+
|
|
118
|
+
until @scanner.eos?
|
|
119
|
+
text = nil
|
|
120
|
+
candidates = self.class.dispatch[@states.last][@string.getbyte(@scanner.pos)]
|
|
121
|
+
rule = candidates.detect { |candidate| text = @scanner.scan(candidate.pattern) }
|
|
122
|
+
raise Error.new(stream_offset, line_number, line_offset, @scanner.rest) unless rule
|
|
123
|
+
|
|
124
|
+
type, value = instance_exec(text, &rule.action)
|
|
125
|
+
|
|
126
|
+
if type
|
|
127
|
+
position = StreamPosition.new(stream_offset, line_number, line_offset, text.length, @file_name)
|
|
128
|
+
@tokens << Token.new(type, value, position)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
stream_offset += text.length
|
|
132
|
+
newlines = text.count("\n")
|
|
133
|
+
if newlines > 0
|
|
134
|
+
line_number += newlines
|
|
135
|
+
line_offset = text.length - text.rindex("\n") - 1
|
|
136
|
+
else
|
|
137
|
+
line_offset += text.length
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
@tokens << Token::EOS
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def push_state(state)
|
|
147
|
+
@states.push(state)
|
|
148
|
+
nil
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def pop_state
|
|
152
|
+
@states.pop
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def rstrip_previous_text
|
|
157
|
+
previous = @tokens.last
|
|
158
|
+
return unless previous&.type == :TEXT
|
|
159
|
+
|
|
160
|
+
@tokens[-1] = Token.new(:TEXT, previous.value.rstrip, previous.position)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# The rule calling this emits a token at index @tokens.length, so the
|
|
164
|
+
# following token lands at @tokens.length + 1.
|
|
165
|
+
def lstrip_next_text
|
|
166
|
+
@lstrip_index = @tokens.length + 1
|
|
167
|
+
end
|
|
62
168
|
end
|
|
63
169
|
end
|
|
64
170
|
# rubocop:enable Style/RegexpLiteral, Style/Semicolon
|
data/lib/curlybars/node/item.rb
CHANGED
data/lib/curlybars/node/path.rb
CHANGED
data/lib/curlybars/node/root.rb
CHANGED
data/lib/curlybars/node/text.rb
CHANGED
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
|
data/lib/curlybars/position.rb
CHANGED
data/lib/curlybars/presenter.rb
CHANGED
data/lib/curlybars/railtie.rb
CHANGED
|
@@ -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
data/lib/curlybars/visitor.rb
CHANGED
data/lib/curlybars.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'digest'
|
|
2
4
|
require 'active_support/cache'
|
|
5
|
+
require 'active_support/notifications'
|
|
3
6
|
require 'active_support/core_ext/object/json'
|
|
4
7
|
require 'curlybars/version'
|
|
5
8
|
|
|
@@ -137,18 +140,22 @@ module Curlybars
|
|
|
137
140
|
def ast(source, identifier, run_processors:)
|
|
138
141
|
tokens = Curlybars::Lexer.lex(source, identifier)
|
|
139
142
|
|
|
140
|
-
Curlybars::Processor::Tilde.process!(tokens, identifier)
|
|
141
|
-
|
|
142
143
|
if run_processors
|
|
143
144
|
Curlybars.configuration.custom_processors.each do |processor|
|
|
144
145
|
processor.process!(tokens, identifier)
|
|
145
146
|
end
|
|
146
147
|
end
|
|
147
148
|
|
|
148
|
-
Curlybars::Parser.
|
|
149
|
-
|
|
149
|
+
parser = Curlybars::Parser.new(tokens, depth_limit: configuration.parse_depth_limit)
|
|
150
|
+
payload = { identifier: identifier, depth_limit: parser.depth_limit }
|
|
151
|
+
ActiveSupport::Notifications.instrument("parse.curlybars", payload) do
|
|
152
|
+
parser.parse
|
|
153
|
+
ensure
|
|
154
|
+
payload[:max_depth] = parser.max_depth
|
|
155
|
+
end
|
|
156
|
+
rescue Curlybars::Lexer::Error => lexing_error
|
|
150
157
|
raise Curlybars::Error::Lex.new(source, identifier, lexing_error)
|
|
151
|
-
rescue
|
|
158
|
+
rescue Curlybars::Parser::Error => not_in_language_error
|
|
152
159
|
raise Curlybars::Error::Parse.new(source, not_in_language_error)
|
|
153
160
|
end
|
|
154
161
|
end
|
|
@@ -163,7 +170,6 @@ require 'curlybars/position'
|
|
|
163
170
|
require 'curlybars/generic'
|
|
164
171
|
require 'curlybars/lexer'
|
|
165
172
|
require 'curlybars/processor/token_factory'
|
|
166
|
-
require 'curlybars/processor/tilde'
|
|
167
173
|
require 'curlybars/error/lex'
|
|
168
174
|
require 'curlybars/error/parse'
|
|
169
175
|
require 'curlybars/error/compile'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: curlybars
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.17.0.pre
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Libo Cannici
|
|
@@ -44,34 +44,6 @@ dependencies:
|
|
|
44
44
|
- - ">="
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
46
|
version: '7.2'
|
|
47
|
-
- !ruby/object:Gem::Dependency
|
|
48
|
-
name: ffi
|
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - ">="
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0'
|
|
54
|
-
type: :runtime
|
|
55
|
-
prerelease: false
|
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
-
requirements:
|
|
58
|
-
- - ">="
|
|
59
|
-
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
61
|
-
- !ruby/object:Gem::Dependency
|
|
62
|
-
name: rltk
|
|
63
|
-
requirement: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
- - ">="
|
|
66
|
-
- !ruby/object:Gem::Version
|
|
67
|
-
version: '0'
|
|
68
|
-
type: :runtime
|
|
69
|
-
prerelease: false
|
|
70
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
-
requirements:
|
|
72
|
-
- - ">="
|
|
73
|
-
- !ruby/object:Gem::Version
|
|
74
|
-
version: '0'
|
|
75
47
|
description: |-
|
|
76
48
|
A view layer for your Rails apps that separates structure and logic, using Handlebars templates.
|
|
77
49
|
Strongly inspired by Curly Template gem by Daniel Schierbeck.
|
|
@@ -116,13 +88,13 @@ files:
|
|
|
116
88
|
- lib/curlybars/path_finder.rb
|
|
117
89
|
- lib/curlybars/position.rb
|
|
118
90
|
- lib/curlybars/presenter.rb
|
|
119
|
-
- lib/curlybars/processor/tilde.rb
|
|
120
91
|
- lib/curlybars/processor/token_factory.rb
|
|
121
92
|
- lib/curlybars/railtie.rb
|
|
122
93
|
- lib/curlybars/rendering_support.rb
|
|
123
94
|
- lib/curlybars/safe_buffer.rb
|
|
124
95
|
- lib/curlybars/serialization_guard.rb
|
|
125
96
|
- lib/curlybars/template_handler.rb
|
|
97
|
+
- lib/curlybars/token.rb
|
|
126
98
|
- lib/curlybars/validation_context.rb
|
|
127
99
|
- lib/curlybars/version.rb
|
|
128
100
|
- lib/curlybars/visitor.rb
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
module Curlybars
|
|
2
|
-
module Processor
|
|
3
|
-
class Tilde
|
|
4
|
-
extend TokenFactory
|
|
5
|
-
|
|
6
|
-
class << self
|
|
7
|
-
def process!(tokens, identifier)
|
|
8
|
-
tokens.each_with_index do |token, index|
|
|
9
|
-
case token.type
|
|
10
|
-
when :TILDE_START
|
|
11
|
-
tokens[index] = create_token(:START, token.value, token.position)
|
|
12
|
-
next if index == 0
|
|
13
|
-
|
|
14
|
-
strip_token_if_text(tokens, index - 1, :rstrip)
|
|
15
|
-
when :TILDE_END
|
|
16
|
-
tokens[index] = create_token(:END, token.value, token.position)
|
|
17
|
-
next if index == (tokens.length - 1)
|
|
18
|
-
|
|
19
|
-
strip_token_if_text(tokens, index + 1, :lstrip)
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def strip_token_if_text(tokens, index, strip_method)
|
|
25
|
-
token = tokens[index]
|
|
26
|
-
return if token.type != :TEXT
|
|
27
|
-
|
|
28
|
-
stripped_value = token.value.public_send(strip_method)
|
|
29
|
-
tokens[index] = create_token(token.type, stripped_value, token.position)
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|