Skip to content

Commit

Permalink
Add Liquid::ParseContext#parse_expression for liquid-c node disabling (
Browse files Browse the repository at this point in the history
…#1333)

We would like to be able to disable liquid-c VM rendering at runtime,
but right now expression parsing is done using Expression.parse, which
isn't aware of the parse context.  That prevents us from conditionally
compiling to VM code based on a parse option.
  • Loading branch information
dylanahsmith committed Oct 27, 2020
1 parent 0e52706 commit 10ea614
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/liquid/condition.rb
Expand Up @@ -45,8 +45,8 @@ def self.operators
@@operators
end

def self.parse_expression(markup)
@@method_literals[markup] || Expression.parse(markup)
def self.parse_expression(parse_context, markup)
@@method_literals[markup] || parse_context.parse_expression(markup)
end

attr_reader :attachment, :child_condition
Expand Down
4 changes: 4 additions & 0 deletions lib/liquid/parse_context.rb
Expand Up @@ -23,6 +23,10 @@ def new_block_body
Liquid::BlockBody.new
end

def parse_expression(markup)
Expression.parse(markup)
end

def partial=(value)
@partial = value
@options = value ? partial_options : @template_options
Expand Down
6 changes: 6 additions & 0 deletions lib/liquid/tag.rb
Expand Up @@ -55,5 +55,11 @@ def render_to_output_buffer(context, output)
def blank?
false
end

private

def parse_expression(markup)
parse_context.parse_expression(markup)
end
end
end
4 changes: 2 additions & 2 deletions lib/liquid/tags/case.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(tag_name, markup, options)
@blocks = []

if markup =~ Syntax
@left = Expression.parse(Regexp.last_match(1))
@left = parse_expression(Regexp.last_match(1))
else
raise SyntaxError, options[:locale].t("errors.syntax.case")
end
Expand Down Expand Up @@ -68,7 +68,7 @@ def record_when_condition(markup)

markup = Regexp.last_match(2)

block = Condition.new(@left, '==', Condition.parse_expression(Regexp.last_match(1)))
block = Condition.new(@left, '==', Condition.parse_expression(parse_context, Regexp.last_match(1)))
block.attach(body)
@blocks << block
end
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/cycle.rb
Expand Up @@ -24,7 +24,7 @@ def initialize(tag_name, markup, options)
case markup
when NamedSyntax
@variables = variables_from_string(Regexp.last_match(2))
@name = Expression.parse(Regexp.last_match(1))
@name = parse_expression(Regexp.last_match(1))
when SimpleSyntax
@variables = variables_from_string(markup)
@name = @variables.to_s
Expand Down Expand Up @@ -61,7 +61,7 @@ def render_to_output_buffer(context, output)
def variables_from_string(markup)
markup.split(',').collect do |var|
var =~ /\s*(#{QuotedFragment})\s*/o
Regexp.last_match(1) ? Expression.parse(Regexp.last_match(1)) : nil
Regexp.last_match(1) ? parse_expression(Regexp.last_match(1)) : nil
end.compact
end

Expand Down
8 changes: 4 additions & 4 deletions lib/liquid/tags/for.rb
Expand Up @@ -97,7 +97,7 @@ def lax_parse(markup)
collection_name = Regexp.last_match(2)
@reversed = !!Regexp.last_match(3)
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
@collection_name = parse_expression(collection_name)
markup.scan(TagAttributes) do |key, value|
set_attribute(key, value)
end
Expand All @@ -112,7 +112,7 @@ def strict_parse(markup)
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')

collection_name = p.expression
@collection_name = Expression.parse(collection_name)
@collection_name = parse_expression(collection_name)

@name = "#{@variable_name}-#{collection_name}"
@reversed = p.id?('reversed')
Expand Down Expand Up @@ -198,10 +198,10 @@ def set_attribute(key, expr)
@from = if expr == 'continue'
:continue
else
Expression.parse(expr)
parse_expression(expr)
end
when 'limit'
@limit = Expression.parse(expr)
@limit = parse_expression(expr)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tags/if.rb
Expand Up @@ -71,7 +71,7 @@ def push_block(tag, markup)
end

def parse_expression(markup)
Condition.parse_expression(markup)
Condition.parse_expression(parse_context, markup)
end

def lax_parse(markup)
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tags/include.rb
Expand Up @@ -32,12 +32,12 @@ def initialize(tag_name, markup, options)
variable_name = Regexp.last_match(3)

@alias_name = Regexp.last_match(5)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
@template_name_expr = parse_expression(template_name)
@attributes = {}

markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end

else
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tags/render.rb
Expand Up @@ -19,13 +19,13 @@ def initialize(tag_name, markup, options)
variable_name = Regexp.last_match(4)

@alias_name = Regexp.last_match(6)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
@template_name_expr = parse_expression(template_name)
@for = (with_or_for == FOR)

@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/table_row.rb
Expand Up @@ -10,10 +10,10 @@ def initialize(tag_name, markup, options)
super
if markup =~ Syntax
@variable_name = Regexp.last_match(1)
@collection_name = Expression.parse(Regexp.last_match(2))
@collection_name = parse_expression(Regexp.last_match(2))
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end
else
raise SyntaxError, options[:locale].t("errors.syntax.table_row")
Expand Down

0 comments on commit 10ea614

Please sign in to comment.