annotate vendor/gems/coderay-0.9.7/lib/coderay/scanners/diff.rb @ 855:7294e8db2515 bug_162

Close obsolete branch bug_162
author Chris Cannam
date Thu, 14 Jul 2011 11:59:19 +0100
parents 0579821a129a
children
rev   line source
Chris@210 1 module CodeRay
Chris@210 2 module Scanners
Chris@210 3
Chris@210 4 class Diff < Scanner
Chris@210 5
Chris@210 6 register_for :diff
Chris@210 7 title 'diff output'
Chris@210 8
Chris@210 9 def scan_tokens tokens, options
Chris@210 10
Chris@210 11 line_kind = nil
Chris@210 12 state = :initial
Chris@210 13
Chris@210 14 until eos?
Chris@210 15 kind = match = nil
Chris@210 16
Chris@210 17 if match = scan(/\n/)
Chris@210 18 if line_kind
Chris@210 19 tokens << [:end_line, line_kind]
Chris@210 20 line_kind = nil
Chris@210 21 end
Chris@210 22 tokens << [match, :space]
Chris@210 23 next
Chris@210 24 end
Chris@210 25
Chris@210 26 case state
Chris@210 27
Chris@210 28 when :initial
Chris@210 29 if match = scan(/--- |\+\+\+ |=+|_+/)
Chris@210 30 tokens << [:begin_line, line_kind = :head]
Chris@210 31 tokens << [match, :head]
Chris@210 32 next unless match = scan(/.+/)
Chris@210 33 kind = :plain
Chris@210 34 elsif match = scan(/Index: |Property changes on: /)
Chris@210 35 tokens << [:begin_line, line_kind = :head]
Chris@210 36 tokens << [match, :head]
Chris@210 37 next unless match = scan(/.+/)
Chris@210 38 kind = :plain
Chris@210 39 elsif match = scan(/Added: /)
Chris@210 40 tokens << [:begin_line, line_kind = :head]
Chris@210 41 tokens << [match, :head]
Chris@210 42 next unless match = scan(/.+/)
Chris@210 43 kind = :plain
Chris@210 44 state = :added
Chris@210 45 elsif match = scan(/\\ /)
Chris@210 46 tokens << [:begin_line, line_kind = :change]
Chris@210 47 tokens << [match, :change]
Chris@210 48 next unless match = scan(/.+/)
Chris@210 49 kind = :plain
Chris@210 50 elsif match = scan(/@@(?>[^@\n]*)@@/)
Chris@210 51 if check(/\n|$/)
Chris@210 52 tokens << [:begin_line, line_kind = :change]
Chris@210 53 else
Chris@210 54 tokens << [:open, :change]
Chris@210 55 end
Chris@210 56 tokens << [match[0,2], :change]
Chris@210 57 tokens << [match[2...-2], :plain]
Chris@210 58 tokens << [match[-2,2], :change]
Chris@210 59 tokens << [:close, :change] unless line_kind
Chris@210 60 next unless match = scan(/.+/)
Chris@210 61 kind = :plain
Chris@210 62 elsif match = scan(/\+/)
Chris@210 63 tokens << [:begin_line, line_kind = :insert]
Chris@210 64 tokens << [match, :insert]
Chris@210 65 next unless match = scan(/.+/)
Chris@210 66 kind = :plain
Chris@210 67 elsif match = scan(/-/)
Chris@210 68 tokens << [:begin_line, line_kind = :delete]
Chris@210 69 tokens << [match, :delete]
Chris@210 70 next unless match = scan(/.+/)
Chris@210 71 kind = :plain
Chris@210 72 elsif scan(/ .*/)
Chris@210 73 kind = :comment
Chris@210 74 elsif scan(/.+/)
Chris@210 75 tokens << [:begin_line, line_kind = :comment]
Chris@210 76 kind = :plain
Chris@210 77 else
Chris@210 78 raise_inspect 'else case rached'
Chris@210 79 end
Chris@210 80
Chris@210 81 when :added
Chris@210 82 if match = scan(/ \+/)
Chris@210 83 tokens << [:begin_line, line_kind = :insert]
Chris@210 84 tokens << [match, :insert]
Chris@210 85 next unless match = scan(/.+/)
Chris@210 86 kind = :plain
Chris@210 87 else
Chris@210 88 state = :initial
Chris@210 89 next
Chris@210 90 end
Chris@210 91 end
Chris@210 92
Chris@210 93 match ||= matched
Chris@210 94 if $CODERAY_DEBUG and not kind
Chris@210 95 raise_inspect 'Error token %p in line %d' %
Chris@210 96 [[match, kind], line], tokens
Chris@210 97 end
Chris@210 98 raise_inspect 'Empty token', tokens unless match
Chris@210 99
Chris@210 100 tokens << [match, kind]
Chris@210 101 end
Chris@210 102
Chris@210 103 tokens << [:end_line, line_kind] if line_kind
Chris@210 104 tokens
Chris@210 105 end
Chris@210 106
Chris@210 107 end
Chris@210 108
Chris@210 109 end
Chris@210 110 end