@@ -271,73 +271,78 @@ nixAntiquoted p =
271271 antiquotedLexeme
272272 <|> Plain <$> p
273273
274- nixString' :: Parser (NString NExprLoc )
275- nixString' = label " string" $ lexeme $ doubleQuoted <|> indented
274+ escapeCode :: Parser Char
275+ escapeCode =
276+ msum
277+ [ c <$ char e | (c, e) <- escapeCodes ]
278+ <|> anySingle
279+
280+ stringChar
281+ :: Parser ()
282+ -> Parser ()
283+ -> Parser (Antiquoted Text NExprLoc )
284+ -> Parser (Antiquoted Text NExprLoc )
285+ stringChar end escStart esc =
286+ antiquoted
287+ <|> Plain . one <$> char ' $'
288+ <|> esc
289+ <|> Plain . fromString <$> some plainChar
290+ where
291+ plainChar :: Parser Char
292+ plainChar =
293+ notFollowedBy (end <|> void (char ' $' ) <|> escStart) *> anySingle
294+
295+ doubleQuoted :: Parser (NString NExprLoc )
296+ doubleQuoted =
297+ label " double quoted string" $
298+ DoubleQuoted . removeEmptyPlains . mergePlain <$>
299+ inQuotationMarks (many $ stringChar quotationMark (void $ char ' \\ ' ) doubleEscape)
300+ where
301+ inQuotationMarks :: Parser a -> Parser a
302+ inQuotationMarks expr = quotationMark *> expr <* quotationMark
303+
304+ quotationMark :: Parser ()
305+ quotationMark = void $ char ' "'
306+
307+ doubleEscape :: Parser (Antiquoted Text r )
308+ doubleEscape = Plain . one <$> (char ' \\ ' *> escapeCode)
309+
310+
311+ indented :: Parser (NString NExprLoc )
312+ indented =
313+ label " indented string" $
314+ stripIndent <$>
315+ inIndentedQuotation (many $ join stringChar indentedQuotationMark indentedEscape)
276316 where
277- doubleQuoted :: Parser (NString NExprLoc )
278- doubleQuoted =
279- label " double quoted string" $
280- DoubleQuoted . removeEmptyPlains . mergePlain <$>
281- inQuotationMarks (many $ stringChar quotationMark (void $ char ' \\ ' ) doubleEscape)
282- where
283- inQuotationMarks :: Parser a -> Parser a
284- inQuotationMarks expr = quotationMark *> expr <* quotationMark
317+ -- | Read escaping inside of the "'' <expr> ''"
318+ indentedEscape :: Parser (Antiquoted Text r )
319+ indentedEscape =
320+ try $
321+ do
322+ indentedQuotationMark
323+ (Plain <$> (" ''" <$ char ' \' ' <|> " $" <$ char ' $' ))
324+ <|>
325+ do
326+ _ <- char ' \\ '
327+ c <- escapeCode
285328
286- quotationMark :: Parser ()
287- quotationMark = void $ char ' "'
329+ pure $
330+ bool
331+ EscapedNewline
332+ (Plain $ one c)
333+ (c /= ' \n ' )
288334
289- doubleEscape :: Parser (Antiquoted Text r )
290- doubleEscape = Plain . one <$> (char ' \\ ' *> escapeCode)
335+ -- | Enclosed into indented quatation "'' <expr> ''"
336+ inIndentedQuotation :: Parser a -> Parser a
337+ inIndentedQuotation expr = indentedQuotationMark *> expr <* indentedQuotationMark
291338
292- indented :: Parser (NString NExprLoc )
293- indented =
294- label " indented string" $
295- stripIndent <$>
296- inIndentedQuotation (many $ join stringChar indentedQuotationMark indentedEscape)
297- where
298- indentedEscape :: Parser (Antiquoted Text r )
299- indentedEscape =
300- try $
301- do
302- indentedQuotationMark
303- (Plain <$> (" ''" <$ char ' \' ' <|> " $" <$ char ' $' ))
304- <|>
305- do
306- _ <- char ' \\ '
307- c <- escapeCode
308-
309- pure $
310- bool
311- EscapedNewline
312- (Plain $ one c)
313- (c /= ' \n ' )
314-
315- inIndentedQuotation :: Parser a -> Parser a
316- inIndentedQuotation expr = indentedQuotationMark *> expr <* indentedQuotationMark
317-
318- indentedQuotationMark :: Parser ()
319- indentedQuotationMark = label " \" ''\" " . void $ chunk " ''"
320-
321- stringChar
322- :: Parser ()
323- -> Parser ()
324- -> Parser (Antiquoted Text NExprLoc )
325- -> Parser (Antiquoted Text NExprLoc )
326- stringChar end escStart esc =
327- antiquoted
328- <|> Plain . one <$> char ' $'
329- <|> esc
330- <|> Plain . fromString <$> some plainChar
331- where
332- plainChar :: Parser Char
333- plainChar =
334- notFollowedBy (end <|> void (char ' $' ) <|> escStart) *> anySingle
339+ -- | Symbol "''"
340+ indentedQuotationMark :: Parser ()
341+ indentedQuotationMark = label " \" ''\" " . void $ chunk " ''"
335342
336- escapeCode :: Parser Char
337- escapeCode =
338- msum
339- [ c <$ char e | (c, e) <- escapeCodes ]
340- <|> anySingle
343+
344+ nixString' :: Parser (NString NExprLoc )
345+ nixString' = label " string" $ lexeme $ doubleQuoted <|> indented
341346
342347nixString :: Parser NExprLoc
343348nixString = annNStr <$> annotateLocation1 nixString'
@@ -587,6 +592,7 @@ nixOperators selector =
587592 one $ binaryR NImpl " ->"
588593 ]
589594
595+ -- 2021-11-09: NOTE: rename OperatorInfo accessors to `get*`
590596-- 2021-08-10: NOTE:
591597-- All this is a sidecar:
592598-- * This type
@@ -596,11 +602,13 @@ nixOperators selector =
596602-- * getSpecialOperation
597603-- can reduced in favour of adding precedence field into @NOperatorDef@.
598604-- details: https://github.com/haskell-nix/hnix/issues/982
599- data OperatorInfo = OperatorInfo
600- { precedence :: Int
601- , associativity :: NAssoc
602- , operatorName :: Text
603- } deriving (Eq , Ord , Generic , Typeable , Data , Show )
605+ data OperatorInfo =
606+ OperatorInfo
607+ { precedence :: Int
608+ , associativity :: NAssoc
609+ , operatorName :: Text
610+ }
611+ deriving (Eq , Ord , Generic , Typeable , Data , Show )
604612
605613detectPrecedence
606614 :: Ord a
0 commit comments