This commit is contained in:
Roberto Alsina 2024-08-02 17:09:05 -03:00
parent 444db0abda
commit 7d7a4294ed

View File

@ -5,21 +5,13 @@ module Tartrazine
class Lexer class Lexer
property config = { property config = {
name: "", name: "",
aliases: [] of String, aliases: [] of String,
filenames: [] of String, filenames: [] of String,
mime_types: [] of String, mime_types: [] of String,
priority: 0, priority: 0,
} }
macro xml_to_s(node, name)
{{node}}.children.find{|n| n.name == "{{name}}".lstrip("_")}.as(XML::Node).content.to_s
end
macro xml_to_a(node, name)
{{node}}.children.select{|n| n.name == "{{name}}".lstrip("_")}.map {|n| n.content.to_s}
end
def self.from_xml(xml : String) : Lexer def self.from_xml(xml : String) : Lexer
l = Lexer.new l = Lexer.new
lexer = XML.parse(xml).first_element_child lexer = XML.parse(xml).first_element_child
@ -27,8 +19,8 @@ module Tartrazine
config = lexer.children.find { |n| n.name == "config" } config = lexer.children.find { |n| n.name == "config" }
if config if config
l.config = { l.config = {
name: xml_to_s(config, name), name: xml_to_s(config, name),
aliases: xml_to_a(config, _alias), aliases: xml_to_a(config, _alias),
filenames: xml_to_a(config, filename), filenames: xml_to_a(config, filename),
mime_types: xml_to_a(config, mime_type), mime_types: xml_to_a(config, mime_type),
priority: xml_to_s(config, priority).to_i, priority: xml_to_s(config, priority).to_i,
@ -40,5 +32,14 @@ module Tartrazine
end end
end end
l = Tartrazine::Lexer.from_xml(File.read("chroma/lexers/embedded/plaintext.xml")) l = Tartrazine::Lexer.from_xml(File.read("lexers/plaintext.xml"))
p! l.config p! l.config
# Convenience macros to parse XML
macro xml_to_s(node, name)
{{node}}.children.find{|n| n.name == "{{name}}".lstrip("_")}.as(XML::Node).content.to_s
end
macro xml_to_a(node, name)
{{node}}.children.select{|n| n.name == "{{name}}".lstrip("_")}.map {|n| n.content.to_s}
end