Class: Carbon::Compiler::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/carbon/compiler/location.rb

Overview

Contains location information for tokens and nodes. This is commonly used in error reporting.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, columns, files) ⇒ Location

Initializes the location. The location is frozen after initialization.

Parameters:

  • lines (Range<Integer>)
  • columns (Range<Integer>)
  • files (String)


47
48
49
50
51
52
53
# File 'lib/carbon/compiler/location.rb', line 47

def initialize(lines, columns, files)
  @line = lines
  @column = columns
  @file = files
  to_a
  freeze
end

Instance Attribute Details

#columnRange<Integer> (readonly)

The columns that the location covers. Despite its name, this is actually a range of columns.

Returns:

  • (Range<Integer>)


20
21
22
# File 'lib/carbon/compiler/location.rb', line 20

def column
  @column
end

#fileString (readonly)

The file that the location is in.

Returns:

  • (String)


24
25
26
# File 'lib/carbon/compiler/location.rb', line 24

def file
  @file
end

#lineRange<Integer> (readonly)

The lines that the location covers. Despite its name, this is actually a range of lines. Tokens may only be on one line; however, nodes can be on multiple lines.

Returns:

  • (Range<Integer>)


15
16
17
# File 'lib/carbon/compiler/location.rb', line 15

def line
  @line
end

Class Method Details

.default(file = "(null)") ⇒ Location

Returns a default location, representing no location information. This is used when no location information can be obtained, or none exists; for example, a compiler error unrelated to the parsing of the file.

Returns:



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

def self.default(file = "(null)")
  new(0..0, 0..0, file)
end

.internalObject



38
39
40
# File 'lib/carbon/compiler/location.rb', line 38

def self.internal
  default("(internal)")
end

Instance Method Details

#<=>(other) ⇒ Numeric

Compares this location to another location. It does so by converting both to arrays and comparing said arrays.

Returns:

  • (Numeric)


59
60
61
62
# File 'lib/carbon/compiler/location.rb', line 59

def <=>(other)
  fail ArgumentError, "Expected a Location" unless other.is_a?(Location)
  to_a <=> other.to_a
end

#dupself

Creates a duplicate. Since the location is frozen, this returns self.

Returns:

  • (self)


101
102
103
# File 'lib/carbon/compiler/location.rb', line 101

def dup
  self
end

#hashNumeric

Returns a numeric representation of this class for use of hashing.

Returns:

  • (Numeric)


75
76
77
# File 'lib/carbon/compiler/location.rb', line 75

def hash
  to_a.hash
end

#inspectString

Pretty inspect.

Returns:

  • (String)


108
109
110
# File 'lib/carbon/compiler/location.rb', line 108

def inspect
  "#(Location #{self})"
end

#to_a(Class, Range<Integer>, Range<Integer>, String)

Converts the location to an array. This array has no meaning outside of comparisons and equality. The array is frozen.

Returns:

  • ((Class, Range<Integer>, Range<Integer>, String))


68
69
70
# File 'lib/carbon/compiler/location.rb', line 68

def to_a
  @array ||= [self.class, @line, @column, @file.to_s].freeze
end

#to_sString

Creates a string representation of the location.

Returns:

  • (String)


115
116
117
# File 'lib/carbon/compiler/location.rb', line 115

def to_s
  "#{file}:#{line_to_s}:#{column_to_s}"
end

#|(other) ⇒ Location

A union operation. Performing a union on two locations expands both of them; the range of lines is expanded so that the start of the new range is the lowest of the start of the ranges from either location, and the end of the range is the highest of the end of the ranges from either location; the same is done with the column. This sets the size of the new location so that both originating locations are contained within the new location.

Parameters:

  • other (Location)

    The location to union with.

Returns:

  • (Location)

    A new location containing the result of the union.



89
90
91
92
93
94
95
96
# File 'lib/carbon/compiler/location.rb', line 89

def |(other)
  fail ArgumentError, "Cannot union #{other.class}" unless
    other.is_a?(Location)

  lines = merge_ranges(line, other.line)
  columns = merge_ranges(column, other.column)
  Location.new(lines, columns, file)
end