Implement 'combined' emitter

This commit is contained in:
Roberto Alsina 2024-08-03 19:25:09 -03:00
parent f396e5dd33
commit 937b9d50e0
2 changed files with 23 additions and 4 deletions

View File

@ -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

View File

@ -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)