Skip to content

Commit b35a730

Browse files
committed
Parser: refactor antiquotation
1 parent 208bf64 commit b35a730

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/Nix/Parser.hs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,25 @@ brackets = on between symbol '[' ']'
194194
-- colon = symbol ":"
195195
-- dot = symbol "."
196196

197-
antiquoteWithEnd :: Parser b -> Parser a -> Parser (Antiquoted v a)
198-
antiquoteWithEnd t expr = Antiquoted <$> (antiStart *> expr <* t)
197+
antiquotedIsHungryForTrailingSpaces :: Bool -> Parser (Antiquoted v NExprLoc)
198+
antiquotedIsHungryForTrailingSpaces hungry = Antiquoted <$> (antiStart *> nixToplevelForm <* antiEnd)
199199
where
200200
antiStart :: Parser Text
201201
antiStart = label "${" $ symbols "${"
202202

203+
antiEnd :: Parser Char
204+
antiEnd = label "}" $
205+
bool
206+
id
207+
lexeme
208+
hungry
209+
(char '}')
210+
211+
antiquotedLexeme :: Parser (Antiquoted v NExprLoc)
212+
antiquotedLexeme = antiquotedIsHungryForTrailingSpaces True
213+
214+
antiquoted :: Parser (Antiquoted v NExprLoc)
215+
antiquoted = antiquotedIsHungryForTrailingSpaces False
203216

204217
---------------------------------------------------------------------------------
205218

@@ -259,7 +272,7 @@ nixUri = lexeme $ annotateLocation $ try $ do
259272
nixAntiquoted :: Parser a -> Parser (Antiquoted a NExprLoc)
260273
nixAntiquoted p =
261274
label "anti-quotation" $
262-
antiquoteWithEnd (symbol '}') nixToplevelForm
275+
antiquotedLexeme
263276
<|> Plain <$> p
264277

265278
nixString' :: Parser (NString NExprLoc)
@@ -306,7 +319,7 @@ nixString' = lexeme $ label "string" $ doubleQuoted <|> indented
306319
(c /= '\n')
307320

308321
stringChar end escStart esc =
309-
antiquoteWithEnd (char '}') nixToplevelForm
322+
antiquoted
310323
<|> Plain . one <$> char '$'
311324
<|> esc
312325
<|> Plain . toText <$> some plainChar

0 commit comments

Comments
 (0)