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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
{-|
Copyright: (c) Tom Smeding 2026
License: AGPL-3.0-or-later
-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Mustache.TH.Parser (Template(..), Chunk(..), parseTemplate, inlinePartials) where
import Control.Monad (when, guard)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Writer.CPS
import Data.Char
import Data.Foldable (toList, foldMap')
import Data.List (intercalate)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Text qualified as T
import Data.Vector (Vector)
import Data.Vector qualified as V
data Template = Template (Vector Chunk)
deriving (Show)
data Chunk
= Text Text
| Var Bool (Vector Text)
| Section Bool (Vector Text) (Vector Chunk)
| Partial (Maybe Int) Text -- ^ indent, if standalone
deriving (Show)
-- | The tags that have the "standalone" logic (i.e. if they're the only thing
-- on a line except for spaces, that line is deleted entirely) have an
-- additional Int that indicates the column (0-based) at which the tag occurs.
data FlatChunk
= FlatText Text
| FlatVar Bool (Vector Text)
| StartSection Int Bool (Vector Text)
| EndSection Int (Vector Text)
| FlatPartial Int (Maybe Int) Text -- ^ tag column, partial indent if standalone
| EmptyTag Int
-- ^ A tag that produces no output, but does count as standalone for line elimination.
deriving (Show)
-- |
-- * If @recurse@ is true, recursively instantiates partials, and thus explodes
-- when given a loop.
-- * If @laxMissing@ is true, expands missing partials to the
-- empty string instead of leaving them unexpanded.
inlinePartials :: Bool -> Bool -> Map Text Template -> Template -> Template
inlinePartials recurse laxMissing partials = \(Template chunks) -> Template (V.fromList (subChunks chunks))
where
subChunks :: Foldable t => t Chunk -> [Chunk]
subChunks chs = toList (foldMap' subChunk chs)
subChunk :: Chunk -> Bag Chunk
subChunk ch@Text{} = BOne ch
subChunk ch@Var{} = BOne ch
subChunk (Section pos var chs) = BOne (Section pos var (V.fromList (subChunks chs)))
subChunk original@(Partial mindent name) =
case Map.lookup name partials of
Just (Template chunks) ->
case mindent of
Just indent ->
let chunks' = snd (indentChunks True indent (toList chunks))
in if recurse then BList (subChunks chunks') else BList chunks'
Nothing -> BList (toList chunks)
Nothing | laxMissing -> mempty
| otherwise -> BOne original
indentChunks :: Bool -> Int -> [Chunk] -> (Bool, [Chunk])
indentChunks sol _ [] = (sol, [])
indentChunks sol n [Text (text T.:> '\n')] = (True, [Text (indentText sol n text T.:> '\n')])
indentChunks sol n (Text text@(_ T.:> '\n') : chs) = (Text (indentText sol n text) :) <$> indentChunks (n == 0) n chs
indentChunks sol n (Text text : chs) = (Text (indentText sol n text) :) <$> indentChunks False n chs
indentChunks sol n (ch@Var{} : chs) = ((if sol then (Text (T.replicate n " ") :) else id) . (ch :)) <$> indentChunks False n chs
indentChunks sol n (Section pos var body : chs) =
let (sol', body') = indentChunks sol n (toList body)
in (Section pos var (V.fromList body') :) <$> indentChunks sol' n chs
indentChunks sol n (Partial Nothing name : chs) = -- inline partial
(if sol then (Text (T.replicate n " ") :) else id) . (Partial Nothing name :) <$> indentChunks False n chs
indentChunks True n (Partial (Just indent) name : chs) = -- standalone partial with existing indent
(Partial (Just (n + indent)) name :) <$> indentChunks True n chs
indentChunks False _ (Partial (Just _) _ : _) = error "indentChunks: standalone partial not at start of line"
indentText :: Bool -> Int -> Text -> Text
indentText _ _ "" = ""
indentText sol n text = (if sol then T.replicate n " " else mempty) <> T.replace "\n" ("\n" <> T.replicate n " ") text
parseTemplate :: Text -> Either String Template
parseTemplate text =
fmap (Template . V.fromList) $ do
flatchunks <-
fmap (elimStandalone . toList)
. runExcept
. execWriterT
. flip runStateT (PState (mkCursor text) ("{{", "}}"))
$ pTemplate
-- traceShowM flatchunks
parseSections flatchunks
-- | This also converts the column index in a 'FlatPartial' to the indent of
-- the partial, if any: if the partial is standalone, the column index is
-- preserved, otherwise it is set to 0.
elimStandalone :: [FlatChunk] -> [FlatChunk]
elimStandalone = \case
[] -> []
(withCol -> Just (0, ch)) : FlatText tafter : chs
| Just tafter' <- afterSpacesNewline tafter
-> ch : elimStandalone (tafter' ++ chs)
FlatText tbefore : (withCol -> Just (col, ch)) : FlatText tafter : chs
-- beforepre includes its final newline; tafter' also does
| (beforepre, beforepost) <- T.splitAt (T.length tbefore - col) tbefore
, T.length beforepost == col
, T.all isSpace beforepost
, Just tafter' <- afterSpacesNewline tafter
-> FlatText beforepre : ch : elimStandalone (tafter' ++ chs)
FlatPartial col Nothing name : chs -> FlatPartial col Nothing name : elimStandalone chs -- not standalone, so retain Nothing
FlatPartial _ Just{} _ : _ -> error "elimStandalone: FlatPartial with annotated indent"
ch : chs -> ch : elimStandalone chs
where
withCol FlatText{} = Nothing
withCol FlatVar{} = Nothing
withCol ch@(StartSection col _ _) = Just (col, ch)
withCol ch@(EndSection col _) = Just (col, ch)
withCol ch@(EmptyTag col) = Just (col, ch)
withCol (FlatPartial col _ name) =
-- withCol results are used only if the tag turns out to be actually standalone, so we can set the indent here
Just (col, FlatPartial col (Just col) name)
afterSpacesNewline tafter
| (pre, post) <- T.span (/= '\n') tafter
, T.all isSpace pre
= case post of
T.Empty -> Just []
'\n' T.:< post' -> Just [FlatText post']
_ T.:< _ -> error "impossible"
| otherwise
= Nothing
parseSections :: [FlatChunk] -> Either String [Chunk]
parseSections = \chs -> do
(chs', rest) <- go [] Nothing chs
case rest of
[] -> return chs'
_ -> error "impossible"
where
go :: [Chunk] -> Maybe (Vector Text) -> [FlatChunk] -> Either String ([Chunk], [FlatChunk])
go _ (Just ctx) [] =
Left $ "Unclosed section at end of template: " ++ showName ctx
go acc Nothing [] = return (reverse acc, [])
go acc mctx (FlatText text : chs) = go (Text text : acc) mctx chs
go acc mctx (FlatVar esc var : chs) = go (Var esc var : acc) mctx chs
go acc mctx (StartSection _ pos var : chs) = do
(body, after) <- go [] (Just var) chs
go (Section pos var (V.fromList body) : acc) mctx after
go acc (Just ctx) (EndSection _ var : chs)
| var == ctx = return (reverse acc, chs)
| otherwise = Left $ "Section " ++ showName ctx ++ " ended with {{/" ++ showName var ++ "}}"
go _ Nothing (EndSection _ var : _) =
Left $ "Found {{/" ++ showName var ++ "}} without opening tag"
go acc mctx (EmptyTag _ : chs) = go acc mctx chs
go acc mctx (FlatPartial _ indent path : chs) = go (Partial indent path : acc) mctx chs
showName :: Vector Text -> String
showName = intercalate "." . map T.unpack . V.toList
data PState = PState
{ psCursor :: Cursor
, psDelim :: (Text, Text) }
cursor_ :: Lens PState Cursor
cursor_ = Lens psCursor (\s x -> s { psCursor = x })
delim_ :: Lens PState (Text, Text)
delim_ = Lens psDelim (\s x -> s { psDelim = x })
type M = StateT PState (WriterT (Bag FlatChunk) (Except String))
raise :: String -> M a
raise err = do
Cursor _ pos <- gets psCursor
lift . lift . throwE $ showCursorPos pos ++ ": " ++ err
output :: FlatChunk -> M ()
output = lift . tell . BOne
pTemplate :: M ()
pTemplate = do
(delim1, delim2) <- gets psDelim
-- Consume until the next tag
ch1 <- FlatText <$> stateL cursor_ (takeUntil delim1)
output ch1
ateof <- gets (eof . psCursor)
(_, tagcol) <- gets (cursorPos . psCursor)
-- Consume delim1; if not at eof, takeUntil stopped at a delim1, so this succeeds
when (not ateof) $ modifyL cursor_ (consume delim1)
-- Ensure we'll be parsing the correct closing delimiter, even with {{{stuff}}}
(triple, delim2') <- do
cursor <- gets psCursor -- if ateof the takeN check will fail
if delim1 == "{{" && delim2 == "}}" && fst (takeN 1 cursor) == "{"
then do modifyL cursor_ (consume "{")
return (True, "}}}")
else return (False, delim2)
-- Consume the body
contents <- stateL cursor_ (takeUntil delim2') -- empty if ateof
-- Consume delim2'; again, if not at eof, we will have delim2' here
when (not ateof) $ modifyL cursor_ (consume delim2')
if | ateof ->
return ()
| '!' T.:< _ <- contents -> do -- comment
output (EmptyTag tagcol)
pTemplate
| '#' T.:< (matchVarRef -> Just var) <- contents -> do
output (StartSection tagcol True var)
pTemplate
| '^' T.:< (matchVarRef -> Just var) <- contents -> do
output (StartSection tagcol False var)
pTemplate
| '/' T.:< (matchVarRef -> Just var) <- contents -> do
output (EndSection tagcol var)
pTemplate
| '>' T.:< (T.strip -> path) <- contents -> do
output (FlatPartial tagcol Nothing path)
pTemplate
| '&' T.:< (matchVarRef -> Just var) <- contents -> do
when triple $
raise $ "Use of {{{&...}}} which is redundant"
output (FlatVar False var)
pTemplate
| Just newdelims <- matchSetDelims contents -> do
modifyL delim_ (const newdelims)
output (EmptyTag tagcol)
pTemplate
| Just var <- matchVarRef contents -> do
output (FlatVar (not triple) var)
pTemplate
| otherwise ->
raise $ "Could not parse tag: " ++ T.unpack (delim1 <> contents <> delim2)
matchSetDelims :: Text -> Maybe (Text, Text)
matchSetDelims ('=' T.:< (text T.:> '=')) =
case lexer [TokLex TLMany isSpace
,TokLex TLSome (\c -> not (isSpace c))
,TokLex TLSome isSpace
,TokLex TLSome (\c -> not (isSpace c))
,TokLex TLMany isSpace]
text of
Just [_, d1, _, d2, _] -> Just (d1, d2)
_ -> Nothing
matchSetDelims _ = Nothing
matchVarRef :: Text -> Maybe (Vector Text)
matchVarRef text = do
let comps = T.splitOn "." (T.strip text)
-- {{.}} results in ["", ""] upon splitting on "."
let implicit = comps == ["", ""]
guard (all isValidName comps || implicit)
return (if implicit then mempty else V.fromList comps)
where
isValidName T.Empty = False
isValidName name@(c1 T.:< _) = T.all isAlphaNum name && isAlpha c1
data Lens a t = Lens (a -> t) (a -> t -> a)
stateL :: Monad m => Lens s s' -> (s' -> (a, s')) -> StateT s m a
stateL (Lens get' set) f = state (\s -> let (x, !s') = f (get' s) in (x,) $! set s s')
modifyL :: Monad m => Lens s s' -> (s' -> s') -> StateT s m ()
modifyL lens f = stateL lens (((),) . f)
data TokLex = TokLex TLKind (Char -> Bool)
data TLKind = TLOne | TLSome | TLMany
-- | Returned list is the same length as the list of predicates.
lexer :: [TokLex] -> Text -> Maybe [Text]
lexer = go []
where
go :: [Text] -> [TokLex] -> Text -> Maybe [Text]
go acc [] T.Empty = Just (reverse acc)
go _ [] _ = Nothing
go acc (TokLex TLOne f : fs) t
| c T.:< t' <- t, f c = go (T.pack [c] : acc) fs t'
| otherwise = Nothing
go acc (TokLex TLSome f : fs) t =
let (tok, post) = T.span f t
in if T.null tok then Nothing else go (tok : acc) fs post
go acc (TokLex TLMany f : fs) t =
let (tok, post) = T.span f t
in go (tok : acc) fs post
-- | tail (line, col); all zero-based
data Cursor = Cursor { cursorText :: Text, cursorPos :: (Int, Int) }
deriving (Show)
mkCursor :: Text -> Cursor
mkCursor t = Cursor t (0, 0)
takeN :: Int -> Cursor -> (Text, Cursor)
takeN n (Cursor t pos) =
let (pre, post) = T.splitAt n t
in (pre, Cursor post (cursorSkipLC pre pos))
-- | Throws if the given text is not found at the cursor
consume :: Text -> Cursor -> Cursor
consume text (Cursor t pos) =
case T.stripPrefix text t of
Just rest -> Cursor rest (cursorSkipLC text pos)
Nothing -> error $ "consume: Text " ++ show text ++ " not found at " ++ showCursorPos pos
takeUntil :: Text -> Cursor -> (Text, Cursor)
takeUntil delim (Cursor t pos) =
let (pre, post) = T.breakOn delim t
in (pre, Cursor post (cursorSkipLC pre pos))
eof :: Cursor -> Bool
eof (Cursor t _) = T.null t
cursorSkipLC :: Text -> (Int, Int) -> (Int, Int)
cursorSkipLC prefix (l, c) =
T.foldl' (\(l', c') -> \case '\n' -> (l'+1, 0)
_ -> (l', c'+1))
(l, c) prefix
showCursorPos :: (Int, Int) -> String
showCursorPos (l0, c0) = show (l0 + 1) ++ ":" ++ show (c0 + 1)
data Bag a = BZero | BOne a | BTwo (Bag a) (Bag a) | BList [a]
deriving (Show, Functor, Foldable)
instance Semigroup (Bag a) where (<>) = BTwo
instance Monoid (Bag a) where mempty = BZero
|