Idiomatic changes

This commit is contained in:
Roberto Alsina 2024-08-15 23:16:29 -03:00
parent 78eff45ea0
commit ada30915c3

View File

@ -83,30 +83,29 @@ module Tartrazine
text += "\n" text += "\n"
end end
# We operate in bytes from now on
text_bytes = text.to_slice text_bytes = text.to_slice
# Loop through the text, applying rules # Loop through the text, matching rules
while pos < text_bytes.size while pos < text_bytes.size
state = states[@state_stack.last] states[@state_stack.last].rules.each do |rule|
# Log.trace { "Stack is #{@state_stack} State is #{state.name}, pos is #{pos}, text is #{text[pos..pos + 10]}" }
state.rules.each do |rule|
matched, new_pos, new_tokens = rule.match(text_bytes, pos, self) matched, new_pos, new_tokens = rule.match(text_bytes, pos, self)
if matched if matched
# Move position forward, save the tokens, # Move position forward, save the tokens,
# tokenize from the new position
pos = new_pos pos = new_pos
tokens += new_tokens tokens += new_tokens
break break
end end
end end
# If no rule matches, emit an error token if !matched
unless matched # at EOL, emit the newline, reset state to "root"
if text_bytes[pos] == 10u8 if text_bytes[pos] == 10u8
# at EOL, reset state to "root"
tokens << {type: "Text", value: "\n"} tokens << {type: "Text", value: "\n"}
@state_stack = ["root"] @state_stack = ["root"]
else else
# Emit an error token
tokens << {type: "Error", value: String.new(text_bytes[pos..pos])} tokens << {type: "Error", value: String.new(text_bytes[pos..pos])}
end end
# Move forward 1
pos += 1 pos += 1
end end
end end