diff options
author | murphy <murphy@rubychan.de> | 2006-10-20 07:03:59 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2006-10-20 07:03:59 +0000 |
commit | b29f1786692e0416a6dc71474859f60af471ed42 (patch) | |
tree | 37ee0ed061c0f75487c2d9c0819182de31ab6dd1 | |
parent | 1a811cec4b84f356da07f61bc2c426792e151f85 (diff) | |
download | coderay-b29f1786692e0416a6dc71474859f60af471ed42.tar.gz |
WordList:
- fixed a bad bug (CaseIgnoringWordList wasn't case ignoring!)
- code and doc cleanup
- optional caching added
- interface minimized (no more WordList.for)
FileType:
- added raybedug type (see next commits)
-rw-r--r-- | lib/coderay/helpers/filetype.rb | 10 | ||||
-rw-r--r-- | lib/coderay/helpers/word_list.rb | 77 |
2 files changed, 51 insertions, 36 deletions
diff --git a/lib/coderay/helpers/filetype.rb b/lib/coderay/helpers/filetype.rb index 7341f01..55b63f5 100644 --- a/lib/coderay/helpers/filetype.rb +++ b/lib/coderay/helpers/filetype.rb @@ -1,10 +1,11 @@ -# =FileType +# = FileType # -# A simple filetype recognizer +# A simple filetype recognizer. # -# Author: murphy (mail to murphy cYcnus de) +# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de> # -# Version: 0.1 (2005.september.1) +# License:: LGPL / ask the author +# Version:: 0.1 (2005-09-01) # # == Documentation # @@ -84,6 +85,7 @@ module FileType 'htm' => :html, 'html' => :html, 'xhtml' => :xhtml, + 'raydebug' => :debug, 'rhtml' => :rhtml, 'yaml' => :yaml, 'yml' => :yaml, diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb index cb67f7a..99f6029 100644 --- a/lib/coderay/helpers/word_list.rb +++ b/lib/coderay/helpers/word_list.rb @@ -1,15 +1,17 @@ # = WordList -# -# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy cYcnus de> +# +# <b>A Hash subclass designed for mapping word lists to token types.</b> +# +# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de> # # License:: LGPL / ask the author -# Version:: 1.0 (2006-Feb-3) +# Version:: 1.1 (2006-Oct-19) # # A WordList is a Hash with some additional features. # It is intended to be used for keyword recognition. # # WordList is highly optimized to be used in Scanners, -# typically to decide whether a given ident is a keyword. +# typically to decide whether a given ident is a special token. # # For case insensitive words use CaseIgnoringWordList. # @@ -47,25 +49,30 @@ # ... class WordList < Hash - # Create a WordList for the given +words+. - # - # This WordList responds to [] with +true+, if the word is - # in +words+, and with +false+ otherwise. - def self.for words - new.add words - end - # Creates a new WordList with +default+ as default value. - def initialize default = false, &block - super default, &block - end - - # Checks if a word is included. - def include? word - has_key? word + # + # You can activate +caching+ to store the results for every [] request. + # + # With caching, methods like +include?+ or +delete+ may no longer behave + # as you expect. Therefore, it is recommended to use the [] method only. + def initialize default = false, caching = false, &block + if block + raise ArgumentError, 'Can\'t combine block with caching.' if caching + super(&block) + else + if caching + super() do |h, k| + h[k] = h.fetch k, default + end + else + super default + end + end end # Add words to the list and associate them with +kind+. + # + # Returns +self+, so you can concat add calls. def add words, kind = true words.each do |word| self[word] = kind @@ -78,24 +85,30 @@ end # A CaseIgnoringWordList is like a WordList, only that # keys are compared case-insensitively. +# +# Ignoring the text case is realized by sending the +downcase+ message to +# all keys. +# +# Caching usually makes a CaseIgnoringWordList faster, but it has to be +# activated explicitely. class CaseIgnoringWordList < WordList - # Creates a new WordList with +default+ as default value. - # - # Text case is ignored. - def initialize default = false, &block - block ||= proc do |h, k| - h[k] = h.fetch k.downcase, default + # Creates a new case-insensitive WordList with +default+ as default value. + # + # You can activate caching to store the results for every [] request. + def initialize default = false, caching = false + if caching + super(default, false) do |h, k| + h[k] = h.fetch k.downcase, default + end + else + def self.[] key # :nodoc: + super(key.downcase) + end end - super default end - # Checks if a word is included. - def include? word - has_key? word.downcase - 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[word.downcase] = kind |