# encoding: utf-8 # frozen_string_literal: true require "carbon/compiler/parser/firsts" require "carbon/compiler/parser/helpers" require "carbon/compiler/parser/common" require "carbon/compiler/parser/root" require "carbon/compiler/parser/statements" require "carbon/compiler/parser/expressions" module Carbon module Compiler # The parser. This takes a series of tokens, generated by the {Scanner}, # and derives structure from these tokens. This makes the fundamental # bases of creating a concrete representation of what is in the file. class Parser include Parser::Helpers include Parser::Common include Parser::Root include Parser::Statements include Parser::Expressions # The scanner that acts as the source of the tokens. # # @return [Scanner] attr_reader :scanner # Initialize the parser. It takes a scanner, and the project that # the parser is a part of. # # @param scanner [Scanner] # @param project [Project] def initialize(scanner) @scanner = scanner @file = @scanner.file @enum = @scanner.each end # Pretty inspect. # # @return [String] def inspect "#<#{self.class} #{@scanner.file.name}>" end # Parses the input. This is cached to prevent multiple runs. # # @return [Parser::Node] def parse @_root ||= parse_root end alias_method :root, :parse # Destroys the cache for {#parse}, allowing the parsing to occur again. # # @return [void] def clear! @enum.reset @_root = nil end end end end