| Class | CodeRay::Scanners::C |
| In: |
lib/coderay/scanners/c.rb
|
| Parent: | Scanner |
| RESERVED_WORDS | = | [ 'asm', 'break', 'case', 'continue', 'default', 'do', 'else', 'enum', 'for', 'goto', 'if', 'return', 'sizeof', 'struct', 'switch', 'typedef', 'union', 'while', 'restrict', # added in C99 ] |
| PREDEFINED_TYPES | = | [ 'int', 'long', 'short', 'char', 'signed', 'unsigned', 'float', 'double', 'bool', 'complex', # added in C99 ] |
| PREDEFINED_CONSTANTS | = | [ 'EOF', 'NULL', 'true', 'false', # added in C99 ] |
| DIRECTIVES | = | [ 'auto', 'extern', 'register', 'static', 'void', 'const', 'volatile', # added in C89 'inline', # added in C99 ] |
| IDENT_KIND | = | WordList.new(:ident). add(RESERVED_WORDS, :reserved). add(PREDEFINED_TYPES, :pre_type). add(DIRECTIVES, :directive). add(PREDEFINED_CONSTANTS, :pre_constant) |
| ESCAPE | = | / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x |
| UNICODE_ESCAPE | = | / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x |
# File lib/coderay/scanners/c.rb, line 43
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