{-|
Description: Locations.

* Locations.
-}
module Parser.Locations (
  File_path_and_location (..),
  Location,
  With_location (..),
  init_location,
  next_char,
  next_line,
  write_file_path_and_location,
  write_location) where
  import Parser.Utilities
  deriving instance Eq Location
  deriving instance Ord Location
  deriving instance Show File_path_and_location
  deriving instance Show Location
  deriving instance Show t => Show (With_location t)
  -- | Locations with a file path.
  data File_path_and_location = File_path_and_location File_path Location
  -- | Locations.
  data Location = Location Integer Integer
  -- | Add a location to any type.
  data With_location t = With_location Location t
  -- | First line, first character.
  init_location :: Location
  init_location :: Location
init_location = Integer -> Integer -> Location
Location Integer
1 Integer
1
  -- | Move one character to the right.
  next_char :: Location -> Location
  next_char :: Location -> Location
next_char (Location Integer
line Integer
char) = Integer -> Integer -> Location
Location Integer
line (Integer
1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
char)
  -- | Move to the next line.
  next_line :: Location -> Location
  next_line :: Location -> Location
next_line (Location Integer
line Integer
_) = Integer -> Integer -> Location
Location (Integer
1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
line) Integer
1
  -- | Write the location with the file path.
  write_file_path_and_location :: File_path -> Location -> String
  write_file_path_and_location :: File_path -> Location -> String
write_file_path_and_location File_path
file_path Location
location = File_path -> String
write_file_path File_path
file_path String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
":" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Location -> String
write_location Location
location
  -- | Write the location.
  write_location :: Location -> String
  write_location :: Location -> String
write_location (Location Integer
line Integer
char) = Integer -> String
forall a. Show a => a -> String
show Integer
line String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
":" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Integer -> String
forall a. Show a => a -> String
show Integer
char