Sha256: 126c9efca22061b4762608c79bb5ffbe6bdf9d46279f7af05e7d872773f7289c

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

# encoding: utf-8

module Antelope
  class Grammar

    # Manages a list of the symbols in the grammar.
    module Symbols

      # A list of all terminals in the grammar.  Checks the compiler
      # options for terminals, and then returns an array of
      # terminals.  Caches the result.
      #
      # @return [Array<Token::Terminal>]
      def terminals
        @_terminals ||= begin
          @compiler.options.fetch(:terminals) { [] }.map do |v|
            Token::Terminal.new(*v)
          end
        end
      end

      # A list of all nonterminals in the grammar.
      #
      # @return [Array<Symbol>]
      # @see #productions
      def nonterminals
        @_nonterminals ||= productions.keys
      end

      # A list of all nonterminals, with types.
      #
      # @return [Array<Token::Nonterminal>>]
      def typed_nonterminals
        @_typed_nonterminals ||= begin
          typed = []
          compiler.options[:nonterminals].each do |data|
            data[1].each do |nonterm|
              typed << Token::Nonterminal.new(nonterm, data[0])
            end
          end
          typed
        end
      end

      # A list of all symbols in the grammar; includes both
      # terminals and nonterminals.
      #
      # @return [Array<Token::Terminal, Symbol>]
      # @see #terminals
      # @see #nonterminals
      def symbols
        @_symbols ||= terminals + nonterminals
      end

      # Checks to see if the grammar uses the `error` terminal
      # anywhere.
      #
      # @return [Boolean]
      def contains_error_token?
        all_productions.any? { |_| _.items.any?(&:error?) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
antelope-0.3.2 lib/antelope/grammar/symbols.rb
antelope-0.3.0 lib/antelope/grammar/symbols.rb