6.79. kink/program/LOCATION

6.79.1. type location

`location` is a position index of Unicode code points in a program text.

A `location` consists of the following attributes:

• Program: a `program`

• Pos: a position index as an integer `num`.

Invariant

0 <= Pos <= Program.text.size

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.program.repr)     # => (program name="foo.kn")
stdout.print_line(Loc.line_num.repr)    # => 2
stdout.print_line(Loc.col_num.repr)     # => 5
stdout.print_line(Loc.line.repr)        # => "foo(Bar)\n"
stdout.print_line(Loc.indicator.repr)   # => "foo(-->Bar)"
stdout.print_line(Loc.desc.repr)        # => "foo.kn L2 C5"

6.79.1.1. Loc.program

`program` returns `Program` attribute of `Loc`.

6.79.1.2. Loc.pos

`pos` returns `Pos` attribute of `Loc`.

6.79.1.3. Loc.line_num

`line_num` returns the line number of `Loc` as an integer `num`.

The line number is the number of LF characters plus one in the program text before `Loc`.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.line_num.repr) # => 2

6.79.1.4. Loc.col_num

`col_num` returns the column number of `Loc` as an integer `num`.

The result is the number of code points plus one in the line before `Loc`.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.col_num.repr) # => 5

6.79.1.5. Loc.line

`line` returns the line of `Loc` as a `str`.

`line` returns a slice of the program text in the range [From, To), where `From` is the code point index of the beginning of the line, and `To` is the code point index of the beginning of the next line or the end of the text. Thus, the result `str` may include an LF character as the last code point.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.line.repr) # => "foo(Bar)\n"

6.79.1.6. Loc.indicator

`indicator` returns a `str` which represents the position of `Loc` in the line.

The result `str` does not contain CR or LF characters.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.indicator.repr) # => "foo(-->Bar)"

6.79.1.7. Loc.desc

`desc` returns a `str` which describes the program name, the line number, and the column number.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.desc.repr) # => "foo.kn L2 C5"

6.79.1.8. Loc1 == Loc2

`==` operator, or `op_eq` method, returns whether two `location` values have the equal `Program` and `Pos` attributes.

6.79.1.9. Loc.repr

`repr` returns a `str` representation of `Loc` for debugging purposes.

Example

:LOCATION.require_from('kink/program/')
:PROGRAM.require_from('kink/program/')

:Program <- PROGRAM.new('foo.kn' "ping(Pong)\nfoo(Bar)\n")
:Loc <- LOCATION.new(Program 15)
stdout.print_line(Loc.repr)  # => (location foo.kn L2 C5)

6.79.2. LOCATION.new(Program Pos)

`new` returns a `location` with the given attributes.

Preconditions

`Program` must be a `program`.

`Pos` must be an integer `num`.

0 <= Pos <= Program.text.size

6.79.3. LOCATION.empty

`empty` returns a `location` with the empty program name, the empty program text, and Pos=0.

6.79.4. LOCATION.is?(Val)

`is?` returns whether `Val` is a `location`.