diff options
author | murphy <murphy@rubychan.de> | 2009-10-19 17:39:28 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2009-10-19 17:39:28 +0000 |
commit | 87783c60d93caa75fc8cd117e5d9cb9be0a558f3 (patch) | |
tree | 2476af94630886747d5f93816ec38b5a3c1ab58a /lib/coderay/scanners | |
parent | 9eba0c7decb383e3ef73a3fee375e725442cde56 (diff) | |
download | coderay-87783c60d93caa75fc8cd117e5d9cb9be0a558f3.tar.gz |
Updating C scanner:
* Ruby 1.9 warnings (yeah, it's really a pain)
* added DIRECTIVES
* fixed a bug that highlighted // as an operator (bah!)
* accept $ in variable names
* accept intLL literals
* recover silently from :include_expected on unexpected tokens
Diffstat (limited to 'lib/coderay/scanners')
-rw-r--r-- | lib/coderay/scanners/c.rb | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/coderay/scanners/c.rb b/lib/coderay/scanners/c.rb index 9ec81ce..f175204 100644 --- a/lib/coderay/scanners/c.rb +++ b/lib/coderay/scanners/c.rb @@ -9,17 +9,14 @@ module Scanners file_extension 'c' RESERVED_WORDS = [ - '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 + 'asm', 'break', 'case', 'continue', 'default', 'do', + 'else', 'enum', 'for', 'goto', 'if', 'return', + 'sizeof', 'struct', 'switch', 'typedef', 'union', 'while', + 'restrict', # C99 ] PREDEFINED_TYPES = [ - 'int', 'long', 'short', 'char', 'void', + 'int', 'long', 'short', 'char', 'signed', 'unsigned', 'float', 'double', 'bool', 'complex', # C99 ] @@ -28,13 +25,19 @@ module Scanners 'EOF', 'NULL', 'true', 'false', # C99 ] + DIRECTIVES = [ + 'auto', 'extern', 'register', 'static', 'void', + 'const', 'volatile', # C89 + 'inline', # C99 + ] IDENT_KIND = WordList.new(:ident). add(RESERVED_WORDS, :reserved). add(PREDEFINED_TYPES, :pre_type). + add(DIRECTIVES, :directive). add(PREDEFINED_CONSTANTS, :pre_constant) - ESCAPE = / [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x + ESCAPE = / [rbfntv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x def scan_tokens tokens, options @@ -60,16 +63,19 @@ module Scanners match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos? kind = :comment - elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%]+ | \.(?!\d) /x) + elsif scan(/ [-+*=<>?:;,!&^|()\[\]{}~%]+ | \/=? | \.(?!\d) /x) kind = :operator elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) kind = IDENT_KIND[match] if kind == :ident and check(/:(?!:)/) - match << scan(/:/) + # FIXME: don't match a?b:c kind = :label end + elsif scan(/\$/) + kind = :ident + elsif match = scan(/L?"/) tokens << [:open, :string] if match[0] == ?L @@ -92,7 +98,7 @@ module Scanners elsif scan(/(?:0[0-7]+)(?![89.eEfF])/) kind = :oct - elsif scan(/(?:\d+)(?![.eEfF])/) + elsif scan(/(?:\d+)(?![.eEfF])L?L?/) kind = :integer elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/) @@ -132,8 +138,8 @@ module Scanners state = :initial if match.index ?\n else - getch - kind = :error + state = :initial + next end |