Added logging for trace

This commit is contained in:
Roberto Alsina 2024-08-04 20:24:48 -03:00
parent 2197facd01
commit b1c8793ded
3 changed files with 19 additions and 16 deletions

View File

@ -40,20 +40,20 @@ module Tartrazine
states_to_push.each do |state| states_to_push.each do |state|
if state == "#pop" if state == "#pop"
# Pop the state # Pop the state
# puts "Popping state" Log.trace { "Popping state"}
lexer.state_stack.pop lexer.state_stack.pop
else else
# Really push # Really push
lexer.state_stack << state lexer.state_stack << state
# puts "Pushed #{lexer.state_stack}" Log.trace {"Pushed #{lexer.state_stack}"}
end end
end end
[] of Token [] of Token
when "pop" when "pop"
depth = xml["depth"].to_i depth = xml["depth"].to_i
# puts "Popping #{depth} states" Log.trace { "Popping #{depth} states" }
if lexer.state_stack.size <= depth if lexer.state_stack.size <= depth
# puts "Can't pop #{depth} states, only have #{lexer.state_stack.size}" Log.trace { "Can't pop #{depth} states, only have #{lexer.state_stack.size}" }
else else
lexer.state_stack.pop(depth) lexer.state_stack.pop(depth)
end end
@ -81,14 +81,14 @@ module Tartrazine
# Shunt to another lexer entirely # Shunt to another lexer entirely
return [] of Token if match.nil? return [] of Token if match.nil?
lexer_name = xml["lexer"].downcase lexer_name = xml["lexer"].downcase
# pp! "to tokenize:", match[match_group] Log.trace { "to tokenize: #{match[match_group]}" }
Tartrazine.get_lexer(lexer_name).tokenize(match[match_group], usingself: true) Tartrazine.get_lexer(lexer_name).tokenize(match[match_group], usingself: true)
when "usingself" when "usingself"
# Shunt to another copy of this lexer # Shunt to another copy of this lexer
return [] of Token if match.nil? return [] of Token if match.nil?
new_lexer = Lexer.from_xml(lexer.xml) new_lexer = Lexer.from_xml(lexer.xml)
# pp! "to tokenize:", match[match_group] Log.trace { "to tokenize: #{match[match_group]}" }
new_lexer.tokenize(match[match_group], usingself: true) new_lexer.tokenize(match[match_group], usingself: true)
when "combined" when "combined"
# Combine two states into one anonymous state # Combine two states into one anonymous state

View File

@ -15,14 +15,14 @@ module Tartrazine
match = pattern.match(text, pos) match = pattern.match(text, pos)
# We don't match if the match doesn't move the cursor # We don't match if the match doesn't move the cursor
# because that causes infinite loops # because that causes infinite loops
# pp! match, pattern.inspect, text, pos Log.trace { "#{match}, #{pattern.inspect}, #{text}, #{pos}" }
return false, pos, [] of Token if match.nil? || match.end == 0 return false, pos, [] of Token if match.nil? || match.end == 0
# Emit the tokens # Emit the tokens
actions.each do |action| actions.each do |action|
# Emit the token # Emit the token
tokens += action.emit(match, lexer) tokens += action.emit(match, lexer)
end end
# p! xml, match.end, tokens Log.trace { "#{xml}, #{match.end}, #{tokens}" }
return true, match.end, tokens return true, match.end, tokens
end end
@ -46,10 +46,10 @@ module Tartrazine
property state : String = "" property state : String = ""
def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token)) def match(text, pos, lexer) : Tuple(Bool, Int32, Array(Token))
# puts "Including state #{state} from #{lexer.state_stack.last}" Log.trace { "Including state #{state} from #{lexer.state_stack.last}" }
lexer.states[state].rules.each do |rule| lexer.states[state].rules.each do |rule|
matched, new_pos, new_tokens = rule.match(text, pos, lexer) matched, new_pos, new_tokens = rule.match(text, pos, lexer)
# p! xml, new_pos, new_tokens if matched Log.trace { "#{xml}, #{new_pos}, #{new_tokens}" } if matched
return true, new_pos, new_tokens if matched return true, new_pos, new_tokens if matched
end end
return false, pos, [] of Token return false, pos, [] of Token

View File

@ -1,12 +1,15 @@
require "./actions"
require "./rules"
require "base58" require "base58"
require "json" require "json"
require "log"
require "xml" require "xml"
require "./rules"
require "./actions"
module Tartrazine module Tartrazine
VERSION = "0.1.0" VERSION = "0.1.0"
Log = ::Log.for("tartrazine")
# This implements a lexer for Pygments RegexLexers as expressed # This implements a lexer for Pygments RegexLexers as expressed
# in Chroma's XML serialization. # in Chroma's XML serialization.
# #
@ -61,12 +64,12 @@ module Tartrazine
end end
while pos < text.size while pos < text.size
state = states[@state_stack.last] state = states[@state_stack.last]
# puts "Stack is #{@state_stack} State is #{state.name}, pos is #{pos}, text is #{text[pos..pos + 10]}" Log.trace { "Stack is #{@state_stack} State is #{state.name}, pos is #{pos}, text is #{text[pos..pos + 10]}" }
state.rules.each do |rule| state.rules.each do |rule|
matched, new_pos, new_tokens = rule.match(text, pos, self) matched, new_pos, new_tokens = rule.match(text, pos, self)
# puts "NOT MATCHED: #{rule.xml}" Log.trace { "NOT MATCHED: #{rule.xml}"}
next unless matched next unless matched
# puts "MATCHED: #{rule.xml}" Log.trace { "MATCHED: #{rule.xml}" }
pos = new_pos pos = new_pos
tokens += new_tokens tokens += new_tokens
@ -74,7 +77,7 @@ module Tartrazine
end end
# If no rule matches, emit an error token # If no rule matches, emit an error token
unless matched unless matched
# p! "Error at #{pos}" Log.trace { "Error at #{pos}" }
tokens << {type: "Error", value: "#{text[pos]}"} tokens << {type: "Error", value: "#{text[pos]}"}
pos += 1 pos += 1
end end