Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Line
- data Column
- data Loc
- data Span
- data Area
- loc :: Line -> Column -> Loc
- origin :: Loc
- spanFromTo :: Loc -> Loc -> Span
- spanFromToMay :: Loc -> Loc -> Maybe Span
- areaFromTo :: Loc -> Loc -> Area
- spanArea :: Span -> Area
- locLine :: Loc -> Line
- locColumn :: Loc -> Column
- spanStart :: Span -> Loc
- spanEnd :: Span -> Loc
- areaStart :: Area -> Maybe Loc
- areaEnd :: Area -> Maybe Loc
- areaSpansAsc :: Area -> [Span]
- spanUnion :: Span -> Span -> OneToTwo Span
- spanDifference :: Span -> Span -> ZeroToTwo Span
- areaUnion :: Area -> Area -> Area
- areaDifference :: Area -> Area -> Area
- data Pos
- data OneToTwo a
- data ZeroToTwo a
- class ToNat a where
- data LocException = EmptySpan
Concepts
Line
and Column
are positive integers representing line and column numbers.
The product of Line
and Column
is a Loc
, which represents a position
between characters in multiline text. The smallest loc is origin
: line 1,
column 1.
Here's a small piece of text for illustration:
1 2 12345678901234567890123456789 ┌───────────────────────────────┐ 1 │ I have my reasons, you │ 2 │ have yours. What's obvious │ 3 │ to me isn't to everyone else, │ 4 │ and vice versa. │ └───────────────────────────────┘
In this example, the word “obvious” starts at line 2, column 20, and it ends at
line 2, column 27. The Show
instance uses a shorthand notation denoting
these locs as 2:20
and 2:27
.
A Span
is a nonempty contiguous region of text between two locs; think of it
like a highlighted area in a simple text editor. In the above example, a span
that covers the word “obvious” starts at 2:20
and ends at 2:27
. The Show
instance describes this tersely as 2:20-2:27
.
Multiple non-overlapping regions form an Area
. You may also think of an
area like a span that can be empty or have “gaps”. In the example above, the
first three words “I have my”, and not the spaces between them, are covered by
the area [1:1-1:2,1:3-1:7,1:8-1:10]
.
Imports
Recommended import:
import Data.Loc.Types import qualified Data.Loc as Loc
Core types
Constructing
Loc
Span
spanFromTo :: Loc -> Loc -> Span Source #
Attempt to construct a Span
from two Loc
s. The lesser loc will be the
start, and the greater loc will be the end. The two locs must not be equal,
or else this throws EmptySpan
.
The safe version of this function is spanFromToMay
.
This is an alias for fromTo
.
spanFromToMay :: Loc -> Loc -> Maybe Span Source #
Attempt to construct a Span
from two Loc
s. The lesser loc will be the
start, and the greater loc will be the end. If the two locs are not equal,
the result is Nothing
, because a span cannot be empty.
This is the safe version of spanFromTo
, which throws an exception instead.
This is an alias for fromToMay
.
Area
Deconstructing
Loc
Span
Area
areaSpansAsc :: Area -> [Span] Source #
Combining
Span
spanDifference :: Span -> Span -> ZeroToTwo Span Source #
The difference between two Spans
s. a
contains what is covered by
-
ba
and not covered by b
.
This is an alias for -
.
Area
Miscellaneous
Pos
stands for positive integer. You can also think of it as position,
because we use it to represent line and column numbers (Line
and Column
).
Pos
has instances of several of the standard numeric typeclasses, although
many of the operations throw Underflow
when non-positive values result.
Pos
does not have an Integral
instance, because there is no sensible
way to implement quotRem
.
Enum Pos Source # |
|
Eq Pos Source # | |
Num Pos Source # |
|
Ord Pos Source # | |
Read Pos Source # | |
Real Pos Source # | |
Show Pos Source # | |
ToNat Pos Source # | |
List of length 1 or 2.
List of length 0, 1, or 2.