Implemented push

This commit is contained in:
Roberto Alsina 2024-08-03 07:24:55 -03:00
parent 2c49457ca3
commit ba9a25e56e

View File

@ -28,7 +28,7 @@ module Tartrazine
# Emit the tokens # Emit the tokens
emitters.each do |emitter| emitters.each do |emitter|
# Emit the token # Emit the token
tokens += emitter.emit(match) tokens += emitter.emit(match, lexer)
end end
return true, match.end, tokens return true, match.end, tokens
end end
@ -66,7 +66,7 @@ module Tartrazine
def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token))
tokens = [] of Token tokens = [] of Token
emitters.each do |emitter| emitters.each do |emitter|
tokens += emitter.emit(nil) tokens += emitter.emit(nil, lexer)
end end
return true, pos, tokens return true, pos, tokens
end end
@ -79,13 +79,16 @@ module Tartrazine
def initialize(@type : String, @xml : XML::Node?) def initialize(@type : String, @xml : XML::Node?)
end end
def emit(match : Regex::MatchData?) : Array(Token) def emit(match : Regex::MatchData?, lexer : Lexer) : Array(Token)
case type case type
when "token" when "token"
raise Exception.new "Can't have a token without a match" if match.nil? raise Exception.new "Can't have a token without a match" if match.nil?
[Token.new(type: xml["type"], value: match[0])] [Token.new(type: xml["type"], value: match[0])]
when "push"
lexer.state_stack << xml["state"]
return [] of Token
else else
raise Exception.new("Unknown emitter type: #{type}") raise Exception.new("Unknown emitter type: #{type}: #{xml}")
end end
end end
end end
@ -223,9 +226,8 @@ end
# Parse some plaintext # Parse some plaintext
puts lexers["plaintext"].tokenize("Hello, world!\n") puts lexers["plaintext"].tokenize("Hello, world!\n")
pp! lexers["pepe"]
# Now some bash # Now some bash
puts lexers["pepe"].tokenize("echo 'Hello, world!'\n") puts lexers["Bash"].tokenize("echo 'Hello, world!'\n")
# Convenience macros to parse XML # Convenience macros to parse XML
macro xml_to_s(node, name) macro xml_to_s(node, name)