##// END OF EJS Templates
Do not html escape code that is going to be highlighted (#2985, #3359)....
Do not html escape code that is going to be highlighted (#2985, #3359). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3582 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r2965:4e3202d2a2c5
r3468:7621463acb1c
Show More
c.rb
165 lines | 3.9 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 module CodeRay
module Scanners
class C < Scanner
Jean-Philippe Lang
Upgrade Coderay to 0.9.0....
r2900 register_for :c
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965
include Streamable
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638
RESERVED_WORDS = [
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 'asm', 'break', 'case', 'continue', 'default', 'do', 'else',
'for', 'goto', 'if', 'return', 'switch', 'while',
'struct', 'union', 'enum', 'typedef',
'static', 'register', 'auto', 'extern',
'sizeof',
'volatile', 'const', # C89
'inline', 'restrict', # C99
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 ]
PREDEFINED_TYPES = [
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 'int', 'long', 'short', 'char', 'void',
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 'signed', 'unsigned', 'float', 'double',
'bool', 'complex', # C99
]
PREDEFINED_CONSTANTS = [
'EOF', 'NULL',
'true', 'false', # C99
]
IDENT_KIND = WordList.new(:ident).
add(RESERVED_WORDS, :reserved).
add(PREDEFINED_TYPES, :pre_type).
add(PREDEFINED_CONSTANTS, :pre_constant)
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 ESCAPE = / [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
def scan_tokens tokens, options
state = :initial
until eos?
kind = nil
match = nil
case state
when :initial
if scan(/ \s+ | \\\n /x)
kind = :space
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
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x)
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 kind = :operator
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
kind = IDENT_KIND[match]
if kind == :ident and check(/:(?!:)/)
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 match << scan(/:/)
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 kind = :label
end
elsif match = scan(/L?"/)
tokens << [:open, :string]
if match[0] == ?L
tokens << ['L', :modifier]
match = '"'
end
state = :string
kind = :delimiter
elsif scan(/#\s*(\w*)/)
kind = :preprocessor # FIXME multiline preprocs
state = :include_expected if self[1] == 'include'
elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
kind = :char
elsif scan(/0[xX][0-9A-Fa-f]+/)
kind = :hex
elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
kind = :oct
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 elsif scan(/(?:\d+)(?![.eEfF])/)
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 kind = :integer
elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
kind = :float
else
getch
kind = :error
end
when :string
if scan(/[^\\\n"]+/)
kind = :content
elsif scan(/"/)
tokens << ['"', :delimiter]
tokens << [:close, :string]
state = :initial
next
elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
kind = :char
elsif scan(/ \\ | $ /x)
tokens << [:close, :string]
kind = :error
state = :initial
else
raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
end
when :include_expected
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 if scan(/[^\n]+/)
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638 kind = :include
state = :initial
elsif match = scan(/\s+/)
kind = :space
state = :initial if match.index ?\n
else
Jean-Philippe Lang
Reverts r3014 (CodeRay back to 0.7.6)....
r2965 getch
kind = :error
Jean-Philippe Lang
Added syntax highlightment for repository files (using CodeRay)....
r638
end
else
raise_inspect 'Unknown state', tokens
end
match ||= matched
if $DEBUG and not kind
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