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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/curlybars/configuration.rb +4 -1
  3. data/lib/curlybars/dependency_tracker.rb +2 -0
  4. data/lib/curlybars/error/base.rb +2 -0
  5. data/lib/curlybars/error/compile.rb +2 -0
  6. data/lib/curlybars/error/lex.rb +2 -0
  7. data/lib/curlybars/error/parse.rb +4 -0
  8. data/lib/curlybars/error/presenter/not_found.rb +2 -0
  9. data/lib/curlybars/error/render.rb +2 -0
  10. data/lib/curlybars/error/validate.rb +2 -0
  11. data/lib/curlybars/generic.rb +2 -0
  12. data/lib/curlybars/lexer.rb +159 -53
  13. data/lib/curlybars/method_whitelist.rb +2 -0
  14. data/lib/curlybars/node/block_helper_else.rb +2 -0
  15. data/lib/curlybars/node/each_else.rb +2 -0
  16. data/lib/curlybars/node/if_else.rb +2 -0
  17. data/lib/curlybars/node/item.rb +2 -0
  18. data/lib/curlybars/node/literal.rb +2 -0
  19. data/lib/curlybars/node/option.rb +2 -0
  20. data/lib/curlybars/node/output.rb +2 -0
  21. data/lib/curlybars/node/partial.rb +2 -0
  22. data/lib/curlybars/node/path.rb +2 -0
  23. data/lib/curlybars/node/root.rb +2 -0
  24. data/lib/curlybars/node/sub_expression.rb +2 -0
  25. data/lib/curlybars/node/template.rb +2 -0
  26. data/lib/curlybars/node/text.rb +2 -0
  27. data/lib/curlybars/node/unless_else.rb +2 -0
  28. data/lib/curlybars/node/variable.rb +2 -0
  29. data/lib/curlybars/node/with_else.rb +2 -0
  30. data/lib/curlybars/parser.rb +247 -146
  31. data/lib/curlybars/partial_presenter.rb +2 -0
  32. data/lib/curlybars/path_finder.rb +2 -0
  33. data/lib/curlybars/position.rb +2 -0
  34. data/lib/curlybars/presenter.rb +2 -0
  35. data/lib/curlybars/processor/token_factory.rb +3 -1
  36. data/lib/curlybars/railtie.rb +2 -0
  37. data/lib/curlybars/rendering_support.rb +2 -0
  38. data/lib/curlybars/safe_buffer.rb +2 -0
  39. data/lib/curlybars/serialization_guard.rb +2 -0
  40. data/lib/curlybars/template_handler.rb +2 -0
  41. data/lib/curlybars/token.rb +28 -0
  42. data/lib/curlybars/validation_context.rb +2 -0
  43. data/lib/curlybars/version.rb +1 -1
  44. data/lib/curlybars/visitor.rb +2 -0
  45. data/lib/curlybars.rb +12 -6
  46. metadata +2 -30
  47. data/lib/curlybars/processor/tilde.rb +0 -34
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 746a0625058bd9a2bca9ebf655c36f7cbd7e11e43000a051f4a76516ce73150b
4
- data.tar.gz: f631902fba5a77cce57a36472636b99a5c03450dfbe5943e058c92b7c09e1d07
3
+ metadata.gz: f6da5518b6b73d9f76f01132039228b8eeb4e7ca2e47fb6148dcdb05bbac949d
4
+ data.tar.gz: '0688645c6e1f342ca1529b64b7797e48f0ffc27f697a73b717bcb1e222bb0ba7'
5
5
  SHA512:
6
- metadata.gz: c0fb5fb5398bf4643f679fdb07403de92892ee84efb70bae6a689297251e1ff9e79311b1c7711a6ba6a247a84eac945d735f72deacd8a555c9cd8ebfd75e9d81
7
- data.tar.gz: ac273a2a866a2634c6d17ebf4a4bfd32b656c765e896214e2f3d6d2d49069232503e96bdc04a23b698d8f569cf5d368f5ab736699e280e404b9765bb370b4132
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  class DependencyTracker
3
5
  def self.call(path, template)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Error
3
5
  class Base < StandardError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/error/base'
2
4
 
3
5
  module Curlybars
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/error/base'
2
4
 
3
5
  module Curlybars
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Error
3
5
  module Presenter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/error/base'
2
4
 
3
5
  module Curlybars
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/error/base'
2
4
 
3
5
  module Curlybars
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/method_whitelist'
2
4
 
3
5
  module Curlybars
@@ -1,64 +1,170 @@
1
- require 'rltk/lexer'
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 < RLTK::Lexer
6
- match_first
7
-
8
- # The following is an identifier Handlebars compliant
9
- # IDENTIFIER = '[A-Za-z_][0-9\w]*'
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_\-][0-9\w\-]*'
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 = '[0-9A-Za-z_\-][0-9\w\-]*'.freeze
17
-
18
- r(/\\{/) { [:TEXT, '{'] }
19
- r(/\\/) { [:TEXT, '\\'] }
20
-
21
- r(/{{!--/) { push_state :comment_block }
22
- r(/--}}/, :comment_block) { pop_state }
23
- r(/./m, :comment_block)
24
-
25
- r(/{{!/) { push_state :comment }
26
- r(/}}/, :comment) { pop_state }
27
- r(/./m, :comment)
28
-
29
- r(/{{~/) { push_state :curly; :TILDE_START }
30
- r(/~}}/, :curly) { pop_state; :TILDE_END }
31
-
32
- r(/{{/) { push_state :curly; :START }
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, match[1].inspect] }
52
- r(/"(.*?)"/, :curly) { [:LITERAL, match[1].inspect] }
53
-
54
- r(/@((?:\.\.\/)*#{IDENTIFIER})/o, :curly) { |variable| [:VARIABLE, match[1]] }
55
-
56
- r(/(#{IDENTIFIER})\s*=/o, :curly) { [:KEY, match[1]] }
57
- r(/(?:\.\.\/)*(?:#{IDENTIFIER}\.)*#{IDENTIFIER}/o, :curly) { |name| [:PATH, name] }
58
-
59
- r(/\s/, :curly)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module MethodWhitelist
3
5
  def allow_methods(*methods_without_type, **methods_with_type, &contextual_block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/error/compile'
2
4
 
3
5
  module Curlybars
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  EachElse = Struct.new(:path, :each_template, :else_template, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  IfElse = Struct.new(:expression, :if_template, :else_template) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Item = Struct.new(:item) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Literal = Struct.new(:literal) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Option = Struct.new(:key, :expression) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Output = Struct.new(:value) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Partial = Struct.new(:path, :options, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Path = Struct.new(:path, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Root = Struct.new(:template, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  SubExpression = Struct.new(:helper, :arguments, :options, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Template = Struct.new(:items, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Text = Struct.new(:text) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  UnlessElse = Struct.new(:expression, :unless_template, :else_template) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  Variable = Struct.new(:variable, :position) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Node
3
5
  WithElse = Struct.new(:path, :with_template, :else_template, :position) do
@@ -1,4 +1,6 @@
1
- require 'rltk/parser'
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
- class Parser < RLTK::Parser
22
- start :root
23
-
24
- production(:root, 'template?') { |template| Node::Root.new(template || VOID, pos(0)) }
25
- production(:template, 'items') { |items| Node::Template.new(items || [], pos(0)) }
26
-
27
- production(:items) do
28
- clause('items item') { |items, item| items << Node::Item.new(item) }
29
- clause('item') { |item| [Node::Item.new(item)] }
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
- production(:item) do
33
- clause('TEXT') { |text| Node::Text.new(text) }
58
+ class ParseDepthExceeded < Error
59
+ # The depth limit that the template exceeded.
60
+ attr_reader :limit
34
61
 
35
- clause('START HASH .path .expressions? .options? END
36
- .template?
37
- START SLASH .path END') do |helper, arguments, options, template, helperclose|
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
- clause('START HASH .path .expressions? .options? END
50
- .template?
51
- START ELSE END
52
- .template?
53
- START SLASH .path END') do |helper, arguments, options, helper_template, else_template, helperclose|
54
- Node::BlockHelperElse.new(
55
- helper,
56
- arguments || [],
57
- options || [],
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
- clause('START .path .expressions? .options? END') do |helper, arguments, options|
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
- clause('START .value END') do |value|
78
- Node::Output.new(value)
79
- end
80
+ def self.parse(tokens, depth_limit: DEFAULT_PARSE_DEPTH_LIMIT)
81
+ new(tokens, depth_limit: depth_limit).parse
82
+ end
80
83
 
81
- clause('START HASH IF .expression END
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
- clause('START HASH IF .expression END
88
- .template?
89
- START ELSE END
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
- clause('START HASH UNLESS .expression END
96
- .template?
97
- START SLASH UNLESS END') do |expression, unless_template|
98
- Node::UnlessElse.new(expression, unless_template || VOID, VOID)
99
- end
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
- clause('START HASH UNLESS .expression END
102
- .template?
103
- START ELSE END
104
- .template?
105
- START SLASH UNLESS END') do |expression, unless_template, else_template|
106
- Node::UnlessElse.new(expression, unless_template || VOID, else_template || VOID)
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
- clause('START HASH EACH .path END
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
- clause('START HASH EACH .subexpression END
116
- .template?
117
- START SLASH EACH END') do |subexpression, each_template|
118
- Node::EachElse.new(subexpression, each_template || VOID, VOID, pos(0))
119
- end
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
- clause('START HASH EACH .path END
122
- .template?
123
- START ELSE END
124
- .template?
125
- START SLASH EACH END') do |path, each_template, else_template|
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
- clause('START HASH EACH .subexpression END
130
- .template?
131
- START ELSE END
132
- .template?
133
- START SLASH EACH END') do |subexpression, each_template, else_template|
134
- Node::EachElse.new(subexpression, each_template || VOID, else_template || VOID, pos(0))
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
- clause('START HASH WITH .path END
138
- .template?
139
- START SLASH WITH END') do |path, with_template|
140
- Node::WithElse.new(path, with_template || VOID, VOID, pos(0))
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
- clause('START HASH WITH .subexpression END
144
- .template?
145
- START SLASH WITH END') do |subexpression, with_template|
146
- Node::WithElse.new(subexpression, with_template || VOID, VOID, pos(0))
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
- clause('START HASH WITH .path END
150
- .template?
151
- START ELSE END
152
- .template?
153
- START SLASH WITH END') do |path, with_template, else_template|
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
- clause('START HASH WITH .subexpression END
158
- .template?
159
- START ELSE END
160
- .template?
161
- START SLASH WITH END') do |subexpression, with_template, else_template|
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
- clause('START GT .path .options? END') do |path, options|
166
- Node::Partial.new(path, options || [], pos(0))
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
- production(:options) do
171
- clause('options option') { |options, option| options << option }
172
- clause('option') { |option| [option] }
241
+ def parse_literal
242
+ token = expect(:LITERAL)
243
+ Node::Literal.new(token.value)
173
244
  end
174
245
 
175
- production(:option, '.KEY .expression') do |key, expression|
176
- Node::Option.new(key, expression)
246
+ def parse_variable
247
+ token = expect(:VARIABLE)
248
+ Node::Variable.new(token.value, token.position)
177
249
  end
178
250
 
179
- production(:expressions) do
180
- clause('expressions expression') { |expressions, expression| expressions << expression }
181
- clause('expression') { |expression| [expression] }
251
+ def parse_path
252
+ token = expect(:PATH)
253
+ Node::Path.new(token.value, token.position)
182
254
  end
183
255
 
184
- production(:expression) do
185
- clause('value') { |value| value }
186
- clause('path') { |path| path }
187
- clause('subexpression') { |subexpression| subexpression }
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
- production(:value) do
191
- clause('LITERAL') { |literal| Node::Literal.new(literal) }
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
- production(:path, 'PATH') { |path| Node::Path.new(path, pos(0)) }
271
+ @depth += 1
272
+ @max_depth = @depth if @depth > @max_depth
273
+ result = yield
274
+ @depth -= 1
275
+ result
276
+ end
196
277
 
197
- production(:subexpression, 'LPAREN .path .expressions? .options? RPAREN') do |helper, arguments, options|
198
- Node::SubExpression.new(
199
- helper,
200
- arguments || [],
201
- options || [],
202
- pos(0)
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
- finalize
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 PartialPresenter
3
5
  extend MethodWhitelist
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  class PathFinder
3
5
  def initialize(ast)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  Position = Struct.new(:file_name, :line_number, :line_offset, :length) do
3
5
  def initialize(file_name, line_number, line_offset, length = 0)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/method_whitelist'
2
4
  require 'curlybars/configuration'
3
5
 
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  module Processor
3
5
  module TokenFactory
4
6
  def create_token(type, value, position)
5
- RLTK::Token.new(type, value, position)
7
+ Curlybars::Token.new(type, value, position)
6
8
  end
7
9
  end
8
10
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'curlybars/template_handler'
2
4
  require 'curlybars/dependency_tracker'
3
5
  require 'action_view/dependency_tracker'
@@ -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)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  class SafeBuffer < ActiveSupport::SafeBuffer
3
5
  def concat(buffer)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  class SerializationGuard
3
5
  def initialize(budget:, depth_limit:)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support'
2
4
  require 'action_view'
3
5
  require 'curlybars'
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Curlybars
2
4
  class ValidationContext
3
5
  attr_reader :partial_resolver, :depth
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Curlybars
4
- VERSION = '1.16.1'
4
+ VERSION = '1.17.0.pre'
5
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/string/inflections'
2
4
 
3
5
  module Curlybars
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.parse(tokens)
149
- rescue RLTK::LexingError => lexing_error
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 RLTK::NotInLanguage => not_in_language_error
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.16.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