Class: Carbon::Compiler::Location
- Inherits:
-
Object
- Object
- Carbon::Compiler::Location
- 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
-
#column ⇒ Range<Integer>
readonly
The columns that the location covers.
-
#file ⇒ String
readonly
The file that the location is in.
-
#line ⇒ Range<Integer>
readonly
The lines that the location covers.
Class Method Summary collapse
-
.default(file = "(null)") ⇒ Location
Returns a default location, representing no location information.
- .internal ⇒ Object
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
Compares this location to another location.
-
#dup ⇒ self
Creates a duplicate.
-
#hash ⇒ Numeric
Returns a numeric representation of this class for use of hashing.
-
#initialize(lines, columns, files) ⇒ Location
constructor
Initializes the location.
-
#inspect ⇒ String
Pretty inspect.
-
#to_a ⇒ (Class, Range<Integer>, Range<Integer>, String)
Converts the location to an array.
-
#to_s ⇒ String
Creates a string representation of the location.
-
#|(other) ⇒ Location
A union operation.
Constructor Details
#initialize(lines, columns, files) ⇒ Location
Initializes the location. The location is frozen after initialization.
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
#column ⇒ Range<Integer> (readonly)
The columns that the location covers. Despite its name, this is actually a range of columns.
20 21 22 |
# File 'lib/carbon/compiler/location.rb', line 20 def column @column end |
#file ⇒ String (readonly)
The file that the location is in.
24 25 26 |
# File 'lib/carbon/compiler/location.rb', line 24 def file @file end |
#line ⇒ Range<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.
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.
34 35 36 |
# File 'lib/carbon/compiler/location.rb', line 34 def self.default(file = "(null)") new(0..0, 0..0, file) end |
.internal ⇒ Object
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.
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 |
#dup ⇒ self
Creates a duplicate. Since the location is frozen, this returns self.
101 102 103 |
# File 'lib/carbon/compiler/location.rb', line 101 def dup self end |
#hash ⇒ Numeric
Returns a numeric representation of this class for use of hashing.
75 76 77 |
# File 'lib/carbon/compiler/location.rb', line 75 def hash to_a.hash end |
#inspect ⇒ String
Pretty inspect.
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.
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_s ⇒ String
Creates a string representation of the location.
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.
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 |