assign regex to a constant

This commit is contained in:
Ashwin Maroli
2018-03-13 23:36:56 +05:30
parent a0bec1f873
commit e4da4d49d2
4 changed files with 24 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ module Liquid
class BlockBody
FullToken = /\A#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
WhitespaceOrNothing = /\A\s*\z/
TAGSTART = "{%".freeze
VARSTART = "{{".freeze
@@ -43,7 +44,7 @@ module Liquid
end
parse_context.trim_whitespace = false
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
@blank &&= !!(token =~ WhitespaceOrNothing)
end
parse_context.line_number = tokenizer.line_number
end

View File

@@ -21,20 +21,24 @@ module Liquid
'empty'.freeze => MethodLiteral.new(:empty?, '').freeze
}
SINGLE_QUOTED_STRING = /\A'(.*)'\z/m
DOUBLE_QUOTED_STRING = /\A"(.*)"\z/m
INTEGERS_REGEX = /\A(-?\d+)\z/
FLOATS_REGEX = /\A(-?\d+\.\d+)\z/
RANGES_REGEX = /\A\((\S+)\.\.(\S+)\)\z/
def self.parse(markup)
if LITERALS.key?(markup)
LITERALS[markup]
else
case markup
when /\A'(.*)'\z/m # Single quoted strings
when SINGLE_QUOTED_STRING, DOUBLE_QUOTED_STRING
$1
when /\A"(.*)"\z/m # Double quoted strings
$1
when /\A(-?\d+)\z/ # Integer and floats
when INTEGERS_REGEX
$1.to_i
when /\A\((\S+)\.\.(\S+)\)\z/ # Ranges
when RANGES_REGEX
RangeLookup.parse($1, $2)
when /\A(-?\d[\d\.]+)\z/ # Floats
when FLOATS_REGEX
$1.to_f
else
VariableLookup.parse(markup)

View File

@@ -19,6 +19,7 @@ module Liquid
NUMBER_LITERAL = /-?\d+(\.\d+)?/
DOTDOT = /\.\./
COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains(?=\s)/
WHITESPACE_OR_NOTHING = /\s*/
def initialize(input)
@ss = StringScanner.new(input)
@@ -28,7 +29,7 @@ module Liquid
@output = []
until @ss.eos?
@ss.skip(/\s*/)
@ss.skip(WHITESPACE_OR_NOTHING)
break if @ss.eos?
tok = case
when t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t]

View File

@@ -10,10 +10,16 @@ module Liquid
# {{ user | link }}
#
class Variable
FilterMarkupRegex = /#{FilterSeparator}\s*(.*)/om
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
JustTagAttributes = /\A#{TagAttributes}\z/o
MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/om
attr_accessor :filters, :name, :line_number
attr_reader :parse_context
alias_method :options, :parse_context
include ParserSwitching
def initialize(markup, parse_context)
@@ -35,17 +41,17 @@ module Liquid
def lax_parse(markup)
@filters = []
return unless markup =~ /(#{QuotedFragment})(.*)/om
return unless markup =~ MarkupWithQuotedFragment
name_markup = $1
filter_markup = $2
@name = Expression.parse(name_markup)
if filter_markup =~ /#{FilterSeparator}\s*(.*)/om
if filter_markup =~ FilterMarkupRegex
filters = $1.scan(FilterParser)
filters.each do |f|
next unless f =~ /\w+/
filtername = Regexp.last_match(0)
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
filterargs = f.scan(FilterArgsRegex).flatten
@filters << parse_filter_expressions(filtername, filterargs)
end
end
@@ -91,7 +97,7 @@ module Liquid
filter_args = []
keyword_args = {}
unparsed_args.each do |a|
if matches = a.match(/\A#{TagAttributes}\z/o)
if matches = a.match(JustTagAttributes)
keyword_args[matches[1]] = Expression.parse(matches[2])
else
filter_args << Expression.parse(a)