A location inside a source file

Introduced properties

private var _column_end: Int

nitc :: Location :: _column_end

End of this location on line_end
private var _column_start: Int

nitc :: Location :: _column_start

Start of this location on line_start
private var _file: nullable SourceFile

nitc :: Location :: _file

The associated source-file
private var _line_end: Int

nitc :: Location :: _line_end

The stopping line number (starting from 1)
private var _line_start: Int

nitc :: Location :: _line_start

The starting line number (starting from 1)
private var _messages: nullable Array[Message]

nitc :: Location :: _messages

Errors and warnings associated to this location.
private var _text_cache: nullable String

nitc :: Location :: _text_cache

private fun add_message(m: Message)

nitc :: Location :: add_message

Add a message to self
fun colored_line(color: String): String

nitc :: Location :: colored_line

Return the associated line with the location highlighted with color and a caret under the starting position
fun column_end: Int

nitc :: Location :: column_end

End of this location on line_end
protected fun column_end=(column_end: Int)

nitc :: Location :: column_end=

End of this location on line_end
fun column_start: Int

nitc :: Location :: column_start

Start of this location on line_start
protected fun column_start=(column_start: Int)

nitc :: Location :: column_start=

Start of this location on line_start
init defaultinit(file: nullable SourceFile, line_start: Int, line_end: Int, column_start: Int, column_end: Int)

nitc :: Location :: defaultinit

fun file: nullable SourceFile

nitc :: Location :: file

The associated source-file
protected fun file=(file: nullable SourceFile)

nitc :: Location :: file=

The associated source-file
init from_string(string: String)

nitc :: Location :: from_string

Builds a location instance from its string representation.
fun line_end: Int

nitc :: Location :: line_end

The stopping line number (starting from 1)
protected fun line_end=(line_end: Int)

nitc :: Location :: line_end=

The stopping line number (starting from 1)
fun line_start: Int

nitc :: Location :: line_start

The starting line number (starting from 1)
protected fun line_start=(line_start: Int)

nitc :: Location :: line_start=

The starting line number (starting from 1)
fun located_in(loc: nullable Location): Bool

nitc :: Location :: located_in

Is self included (or equals) to loc?
fun messages: nullable Array[Message]

nitc :: Location :: messages

Errors and warnings associated to this location.
protected fun messages=(messages: nullable Array[Message])

nitc :: Location :: messages=

Errors and warnings associated to this location.
init opaque_file(path: String)

nitc :: Location :: opaque_file

Initialize a location corresponding to an opaque file.
fun pend: Int

nitc :: Location :: pend

The index on the end character in the source
fun pstart: Int

nitc :: Location :: pstart

The index in the start character in the source
fun relative_to(loc: nullable Location): String

nitc :: Location :: relative_to

Return a location message according to an observer.
fun short_location: String

nitc :: Location :: short_location

Return a shortened version of the location with "{file}:{line_start}"
fun text: String

nitc :: Location :: text

The verbatim associated text in the source-file
private fun text_cache: nullable String

nitc :: Location :: text_cache

private fun text_cache=(text_cache: nullable String)

nitc :: Location :: text_cache=

Redefined properties

redef fun <(other: OTHER): Bool

nitc $ Location :: <

Is self lesser than other?
redef fun ==(other: nullable Object): Bool

nitc $ Location :: ==

Have self and other the same value?
redef type OTHER: Location

nitc $ Location :: OTHER

What self can be compared to?
redef type SELF: Location

nitc $ Location :: SELF

Type of this instance, automatically specialized in every class
redef fun core_serialize_to(v: Serializer)

nitc :: json_model $ Location :: core_serialize_to

Actual serialization of self to serializer
redef init from_deserializer(v: Deserializer)

nitc :: json_model $ Location :: from_deserializer

Create an instance of this class from the deserializer
redef fun to_s: String

nitc $ Location :: to_s

User readable representation of self.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
abstract fun <(other: OTHER): Bool

core :: Comparable :: <

Is self lesser than other?
fun <=(other: OTHER): Bool

core :: Comparable :: <=

not other < self
fun <=>(other: OTHER): Int

core :: Comparable :: <=>

-1 if <, +1 if > and 0 otherwise
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
fun >(other: OTHER): Bool

core :: Comparable :: >

other < self
fun >=(other: OTHER): Bool

core :: Comparable :: >=

not self < other
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type OTHER: Comparable

core :: Comparable :: OTHER

What self can be compared to?
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _column_end: Int

nitc :: Location :: _column_end

End of this location on line_end
private var _column_start: Int

nitc :: Location :: _column_start

Start of this location on line_start
private var _file: nullable SourceFile

nitc :: Location :: _file

The associated source-file
private var _line_end: Int

nitc :: Location :: _line_end

The stopping line number (starting from 1)
private var _line_start: Int

nitc :: Location :: _line_start

The starting line number (starting from 1)
private var _messages: nullable Array[Message]

nitc :: Location :: _messages

Errors and warnings associated to this location.
private var _text_cache: nullable String

nitc :: Location :: _text_cache

protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
private fun add_message(m: Message)

nitc :: Location :: add_message

Add a message to self
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
fun clamp(min: OTHER, max: OTHER): OTHER

core :: Comparable :: clamp

Constraint self within [min..max]
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun colored_line(color: String): String

nitc :: Location :: colored_line

Return the associated line with the location highlighted with color and a caret under the starting position
fun column_end: Int

nitc :: Location :: column_end

End of this location on line_end
protected fun column_end=(column_end: Int)

nitc :: Location :: column_end=

End of this location on line_end
fun column_start: Int

nitc :: Location :: column_start

Start of this location on line_start
protected fun column_start=(column_start: Int)

nitc :: Location :: column_start=

Start of this location on line_start
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
init defaultinit(file: nullable SourceFile, line_start: Int, line_end: Int, column_start: Int, column_end: Int)

nitc :: Location :: defaultinit

fun file: nullable SourceFile

nitc :: Location :: file

The associated source-file
protected fun file=(file: nullable SourceFile)

nitc :: Location :: file=

The associated source-file
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
init from_string(string: String)

nitc :: Location :: from_string

Builds a location instance from its string representation.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_between(c: OTHER, d: OTHER): Bool

core :: Comparable :: is_between

c <= self <= d
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun line_end: Int

nitc :: Location :: line_end

The stopping line number (starting from 1)
protected fun line_end=(line_end: Int)

nitc :: Location :: line_end=

The stopping line number (starting from 1)
fun line_start: Int

nitc :: Location :: line_start

The starting line number (starting from 1)
protected fun line_start=(line_start: Int)

nitc :: Location :: line_start=

The starting line number (starting from 1)
fun located_in(loc: nullable Location): Bool

nitc :: Location :: located_in

Is self included (or equals) to loc?
fun max(other: OTHER): OTHER

core :: Comparable :: max

The maximum between self and other (prefers self if equals).
fun messages: nullable Array[Message]

nitc :: Location :: messages

Errors and warnings associated to this location.
protected fun messages=(messages: nullable Array[Message])

nitc :: Location :: messages=

Errors and warnings associated to this location.
fun min(c: OTHER): OTHER

core :: Comparable :: min

The minimum between self and c (prefer self if equals)
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
init opaque_file(path: String)

nitc :: Location :: opaque_file

Initialize a location corresponding to an opaque file.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun pend: Int

nitc :: Location :: pend

The index on the end character in the source
fun pstart: Int

nitc :: Location :: pstart

The index in the start character in the source
fun relative_to(loc: nullable Location): String

nitc :: Location :: relative_to

Return a location message according to an observer.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
private fun serialize_to_or_delay(v: Serializer)

serialization :: Serializable :: serialize_to_or_delay

Accept references or force direct serialization (using serialize_to)
fun short_location: String

nitc :: Location :: short_location

Return a shortened version of the location with "{file}:{line_start}"
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun text: String

nitc :: Location :: text

The verbatim associated text in the source-file
private fun text_cache: nullable String

nitc :: Location :: text_cache

private fun text_cache=(text_cache: nullable String)

nitc :: Location :: text_cache=

fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram nitc::Location Location core::Comparable Comparable nitc::Location->core::Comparable serialization::Serializable Serializable nitc::Location->serialization::Serializable core::Object Object core::Comparable->core::Object serialization::Serializable->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

interface Comparable

core :: Comparable

The ancestor of class where objects are in a total order.
interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize

Class definitions

nitc $ Location
# A location inside a source file
class Location
	super Comparable
	redef type OTHER: Location

	# The associated source-file
	var file: nullable SourceFile

	# The starting line number (starting from 1)
	#
	# If `line_start==0` then the whole file is considered
	var line_start: Int

	# The stopping line number (starting from 1)
	var line_end: Int

	# Start of this location on `line_start`
	#
	# A `column_start` of 1 means the first column or character.
	#
	# If `column_start == 0` this location concerns the whole line.
	#
	# Require: `column_start >= 0`
	var column_start: Int

	# End of this location on `line_end`
	var column_end: Int

	# Builds a location instance from its string representation.
	#
	# Examples:
	#
	# ~~~
	# var loc = new Location.from_string("location.nit:82,2--105,8")
	# assert loc.to_s == "location.nit:82,2--105,8"
	#
	# loc = new Location.from_string("location.nit")
	# assert loc.to_s == "location.nit"
	#
	# loc = new Location.from_string("location.nit:82,2")
	# assert loc.to_s == "location.nit:82,2--0,0"
	#
	# loc = new Location.from_string("location.nit:82--105")
	# assert loc.to_s == "location.nit:82,0--105,0"
	#
	# loc = new Location.from_string("location.nit:82,2--105")
	# assert loc.to_s == "location.nit:82,2--105,0"
	#
	# loc = new Location.from_string("location.nit:82--105,8")
	# assert loc.to_s == "location.nit:82,0--105,8"
	# ~~~
	init from_string(string: String) is
		nosuper
	do
		self.line_start = 0
		self.line_end = 0
		self.column_start = 0
		self.column_end = 0
		# parses the location string and init position vars
		var parts = string.split_with(":")
		var filename = parts.shift
		self.file = new SourceFile(filename, new FileReader.open(filename))
		# split position
		if parts.is_empty then return
		var pos = parts.first.split_with("--")
		# split start position
		if pos.first.has(",") then
			var pos1 = pos.first.split_with(",")
			self.line_start = pos1[0].to_i
			if pos1.length > 1 then
				self.column_start = pos1[1].to_i
			end
		else
			self.line_start = pos.first.to_i
		end
		# split end position
		if pos.length <= 1 then return
		if pos[1].has(",") then
			var pos2 = pos[1].split_with(",")
			if pos2.length > 1 then
				self.line_end = pos2[0].to_i
				self.column_end = pos2[1].to_i
			else
				self.line_end = self.line_start
				self.column_end = pos2[0].to_i
			end
		else
			self.line_end = pos[1].to_i
		end
	end

	# Initialize a location corresponding to an opaque file.
	#
	# The path is used as is and is not open nor read.
	init opaque_file(path: String)
	do
		var source = new SourceFile.from_string(path, "")
		init(source, 0, 0, 0, 0)
	end

	# The index in the start character in the source
	fun pstart: Int do return file.line_starts[line_start-1] + column_start-1

	# The index on the end character in the source
	fun pend: Int do return file.line_starts[line_end-1] + column_end-1

	# The verbatim associated text in the source-file
	fun text: String
	do
		var res = self.text_cache
		if res != null then return res
		var l = self
		var pstart = self.pstart
		var pend = self.pend
		res = l.file.string.substring(pstart, pend-pstart+1)
		self.text_cache = res
		return res
	end

	private var text_cache: nullable String = null

	redef fun ==(other: nullable Object): Bool do
		if other == null then return false
		if not other isa Location then return false

		if other.file != file then return false
		if other.line_start != line_start then return false
		if other.line_end != line_end then return false
		if other.column_start != column_start then return false
		if other.column_end != column_end then return false

		return true
	end

	# Is `self` included (or equals) to `loc`?
	fun located_in(loc: nullable Location): Bool do
		if loc == null then return false

		if line_start < loc.line_start then return false
		if line_start > loc.line_end then return false

		if line_end > loc.line_end then return false

		if line_start == loc.line_start then
			if column_start < loc.column_start then return false
			if line_start == loc.line_end and column_start > loc.column_end then return false
		end

		if line_end == loc.line_end and column_end > loc.column_end then return false

		return true
	end

	redef fun to_s: String do
		var file_part = ""
		if file != null then
			file_part = file.filename
		end

		if line_start <= 0 then return file_part

		if file != null and file.filename.length > 0 then file_part += ":"

		if line_start == line_end then
			if column_start == column_end then
				return "{file_part}{line_start},{column_start}"
			else
				return "{file_part}{line_start},{column_start}--{column_end}"
			end
		else
			return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
		end
	end

	# Return a location message according to an observer.
	#
	# Currently, if both are in the same file, the file information is not present in the result.
	fun relative_to(loc: nullable Location): String do
		var relative: Location
		if loc != null and loc.file == self.file then
			relative = new Location(null, self.line_start, self.line_end, self.column_start, self.column_end)
		else
			relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
		end
		return relative.to_s
	end

	redef fun <(other: OTHER): Bool do
		if self == other then return false
		if self.located_in(other) then return true
		if other.located_in(self) then return false

		if line_start != other.line_start then return line_start < other.line_start
		if column_start != other.column_start then return column_start < other.column_start
		if line_end != other.line_end then return line_end < other.line_end

		return column_end < other.column_end
	end

	# Return the associated line with the location highlighted with color and a caret under the starting position
	# `color` must be and terminal escape sequence used as `"{escape}[{color}m;"`
	# * `"0;31"` for red
	# * `"1;31"` for bright red
	# * `"0;32"` for green
	fun colored_line(color: String): String
	do
		var esc = 27.code_point
		var def = "{esc}[0m"
		var col = "{esc}[{color}m"

		var l = self
		var i = l.line_start
		if i <= 0 then return ""

		var line_start = l.file.line_starts[i-1]
		var line_end = line_start
		var string = l.file.string
		while line_end+1 < string.length and string.chars[line_end+1] != '\n' and string.chars[line_end+1] != '\r' do
			line_end += 1
		end
		var lstart
		if l.column_start > 0 then
			lstart = string.substring(line_start, l.column_start - 1)
		else
			lstart = ""
		end
		var cend
		if i != l.line_end then
			cend = line_end - line_start + 1
		else
			cend = l.column_end
		end
		var lmid
		var lend
		if line_start + cend <= string.length then
			lmid = string.substring(line_start + l.column_start - 1, cend - l.column_start + 1)
			lend = string.substring(line_start + cend, line_end - line_start - cend + 1)
		else
			lmid = ""
			lend = ""
		end
		var indent = new FlatBuffer
		for j in [line_start..line_start+l.column_start-1[ do
			if string.chars[j] == '\t' then
				indent.add '\t'
			else
				indent.add ' '
			end
		end
		return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^"
	end
end
src/location.nit:66,1--317,3

nitc :: toolcontext $ Location
redef class Location
	# Errors and warnings associated to this location.
	var messages: nullable Array[Message]

	# Add a message to `self`
	#
	# See `messages`
	private fun add_message(m: Message)
	do
		var ms = messages
		if ms == null then
			ms = new Array[Message]
			messages = ms
		end
		ms.add m
		var s = file
		if s != null then s.messages.add m
	end
end
src/toolcontext.nit:118,1--136,3

nitc :: light_c $ Location
redef class Location
	fun as_line_pragma: String do return "#line {line_start-1} \"{file.filename}\"\n"
end
src/ffi/light_c.nit:69,1--71,3

nitc :: java_compiler $ Location
redef class Location
	# Return a shortened version of the location with `"{file}:{line_start}"`
	fun short_location: String do
		var file = self.file
		if file == null then return "<no file>:{line_start}"
		return "{file.filename.escape_to_c}:{line_start}"
	end
end
src/compiler/java_compiler.nit:1289,1--1296,3

nitc :: json_model $ Location
redef class nitc::Location
	serialize

	redef fun core_serialize_to(v) do
		v.serialize_attribute("column_end", column_end)
		v.serialize_attribute("column_start", column_start)
		v.serialize_attribute("line_end", line_end)
		v.serialize_attribute("line_start", line_start)
		var file = self.file
		if file != null then
			v.serialize_attribute("file", file.filename)
		end
	end
end
src/doc/templates/json_model.nit:80,1--93,3