Shopify/liquid — pull request #325 by dylanahsmith (2014-03-21 06:01:05+00:00)
Title: Allow quoted single curly braces in variables.

@shopify/liquid for review
cc @shenberg

Fixes #213

## Problem

Previously `{{ variable | remove '}' }}` would get tokenized into tokens ``{{ variable | remove '}` and `' }}`, then the first token would cause a variable was not properly terminated SyntaxError.

The reason for this was the VariableIncompleteEnd regex `/\}\}?/` which would match the single curly brace in the variable tag rather than the following curly brace pair.

## Solution

Use VariableEnd instead of VariableIncompleteEnd.

It appeared as if the purpose of VariableIncompleteEnd was to make parsing more lax, but it turns out that it was only making parsing more strict, since it would always raise a SyntaxError if it matched a single curly brace.

```ruby
Liquid::Template.parse("{{ name }")
```

used to result in the error

`Liquid::SyntaxError: Variable '{{ name }' was not properly terminated with regexp: /\}\}/`

but with just the change to use VariableEnd instead of VariableIncompleteEnd the error message would end up being

`Liquid::SyntaxError: Variable '{{' was not properly terminated with regexp: /\}\}/`

So this pull request also improves the error message so that it shows truncated content of the unterminated variable, even without matching a single `}`

`Liquid::SyntaxError: Variable '{{ name }' was not properly terminated with '}}'`

## Benchmark

I also found that the parsing simplifications in this pull request speed up the benchmark

before

```
Rehearsal ------------------------------------------------
parse: 4.240000 0.040000 4.280000 ( 4.292421)
parse & run: 9.220000 0.050000 9.270000 ( 9.266793)
-------------------------------------- total: 13.550000sec

user system total real
parse: 4.180000 0.030000 4.210000 ( 4.224276)
parse & run: 9.500000 0.050000 9.550000 ( 9.545989)
```

after

```
Rehearsal ------------------------------------------------
parse: 3.980000 0.040000 4.020000 ( 4.016855)
parse & run: 8.970000 0.040000 9.010000 ( 9.011526)
-------------------------------------- total: 13.030000sec

user system total real
parse: 3.940000 0.030000 3.970000 ( 3.974736)
parse & run: 8.920000 0.030000 8.950000 ( 8.946374)
```