summaryrefslogtreecommitdiff
path: root/lib/coderay
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2006-02-20 01:56:06 +0000
committermurphy <murphy@rubychan.de>2006-02-20 01:56:06 +0000
commit340013c50581fc2c471bee1517d001e1f74bd87a (patch)
tree25bd4084513c92ffaac64ee76fb86aa88e832822 /lib/coderay
parent79b36aba787d03ff57b214613ba2bd21f076a0c8 (diff)
downloadcoderay-340013c50581fc2c471bee1517d001e1f74bd87a.tar.gz
WordList is now a first-class Class independent from CodeRay.
I used it in my HTMLParser, so it became WordList instead of CodeRay::Scanners::Scanner::WordList. Some functionality was added and changed, too. Speed should be the same.
Diffstat (limited to 'lib/coderay')
-rw-r--r--lib/coderay/helpers/word_list.rb189
1 files changed, 94 insertions, 95 deletions
diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb
index be81fd0..7580a5a 100644
--- a/lib/coderay/helpers/word_list.rb
+++ b/lib/coderay/helpers/word_list.rb
@@ -1,108 +1,107 @@
-module CodeRay
- module Scanners
+# = WordList
+#
+# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy cYcnus de>
+#
+# License:: LGPL / ask the author
+# Version:: 1.0 (2006-Feb-3)
+#
+# 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.
+#
+# For case insensitive words use CaseIgnoringWordList.
+#
+# Example:
+#
+# # define word arrays
+# RESERVED_WORDS = %w[
+# asm break case continue default do else
+# ...
+# ]
+#
+# PREDEFINED_TYPES = %w[
+# int long short char void
+# ...
+# ]
+#
+# PREDEFINED_CONSTANTS = %w[
+# EOF NULL ...
+# ]
+#
+# # make a WordList
+# IDENT_KIND = WordList.new(:ident).
+# add(RESERVED_WORDS, :reserved).
+# add(PREDEFINED_TYPES, :pre_type).
+# add(PREDEFINED_CONSTANTS, :pre_constant)
+#
+# ...
+#
+# def scan_tokens tokens, options
+# ...
+#
+# elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
+# # use it
+# kind = IDENT_KIND[match]
+# ...
+#
+class WordList < Hash
- class Scanner
-
- # 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.
- #
- # For case insensitive words use CaseIgnoringWordList.
- #
- # Example:
- #
- # # define word arrays
- # RESERVED_WORDS = %w[
- # asm break case continue default do else
- # ...
- # ]
- #
- # PREDEFINED_TYPES = %w[
- # int long short char void
- # ...
- # ]
- #
- # PREDEFINED_CONSTANTS = %w[
- # EOF NULL ...
- # ]
- #
- # # make a WordList
- # IDENT_KIND = WordList.new(:ident).
- # add(RESERVED_WORDS, :reserved).
- # add(PREDEFINED_TYPES, :pre_type).
- # add(PREDEFINED_CONSTANTS, :pre_constant)
- #
- # ...
- #
- # def scan_tokens tokens, options
- # ...
- #
- # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/)
- # # use it
- # kind = IDENT_KIND[match]
- # ...
- #
- class WordList < Hash
-
- # Creates a new WordList with +default+ as default value.
- def initialize default = false, &block
- super default, &block
- end
+ # 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
- # Checks if a word is included.
- def include? word
- has_key? word
- end
+ # Creates a new WordList with +default+ as default value.
+ def initialize default = false, &block
+ super default, &block
+ end
- # Add words to the list and associate them with +kind+.
- def add words, kind = true
- words.each do |word|
- self[word] = kind
- end
- self
- end
+ # Checks if a word is included.
+ def include? word
+ has_key? word
+ end
- end
-
+ # Add words to the list and associate them with +kind+.
+ def add words, kind = true
+ words.each do |word|
+ self[word] = kind
+ end
+ self
+ end
- # A WordList is a Hash with some additional features.
- # It is intended to be used for keyword recognition.
- #
- # Keys are compared case-insensitively.
- #
- # See WordList.
- class CaseIgnoringWordList < WordList
+end
- # 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
- end
- super default
- end
- # Checks if a word is included.
- def include? word
- has_key? word.downcase
- end
+# A CaseIgnoringWordList is like a WordList, only that
+# keys are compared case-insensitively.
+class CaseIgnoringWordList < WordList
- # 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
+ # 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
+ end
+ super default
+ end
- 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+.
+ def add words, kind = true
+ words.each do |word|
+ self[word.downcase] = kind
end
-
+ self
end
-end
-# vim:sw=2:ts=2:noet:tw=78
+end