mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-12 22:42:23 +00:00
Added logging for trace
This commit is contained in:
parent
2197facd01
commit
b1c8793ded
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user