Skip to content

Change Position to Int #162

Open
Open
@jamesdbrock

Description

@jamesdbrock

Delete Position { column :: Int, line :: Int } and replace it with Int representing the position index from the beginning of the input. For String, the position index would be in units of CodePoints.

Delete the updatePosString and updatePosSingle functions.

-- | Updates a `Position` by adding the columns and lines in `String`.
updatePosString :: Position -> String -> String -> Position
updatePosString pos before after = case uncons before of
Nothing -> pos
Just { head, tail } -> do
let
newPos
| String.null tail = updatePosSingle pos head after
| otherwise = updatePosSingle pos head tail
updatePosString newPos tail after
-- | Updates a `Position` by adding the columns and lines in a
-- | single `CodePoint`.
updatePosSingle :: Position -> CodePoint -> String -> Position
updatePosSingle (Position { line, column }) cp after = case fromEnum cp of
10 -> Position { line: line + 1, column: 1 } -- "\n"
13 ->
case codePointAt 0 after of
Just nextCp | fromEnum nextCp == 10 -> Position { line, column } -- "\r\n" lookahead
_ -> Position { line: line + 1, column: 1 } -- "\r"
9 -> Position { line, column: column + 8 - ((column - 1) `mod` 8) } -- "\t" Who says that one tab is 8 columns?
_ -> Position { line, column: column + 1 }

In updatePosString there is an assumption that 1 tab = 8 spaces and there is no way for the library user to change that behavior. So I think updatePosString has always been fundamentally broken.

We want to provide a way to track the line and column during the parse so that

  1. We can write indentation-sensitive parsers.
  2. We can report the line and column in a ParseError.

The Text.Parsing.Indent module is used by some packages so we should try to keep it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions