Make formatter a bit more convenient

This commit is contained in:
2024-08-19 11:26:34 -03:00
parent 57e63f2308
commit 3ebedec6c1
4 changed files with 32 additions and 14 deletions

View File

@@ -4,11 +4,16 @@ module Tartrazine
class Json < Formatter
property name = "json"
def format(text : String, lexer : Lexer, io : IO? = nil) : String?
outp = io.nil? ? String::Builder.new("") : io
def format(text : String, lexer : Lexer) : String
outp = String::Builder.new("")
format(text, lexer, outp)
return outp.to_s
end
def format(text : String, lexer : Lexer, io : IO) : Nil
tokenizer = Tokenizer.new(lexer, text)
outp << Tartrazine::Lexer.collapse_tokens(tokenizer.to_a).to_json
return outp.to_s if io.nil?
io << Tartrazine::Lexer.collapse_tokens(tokenizer.to_a).to_json
end
end
end