annotate vendor/gems/coderay-1.0.0/lib/coderay/encoders/debug.rb @ 1022:f2ec92061fca browsing

Merge from live branch
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 13 Nov 2012 10:35:40 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 module CodeRay
Chris@909 2 module Encoders
Chris@909 3
Chris@909 4 # = Debug Encoder
Chris@909 5 #
Chris@909 6 # Fast encoder producing simple debug output.
Chris@909 7 #
Chris@909 8 # It is readable and diff-able and is used for testing.
Chris@909 9 #
Chris@909 10 # You cannot fully restore the tokens information from the
Chris@909 11 # output, because consecutive :space tokens are merged.
Chris@909 12 # Use Tokens#dump for caching purposes.
Chris@909 13 #
Chris@909 14 # See also: Scanners::Debug
Chris@909 15 class Debug < Encoder
Chris@909 16
Chris@909 17 register_for :debug
Chris@909 18
Chris@909 19 FILE_EXTENSION = 'raydebug'
Chris@909 20
Chris@909 21 def initialize options = {}
Chris@909 22 super
Chris@909 23 @opened = []
Chris@909 24 end
Chris@909 25
Chris@909 26 def text_token text, kind
Chris@909 27 if kind == :space
Chris@909 28 @out << text
Chris@909 29 else
Chris@909 30 # TODO: Escape (
Chris@909 31 text = text.gsub(/[)\\]/, '\\\\\0') # escape ) and \
Chris@909 32 @out << kind.to_s << '(' << text << ')'
Chris@909 33 end
Chris@909 34 end
Chris@909 35
Chris@909 36 def begin_group kind
Chris@909 37 @opened << kind
Chris@909 38 @out << kind.to_s << '<'
Chris@909 39 end
Chris@909 40
Chris@909 41 def end_group kind
Chris@909 42 if @opened.last != kind
Chris@909 43 puts @out
Chris@909 44 raise "we are inside #{@opened.inspect}, not #{kind}"
Chris@909 45 end
Chris@909 46 @opened.pop
Chris@909 47 @out << '>'
Chris@909 48 end
Chris@909 49
Chris@909 50 def begin_line kind
Chris@909 51 @out << kind.to_s << '['
Chris@909 52 end
Chris@909 53
Chris@909 54 def end_line kind
Chris@909 55 @out << ']'
Chris@909 56 end
Chris@909 57
Chris@909 58 end
Chris@909 59
Chris@909 60 end
Chris@909 61 end