summaryrefslogtreecommitdiff
path: root/lib/coderay/scanners/c.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/coderay/scanners/c.rb')
-rw-r--r--lib/coderay/scanners/c.rb55
1 files changed, 43 insertions, 12 deletions
diff --git a/lib/coderay/scanners/c.rb b/lib/coderay/scanners/c.rb
index f175204..ac1d0d2 100644
--- a/lib/coderay/scanners/c.rb
+++ b/lib/coderay/scanners/c.rb
@@ -12,23 +12,23 @@ module Scanners
'asm', 'break', 'case', 'continue', 'default', 'do',
'else', 'enum', 'for', 'goto', 'if', 'return',
'sizeof', 'struct', 'switch', 'typedef', 'union', 'while',
- 'restrict', # C99
+ 'restrict', # added in C99
]
PREDEFINED_TYPES = [
'int', 'long', 'short', 'char',
'signed', 'unsigned', 'float', 'double',
- 'bool', 'complex', # C99
+ 'bool', 'complex', # added in C99
]
PREDEFINED_CONSTANTS = [
'EOF', 'NULL',
- 'true', 'false', # C99
+ 'true', 'false', # added in C99
]
DIRECTIVES = [
'auto', 'extern', 'register', 'static', 'void',
- 'const', 'volatile', # C89
- 'inline', # C99
+ 'const', 'volatile', # added in C89
+ 'inline', # added in C99
]
IDENT_KIND = WordList.new(:ident).
@@ -43,6 +43,10 @@ module Scanners
def scan_tokens tokens, options
state = :initial
+ label_expected = true
+ case_expected = false
+ label_expected_before_preproc_line = nil
+ in_preproc_line = false
until eos?
@@ -53,8 +57,13 @@ module Scanners
when :initial
- if scan(/ \s+ | \\\n /x)
- kind = :space
+ 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
elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
kind = :comment
@@ -63,14 +72,27 @@ module Scanners
match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
kind = :comment
- elsif scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
+ elsif match = scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x)
+ label_expected = match =~ /[;\{\}]/
+ if case_expected
+ label_expected = true if match == ':'
+ case_expected = false
+ end
kind = :operator
elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
kind = IDENT_KIND[match]
- if kind == :ident and check(/:(?!:)/)
- # FIXME: don't match a?b:c
+ if kind == :ident && label_expected && !in_preproc_line && scan(/:(?!:)/)
kind = :label
+ match << matched
+ else
+ label_expected = false
+ if kind == :reserved
+ case match
+ when 'case', 'default'
+ case_expected = true
+ end
+ end
end
elsif scan(/\$/)
@@ -85,23 +107,30 @@ module Scanners
state = :string
kind = :delimiter
- elsif scan(/#\s*(\w*)/)
- kind = :preprocessor # FIXME multiline preprocs
+ elsif scan(/#[ \t]*(\w*)/)
+ kind = :preprocessor
+ in_preproc_line = true
+ label_expected_before_preproc_line = label_expected
state = :include_expected if self[1] == 'include'
elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
+ label_expected = false
kind = :char
elsif scan(/0[xX][0-9A-Fa-f]+/)
+ label_expected = false
kind = :hex
elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
+ label_expected = false
kind = :oct
elsif scan(/(?:\d+)(?![.eEfF])L?L?/)
+ label_expected = false
kind = :integer
elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
+ label_expected = false
kind = :float
else
@@ -117,6 +146,7 @@ module Scanners
tokens << ['"', :delimiter]
tokens << [:close, :string]
state = :initial
+ label_expected = false
next
elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
kind = :char
@@ -124,6 +154,7 @@ module Scanners
tokens << [:close, :string]
kind = :error
state = :initial
+ label_expected = false
else
raise_inspect "else case \" reached; %p not handled." % peek(1), tokens
end