3.12. kink/LOC

The companion mod for loc vals.

3.12.1. type loc

A loc val represents a location in a program text.

A loc val consists of (Program_name, Program_text, Pos), where Pos is the pos index in the runes of the Program_text.

Example:

:LOC.require_from('kink/')
:Loc <- LOC.new('foo.kn' "ping(Pong)\nfoo(Bar)\n" 15)
stdout.print_line(Loc.program_name.repr)  # => "foo.kn"
stdout.print_line(Loc.program_text.repr)  # => "ping(Pong)\nfoo(Bar)\n"
stdout.print_line(Loc.line_num.repr)      # => 2
stdout.print_line(Loc.col_offset.repr)    # => 4
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"

Loc.program_name

`program_name` returns `Program_name` attribute of `Loc`.

Loc.program_text

`program_name` returns `Program_text` attribute of `Loc`.

Loc.pos

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

The result is an int num, in the range [0, Program_text.size].

Loc.line_num

`line_num` returns the line number of `Loc` in the program.

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

Loc.col_offset

`col_offset` returns the column offset of `Loc`.

The column offset is the number of runes from the start pos of the line to `Pos`. The start pos of the line is the pos just after the nearest preceding LF character, or zero if there is no preceding LF character.

Loc.col_num

`col_num` returns the column number of the loc.

The result is equal to Loc.col_offset + 1.

Loc.line

`line` returns the line str where the loc resides.

`line` returns a slice of `Program_text` from the start pos of the line which the loc resides, to the start pos of the next line or the end of the text. Thus, the result str may include an LF character as the last rune.

Loc.indicator

`indicator` returns a str indicating the pos of `Loc` in the line, such as "stdout.-->write(Bin)".

The result str does not contain LF characters.

Loc.desc

`desc` returns a str describing `Loc`.

`desc` returns a str such as "foo.kn L42 C9", when the program text is "foo.kn", the line number is 42, and the column number is 9.

Loc1.op_eq(Loc2)

`op_eq` returns whether two locs indicate the same pos in the same program.

Two locs are equal if and only if the program name, the program text and the pos are equal.

3.12.2. LOC.new(Program_name Program_text Pos)

`new` returns a loc with the given attributes.

3.12.3. LOC.empty

`empty` returns a loc with the empty Program_name, the empty Program_text, and Pos=0.

3.12.4. LOC.is?(Val)

`is?` returns whether `Val` is a loc val.