Sha256: 154861ecf6ea9f994635c415061ae053d94a0eef52239aa115d9d65d09745140

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8

module Antelope
  module Ace
    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
antelope-0.2.0 lib/antelope/ace/grammar/symbols.rb
antelope-0.1.11 lib/antelope/ace/grammar/symbols.rb
antelope-0.1.10 lib/antelope/ace/grammar/symbols.rb
antelope-0.1.9 lib/antelope/ace/grammar/symbols.rb