refactor Lexer to only take StringScanner

This commit is contained in:
Michael Go
2025-01-07 14:32:46 -04:00
parent 253ec81b56
commit 04bd9dbe90
5 changed files with 8 additions and 10 deletions
+2 -4
View File
@@ -26,8 +26,7 @@ module Liquid
WHITESPACE_OR_NOTHING = /\s*/ WHITESPACE_OR_NOTHING = /\s*/
class << self class << self
def tokenize(input, ss = StringScanner.new(input)) def tokenize(ss)
ss.string = input
output = [] output = []
until ss.eos? until ss.eos?
@@ -158,8 +157,7 @@ module Liquid
# rubocop:disable Metrics/BlockNesting # rubocop:disable Metrics/BlockNesting
class << self class << self
def tokenize(input, ss) def tokenize(ss)
ss.string = input
output = [] output = []
until ss.eos? until ss.eos?
+2 -2
View File
@@ -3,7 +3,7 @@
module Liquid module Liquid
class ParseContext class ParseContext
attr_accessor :locale, :line_number, :trim_whitespace, :depth attr_accessor :locale, :line_number, :trim_whitespace, :depth
attr_reader :partial, :warnings, :error_mode, :environment, :string_scanner, :expression_cache attr_reader :partial, :warnings, :error_mode, :environment
def initialize(options = Const::EMPTY_HASH) def initialize(options = Const::EMPTY_HASH)
@environment = options.fetch(:environment, Environment.default) @environment = options.fetch(:environment, Environment.default)
@@ -44,7 +44,7 @@ module Liquid
end end
def parse_expression(markup) def parse_expression(markup)
Expression.parse(markup, string_scanner, expression_cache) Expression.parse(markup, @string_scanner, @expression_cache)
end end
def partial=(value) def partial=(value)
+2 -2
View File
@@ -3,8 +3,8 @@
module Liquid module Liquid
class Parser class Parser
def initialize(input) def initialize(input)
input = input.is_a?(StringScanner) ? input : StringScanner.new(input.to_s) ss = input.is_a?(StringScanner) ? input : StringScanner.new(input)
@tokens = Lexer.tokenize(input, string_scanner) @tokens = Lexer.tokenize(ss)
@p = 0 # pointer to current location @p = 0 # pointer to current location
end end
+1 -1
View File
@@ -134,6 +134,6 @@ class LexerUnitTest < Minitest::Test
private private
def tokenize(input) def tokenize(input)
Lexer.tokenize(input, StringScanner.new("")) Lexer.tokenize(StringScanner.new(input))
end end
end end
+1 -1
View File
@@ -85,6 +85,6 @@ class ParserUnitTest < Minitest::Test
private private
def new_parser(str) def new_parser(str)
Parser.new(str, StringScanner.new("")) Parser.new(StringScanner.new(str))
end end
end end