2007-08-16 00:20:18 +04:00
|
|
|
module CodeRay
|
|
|
|
module Scanners
|
|
|
|
|
|
|
|
class C < Scanner
|
|
|
|
|
2009-11-20 18:50:06 +03:00
|
|
|
include Streamable
|
2010-03-16 23:29:12 +03:00
|
|
|
|
|
|
|
register_for :c
|
|
|
|
file_extension 'c'
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
RESERVED_WORDS = [
|
2010-03-16 23:29:12 +03:00
|
|
|
'asm', 'break', 'case', 'continue', 'default', 'do',
|
|
|
|
'else', 'enum', 'for', 'goto', 'if', 'return',
|
|
|
|
'sizeof', 'struct', 'switch', 'typedef', 'union', 'while',
|
|
|
|
'restrict', # added in C99
|
2007-08-16 00:20:18 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
PREDEFINED_TYPES = [
|
2010-03-16 23:29:12 +03:00
|
|
|
'int', 'long', 'short', 'char',
|
2007-08-16 00:20:18 +04:00
|
|
|
'signed', 'unsigned', 'float', 'double',
|
2010-03-16 23:29:12 +03:00
|
|
|
'bool', 'complex', # added in C99
|
2007-08-16 00:20:18 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
PREDEFINED_CONSTANTS = [
|
|
|
|
'EOF', 'NULL',
|
2010-03-16 23:29:12 +03:00
|
|
|
'true', 'false', # added in C99
|
|
|
|
]
|
|
|
|
DIRECTIVES = [
|
|
|
|
'auto', 'extern', 'register', 'static', 'void',
|
|
|
|
'const', 'volatile', # added in C89
|
|
|
|
'inline', # added in C99
|
2007-08-16 00:20:18 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
IDENT_KIND = WordList.new(:ident).
|
|
|
|
add(RESERVED_WORDS, :reserved).
|
|
|
|
add(PREDEFINED_TYPES, :pre_type).
|
2010-03-16 23:29:12 +03:00
|
|
|
add(DIRECTIVES, :directive).
|
2007-08-16 00:20:18 +04:00
|
|
|
add(PREDEFINED_CONSTANTS, :pre_constant)
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
|
2007-08-16 00:20:18 +04:00
|
|
|
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
|
|
|
|
|
|
|
|
def scan_tokens tokens, options
|
|
|
|
|
|
|
|
state = :initial
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = true
|
|
|
|
case_expected = false
|
|
|
|
label_expected_before_preproc_line = nil
|
|
|
|
in_preproc_line = false
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
until eos?
|
|
|
|
|
|
|
|
kind = nil
|
|
|
|
match = nil
|
|
|
|
|
|
|
|
case state
|
|
|
|
|
|
|
|
when :initial
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
if match = scan(/ \s+ | \\\n /x)
|
|
|
|
if in_preproc_line && match != "\\\n" && match.index(?\n)
|
|
|
|
in_preproc_line = false
|
|
|
|
label_expected = label_expected_before_preproc_line
|
|
|
|
end
|
|
|
|
tokens << [match, :space]
|
|
|
|
next
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
|
|
|
|
kind = :comment
|
|
|
|
|
|
|
|
elsif match = scan(/ \# \s* if \s* 0 /x)
|
|
|
|
match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
|
|
|
|
kind = :comment
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
|
|
|
|
label_expected = match =~ /[;\{\}]/
|
|
|
|
if case_expected
|
|
|
|
label_expected = true if match == ':'
|
|
|
|
case_expected = false
|
|
|
|
end
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :operator
|
|
|
|
|
|
|
|
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
|
|
|
|
kind = IDENT_KIND[match]
|
2010-03-16 23:29:12 +03:00
|
|
|
if kind == :ident && label_expected && !in_preproc_line && scan(/:(?!:)/)
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :label
|
2010-03-16 23:29:12 +03:00
|
|
|
match << matched
|
|
|
|
else
|
|
|
|
label_expected = false
|
|
|
|
if kind == :reserved
|
|
|
|
case match
|
|
|
|
when 'case', 'default'
|
|
|
|
case_expected = true
|
|
|
|
end
|
|
|
|
end
|
2007-08-16 00:20:18 +04:00
|
|
|
end
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
elsif scan(/\$/)
|
|
|
|
kind = :ident
|
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
elsif match = scan(/L?"/)
|
|
|
|
tokens << [:open, :string]
|
|
|
|
if match[0] == ?L
|
|
|
|
tokens << ['L', :modifier]
|
|
|
|
match = '"'
|
|
|
|
end
|
|
|
|
state = :string
|
|
|
|
kind = :delimiter
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
elsif scan(/#[ \t]*(\w*)/)
|
|
|
|
kind = :preprocessor
|
|
|
|
in_preproc_line = true
|
|
|
|
label_expected_before_preproc_line = label_expected
|
2007-08-16 00:20:18 +04:00
|
|
|
state = :include_expected if self[1] == 'include'
|
|
|
|
|
|
|
|
elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :char
|
|
|
|
|
|
|
|
elsif scan(/0[xX][0-9A-Fa-f]+/)
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :hex
|
|
|
|
|
|
|
|
elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :oct
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
elsif scan(/(?:\d+)(?![.eEfF])L?L?/)
|
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :integer
|
|
|
|
|
|
|
|
elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :float
|
|
|
|
|
|
|
|
else
|
|
|
|
getch
|
|
|
|
kind = :error
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
when :string
|
|
|
|
if scan(/[^\\\n"]+/)
|
|
|
|
kind = :content
|
|
|
|
elsif scan(/"/)
|
|
|
|
tokens << ['"', :delimiter]
|
|
|
|
tokens << [:close, :string]
|
|
|
|
state = :initial
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
next
|
|
|
|
elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
|
|
|
|
kind = :char
|
|
|
|
elsif scan(/ \\ | $ /x)
|
|
|
|
tokens << [:close, :string]
|
|
|
|
kind = :error
|
|
|
|
state = :initial
|
2010-03-16 23:29:12 +03:00
|
|
|
label_expected = false
|
2007-08-16 00:20:18 +04:00
|
|
|
else
|
|
|
|
raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
when :include_expected
|
2010-03-14 12:07:21 +03:00
|
|
|
if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
|
2007-08-16 00:20:18 +04:00
|
|
|
kind = :include
|
|
|
|
state = :initial
|
|
|
|
|
|
|
|
elsif match = scan(/\s+/)
|
|
|
|
kind = :space
|
|
|
|
state = :initial if match.index ?\n
|
|
|
|
|
|
|
|
else
|
2010-03-16 23:29:12 +03:00
|
|
|
state = :initial
|
|
|
|
next
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
raise_inspect 'Unknown state', tokens
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
match ||= matched
|
2010-03-16 23:29:12 +03:00
|
|
|
if $CODERAY_DEBUG and not kind
|
2007-08-16 00:20:18 +04:00
|
|
|
raise_inspect 'Error token %p in line %d' %
|
|
|
|
[[match, kind], line], tokens
|
|
|
|
end
|
|
|
|
raise_inspect 'Empty token', tokens unless match
|
|
|
|
|
|
|
|
tokens << [match, kind]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if state == :string
|
|
|
|
tokens << [:close, :string]
|
|
|
|
end
|
|
|
|
|
|
|
|
tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|