kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.3 KiB
204 lines
5.3 KiB
18 years ago
|
module CodeRay
|
||
|
module Scanners
|
||
|
|
||
|
class C < Scanner
|
||
|
|
||
15 years ago
|
include Streamable
|
||
15 years ago
|
|
||
|
register_for :c
|
||
|
file_extension 'c'
|
||
18 years ago
|
|
||
|
RESERVED_WORDS = [
|
||
15 years ago
|
'asm', 'break', 'case', 'continue', 'default', 'do',
|
||
|
'else', 'enum', 'for', 'goto', 'if', 'return',
|
||
|
'sizeof', 'struct', 'switch', 'typedef', 'union', 'while',
|
||
|
'restrict', # added in C99
|
||
18 years ago
|
]
|
||
|
|
||
|
PREDEFINED_TYPES = [
|
||
15 years ago
|
'int', 'long', 'short', 'char',
|
||
18 years ago
|
'signed', 'unsigned', 'float', 'double',
|
||
15 years ago
|
'bool', 'complex', # added in C99
|
||
18 years ago
|
]
|
||
|
|
||
|
PREDEFINED_CONSTANTS = [
|
||
|
'EOF', 'NULL',
|
||
15 years ago
|
'true', 'false', # added in C99
|
||
|
]
|
||
|
DIRECTIVES = [
|
||
|
'auto', 'extern', 'register', 'static', 'void',
|
||
|
'const', 'volatile', # added in C89
|
||
|
'inline', # added in C99
|
||
18 years ago
|
]
|
||
|
|
||
|
IDENT_KIND = WordList.new(:ident).
|
||
|
add(RESERVED_WORDS, :reserved).
|
||
|
add(PREDEFINED_TYPES, :pre_type).
|
||
15 years ago
|
add(DIRECTIVES, :directive).
|
||
18 years ago
|
add(PREDEFINED_CONSTANTS, :pre_constant)
|
||
|
|
||
15 years ago
|
ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
|
||
18 years ago
|
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
|
||
|
|
||
|
def scan_tokens tokens, options
|
||
|
|
||
|
state = :initial
|
||
15 years ago
|
label_expected = true
|
||
|
case_expected = false
|
||
|
label_expected_before_preproc_line = nil
|
||
|
in_preproc_line = false
|
||
18 years ago
|
|
||
|
until eos?
|
||
|
|
||
|
kind = nil
|
||
|
match = nil
|
||
|
|
||
|
case state
|
||
|
|
||
|
when :initial
|
||
|
|
||
15 years ago
|
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
|
||
18 years ago
|
|
||
|
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
|
||
|
|
||
15 years ago
|
elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
|
||
|
label_expected = match =~ /[;\{\}]/
|
||
|
if case_expected
|
||
|
label_expected = true if match == ':'
|
||
|
case_expected = false
|
||
|
end
|
||
18 years ago
|
kind = :operator
|
||
|
|
||
|
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
|
||
|
kind = IDENT_KIND[match]
|
||
15 years ago
|
if kind == :ident && label_expected && !in_preproc_line && scan(/:(?!:)/)
|
||
18 years ago
|
kind = :label
|
||
15 years ago
|
match << matched
|
||
|
else
|
||
|
label_expected = false
|
||
|
if kind == :reserved
|
||
|
case match
|
||
|
when 'case', 'default'
|
||
|
case_expected = true
|
||
|
end
|
||
|
end
|
||
18 years ago
|
end
|
||
|
|
||
15 years ago
|
elsif scan(/\$/)
|
||
|
kind = :ident
|
||
|
|
||
18 years ago
|
elsif match = scan(/L?"/)
|
||
|
tokens << [:open, :string]
|
||
|
if match[0] == ?L
|
||
|
tokens << ['L', :modifier]
|
||
|
match = '"'
|
||
|
end
|
||
|
state = :string
|
||
|
kind = :delimiter
|
||
|
|
||
15 years ago
|
elsif scan(/#[ \t]*(\w*)/)
|
||
|
kind = :preprocessor
|
||
|
in_preproc_line = true
|
||
|
label_expected_before_preproc_line = label_expected
|
||
18 years ago
|
state = :include_expected if self[1] == 'include'
|
||
|
|
||
|
elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
kind = :char
|
||
|
|
||
|
elsif scan(/0[xX][0-9A-Fa-f]+/)
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
kind = :hex
|
||
|
|
||
|
elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
kind = :oct
|
||
|
|
||
15 years ago
|
elsif scan(/(?:\d+)(?![.eEfF])L?L?/)
|
||
|
label_expected = false
|
||
18 years ago
|
kind = :integer
|
||
|
|
||
|
elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
kind = :float
|
||
|
|
||
|
else
|
||
|
getch
|
||
|
kind = :error
|
||
|
|
||
|
end
|
||
|
|
||
|
when :string
|
||
|
if scan(/[^\\\n"]+/)
|
||
|
kind = :content
|
||
|
elsif scan(/"/)
|
||
|
tokens << ['"', :delimiter]
|
||
|
tokens << [:close, :string]
|
||
|
state = :initial
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
next
|
||
|
elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
|
||
|
kind = :char
|
||
|
elsif scan(/ \\ | $ /x)
|
||
|
tokens << [:close, :string]
|
||
|
kind = :error
|
||
|
state = :initial
|
||
15 years ago
|
label_expected = false
|
||
18 years ago
|
else
|
||
|
raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
|
||
|
end
|
||
|
|
||
|
when :include_expected
|
||
15 years ago
|
if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
|
||
18 years ago
|
kind = :include
|
||
|
state = :initial
|
||
|
|
||
|
elsif match = scan(/\s+/)
|
||
|
kind = :space
|
||
|
state = :initial if match.index ?\n
|
||
|
|
||
|
else
|
||
15 years ago
|
state = :initial
|
||
|
next
|
||
18 years ago
|
|
||
|
end
|
||
|
|
||
|
else
|
||
|
raise_inspect 'Unknown state', tokens
|
||
|
|
||
|
end
|
||
|
|
||
|
match ||= matched
|
||
15 years ago
|
if $CODERAY_DEBUG and not kind
|
||
18 years ago
|
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
|