module Agda.Utils.Suffix where

import Data.Char

import Agda.Utils.Impossible

------------------------------------------------------------------------
-- Subscript digits

-- | Is the character one of the subscripts @'₀'@-@'₉'@?

isSubscriptDigit :: Char -> Bool
isSubscriptDigit :: Char -> Bool
isSubscriptDigit Char
c = Char
'₀' Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'₉'

-- | Converts @'0'@-@'9'@ to @'₀'@-@'₉'@
--
-- Precondition: The digit needs to be in range.

toSubscriptDigit :: Char -> Char
toSubscriptDigit :: Char -> Char
toSubscriptDigit Char
d
  | Char -> Bool
isDigit Char
d = Int -> Char
forall a. Enum a => Int -> a
toEnum (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'₀' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'0'))
  | Bool
otherwise = Char
forall a. HasCallStack => a
__IMPOSSIBLE__

-- | Converts @'₀'@-@'₉'@ to @'0'@-@'9'@.
--
-- Precondition: The digit needs to be in range.

fromSubscriptDigit :: Char -> Char
fromSubscriptDigit :: Char -> Char
fromSubscriptDigit Char
d
  | Char -> Bool
isSubscriptDigit Char
d = Int -> Char
forall a. Enum a => Int -> a
toEnum (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'0' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
d Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'₀'))
  | Bool
otherwise          = Char
forall a. HasCallStack => a
__IMPOSSIBLE__

------------------------------------------------------------------------
-- Suffices

-- | Classification of identifier variants.

data Suffix
  = Prime     Int  -- ^ Identifier ends in @Int@ many primes.
  | Index     Int  -- ^ Identifier ends in number @Int@ (ordinary digits).
  | Subscript Int  -- ^ Identifier ends in number @Int@ (subscript digits).

-- | Increase the suffix by one.  If no suffix yet, put a subscript @1@
--   unless users do not want us to use any unicode.

nextSuffix :: Suffix -> Suffix
nextSuffix :: Suffix -> Suffix
nextSuffix (Prime Int
i)     = Int -> Suffix
Prime (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
nextSuffix (Index Int
i)     = Int -> Suffix
Index (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
nextSuffix (Subscript Int
i) = Int -> Suffix
Subscript (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1

-- | Parse suffix.

suffixView :: String -> (String, Maybe Suffix)
suffixView :: String -> (String, Maybe Suffix)
suffixView String
s
    | (ps :: String
ps@(Char
_:String
_), String
s') <- (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'\'') String
rs         = (String -> String
forall a. [a] -> [a]
reverse String
s', Suffix -> Maybe Suffix
forall a. a -> Maybe a
Just (Suffix -> Maybe Suffix) -> Suffix -> Maybe Suffix
forall a b. (a -> b) -> a -> b
$ Int -> Suffix
Prime (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
ps)
    | (ns :: String
ns@(Char
_:String
_), String
s') <- (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isDigit String
rs          = (String -> String
forall a. [a] -> [a]
reverse String
s', Suffix -> Maybe Suffix
forall a. a -> Maybe a
Just (Suffix -> Maybe Suffix) -> Suffix -> Maybe Suffix
forall a b. (a -> b) -> a -> b
$ Int -> Suffix
Index (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. Read a => String -> a
read (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$ String -> String
forall a. [a] -> [a]
reverse String
ns)
    | (ns :: String
ns@(Char
_:String
_), String
s') <- (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Char -> Bool
isSubscriptDigit String
rs = (String -> String
forall a. [a] -> [a]
reverse String
s', Suffix -> Maybe Suffix
forall a. a -> Maybe a
Just (Suffix -> Maybe Suffix) -> Suffix -> Maybe Suffix
forall a b. (a -> b) -> a -> b
$ Int -> Suffix
Subscript (Int -> Suffix) -> Int -> Suffix
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. Read a => String -> a
read (String -> Int) -> String -> Int
forall a b. (a -> b) -> a -> b
$
                                                      (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
fromSubscriptDigit (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String
forall a. [a] -> [a]
reverse String
ns)
    | Bool
otherwise                                  = (String
s, Maybe Suffix
forall a. Maybe a
Nothing)
    where rs :: String
rs = String -> String
forall a. [a] -> [a]
reverse String
s

-- | Print suffix.

renderSuffix :: Suffix -> String
renderSuffix :: Suffix -> String
renderSuffix (Prime Int
n)     = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
n Char
'\''
renderSuffix (Index Int
i)     = Int -> String
forall a. Show a => a -> String
show Int
i
renderSuffix (Subscript Int
i) = (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toSubscriptDigit (Int -> String
forall a. Show a => a -> String
show Int
i)

addSuffix :: String -> Suffix -> String
addSuffix :: String -> Suffix -> String
addSuffix String
str Suffix
suffix = String
str String -> String -> String
forall a. [a] -> [a] -> [a]
++ Suffix -> String
renderSuffix Suffix
suffix