annotate vendor/gems/coderay-1.0.0/lib/coderay/scanners/delphi.rb @ 1472:f0b798dad2d6 feature_526

Close obsolete branch feature_526
author Chris Cannam
date Tue, 20 Nov 2012 19:45:51 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 module CodeRay
Chris@909 2 module Scanners
Chris@909 3
Chris@909 4 # Scanner for the Delphi language (Object Pascal).
Chris@909 5 #
Chris@909 6 # Alias: +pascal+
Chris@909 7 class Delphi < Scanner
Chris@909 8
Chris@909 9 register_for :delphi
Chris@909 10 file_extension 'pas'
Chris@909 11
Chris@909 12 KEYWORDS = [
Chris@909 13 'and', 'array', 'as', 'at', 'asm', 'at', 'begin', 'case', 'class',
Chris@909 14 'const', 'constructor', 'destructor', 'dispinterface', 'div', 'do',
Chris@909 15 'downto', 'else', 'end', 'except', 'exports', 'file', 'finalization',
Chris@909 16 'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in',
Chris@909 17 'inherited', 'initialization', 'inline', 'interface', 'is', 'label',
Chris@909 18 'library', 'mod', 'nil', 'not', 'object', 'of', 'or', 'out', 'packed',
Chris@909 19 'procedure', 'program', 'property', 'raise', 'record', 'repeat',
Chris@909 20 'resourcestring', 'set', 'shl', 'shr', 'string', 'then', 'threadvar',
Chris@909 21 'to', 'try', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with',
Chris@909 22 'xor', 'on',
Chris@909 23 ] # :nodoc:
Chris@909 24
Chris@909 25 DIRECTIVES = [
Chris@909 26 'absolute', 'abstract', 'assembler', 'at', 'automated', 'cdecl',
Chris@909 27 'contains', 'deprecated', 'dispid', 'dynamic', 'export',
Chris@909 28 'external', 'far', 'forward', 'implements', 'local',
Chris@909 29 'near', 'nodefault', 'on', 'overload', 'override',
Chris@909 30 'package', 'pascal', 'platform', 'private', 'protected', 'public',
Chris@909 31 'published', 'read', 'readonly', 'register', 'reintroduce',
Chris@909 32 'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs',
Chris@909 33 'virtual', 'write', 'writeonly',
Chris@909 34 ] # :nodoc:
Chris@909 35
Chris@909 36 IDENT_KIND = WordList::CaseIgnoring.new(:ident).
Chris@909 37 add(KEYWORDS, :keyword).
Chris@909 38 add(DIRECTIVES, :directive) # :nodoc:
Chris@909 39
Chris@909 40 NAME_FOLLOWS = WordList::CaseIgnoring.new(false).
Chris@909 41 add(%w(procedure function .)) # :nodoc:
Chris@909 42
Chris@909 43 protected
Chris@909 44
Chris@909 45 def scan_tokens encoder, options
Chris@909 46
Chris@909 47 state = :initial
Chris@909 48 last_token = ''
Chris@909 49
Chris@909 50 until eos?
Chris@909 51
Chris@909 52 if state == :initial
Chris@909 53
Chris@909 54 if match = scan(/ \s+ /x)
Chris@909 55 encoder.text_token match, :space
Chris@909 56 next
Chris@909 57
Chris@909 58 elsif match = scan(%r! \{ \$ [^}]* \}? | \(\* \$ (?: .*? \*\) | .* ) !mx)
Chris@909 59 encoder.text_token match, :preprocessor
Chris@909 60 next
Chris@909 61
Chris@909 62 elsif match = scan(%r! // [^\n]* | \{ [^}]* \}? | \(\* (?: .*? \*\) | .* ) !mx)
Chris@909 63 encoder.text_token match, :comment
Chris@909 64 next
Chris@909 65
Chris@909 66 elsif match = scan(/ <[>=]? | >=? | :=? | [-+=*\/;,@\^|\(\)\[\]] | \.\. /x)
Chris@909 67 encoder.text_token match, :operator
Chris@909 68
Chris@909 69 elsif match = scan(/\./)
Chris@909 70 encoder.text_token match, :operator
Chris@909 71 next if last_token == 'end'
Chris@909 72
Chris@909 73 elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x)
Chris@909 74 encoder.text_token match, NAME_FOLLOWS[last_token] ? :ident : IDENT_KIND[match]
Chris@909 75
Chris@909 76 elsif match = skip(/ ' ( [^\n']|'' ) (?:'|$) /x)
Chris@909 77 encoder.begin_group :char
Chris@909 78 encoder.text_token "'", :delimiter
Chris@909 79 encoder.text_token self[1], :content
Chris@909 80 encoder.text_token "'", :delimiter
Chris@909 81 encoder.end_group :char
Chris@909 82 next
Chris@909 83
Chris@909 84 elsif match = scan(/ ' /x)
Chris@909 85 encoder.begin_group :string
Chris@909 86 encoder.text_token match, :delimiter
Chris@909 87 state = :string
Chris@909 88
Chris@909 89 elsif match = scan(/ \# (?: \d+ | \$[0-9A-Fa-f]+ ) /x)
Chris@909 90 encoder.text_token match, :char
Chris@909 91
Chris@909 92 elsif match = scan(/ \$ [0-9A-Fa-f]+ /x)
Chris@909 93 encoder.text_token match, :hex
Chris@909 94
Chris@909 95 elsif match = scan(/ (?: \d+ ) (?![eE]|\.[^.]) /x)
Chris@909 96 encoder.text_token match, :integer
Chris@909 97
Chris@909 98 elsif match = scan(/ \d+ (?: \.\d+ (?: [eE][+-]? \d+ )? | [eE][+-]? \d+ ) /x)
Chris@909 99 encoder.text_token match, :float
Chris@909 100
Chris@909 101 else
Chris@909 102 encoder.text_token getch, :error
Chris@909 103 next
Chris@909 104
Chris@909 105 end
Chris@909 106
Chris@909 107 elsif state == :string
Chris@909 108 if match = scan(/[^\n']+/)
Chris@909 109 encoder.text_token match, :content
Chris@909 110 elsif match = scan(/''/)
Chris@909 111 encoder.text_token match, :char
Chris@909 112 elsif match = scan(/'/)
Chris@909 113 encoder.text_token match, :delimiter
Chris@909 114 encoder.end_group :string
Chris@909 115 state = :initial
Chris@909 116 next
Chris@909 117 elsif match = scan(/\n/)
Chris@909 118 encoder.end_group :string
Chris@909 119 encoder.text_token match, :space
Chris@909 120 state = :initial
Chris@909 121 else
Chris@909 122 raise "else case \' reached; %p not handled." % peek(1), encoder
Chris@909 123 end
Chris@909 124
Chris@909 125 else
Chris@909 126 raise 'else-case reached', encoder
Chris@909 127
Chris@909 128 end
Chris@909 129
Chris@909 130 last_token = match
Chris@909 131
Chris@909 132 end
Chris@909 133
Chris@909 134 if state == :string
Chris@909 135 encoder.end_group state
Chris@909 136 end
Chris@909 137
Chris@909 138 encoder
Chris@909 139 end
Chris@909 140
Chris@909 141 end
Chris@909 142
Chris@909 143 end
Chris@909 144 end