From c3dfbc335b14012df9968c6d64221cced3d444b6 Mon Sep 17 00:00:00 2001 From: murphy Date: Sat, 5 Nov 2005 02:58:16 +0000 Subject: scanner_helper.rb --> word_list.rb --- lib/coderay/helpers/scanner_helper.rb | 107 ---------------------------------- lib/coderay/helpers/word_list.rb | 107 ++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 107 deletions(-) delete mode 100644 lib/coderay/helpers/scanner_helper.rb create mode 100644 lib/coderay/helpers/word_list.rb (limited to 'lib/coderay/helpers') diff --git a/lib/coderay/helpers/scanner_helper.rb b/lib/coderay/helpers/scanner_helper.rb deleted file mode 100644 index 9f48b83..0000000 --- a/lib/coderay/helpers/scanner_helper.rb +++ /dev/null @@ -1,107 +0,0 @@ -module CodeRay - module Scanners - - 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. - # - # 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 = Scanner::WordList.new(:ident). - # add(RESERVED_WORDS, :reserved). - # add(PREDEFINED_TYPES, :pre_type). - # add(PREDEFINED_CONSTANTS, :pre_constant) - # - # ... - # - # def scan_tokens tokens, options - # ... - # - # elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) - # # use it - # kind = IDENT_KIND[match] - # ... - # - 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 - 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+. - def add words, kind = true - words.each do |word| - self[mind_case(word)] = kind - end - self - end - - alias words keys - - # Returns whether key comparing is done case insensitive. - def case_ignore? - @case_mode - end - - private - # helper method for key - def mind_case word - if @case_ignore - word.downcase - else - word.dup - end - end - - end - - end - - end -end - -# vim:sw=2:ts=2:noet:tw=78 diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb new file mode 100644 index 0000000..9f48b83 --- /dev/null +++ b/lib/coderay/helpers/word_list.rb @@ -0,0 +1,107 @@ +module CodeRay + module Scanners + + 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. + # + # 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 = Scanner::WordList.new(:ident). + # add(RESERVED_WORDS, :reserved). + # add(PREDEFINED_TYPES, :pre_type). + # add(PREDEFINED_CONSTANTS, :pre_constant) + # + # ... + # + # def scan_tokens tokens, options + # ... + # + # elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) + # # use it + # kind = IDENT_KIND[match] + # ... + # + 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 + 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+. + def add words, kind = true + words.each do |word| + self[mind_case(word)] = kind + end + self + end + + alias words keys + + # Returns whether key comparing is done case insensitive. + def case_ignore? + @case_mode + end + + private + # helper method for key + def mind_case word + if @case_ignore + word.downcase + else + word.dup + end + end + + end + + end + + end +end + +# vim:sw=2:ts=2:noet:tw=78 -- cgit v1.2.1