1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{- |
     Author  : Kai-Oliver Prott
     Version : October 2024

     Datatypes for identifiers from the curry-frontend
-}





module Curry.Ident where

import Curry.SpanInfo
import Curry.Span
import Curry.Position

-- | Identifier for modules, from the curry-frontend.
data ModuleIdent = ModuleIdent SpanInfo [String]
  deriving (Show, Read)

instance Eq ModuleIdent where
  ModuleIdent _ ss1 == ModuleIdent _ ss2 = ss1 == ss2

instance Ord ModuleIdent where
  compare (ModuleIdent _ ss1) (ModuleIdent _ ss2) = compare ss1 ss2

instance HasSpanInfo ModuleIdent where
  getSpanInfo     (ModuleIdent spi _ ) = spi
  setSpanInfo spi (ModuleIdent _   ss) = ModuleIdent spi ss
  updateEndPos i =
    setEndPosition (incr (getStartPosition i) (mIdentLength i - 1)) i

-- | Identifier, from the curry-frontend.
data Ident = Ident SpanInfo String Int
  deriving (Show, Read)

instance Eq Ident where
  Ident _ s1 id1 == Ident _ s2 id2 = (s1, id1) == (s2, id2)

instance Ord Ident where
  compare (Ident _ s1 id1) (Ident _ s2 id2) = compare (s1, id1) (s2, id2)

instance HasSpanInfo Ident where
  getSpanInfo (Ident spi _ _)= spi
  setSpanInfo spi (Ident _ s idt) = Ident spi s idt
  updateEndPos i = case i of
    Ident (SpanInfo _ [_,ss]) _ _
      -> setEndPosition (end ss) i
    _ -> setEndPosition (incr (getStartPosition i) (identLength i - 1)) i

-- | Qualified identifier, from the curry-frontend.
data QualIdent = QualIdent SpanInfo (Maybe ModuleIdent) Ident
  deriving (Show, Read)

instance Eq QualIdent where
  QualIdent _ mid1 idt1 == QualIdent _ mid2 idt2 = (mid1, idt1) == (mid2, idt2)

instance Ord QualIdent where
  compare (QualIdent _ mid1 idt1) (QualIdent _ mid2 idt2) =
    compare (mid1, idt1) (mid2, idt2)

instance HasSpanInfo QualIdent where
  getSpanInfo (QualIdent spi _ _ ) = spi
  setSpanInfo spi (QualIdent _ mid idt) = QualIdent spi mid idt
  updateEndPos i = case i of
    QualIdent (SpanInfo _ [_,ss]) _ _
      -> setEndPosition (end ss) i
    _ -> setEndPosition (incr (getStartPosition i) (qIdentLength i - 1)) i

qIdentLength :: QualIdent -> Int
qIdentLength (QualIdent _ (Just m) i) = identLength i + mIdentLength m
qIdentLength (QualIdent _ Nothing  i) = identLength i

mIdentLength :: ModuleIdent -> Int
mIdentLength a = length (concat (midQualifiers a))
               + length (midQualifiers a)

identLength :: Ident -> Int
identLength a = length (idName a)

-- | Remove the qualification of an 'QualIdent'
unqualify :: QualIdent -> Ident
unqualify (QualIdent _ _ i) = i

idName :: Ident -> String
idName (Ident _ n _) = n

midQualifiers :: ModuleIdent -> [String]
midQualifiers (ModuleIdent _ mid) = mid