comparison vendor/gems/coderay-0.9.7/lib/coderay/scanners/.svn/text-base/c.rb.svn-base @ 210:0579821a129a

Update to Redmine trunk rev 4802
author Chris Cannam
date Tue, 08 Feb 2011 13:51:46 +0000
parents vendor/plugins/coderay-0.9.2/lib/coderay/scanners/c.rb@513646585e45
children
comparison
equal deleted inserted replaced
128:07fa8a8b56a8 210:0579821a129a
1 module CodeRay
2 module Scanners
3
4 class C < Scanner
5
6 include Streamable
7
8 register_for :c
9 file_extension 'c'
10
11 RESERVED_WORDS = [
12 'asm', 'break', 'case', 'continue', 'default', 'do',
13 'else', 'enum', 'for', 'goto', 'if', 'return',
14 'sizeof', 'struct', 'switch', 'typedef', 'union', 'while',
15 'restrict', # added in C99
16 ]
17
18 PREDEFINED_TYPES = [
19 'int', 'long', 'short', 'char',
20 'signed', 'unsigned', 'float', 'double',
21 'bool', 'complex', # added in C99
22 ]
23
24 PREDEFINED_CONSTANTS = [
25 'EOF', 'NULL',
26 'true', 'false', # added in C99
27 ]
28 DIRECTIVES = [
29 'auto', 'extern', 'register', 'static', 'void',
30 'const', 'volatile', # added in C89
31 'inline', # added in C99
32 ]
33
34 IDENT_KIND = WordList.new(:ident).
35 add(RESERVED_WORDS, :reserved).
36 add(PREDEFINED_TYPES, :pre_type).
37 add(DIRECTIVES, :directive).
38 add(PREDEFINED_CONSTANTS, :pre_constant)
39
40 ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
41 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
42
43 def scan_tokens tokens, options
44
45 state = :initial
46 label_expected = true
47 case_expected = false
48 label_expected_before_preproc_line = nil
49 in_preproc_line = false
50
51 until eos?
52
53 kind = nil
54 match = nil
55
56 case state
57
58 when :initial
59
60 if match = scan(/ \s+ | \\\n /x)
61 if in_preproc_line && match != "\\\n" && match.index(?\n)
62 in_preproc_line = false
63 label_expected = label_expected_before_preproc_line
64 end
65 tokens << [match, :space]
66 next
67
68 elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
69 kind = :comment
70
71 elsif match = scan(/ \# \s* if \s* 0 /x)
72 match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
73 kind = :comment
74
75 elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
76 label_expected = match =~ /[;\{\}]/
77 if case_expected
78 label_expected = true if match == ':'
79 case_expected = false
80 end
81 kind = :operator
82
83 elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
84 kind = IDENT_KIND[match]
85 if kind == :ident && label_expected && !in_preproc_line && scan(/:(?!:)/)
86 kind = :label
87 match << matched
88 else
89 label_expected = false
90 if kind == :reserved
91 case match
92 when 'case', 'default'
93 case_expected = true
94 end
95 end
96 end
97
98 elsif scan(/\$/)
99 kind = :ident
100
101 elsif match = scan(/L?"/)
102 tokens << [:open, :string]
103 if match[0] == ?L
104 tokens << ['L', :modifier]
105 match = '"'
106 end
107 state = :string
108 kind = :delimiter
109
110 elsif scan(/#[ \t]*(\w*)/)
111 kind = :preprocessor
112 in_preproc_line = true
113 label_expected_before_preproc_line = label_expected
114 state = :include_expected if self[1] == 'include'
115
116 elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
117 label_expected = false
118 kind = :char
119
120 elsif scan(/0[xX][0-9A-Fa-f]+/)
121 label_expected = false
122 kind = :hex
123
124 elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
125 label_expected = false
126 kind = :oct
127
128 elsif scan(/(?:\d+)(?![.eEfF])L?L?/)
129 label_expected = false
130 kind = :integer
131
132 elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
133 label_expected = false
134 kind = :float
135
136 else
137 getch
138 kind = :error
139
140 end
141
142 when :string
143 if scan(/[^\\\n"]+/)
144 kind = :content
145 elsif scan(/"/)
146 tokens << ['"', :delimiter]
147 tokens << [:close, :string]
148 state = :initial
149 label_expected = false
150 next
151 elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
152 kind = :char
153 elsif scan(/ \\ | $ /x)
154 tokens << [:close, :string]
155 kind = :error
156 state = :initial
157 label_expected = false
158 else
159 raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
160 end
161
162 when :include_expected
163 if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
164 kind = :include
165 state = :initial
166
167 elsif match = scan(/\s+/)
168 kind = :space
169 state = :initial if match.index ?\n
170
171 else
172 state = :initial
173 next
174
175 end
176
177 else
178 raise_inspect 'Unknown state', tokens
179
180 end
181
182 match ||= matched
183 if $CODERAY_DEBUG and not kind
184 raise_inspect 'Error token %p in line %d' %
185 [[match, kind], line], tokens
186 end
187 raise_inspect 'Empty token', tokens unless match
188
189 tokens << [match, kind]
190
191 end
192
193 if state == :string
194 tokens << [:close, :string]
195 end
196
197 tokens
198 end
199
200 end
201
202 end
203 end