|
|
@ -96,11 +96,15 @@ module Lox
|
|
|
|
while WHILE
|
|
|
|
while WHILE
|
|
|
|
].each_slice(2).to_h.transform_values(&:to_sym)
|
|
|
|
].each_slice(2).to_h.transform_values(&:to_sym)
|
|
|
|
|
|
|
|
|
|
|
|
State = Struct.new(:ss, :tokens, :errors, :line) do
|
|
|
|
class State < Struct.new(:ss, :tokens, :errors, :line)
|
|
|
|
def eos? = ss.eos?
|
|
|
|
def eos? = ss.eos?
|
|
|
|
def scan(re) = ss.scan(re)
|
|
|
|
def scan(re) = ss.scan(re)
|
|
|
|
def pos = ss.pos
|
|
|
|
def pos = ss.pos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(src)
|
|
|
|
|
|
|
|
super(StringScanner.new(src), [], [], 1)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def add_token(type, text: nil, literal: nil)
|
|
|
|
def add_token(type, text: nil, literal: nil)
|
|
|
|
text ||= ss.matched
|
|
|
|
text ||= ss.matched
|
|
|
|
self.tokens << Token.new(type, text, literal, line)
|
|
|
|
self.tokens << Token.new(type, text, literal, line)
|
|
|
@ -108,7 +112,7 @@ module Lox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def scan(src)
|
|
|
|
def scan(src)
|
|
|
|
state = State.new(StringScanner.new(src), [], [], 1)
|
|
|
|
state = State.new(src)
|
|
|
|
|
|
|
|
|
|
|
|
until state.eos?
|
|
|
|
until state.eos?
|
|
|
|
case
|
|
|
|
case
|
|
|
@ -135,6 +139,8 @@ module Lox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fail unless state.errors.empty?
|
|
|
|
|
|
|
|
|
|
|
|
state.add_token(:EOF, text: "")
|
|
|
|
state.add_token(:EOF, text: "")
|
|
|
|
state.tokens
|
|
|
|
state.tokens
|
|
|
|
end
|
|
|
|
end
|
|
|
|