megaparsec-5.0.0: Monadic parser combinators

Copyright© 2015–2016 Megaparsec contributors
MaintainerMark Karpov <>
Safe HaskellNone




Textual source position. The position includes name of file, line number, and column number. List of such positions can be used to model stack of include files.


Abstract position

data Pos Source

Positive integer that is used to represent line number, column number, and similar things like indentation level. Semigroup instance can be used to safely and purely add Poses together.

Since: 5.0.0

mkPos :: (Integral a, MonadThrow m) => a -> m Pos Source

Construction of Pos from an instance of Integral. The function throws InvalidPosException when given non-positive argument. Note that the function is polymorphic with respect to MonadThrow m, so you can get result inside of Maybe, for example.

Since: 5.0.0

unPos :: Pos -> Word Source

Extract Word from Pos.

Since: 5.0.0

unsafePos :: Word -> Pos Source

Dangerous construction of Pos. Use when you know for sure that argument is positive.

Since: 5.0.0

data InvalidPosException Source

The exception is thrown by mkPos when its argument is not a positive number.

Since: 5.0.0



Source position

data SourcePos Source

The data type SourcePos represents source positions. It contains the name of the source file, a line number, and a column number. Source line and column positions change intensively during parsing, so we need to make them strict to avoid memory leaks.




sourceName :: FilePath

Name of source file

sourceLine :: !Pos

Line number

sourceColumn :: !Pos

Column number

initialPos :: String -> SourcePos Source

Construct initial position (line 1, column 1) given name of source file.

sourcePosPretty :: SourcePos -> String Source

Pretty-print a SourcePos.

Since: 5.0.0

Helpers implementing default behaviors

defaultUpdatePos Source


:: Pos

Tab width

-> SourcePos

Current position

-> Char

Current token

-> (SourcePos, SourcePos)

Actual position and incremented position

Update a source position given a character. The first argument specifies tab width. If the character is a newline ('\n') the line number is incremented by 1. If the character is a tab ('\t') the column number is incremented to the nearest tab position. In all other cases, the column is incremented by 1.

Since: 5.0.0

defaultTabWidth :: Pos Source

Value of tab width used by default. Always prefer this constant when you want to refer to default tab width because actual value may change in future. Current value is 8.