prequel/span

Types

The position of a line (or column) relative to a span: it could either be its first or last line, it could be inside the span, or it could be outside of it.

You can look at the classify_line for an example usage of this type.

pub type Position {
  First
  Inside
  Last
  Outside
}

Constructors

  • First
  • Inside
  • Last
  • Outside

A span indicating a slice of a source code file.

pub type Span {
  Span(
    start_line: Int,
    end_line: Int,
    start_column: Int,
    end_column: Int,
  )
}

Constructors

  • Span(
      start_line: Int,
      end_line: Int,
      start_column: Int,
      end_column: Int,
    )

Functions

pub fn classify_line(span: Span, line: Int) -> Position

Returns the position of a line relative to the given span:

  • First means that the given line is the first line of the span
  • Last means that the given line is the last line of the span
  • Inside means that the line falls between the first and last lines of the span
  • Outside is for a line that is not contained inside the span

Examples

> classify_line(new(1, 3, 2, 2), 1)
First
> classify_line(new(1, 3, 2, 2), 2)
Inside
> classify_line(new(1, 3, 2, 2), 3)
Last
> classify_line(new(1, 3, 2, 2), 4)
Outside
pub fn columns(span: Span) -> NonEmptyList(Int)

Returns a non empty list with the starting and ending columns of a span.

Examples

> lines(point(11, 1)) |> non_empty_list.to_list
[1]
> lines(new(11, 15, 2, 4)) |> non_empty_list.to_list
[2, 4]
pub fn contains_line(span: Span, line: Int) -> Bool

Returns true if the given line is contained inside the span.

## Examples

> new(1, 11, 2, 3) |> contains_line(1)
True
> new(1, 11, 2, 3) |> contains_line(11)
True
> new(1, 11, 2, 3) |> contains_line(10)
True
> new(1, 11, 2, 3) |> contains_line(12)
False
pub fn ends_on_line(span: Span, line: Int) -> Bool

Returns true if the span ends on the given line.

## Examples

> point(1, 2) |> ends_on_line(1)
True
> point(11, 2) |> ends_on_line(1)
False
pub fn is_segment(span: Span) -> Bool

Returns true if the given span is a single-line segment, that is if its starting and ending line are the same.

Examples

> is_segment(segment(1, 2, 3))
True
> is_segment(point(2, 5))
True
> is_segment(new(1, 2, 3, 4))
False
pub fn lines(span: Span) -> NonEmptyList(Int)

Returns a non empty list with the starting and ending lines of a span.

Examples

> lines(point(11, 1)) |> non_empty_list.to_list
[11]
> lines(new(11, 15, 2, 4)) |> non_empty_list.to_list
[11, 15]
pub fn max_column(spans: NonEmptyList(Span)) -> Int

Returns the smallest line in a non empty list of spans.

## Examples

> non_empty_list.new(point(14, 1), [segment(2, 4, 5)])
> |> max_column
5
pub fn max_line(spans: NonEmptyList(Span)) -> Int

Returns the greatest line in a non empty list of spans.

Examples

> non_empty_list.new(point(14, 1), [segment(2, 4, 5)])
> |> max_line
14
pub fn merge(one: Span, with other: Span) -> Span

Merge two spans together obtaining the largest possible span: it starts from the smallest line and column and ends at the largest line and column.

Examples

> point(1, 2) |> merge(point(3, 1))
Span(1, 3, 1, 2)
> new(1, 4, 3, 4) |> merge(new(3, 5, 2, 3))
Span(1, 5, 2, 4)
pub fn min_column(spans: NonEmptyList(Span)) -> Int

Returns the smallest column in a non empty list of spans.

Examples

> non_empty_list.new(point(14, 1), [segment(2, 4, 5)])
> |> min_column
1
pub fn min_line(spans: NonEmptyList(Span)) -> Int

Returns the smallest line in a non empty list of spans.

Examples

> non_empty_list.new(point(14, 1), [segment(2, 4, 5)])
> |> min_line
2
pub fn new(start_line: Int, end_line: Int, start_column: Int, end_column: Int) -> Span

Creates a new span given its start line, end line, start column and end column.

Examples

> new(1, 2, 3, 4)
Span(1, 2, 3, 4)
pub fn point(line: Int, column: Int) -> Span

Creates a span that spans over a single line and a single column.

## Examples

> point(1, 2)
Span(1, 1, 2, 2)
pub fn segment(line: Int, start_column: Int, end_column: Int) -> Span

Creates a span taht spans over a single line and multiple columns.

Examples

> segment(1, 2 ,5)
Span(1, 1, 2, 5)
pub fn starts_at_line(span: Span, line: Int) -> Bool

Returns true if the span starts at the given line.

## Examples

> point(1, 2) |> starts_at_line(1)
True
> point(11, 1) |> starts_at_line(1)
False
Search Document