Parent

Methods

CodeRay::WordList

WordList

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

Copyright © 2006-2011 by murphy (Kornelius Kalnbach)

License

LGPL / ask the author

Version

2.0 (2011-05-08)

A WordList is a Hash with some additional features. It is intended to be used for keyword recognition.

WordList is optimized to be used in Scanners, typically to decide whether a given ident is a special token.

For case insensitive words use WordList::CaseIgnoring.

Example:

 # define word arrays
 RESERVED_WORDS = %w[
   asm break case continue default do else
 ]
 
 PREDEFINED_TYPES = %w[
   int long short char void
 ]
 
 # make a WordList
 IDENT_KIND = WordList.new(:ident).
   add(RESERVED_WORDS, :reserved).
   add(PREDEFINED_TYPES, :predefined_type)
 
 ...
 
 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) click to toggle source

Create a new WordList with default as default value.

    # File lib/coderay/helpers/word_list.rb, line 48
48:     def initialize default = false
49:       super default
50:     end

Public Instance Methods

add(words, value = true) click to toggle source

Add words to the list and associate them with value.

Returns self, so you can concat add calls.

    # File lib/coderay/helpers/word_list.rb, line 55
55:     def add words, value = true
56:       words.each { |word| self[word] = value }
57:       self
58:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.