Class: Carbon::Compiler::Scanner

Inherits:
Object
  • Object
show all
Includes:
Main, Enumerable
Defined in:
lib/carbon/compiler/scanner.rb,
lib/carbon/compiler/scanner/main.rb,
lib/carbon/compiler/scanner/token.rb

Overview

Scans over the file, taking a squence of characters and turning them into tokens.

Defined Under Namespace

Modules: Main Classes: Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Scanner

Initialize the scanner. Takes a file, which contains information about the file, and can read from the file.



24
25
26
27
28
29
# File 'lib/carbon/compiler/scanner.rb', line 24

def initialize(file)
  @file = file
  @scanner = StringScanner.new(@file.read)
  @line = 1
  @last = 0
end

Instance Attribute Details

#fileProject::File (readonly)

The file that the scanner will run over.



18
19
20
# File 'lib/carbon/compiler/scanner.rb', line 18

def file
  @file
end

Instance Method Details

#each {|token| ... } ⇒ Enumerator<Token>

Yields each token to the block, if one is given. Returns an enumerator that enumerates over all generated tokens.

Yields:

  • (token)

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
# File 'lib/carbon/compiler/scanner.rb', line 44

def each
  return to_enum unless block_given?
  until @scanner.eos?
    token = scan
    yield token if token.is_a?(Token)
  end

  yield Token.new(:EOF, "", location)
  @scanner.reset
end

#inspectvoid

This method returns an undefined value.

Pretty inspect.



34
35
36
# File 'lib/carbon/compiler/scanner.rb', line 34

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