mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Parse up to emitters and transformers
This commit is contained in:
parent
ea35f38db1
commit
50d1873c7c
@ -24,10 +24,14 @@ module Tartrazine
|
|||||||
# <token type="Keyword"/>
|
# <token type="Keyword"/>
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
class IncludeState < Rule
|
class IncludeStateRule < Rule
|
||||||
property state : String = ""
|
property state : String = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Emitter
|
||||||
|
property type : String = ""
|
||||||
|
end
|
||||||
|
|
||||||
class Lexer
|
class Lexer
|
||||||
property config = {
|
property config = {
|
||||||
name: "",
|
name: "",
|
||||||
@ -73,11 +77,26 @@ module Tartrazine
|
|||||||
rule.pattern = /#{rule_node["pattern"]}/
|
rule.pattern = /#{rule_node["pattern"]}/
|
||||||
else
|
else
|
||||||
# And rules that include a state
|
# And rules that include a state
|
||||||
rule = IncludeState.new
|
rule = IncludeStateRule.new
|
||||||
include_node = rule_node.children.find { |n| n.name == "include" }
|
include_node = rule_node.children.find { |n| n.name == "include" }
|
||||||
rule.state = include_node["state"] if include_node
|
rule.state = include_node["state"] if include_node
|
||||||
end
|
end
|
||||||
state.rules << rule
|
state.rules << rule
|
||||||
|
|
||||||
|
# Rules contain maybe an emitter and maybe a transformer
|
||||||
|
# emitters emit tokens, transformers do things to
|
||||||
|
# the state stack. The transformers go last, but
|
||||||
|
# both kinds are optional 😭
|
||||||
|
|
||||||
|
rule_node.children.each do |node|
|
||||||
|
next unless node.element?
|
||||||
|
case node.name
|
||||||
|
when "pop", "push", "include", "multi", "combine"
|
||||||
|
p! "transformer", node.to_s
|
||||||
|
else
|
||||||
|
p! "emitter", node.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -87,8 +106,7 @@ module Tartrazine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
l = Tartrazine::Lexer.from_xml(File.read("lexers/bash_session.xml"))
|
l = Tartrazine::Lexer.from_xml(File.read("lexers/ada.xml"))
|
||||||
pp! l.states["root"].rules
|
|
||||||
|
|
||||||
# Convenience macros to parse XML
|
# Convenience macros to parse XML
|
||||||
macro xml_to_s(node, name)
|
macro xml_to_s(node, name)
|
||||||
|
Loading…
Reference in New Issue
Block a user