annotate vendor/gems/coderay-1.0.0/lib/coderay/scanners/c.rb @ 1481:93934eec7b56 issue_540

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