Parent

Methods

Files

Class Index [+]

Quicksearch

CodeRay::WordList

WordList

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

Copyright © 2006 by murphy (Kornelius Kalnbach)

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

Public Class Methods

new(default = false, caching = false, &block) click to toggle source

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.

    # 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, kind = true) click to toggle source

Add words to the list and associate them with kind.

Returns self, so you can concat add calls.

    # 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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.