mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Improved config parsing
This commit is contained in:
parent
7d7a4294ed
commit
328197add5
@ -3,6 +3,11 @@ require "xml"
|
|||||||
module Tartrazine
|
module Tartrazine
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
|
|
||||||
|
class State
|
||||||
|
property name : String = ""
|
||||||
|
property rules = [] of String
|
||||||
|
end
|
||||||
|
|
||||||
class Lexer
|
class Lexer
|
||||||
property config = {
|
property config = {
|
||||||
name: "",
|
name: "",
|
||||||
@ -12,6 +17,8 @@ module Tartrazine
|
|||||||
priority: 0,
|
priority: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
property states = [] of State
|
||||||
|
|
||||||
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
|
||||||
@ -19,25 +26,39 @@ 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) || [] of String,
|
||||||
filenames: xml_to_a(config, filename),
|
filenames: xml_to_a(config, filename) || [] of String,
|
||||||
mime_types: xml_to_a(config, mime_type),
|
mime_types: xml_to_a(config, mime_type) || [] of String,
|
||||||
priority: xml_to_s(config, priority).to_i,
|
priority: xml_to_i(config, priority) || 0,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
rules = lexer.children.find { |n| n.name == "rules" }
|
||||||
|
if rules
|
||||||
|
# Rules contains states 🤷
|
||||||
|
rules.children.select { |n| n.name == "state" }.each do |node|
|
||||||
|
state = State.new
|
||||||
|
state.name = node["name"]
|
||||||
|
l.states << state
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
l
|
l
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
l = Tartrazine::Lexer.from_xml(File.read("lexers/plaintext.xml"))
|
l = Tartrazine::Lexer.from_xml(File.read("lexers/bash.xml"))
|
||||||
p! l.config
|
p! l.config, l.states
|
||||||
|
|
||||||
# Convenience macros to parse XML
|
# Convenience macros to parse XML
|
||||||
macro xml_to_s(node, name)
|
macro xml_to_s(node, name)
|
||||||
{{node}}.children.find{|n| n.name == "{{name}}".lstrip("_")}.as(XML::Node).content.to_s
|
{{node}}.children.find{|n| n.name == "{{name}}".lstrip("_")}.try &.content.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
macro xml_to_i(node, name)
|
||||||
|
({{node}}.children.find{|n| n.name == "{{name}}".lstrip("_")}.try &.content.to_s.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
macro xml_to_a(node, name)
|
macro xml_to_a(node, name)
|
||||||
|
Loading…
Reference in New Issue
Block a user