Skip to content

Commit

Permalink
kaico.source: add doc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuro9715 committed Mar 29, 2024
1 parent 89f2623 commit f83f4bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions kaico/source/pos.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
module source

// Pos represents a position in a `Source`.
@[noinit]
pub struct Pos {
pub:
Expand All @@ -14,13 +15,15 @@ pub:
column int @[required]
}

// PosParams is the parameters to create a new `Pos`.
@[params]
pub struct PosParams {
offset int @[required]
line int @[required]
column int @[required]
}

// new_pos creates a new `Pos` in `Source`
pub fn (s &Source) new_pos(pos PosParams) Pos {
return Pos{
source: s
Expand All @@ -38,6 +41,7 @@ fn (lhs Pos) == (rhs Pos) bool {
return lhs.offset == rhs.offset
}

// in_range returns true if `Pos` is in `Range`
pub fn (p Pos) in_range(r Range) bool {
return r.begin <= p && p < r.end
}
4 changes: 2 additions & 2 deletions kaico/source/pos_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
import kaico.source { Range, Source }
import kaico.source as _ { Range, Source }

fn test_op() {
s := Source.from_text('')
s := source.Source.from_text('')
p1 := s.new_pos(offset: 1, line: 1, column: 2)
p2 := s.new_pos(offset: 2, line: 1, column: 3)
assert p1 < p2
Expand Down
11 changes: 9 additions & 2 deletions kaico/source/range.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
module source

// Range represents a range in a `Source`.
@[noinit]
pub struct Range {
pub:
begin Pos
end Pos
}

// Range.new creates a new `Range` between `p1` and `p2`
pub fn Range.new(p1 Pos, p2 Pos) Range {
begin, end := if p1 <= p2 { p1, p2 } else { p2, p1 }
return Range{begin, end}
}

// source returns the `Source` of `Range`
pub fn (r Range) source() &Source {
return r.begin.source
}

// len returns the length of the `Range`
pub fn (r Range) len() int {
return r.end.offset - r.begin.offset
}

// text returns the text between `begin` and `end` of the `Range`
pub fn (r Range) text() string {
return r.source().code[r.begin.offset..r.end.offset]
}

// extend returns a new `Range` that extends the `Range` with `p`
pub fn (r Range) extend(p Pos) Range {
return if p < r.begin {
Range.new(p, r.end)
Expand All @@ -38,7 +44,8 @@ pub fn (r Range) extend(p Pos) Range {
}
}

pub fn (r Range) contains(item Range) bool {
begin, end := item.begin, item.end
// contains returns true if the `Range` contains the another `Range`
pub fn (r Range) contains(target Range) bool {
begin, end := target.begin, target.end
return begin.in_range(r) && (end == r.end || end.in_range(r))
}

0 comments on commit f83f4bc

Please sign in to comment.