mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
Implement 'combined' emitter
This commit is contained in:
parent
f396e5dd33
commit
937b9d50e0
@ -8,6 +8,10 @@ targets:
|
|||||||
tartrazine:
|
tartrazine:
|
||||||
main: src/tartrazine.cr
|
main: src/tartrazine.cr
|
||||||
|
|
||||||
crystal: '>= 1.13.0'
|
dependencies:
|
||||||
|
base58:
|
||||||
|
github: crystal-china/base58.cr
|
||||||
|
|
||||||
|
crystal: ">= 1.13.0"
|
||||||
|
|
||||||
license: MIT
|
license: MIT
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
require "xml"
|
require "base58"
|
||||||
require "json"
|
require "json"
|
||||||
|
require "xml"
|
||||||
|
|
||||||
module Tartrazine
|
module Tartrazine
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
@ -13,6 +14,13 @@ module Tartrazine
|
|||||||
class State
|
class State
|
||||||
property name : String = ""
|
property name : String = ""
|
||||||
property rules = [] of Rule
|
property rules = [] of Rule
|
||||||
|
|
||||||
|
def +(other : State)
|
||||||
|
new_state = State.new
|
||||||
|
new_state.name = Random.base58(8)
|
||||||
|
new_state.rules = rules + other.rules
|
||||||
|
new_state
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Rule
|
class Rule
|
||||||
@ -131,6 +139,13 @@ module Tartrazine
|
|||||||
return [] of Token if match.nil?
|
return [] of Token if match.nil?
|
||||||
lexer_name = xml["lexer"].downcase
|
lexer_name = xml["lexer"].downcase
|
||||||
LEXERS[lexer_name].tokenize(match[0])
|
LEXERS[lexer_name].tokenize(match[0])
|
||||||
|
when "combined"
|
||||||
|
# Combine two states into one anonymous state
|
||||||
|
states = xml.attributes.select { |a| a.name == "state" }.map &.content
|
||||||
|
new_state = states.map {|name| lexer.states[name]}.reduce { |s1, s2| s1 + s2 }
|
||||||
|
lexer.states[new_state.name] = new_state
|
||||||
|
lexer.state_stack << new_state.name
|
||||||
|
[] of Token
|
||||||
else
|
else
|
||||||
raise Exception.new("Unknown emitter type: #{type}: #{xml}")
|
raise Exception.new("Unknown emitter type: #{type}: #{xml}")
|
||||||
end
|
end
|
||||||
@ -316,7 +331,7 @@ def test_file(testname, lexer)
|
|||||||
rescue ex : Exception
|
rescue ex : Exception
|
||||||
puts ">>>ERROR"
|
puts ">>>ERROR"
|
||||||
p! ex
|
p! ex
|
||||||
return
|
exit 1
|
||||||
end
|
end
|
||||||
outp = IO::Memory.new
|
outp = IO::Memory.new
|
||||||
i = IO::Memory.new(test)
|
i = IO::Memory.new(test)
|
||||||
|
Loading…
Reference in New Issue
Block a user