From ee9c870bfbb4032ca08c1f1760716b5472b64293 Mon Sep 17 00:00:00 2001 From: murphy Date: Sat, 5 Nov 2005 03:09:21 +0000 Subject: helpers/word_list.rb: new CaseIgnoringWordList class simplified implementation Performance gain should be small. Adjusted scanner.rb and scanners/delphi.rb --- lib/coderay/helpers/word_list.rb | 62 +++++++++++++++++----------------------- lib/coderay/scanner.rb | 2 +- lib/coderay/scanners/c.rb | 2 +- lib/coderay/scanners/delphi.rb | 2 +- 4 files changed, 30 insertions(+), 38 deletions(-) (limited to 'lib/coderay') diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb index 9f48b83..a74705d 100644 --- a/lib/coderay/helpers/word_list.rb +++ b/lib/coderay/helpers/word_list.rb @@ -27,7 +27,7 @@ module CodeRay # ] # # # make a WordList - # IDENT_KIND = Scanner::WordList.new(:ident). + # IDENT_KIND = WordList.new(:ident). # add(RESERVED_WORDS, :reserved). # add(PREDEFINED_TYPES, :pre_type). # add(PREDEFINED_CONSTANTS, :pre_constant) @@ -37,7 +37,7 @@ module CodeRay # def scan_tokens tokens, options # ... # - # elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) + # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/) # # use it # kind = IDENT_KIND[match] # ... @@ -45,56 +45,48 @@ module CodeRay class WordList < Hash # Creates a new WordList with +default+ as default value. - # case_mode controls how keys are compared; - # :case_match is faster. - def initialize default = false, case_mode = :case_match - @case_ignore = - case case_mode - when :case_match then false - when :case_ignore then true - else raise ArgumentError, - ":case_ignore or :case_match expected, but #{case_mode} given" - end - - if @case_ignore - super() do |h, k| - h[k] = h.fetch k.downcase, default - end - else - super default - end + def initialize default = false, &block + super default, &block end # Checks if a word is included. def include? word - self[word] if @case_ignore has_key? word end - # Add words to the list and associate them with - # +kind+. + # Add words to the list and associate them with +kind+. def add words, kind = true words.each do |word| - self[mind_case(word)] = kind + self[word] = kind end self end - alias words keys + end + + + class CaseIgnoringWordList < WordList + + # Creates a new WordList with +default+ as default value. + # + # Text case is ignored. + def initialize default = false + super() do |h, k| + h[k] = h.fetch k.downcase, default + end + end - # Returns whether key comparing is done case insensitive. - def case_ignore? - @case_mode + # Checks if a word is included. + def include? word + has_key? word.downcase end - private - # helper method for key - def mind_case word - if @case_ignore - word.downcase - else - word.dup + # Add words to the list and associate them with +kind+. + def add words, kind = true + words.each do |word| + self[word.downcase] = kind end + self end end diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb index cf4f3c6..609f4da 100644 --- a/lib/coderay/scanner.rb +++ b/lib/coderay/scanner.rb @@ -51,7 +51,7 @@ module CodeRay # Raised if a Scanner fails while scanning ScanError = Class.new(Exception) - require 'coderay/helpers/scanner_helper' + require 'coderay/helpers/word_list' # The default options for all scanner classes. # diff --git a/lib/coderay/scanners/c.rb b/lib/coderay/scanners/c.rb index 3420822..8fae829 100644 --- a/lib/coderay/scanners/c.rb +++ b/lib/coderay/scanners/c.rb @@ -25,7 +25,7 @@ module CodeRay module Scanners 'true', 'false', # C99 ] - IDENT_KIND = Scanner::WordList.new(:ident). + IDENT_KIND = WordList.new(:ident). add(RESERVED_WORDS, :reserved). add(PREDEFINED_TYPES, :pre_type). add(PREDEFINED_CONSTANTS, :pre_constant) diff --git a/lib/coderay/scanners/delphi.rb b/lib/coderay/scanners/delphi.rb index 4c03147..77c3839 100644 --- a/lib/coderay/scanners/delphi.rb +++ b/lib/coderay/scanners/delphi.rb @@ -28,7 +28,7 @@ module CodeRay module Scanners 'virtual', 'write', 'writeonly' ] - IDENT_KIND = Scanner::WordList.new(:ident, :case_ignore). + IDENT_KIND = CaseIgnoringWordList.new(:ident). add(RESERVED_WORDS, :reserved). add(DIRECTIVES, :directive) -- cgit v1.2.1