From b1c8793ded6c0f27683905234f8dd9ee4c54017c Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 4 Aug 2024 20:24:48 -0300 Subject: [PATCH] Added logging for trace --- src/actions.cr | 12 ++++++------ src/rules.cr | 8 ++++---- src/tartrazine.cr | 15 +++++++++------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/actions.cr b/src/actions.cr index 3a289a7..1581f72 100644 --- a/src/actions.cr +++ b/src/actions.cr @@ -40,20 +40,20 @@ module Tartrazine states_to_push.each do |state| if state == "#pop" # Pop the state - # puts "Popping state" + Log.trace { "Popping state"} lexer.state_stack.pop else # Really push lexer.state_stack << state - # puts "Pushed #{lexer.state_stack}" + Log.trace {"Pushed #{lexer.state_stack}"} end end [] of Token when "pop" depth = xml["depth"].to_i - # puts "Popping #{depth} states" + Log.trace { "Popping #{depth} states" } 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 lexer.state_stack.pop(depth) end @@ -81,14 +81,14 @@ module Tartrazine # Shunt to another lexer entirely return [] of Token if match.nil? 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) when "usingself" # Shunt to another copy of this lexer return [] of Token if match.nil? 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) when "combined" # Combine two states into one anonymous state diff --git a/src/rules.cr b/src/rules.cr index cfacda9..e0f0f0a 100644 --- a/src/rules.cr +++ b/src/rules.cr @@ -15,14 +15,14 @@ module Tartrazine match = pattern.match(text, pos) # We don't match if the match doesn't move the cursor # 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 # Emit the tokens actions.each do |action| # Emit the token tokens += action.emit(match, lexer) end - # p! xml, match.end, tokens + Log.trace { "#{xml}, #{match.end}, #{tokens}" } return true, match.end, tokens end @@ -46,10 +46,10 @@ module Tartrazine property state : String = "" 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| 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 end return false, pos, [] of Token diff --git a/src/tartrazine.cr b/src/tartrazine.cr index e7c200e..8130dbc 100644 --- a/src/tartrazine.cr +++ b/src/tartrazine.cr @@ -1,12 +1,15 @@ +require "./actions" +require "./rules" require "base58" require "json" +require "log" require "xml" -require "./rules" -require "./actions" module Tartrazine VERSION = "0.1.0" + Log = ::Log.for("tartrazine") + # This implements a lexer for Pygments RegexLexers as expressed # in Chroma's XML serialization. # @@ -61,12 +64,12 @@ module Tartrazine end while pos < text.size 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| matched, new_pos, new_tokens = rule.match(text, pos, self) - # puts "NOT MATCHED: #{rule.xml}" + Log.trace { "NOT MATCHED: #{rule.xml}"} next unless matched - # puts "MATCHED: #{rule.xml}" + Log.trace { "MATCHED: #{rule.xml}" } pos = new_pos tokens += new_tokens @@ -74,7 +77,7 @@ module Tartrazine end # If no rule matches, emit an error token unless matched - # p! "Error at #{pos}" + Log.trace { "Error at #{pos}" } tokens << {type: "Error", value: "#{text[pos]}"} pos += 1 end