Module: Carbon::Compiler::Parser::Firsts::ClassMethods

Defined in:
lib/carbon/compiler/parser/firsts.rb

Overview

The class methods that should be placed on the parser.

Instance Method Summary collapse

Instance Method Details

#first(name) ⇒ Set<::Symbol> #first(name, tokens) ⇒ void

Overloads:

  • #first(name) ⇒ Set<::Symbol>

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    Retrieves the first set for the given node name. If none exists, a ‘KeyError` is raised.

    Parameters:

    • name (::Symbol)

      The name of the node to look up the first set for.

    Returns:

    • (Set<::Symbol>)

      The first set

    Raises:

    • (KeyError)

      If the node has no defined first set.

  • #first(name, tokens) ⇒ void

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    This method returns an undefined value.

    Merges the given tokens into the first set of the name of the node given.

    Parameters:

    • name (::Symbol)

      The name of the node that the first set is for.

    • tokens (<::Symbol>)

      The tokens that act as the first set for the node.



40
41
42
43
44
45
46
# File 'lib/carbon/compiler/parser/firsts.rb', line 40

def first(name, tokens = nil)
  if tokens
    firsts[name].merge(tokens)
  else
    firsts.fetch(name)
  end
end

#firsts{Symbol => Set<Symbol>}

The first sets. The key is the name of a node, and the value is a set of all tokens that can be at the start of that node.

Returns:

  • ({Symbol => Set<Symbol>})


17
18
19
# File 'lib/carbon/compiler/parser/firsts.rb', line 17

def firsts
  @firsts ||= Hash.new { |h, k| h[k] = Set.new }
end