summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/yaml.rb
blob: 32c8e2cb982dfa327538fafdc4ced9544978a2f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
module CodeRay
module Scanners
  
  # Scanner for YAML.
  #
  # Based on the YAML scanner from Syntax by Jamis Buck.
  class YAML < Scanner
    
    register_for :yaml
    file_extension 'yml'
    
    KINDS_NOT_LOC = :all
    
  protected
    
    def scan_tokens encoder, options
      
      state = :initial
      key_indent = string_indent = 0
      
      until eos?
        
        key_indent = nil if bol?
        
        if match = scan(/ +[\t ]*/)
          encoder.text_token match, :space
          
        elsif match = scan(/\n+/)
          encoder.text_token match, :space
          state = :initial if match.index(?\n)
          
        elsif match = scan(/#.*/)
          encoder.text_token match, :comment
          
        elsif bol? and case
          when match = scan(/---|\.\.\./)
            encoder.begin_group :head
            encoder.text_token match, :head
            encoder.end_group :head
            next
          when match = scan(/%.*/)
            encoder.text_token match, :doctype
            next
          end
        
        elsif state == :value and case
          when !check(/(?:"[^"]*")(?=: |:$)/) && match = scan(/"/)
            encoder.begin_group :string
            encoder.text_token match, :delimiter
            encoder.text_token match, :content if (match = scan(/ [^"\\]* (?: \\. [^"\\]* )* /mx)) && !match.empty?
            encoder.text_token match, :delimiter if match = scan(/"/)
            encoder.end_group :string
            next
          when match = scan(/[|>][-+]?/)
            encoder.begin_group :string
            encoder.text_token match, :delimiter
            string_indent = key_indent || column(pos - match.size) - 1
            encoder.text_token matched, :content if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
            encoder.end_group :string
            next
          when match = scan(/(?![!"*&]).+?(?=$|\s+#)/)
            encoder.begin_group :string
            encoder.text_token match, :content
            string_indent = key_indent || column(pos - match.size) - 1
            encoder.text_token matched, :content if scan(/(?:\n+ {#{string_indent + 1}}.*)+/)
            encoder.end_group :string
            next
          end
          
        elsif case
          when match = scan(/[-:](?= |$)/)
            state = :value if state == :colon && (match == ':' || match == '-')
            state = :value if state == :initial && match == '-'
            encoder.text_token match, :operator
            next
          when match = scan(/[,{}\[\]]/)
            encoder.text_token match, :operator
            next
          when state == :initial && match = scan(/[-\w.()\/ ]*\S(?= *:(?: |$))/)
            encoder.text_token match, :key
            key_indent = column(pos - match.size) - 1
            state = :colon
            next
          when match = scan(/(?:"[^"\n]*"|'[^'\n]*')(?= *:(?: |$))/)
            encoder.begin_group :key
            encoder.text_token match[0,1], :delimiter
            encoder.text_token match[1..-2], :content if match.size > 2
            encoder.text_token match[-1,1], :delimiter
            encoder.end_group :key
            key_indent = column(pos - match.size) - 1
            state = :colon
            next
          when match = scan(/(![\w\/]+)(:([\w:]+))?/)
            encoder.text_token self[1], :type
            if self[2]
              encoder.text_token ':', :operator
              encoder.text_token self[3], :class
            end
            next
          when match = scan(/&\S+/)
            encoder.text_token match, :variable
            next
          when match = scan(/\*\w+/)
            encoder.text_token match, :global_variable
            next
          when match = scan(/<</)
            encoder.text_token match, :class_variable
            next
          when match = scan(/\d\d:\d\d:\d\d/)
            encoder.text_token match, :octal
            next
          when match = scan(/\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d(\.\d+)? [-+]\d\d:\d\d/)
            encoder.text_token match, :octal
            next
          when match = scan(/:\w+/)
            encoder.text_token match, :symbol
            next
          when match = scan(/[^:\s]+(:(?! |$)[^:\s]*)* .*/)
            encoder.text_token match, :error
            next
          when match = scan(/[^:\s]+(:(?! |$)[^:\s]*)*/)
            encoder.text_token match, :error
            next
          end
          
        else
          raise if eos?
          encoder.text_token getch, :error
          
        end
        
      end
      
      encoder
    end
    
  end
  
end
end