Skip to content

Commit 8bb95c4

Browse files
committed
Parser: add symbols & use
1 parent e13e623 commit 8bb95c4

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/Nix/Parser.hs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Nix.Parser
3636
, nixBool
3737
, nixNull
3838
, symbol
39+
, symbols
3940
, whiteSpace
4041
)
4142
where
@@ -165,8 +166,11 @@ whiteSpace =
165166
lexeme :: Parser a -> Parser a
166167
lexeme p = p <* whiteSpace
167168

168-
symbol :: Text -> Parser Text
169-
symbol = lexeme . chunk
169+
symbol :: Char -> Parser Char
170+
symbol = lexeme . char
171+
172+
symbols :: Text -> Parser Text
173+
symbols = lexeme . chunk
170174

171175
-- We restrict the type of 'parens' and 'brackets' here because if they were to
172176
-- take a @Parser NExprLoc@ argument they would parse additional text which
@@ -175,14 +179,14 @@ symbol = lexeme . chunk
175179
-- Braces and angles in hnix don't enclose a single expression so this type
176180
-- restriction would not be useful.
177181
parens :: Parser (NExprF f) -> Parser (NExprF f)
178-
parens = on between symbol "(" ")"
182+
parens = on between symbol '(' ')'
179183

180184
braces :: Parser a -> Parser a
181-
braces = on between symbol "{" "}"
185+
braces = on between symbol '{' '}'
182186

183187
-- angles = between (symbol "<") (symbol ">")
184188
brackets :: Parser (NExprF f) -> Parser (NExprF f)
185-
brackets = on between symbol "[" "]"
189+
brackets = on between symbol '[' ']'
186190

187191
-- colon = symbol ":"
188192
-- dot = symbol "."
@@ -191,7 +195,7 @@ antiquoteWithEnd :: Parser b -> Parser a -> Parser (Antiquoted v a)
191195
antiquoteWithEnd t expr = Antiquoted <$> (antiStart *> expr <* t)
192196
where
193197
antiStart :: Parser Text
194-
antiStart = label "${" $ symbol "${"
198+
antiStart = label "${" $ symbols "${"
195199

196200

197201
---------------------------------------------------------------------------------
@@ -252,7 +256,7 @@ nixUri = lexeme $ annotateLocation $ try $ do
252256
nixAntiquoted :: Parser a -> Parser (Antiquoted a NExprLoc)
253257
nixAntiquoted p =
254258
label "anti-quotation" $
255-
antiquoteWithEnd (symbol "}") nixToplevelForm
259+
antiquoteWithEnd (symbol '}') nixToplevelForm
256260
<|> Plain <$> p
257261

258262
nixString' :: Parser (NString NExprLoc)
@@ -358,7 +362,7 @@ nixList =
358362
-- ** { } set
359363

360364
nixBinders :: Parser [Binding NExprLoc]
361-
nixBinders = (inherit <|> namedVar) `endBy` symbol ";" where
365+
nixBinders = (inherit <|> namedVar) `endBy` symbol ';' where
362366
inherit =
363367
do
364368
-- We can't use 'reserved' here because it would consume the whitespace
@@ -376,7 +380,7 @@ nixBinders = (inherit <|> namedVar) `endBy` symbol ";" where
376380
label "variable binding" $
377381
liftA3 NamedVar
378382
(annotated <$> nixSelector)
379-
(symbol "=" *> nixToplevelForm)
383+
(symbol '=' *> nixToplevelForm)
380384
(pure p)
381385
scope = label "inherit scope" nixParens
382386

@@ -427,7 +431,7 @@ nixPath =
427431
nixSearchPath :: Parser NExprLoc
428432
nixSearchPath =
429433
annotateNamedLocation "spath" $
430-
mkPathF True <$> try (char '<' *> many (satisfy pathChar <|> slash) <* symbol ">")
434+
mkPathF True <$> try (char '<' *> many (satisfy pathChar <|> slash) <* symbol '>')
431435

432436

433437
-- ** Operators
@@ -454,7 +458,7 @@ operator op =
454458
c@"/" -> c `without` '/'
455459
c@"<" -> c `without` '='
456460
c@">" -> c `without` '='
457-
n -> symbol n
461+
n -> symbols n
458462
where
459463
without :: Text -> Char -> Parser Text
460464
without opChar noNextChar =
@@ -519,14 +523,14 @@ nixOperators selector =
519523
[ ( NBinaryDef NAssocLeft NApp " "
520524
,
521525
-- Thanks to Brent Yorgey for showing me this trick!
522-
InfixL $ annNApp <$ symbol ""
526+
InfixL $ annNApp <$ symbols ""
523527
)
524528
]
525529
, {- 3 -}
526530
[ prefix NNeg "-" ]
527531
, {- 4 -}
528532
[ ( NSpecialDef NAssocLeft NHasAttrOp "?"
529-
, Postfix $ symbol "?" *> (flip annNHasAttr <$> selector)
533+
, Postfix $ symbol '?' *> (flip annNHasAttr <$> selector)
530534
)
531535
]
532536
, {- 5 -}
@@ -634,7 +638,7 @@ argExpr =
634638
, onlyname
635639
, atRight
636640
]
637-
<* symbol ":"
641+
<* symbol ':'
638642
where
639643
-- An argument not in curly braces. There's some potential ambiguity
640644
-- in the case of, for example `x:y`. Is it a lambda function `x: y`, or
@@ -650,15 +654,15 @@ argExpr =
650654
atLeft =
651655
try $
652656
do
653-
name <- identifier <* symbol "@"
657+
name <- identifier <* symbol '@'
654658
(pset, variadic) <- params
655659
pure $ ParamSet (pure name) variadic pset
656660

657661
-- Parameters named by an identifier on the right, or none (`{x, y} @ args`)
658662
atRight =
659663
do
660664
(pset, variadic) <- params
661-
name <- optional $ symbol "@" *> identifier
665+
name <- optional $ symbol '@' *> identifier
662666
pure $ ParamSet name variadic pset
663667

664668
-- Return the parameters set.
@@ -672,7 +676,7 @@ argExpr =
672676
-- Otherwise, attempt to parse an argument, optionally with a
673677
-- default. If this fails, then return what has been accumulated
674678
-- so far.
675-
go acc = ((acc, Variadic) <$ symbol "...") <|> getMore
679+
go acc = ((acc, Variadic) <$ symbols "...") <|> getMore
676680
where
677681
getMore :: Parser ([(VarName, Maybe NExprLoc)], Variadic)
678682
getMore =
@@ -683,12 +687,12 @@ argExpr =
683687
pair <-
684688
liftA2 (,)
685689
identifier
686-
(optional $ symbol "?" *> nixToplevelForm)
690+
(optional $ symbol '?' *> nixToplevelForm)
687691

688692
let args = acc <> [pair]
689693

690694
-- Either return this, or attempt to get a comma and restart.
691-
option (args, mempty) $ symbol "," *> go args
695+
option (args, mempty) $ symbol ',' *> go args
692696

693697
nixLambda :: Parser NExprLoc
694698
nixLambda =
@@ -719,7 +723,7 @@ nixIf :: Parser NExprLoc
719723
nixIf =
720724
annotateNamedLocation "if" $
721725
liftA3 NIf
722-
(reserved "if" *> nixExpr )
726+
(reserved "if" *> nixExpr )
723727
(getExprAfterReservedWord "then")
724728
(getExprAfterReservedWord "else")
725729

@@ -730,7 +734,7 @@ nixWith =
730734
annotateNamedLocation "with" $
731735
liftA2 NWith
732736
(getExprAfterReservedWord "with")
733-
(symbol ";" *> nixToplevelForm)
737+
(symbol ';' *> nixToplevelForm)
734738

735739

736740
-- ** assert
@@ -740,12 +744,12 @@ nixAssert =
740744
annotateNamedLocation "assert" $
741745
liftA2 NAssert
742746
(getExprAfterReservedWord "assert")
743-
(symbol ";" *> nixToplevelForm)
747+
(symbol ';' *> nixToplevelForm)
744748

745749
-- ** . - reference (selector) into attr
746750

747751
selDot :: Parser ()
748-
selDot = label "." $ try (symbol "." *> notFollowedBy nixPath)
752+
selDot = label "." $ try (symbol '.' *> notFollowedBy nixPath)
749753

750754
keyName :: Parser (NKeyName NExprLoc)
751755
keyName = dynamicKey <|> staticKey

0 commit comments

Comments
 (0)