Class: CodeRay::WordList

Inherits:
Hash show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/helpers/word_list.rb

Overview

WordList

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

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

Direct Known Subclasses

CaseIgnoring

Defined Under Namespace

Classes: CaseIgnoring

Instance Method Summary collapse

Methods inherited from Hash

#as_json, #assert_valid_keys, #compact_blank, #compact_blank!, #deep_dup, #deep_merge, #deep_merge!, #deep_stringify_keys, #deep_stringify_keys!, #deep_symbolize_keys, #deep_symbolize_keys!, #deep_transform_keys, #deep_transform_keys!, #deep_transform_values, #deep_transform_values!, #except, #except!, #extract!, #extractable_options?, from_trusted_xml, from_xml, #reverse_merge, #reverse_merge!, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_query, #to_xml, #with_indifferent_access

Constructor Details

#initialize(default = false) ⇒ WordList

Create a new WordList with default as default value.



43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/helpers/word_list.rb', line 43

def initialize default = false
  super default
end

Instance Method Details

#add(words, value = true) ⇒ Object

Add words to the list and associate them with value.

Returns self, so you can concat add calls.



50
51
52
53
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/helpers/word_list.rb', line 50

def add words, value = true
  words.each { |word| self[word] = value }
  self
end