Class CodeRay::WordList
In: lib/coderay/helpers/word_list.rb
Parent: Hash

WordList

A Hash subclass designed for mapping word lists to token types.

Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de>

License:LGPL / ask the author
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 special token.

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]
     ...

Methods

add   new  

Public Class methods

Creates a new WordList with default as default value.

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.

[Source]

    # File lib/coderay/helpers/word_list.rb, line 60
60:   def initialize default = false, caching = false, &block
61:     if block
62:       raise ArgumentError, 'Can\'t combine block with caching.' if caching
63:       super(&block)
64:     else
65:       if caching
66:         super() do |h, k|
67:           h[k] = h.fetch k, default
68:         end
69:       else
70:         super default
71:       end
72:     end
73:   end

Public Instance methods

Add words to the list and associate them with kind.

Returns self, so you can concat add calls.

[Source]

    # File lib/coderay/helpers/word_list.rb, line 78
78:   def add words, kind = true
79:     words.each do |word|
80:       self[word] = kind
81:     end
82:     self
83:   end

[Validate]