Debian Codegraph @

src/Yi/Syntax/Layout.hs

raw
1 {-# LANGUAGE PatternGuards #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3
4 -- Note: If the first line of the file has wrong indentation, some of the
5 -- code might be left outside of the blocks
6 module Yi.Syntax.Layout (layoutHandler, State) where
7
8 import Data.List (find)
9 import Data.Maybe (isJust)
10 import Yi.Lexer.Alex (AlexState (..), Posn (Posn), Tok (Tok, tokPosn, tokT), startPosn)
11 import Yi.Syntax (Scanner (..))
12
13 data BlockOpen t = Indent Int -- block opened because of indentation; parameter is the column of it.
14 | Paren t -- block opened because of parentheses
15 deriving Show
16
17 isParen :: BlockOpen t -> Bool
18 isParen (Paren _) = True
19 isParen _ = False
20
21 data IState t = IState [BlockOpen t] -- current block nesting
22 Bool -- should we open a compound now ?
23 Int -- last line number
24 deriving Show
25 type State t lexState = (IState t, AlexState lexState)
26
27
28 -- | Transform a scanner into a scanner that also adds opening,
29 -- closing and "next" tokens to indicate layout.
30
31 -- @isSpecial@ predicate indicates a token that starts a compound,
32 -- like "where", "do", ...
33
34 -- @isIgnore@ predicate indicates a token that is to be ignored for
35 -- layout. (eg. pre-processor directive...)
36
37 -- @parens@ is a list of couple of matching parenthesis-like tokens
38 -- "()[]{}...".
39
40
41 layoutHandler :: forall t lexState. (Show t, Eq t) => (t -> Bool) -> [(t,t)] ->
42 (Tok t -> Bool) ->
43 (t,t,t) -> (Tok t -> Bool) ->
44 Scanner (AlexState lexState) (Tok t) -> Scanner (State t lexState) (Tok t)
45 layoutHandler isSpecial parens isIgnored (openT, closeT, nextT) isGroupOpen lexSource = Scanner
46 {
47 scanLooked = scanLooked lexSource . snd,
48 scanEmpty = error "layoutHandler: scanEmpty",
49 scanInit = (IState [] True (-1), scanInit lexSource),
50 scanRun = \st -> let result = parse (fst st) (scanRun lexSource (snd st))
51 in --trace ("toks = " ++ show (fmap snd result)) $
52 result
53 }
54 where dummyAlexState = AlexState
55 {
56 stLexer = error "dummyAlexState: should not be reused for restart!",
57 lookedOffset = maxBound, -- setting this to maxBound ensures nobody ever uses it.
58 stPosn = startPosn
59 }
60
61 deepestIndent [] = -1
62 deepestIndent (Indent i:_) = i
63 deepestIndent (_:levs) = deepestIndent levs
64
65 deepestParen _ [] = False
66 deepestParen p (Paren t:levs) = p == t || deepestParen p levs
67 deepestParen p (_:levs) = deepestParen p levs
68
69 findParen f t = find ((== t) . f) parens
70
71 parse :: IState t -> [(AlexState lexState, Tok t)] -> [(State t lexState, Tok t)]
72 parse iSt@(IState levels doOpen lastLine)
73 toks@((aSt, tok@Tok {tokPosn = Posn _nextOfs line col}) : tokss)
74
75 -- ignore this token
76 | isIgnored tok
77 = (st, tok) : parse (IState levels doOpen line) tokss
78
79 -- start a compound if the rest of the line is empty then skip to it!
80 | doOpen
81 = if isGroupOpen tok -- check so that the do is not followed by a {
82 then parse (IState levels False lastLine) toks
83 else (st', tt openT) : parse (IState (Indent col : levels) False line) toks
84 -- if it's a block opening, we ignore the layout, and just let the "normal" rule
85 -- handle the creation of another level.
86
87 -- close, or prepare to close, a paren block
88 | Just (openTok,_) <- findParen snd $ tokT tok,
89 deepestParen openTok levels
90
91 = case levels of
92 Indent _:levs -> (st',tt closeT) : parse (IState levs False lastLine) toks
93 -- close an indent level inside the paren block
94 Paren openTok' :levs
95 | openTok == openTok' -> (st', tok) : parse (IState levs False line) tokss
96 | otherwise -> parse (IState levs False line) toks
97 -- close one level of nesting.
98 [] -> error $ "Parse: " ++ show iSt
99
100 -- pop an indent block
101 | col < deepestIndent levels
102 = let (_lev:levs) = dropWhile isParen levels
103 in (st', tt closeT) : parse (IState levs doOpen lastLine) toks
104 -- drop all paren levels inside the indent
105
106 -- next item
107 | line > lastLine &&
108 col == deepestIndent levels
109 = (st', tt nextT) : parse (IState (dropWhile isParen levels) doOpen line) toks
110 -- drop all paren levels inside the indent
111
112 -- open a paren block
113 | isJust $ findParen fst $ tokT tok
114 = (st', tok) : parse (IState (Paren (tokT tok):levels) (isSpecial (tokT tok)) line) tokss
115 -- important note: the the token can be both special and an opening. This is the case of the
116 -- haskell 'let' (which is closed by 'in'). In that case the inner block is that of the indentation.
117
118 -- prepare to open a compound
119 | isSpecial (tokT tok)
120 = (st', tok) : parse (IState levels True line) tokss
121
122 | otherwise
123 = (st', tok) : parse (IState levels doOpen line) tokss
124 where st = (iSt, aSt)
125 st' = (iSt, aSt {lookedOffset = max peeked (lookedOffset aSt)})
126 tt t = Tok t 0 (tokPosn tok)
127 peeked = case tokss of
128 [] -> maxBound
129 (AlexState {lookedOffset = p},_):_ -> p
130 -- This function checked the position and kind of the
131 -- next token. We peeked further, and so must
132 -- update the lookedOffset accordingly.
133
134 -- finish by closing all the indent states.
135 parse iSt@(IState (Indent _:levs) doOpen posn) []
136 = ((iSt,dummyAlexState), Tok closeT 0 maxPosn) : parse (IState levs doOpen posn) []
137 parse (IState (Paren _:levs) doOpen posn) []
138 = parse (IState levs doOpen posn) []
139 parse (IState [] _ _) [] = []
140
141
142 maxPosn :: Posn
143 maxPosn = Posn (-1) (-1) 0
144 -- HACK! here we have collusion between using (-1) here and the parsing of
145 -- OnlineTrees, which relies on the position of the last token to stop
146 -- the parsing.
147