mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Idiomatic changes
This commit is contained in:
parent
78eff45ea0
commit
ada30915c3
15
src/lexer.cr
15
src/lexer.cr
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user