Chris@0: module CodeRay Chris@0: module Scanners Chris@0: Chris@0: class Delphi < Scanner Chris@0: Chris@0: register_for :delphi Chris@0: file_extension 'pas' Chris@0: Chris@0: RESERVED_WORDS = [ Chris@0: 'and', 'array', 'as', 'at', 'asm', 'at', 'begin', 'case', 'class', Chris@0: 'const', 'constructor', 'destructor', 'dispinterface', 'div', 'do', Chris@0: 'downto', 'else', 'end', 'except', 'exports', 'file', 'finalization', Chris@0: 'finally', 'for', 'function', 'goto', 'if', 'implementation', 'in', Chris@0: 'inherited', 'initialization', 'inline', 'interface', 'is', 'label', Chris@0: 'library', 'mod', 'nil', 'not', 'object', 'of', 'or', 'out', 'packed', Chris@0: 'procedure', 'program', 'property', 'raise', 'record', 'repeat', Chris@0: 'resourcestring', 'set', 'shl', 'shr', 'string', 'then', 'threadvar', Chris@0: 'to', 'try', 'type', 'unit', 'until', 'uses', 'var', 'while', 'with', Chris@0: 'xor', 'on' Chris@0: ] Chris@0: Chris@0: DIRECTIVES = [ Chris@0: 'absolute', 'abstract', 'assembler', 'at', 'automated', 'cdecl', Chris@0: 'contains', 'deprecated', 'dispid', 'dynamic', 'export', Chris@0: 'external', 'far', 'forward', 'implements', 'local', Chris@0: 'near', 'nodefault', 'on', 'overload', 'override', Chris@0: 'package', 'pascal', 'platform', 'private', 'protected', 'public', Chris@0: 'published', 'read', 'readonly', 'register', 'reintroduce', Chris@0: 'requires', 'resident', 'safecall', 'stdcall', 'stored', 'varargs', Chris@0: 'virtual', 'write', 'writeonly' Chris@0: ] Chris@0: Chris@0: IDENT_KIND = CaseIgnoringWordList.new(:ident). Chris@0: add(RESERVED_WORDS, :reserved). Chris@0: add(DIRECTIVES, :directive) Chris@0: Chris@0: NAME_FOLLOWS = CaseIgnoringWordList.new(false). Chris@0: add(%w(procedure function .)) Chris@0: Chris@0: private Chris@0: def scan_tokens tokens, options Chris@0: Chris@0: state = :initial Chris@0: last_token = '' Chris@0: Chris@0: until eos? Chris@0: Chris@0: kind = nil Chris@0: match = nil Chris@0: Chris@0: if state == :initial Chris@0: Chris@0: if scan(/ \s+ /x) Chris@0: tokens << [matched, :space] Chris@0: next Chris@0: Chris@0: elsif scan(%r! \{ \$ [^}]* \}? | \(\* \$ (?: .*? \*\) | .* ) !mx) Chris@0: tokens << [matched, :preprocessor] Chris@0: next Chris@0: Chris@0: elsif scan(%r! // [^\n]* | \{ [^}]* \}? | \(\* (?: .*? \*\) | .* ) !mx) Chris@0: tokens << [matched, :comment] Chris@0: next Chris@0: Chris@0: elsif match = scan(/ <[>=]? | >=? | :=? | [-+=*\/;,@\^|\(\)\[\]] | \.\. /x) Chris@0: kind = :operator Chris@0: Chris@0: elsif match = scan(/\./) Chris@0: kind = :operator Chris@0: if last_token == 'end' Chris@0: tokens << [match, kind] Chris@0: next Chris@0: end Chris@0: Chris@0: elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) Chris@0: kind = NAME_FOLLOWS[last_token] ? :ident : IDENT_KIND[match] Chris@0: Chris@0: elsif match = scan(/ ' ( [^\n']|'' ) (?:'|$) /x) Chris@0: tokens << [:open, :char] Chris@0: tokens << ["'", :delimiter] Chris@0: tokens << [self[1], :content] Chris@0: tokens << ["'", :delimiter] Chris@0: tokens << [:close, :char] Chris@0: next Chris@0: Chris@0: elsif match = scan(/ ' /x) Chris@0: tokens << [:open, :string] Chris@0: state = :string Chris@0: kind = :delimiter Chris@0: Chris@0: elsif scan(/ \# (?: \d+ | \$[0-9A-Fa-f]+ ) /x) Chris@0: kind = :char Chris@0: Chris@0: elsif scan(/ \$ [0-9A-Fa-f]+ /x) Chris@0: kind = :hex Chris@0: Chris@0: elsif scan(/ (?: \d+ ) (?![eE]|\.[^.]) /x) Chris@0: kind = :integer Chris@0: Chris@0: elsif scan(/ \d+ (?: \.\d+ (?: [eE][+-]? \d+ )? | [eE][+-]? \d+ ) /x) Chris@0: kind = :float Chris@0: Chris@0: else Chris@0: kind = :error Chris@0: getch Chris@0: Chris@0: end Chris@0: Chris@0: elsif state == :string Chris@0: if scan(/[^\n']+/) Chris@0: kind = :content Chris@0: elsif scan(/''/) Chris@0: kind = :char Chris@0: elsif scan(/'/) Chris@0: tokens << ["'", :delimiter] Chris@0: tokens << [:close, :string] Chris@0: state = :initial Chris@0: next Chris@0: elsif scan(/\n/) Chris@0: tokens << [:close, :string] Chris@0: kind = :error Chris@0: state = :initial Chris@0: else Chris@0: raise "else case \' reached; %p not handled." % peek(1), tokens Chris@0: end Chris@0: Chris@0: else Chris@0: raise 'else-case reached', tokens Chris@0: Chris@0: end Chris@0: Chris@0: match ||= matched Chris@0: if $CODERAY_DEBUG and not kind Chris@0: raise_inspect 'Error token %p in line %d' % Chris@0: [[match, kind], line], tokens, state Chris@0: end Chris@0: raise_inspect 'Empty token', tokens unless match Chris@0: Chris@0: last_token = match Chris@0: tokens << [match, kind] Chris@0: Chris@0: end Chris@0: Chris@0: tokens Chris@0: end Chris@0: Chris@0: end Chris@0: Chris@0: end Chris@0: end