Class: Carbon::Compiler::Parser

Inherits:
Object
  • Object
show all
Includes:
Common, Expressions, Firsts, Helpers, Root, Statements
Defined in:
lib/carbon/compiler/parser.rb,
lib/carbon/compiler/parser/root.rb,
lib/carbon/compiler/parser/common.rb,
lib/carbon/compiler/parser/firsts.rb,
lib/carbon/compiler/parser/helpers.rb,
lib/carbon/compiler/parser/root/enum.rb,
lib/carbon/compiler/parser/root/class.rb,
lib/carbon/compiler/parser/root/trait.rb,
lib/carbon/compiler/parser/statements.rb,
lib/carbon/compiler/parser/expressions.rb,
lib/carbon/compiler/parser/root/struct.rb,
lib/carbon/compiler/parser/root/function.rb,
lib/carbon/compiler/parser/statements/if.rb,
lib/carbon/compiler/parser/root/directive.rb,
lib/carbon/compiler/parser/statements/try.rb,
lib/carbon/compiler/parser/statements/match.rb,
lib/carbon/compiler/parser/expressions/primary.rb,
lib/carbon/compiler/parser/expressions/precedence.rb

Overview

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.

Defined Under Namespace

Modules: Common, Expressions, Firsts, Helpers, Root, Statements

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Firsts

included

Methods included from Helpers

#error, #expect, #peek, #peek?, #shift

Constructor Details

#initialize(scanner) ⇒ Parser

Initialize the parser. It takes a scanner, and the project that the parser is a part of.

Parameters:



33
34
35
36
37
# File 'lib/carbon/compiler/parser.rb', line 33

def initialize(scanner)
  @scanner = scanner
  @file = @scanner.file
  @enum = @scanner.each
end

Instance Attribute Details

#scannerScanner (readonly)

The scanner that acts as the source of the tokens.

Returns:



26
27
28
# File 'lib/carbon/compiler/parser.rb', line 26

def scanner
  @scanner
end

Instance Method Details

#clear!void

This method returns an undefined value.

Destroys the cache for #parse, allowing the parsing to occur again.



57
58
59
60
# File 'lib/carbon/compiler/parser.rb', line 57

def clear!
  @enum.reset
  @_root = nil
end

#inspectString

Pretty inspect.

Returns:

  • (String)


42
43
44
# File 'lib/carbon/compiler/parser.rb', line 42

def inspect
  "#<#{self.class} #{@scanner.file.name}>"
end

#parseParser::Node Also known as: root

Parses the input. This is cached to prevent multiple runs.

Returns:

  • (Parser::Node)


49
50
51
# File 'lib/carbon/compiler/parser.rb', line 49

def parse
  @_root ||= parse_root
end