##// END OF EJS Templates
Fixed: Unable to use angular braces after include word in c code highlighting (#1230)....
Jean-Philippe Lang -
r1462:6ebdf848a583
parent child
Show More
@@ -1,165 +1,165
1 1 module CodeRay
2 2 module Scanners
3 3
4 4 class C < Scanner
5 5
6 6 register_for :c
7 7
8 8 include Streamable
9 9
10 10 RESERVED_WORDS = [
11 11 'asm', 'break', 'case', 'continue', 'default', 'do', 'else',
12 12 'for', 'goto', 'if', 'return', 'switch', 'while',
13 13 'struct', 'union', 'enum', 'typedef',
14 14 'static', 'register', 'auto', 'extern',
15 15 'sizeof',
16 16 'volatile', 'const', # C89
17 17 'inline', 'restrict', # C99
18 18 ]
19 19
20 20 PREDEFINED_TYPES = [
21 21 'int', 'long', 'short', 'char', 'void',
22 22 'signed', 'unsigned', 'float', 'double',
23 23 'bool', 'complex', # C99
24 24 ]
25 25
26 26 PREDEFINED_CONSTANTS = [
27 27 'EOF', 'NULL',
28 28 'true', 'false', # C99
29 29 ]
30 30
31 31 IDENT_KIND = WordList.new(:ident).
32 32 add(RESERVED_WORDS, :reserved).
33 33 add(PREDEFINED_TYPES, :pre_type).
34 34 add(PREDEFINED_CONSTANTS, :pre_constant)
35 35
36 36 ESCAPE = / [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
37 37 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
38 38
39 39 def scan_tokens tokens, options
40 40
41 41 state = :initial
42 42
43 43 until eos?
44 44
45 45 kind = nil
46 46 match = nil
47 47
48 48 case state
49 49
50 50 when :initial
51 51
52 52 if scan(/ \s+ | \\\n /x)
53 53 kind = :space
54 54
55 55 elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
56 56 kind = :comment
57 57
58 58 elsif match = scan(/ \# \s* if \s* 0 /x)
59 59 match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
60 60 kind = :comment
61 61
62 62 elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x)
63 63 kind = :operator
64 64
65 65 elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
66 66 kind = IDENT_KIND[match]
67 67 if kind == :ident and check(/:(?!:)/)
68 68 match << scan(/:/)
69 69 kind = :label
70 70 end
71 71
72 72 elsif match = scan(/L?"/)
73 73 tokens << [:open, :string]
74 74 if match[0] == ?L
75 75 tokens << ['L', :modifier]
76 76 match = '"'
77 77 end
78 78 state = :string
79 79 kind = :delimiter
80 80
81 81 elsif scan(/#\s*(\w*)/)
82 82 kind = :preprocessor # FIXME multiline preprocs
83 83 state = :include_expected if self[1] == 'include'
84 84
85 85 elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
86 86 kind = :char
87 87
88 88 elsif scan(/0[xX][0-9A-Fa-f]+/)
89 89 kind = :hex
90 90
91 91 elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
92 92 kind = :oct
93 93
94 94 elsif scan(/(?:\d+)(?![.eEfF])/)
95 95 kind = :integer
96 96
97 97 elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
98 98 kind = :float
99 99
100 100 else
101 101 getch
102 102 kind = :error
103 103
104 104 end
105 105
106 106 when :string
107 107 if scan(/[^\\\n"]+/)
108 108 kind = :content
109 109 elsif scan(/"/)
110 110 tokens << ['"', :delimiter]
111 111 tokens << [:close, :string]
112 112 state = :initial
113 113 next
114 114 elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
115 115 kind = :char
116 116 elsif scan(/ \\ | $ /x)
117 117 tokens << [:close, :string]
118 118 kind = :error
119 119 state = :initial
120 120 else
121 121 raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
122 122 end
123 123
124 124 when :include_expected
125 if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
125 if scan(/[^\n]+/)
126 126 kind = :include
127 127 state = :initial
128 128
129 129 elsif match = scan(/\s+/)
130 130 kind = :space
131 131 state = :initial if match.index ?\n
132 132
133 133 else
134 134 getch
135 135 kind = :error
136 136
137 137 end
138 138
139 139 else
140 140 raise_inspect 'Unknown state', tokens
141 141
142 142 end
143 143
144 144 match ||= matched
145 145 if $DEBUG and not kind
146 146 raise_inspect 'Error token %p in line %d' %
147 147 [[match, kind], line], tokens
148 148 end
149 149 raise_inspect 'Empty token', tokens unless match
150 150
151 151 tokens << [match, kind]
152 152
153 153 end
154 154
155 155 if state == :string
156 156 tokens << [:close, :string]
157 157 end
158 158
159 159 tokens
160 160 end
161 161
162 162 end
163 163
164 164 end
165 165 end
General Comments 0
You need to be logged in to leave comments. Login now