module Yarn.Lock.Parse
( PackageFields(..), Package
, Parser
, packageList
, packageEntry
, field, nestedField, simpleField
, packageKeys, packageKey
) where
import Protolude hiding (try)
import qualified Data.Char as Ch
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
import qualified Data.Map.Strict as M
import Control.Monad (fail)
import Text.Megaparsec as MP hiding (space)
import Text.Megaparsec.Text
import qualified Text.Megaparsec.Lexer as MPL
import qualified Yarn.Lock.Types as YLT
newtype PackageFields = PackageFields (Map Text (Either Text PackageFields))
deriving (Show, Eq, Monoid)
type Package = YLT.Keyed (SourcePos, PackageFields)
packageList :: Parser [Package]
packageList = many $ (skipMany (comment <|> eol)) *> packageEntry
where comment = char '#' *> manyTill anyChar eol
packageEntry :: Parser (YLT.Keyed (SourcePos, PackageFields))
packageEntry = label "package entry" $ do
pos <- getPosition
(keys, pkgs) <- nonIndented
$ indentedFieldsWithHeader packageKeys
pure $ YLT.Keyed keys (pos, pkgs)
packageKeys :: Parser (NE.NonEmpty YLT.PackageKey)
packageKeys = label "package keys" $ do
firstEls <- many (try $ lexeme $ packageKey ":," <* char ',')
lastEl <- packageKey ":" <* char ':'
pure $ NE.fromList $ firstEls <> [lastEl]
packageKey :: [Char] -> Parser YLT.PackageKey
packageKey separators = inString (pkgKey "\"")
<|> pkgKey separators
<?> "package key"
where
pkgKey :: [Char] -> Parser YLT.PackageKey
pkgKey valueChars = label "package key" $ do
key <- someTextOf (noneOf valueChars)
case (\(n, v) -> (T.dropEnd 1 n, v)) $ T.breakOnEnd "@" key of
("", _) -> fail "packageKey: package name can not be empty"
(n, "") -> pure $ YLT.PackageKey n ""
(n, v) -> pure $ YLT.PackageKey n v
field :: Parser (Text, Either Text PackageFields)
field = try nested <|> simple <?> "field"
where
simple = fmap Left <$> simpleField
nested = fmap Right <$> nestedField
simpleField :: Parser (Text, Text)
simpleField = (,) <$> lexeme (strSymbolChars <|> symbolChars)
<*> (strValueChars <|> valueChars)
<?> "simple field"
where
valueChars, strValueChars :: Parser Text
valueChars = someTextOf (noneOf ("\n\r\"" :: [Char]))
strSymbolChars = inString $ symbolChars
strValueChars = inString $ valueChars
<|> (pure T.empty <?> "an empty value field")
nestedField :: Parser (Text, PackageFields)
nestedField = label "nested field" $
indentedFieldsWithHeader (symbolChars <* char ':')
indentedFieldsWithHeader :: Parser a -> Parser (a, PackageFields)
indentedFieldsWithHeader header = indentBlock $ do
hdr <- header
pure $ MPL.IndentSome Nothing
(\fields -> pure (hdr, toPfs fields)) field
where
toPfs :: [(Text, Either Text PackageFields)] -> PackageFields
toPfs = PackageFields . M.fromList
symbolChars :: Parser Text
symbolChars = label "key symbol" $ someTextOf $ satisfy
(\c -> Ch.isAscii c &&
(Ch.isLower c || Ch.isUpper c || Ch.isNumber c || c `elem` special))
where special = "-_.@/" :: [Char]
someTextOf :: Parser Char -> Parser Text
someTextOf c = T.pack <$> some c
inString :: Parser a -> Parser a
inString = between (char '"') (char '"')
space :: Parser ()
space = MPL.space (void MP.spaceChar)
(MPL.skipLineComment "# ")
(void $ satisfy (const False))
lexeme :: Parser a -> Parser a
lexeme = MPL.lexeme space
nonIndented :: Parser a -> Parser a
nonIndented = MPL.nonIndented space
indentBlock :: ParsecT Dec Text Identity (MPL.IndentOpt (ParsecT Dec Text Identity) a b)
-> ParsecT Dec Text Identity a
indentBlock = MPL.indentBlock space