Skip to content

Commit

Permalink
Parser refactoring and hide internal functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsaunders committed Apr 28, 2019
1 parent 95548eb commit 3df29dc
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/Configuration/Dotenv/Parse.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- | This module encapsulates the parsing logic for a `.env` file.

module Configuration.Dotenv.Parse where
module Configuration.Dotenv.Parse (configParser) where

import Prelude
import Control.Alt ((<|>))
Expand Down Expand Up @@ -32,26 +32,33 @@ eof :: Parser String Unit
eof = notFollowedBy anyChar

-- | Parses the remainder of the line.
tillEnd :: Parser String String
tillEnd = unfoldToString <$> manyTill anyChar (lookAhead eol <|> eof)
tillEnd :: Parser String (List Char)
tillEnd = manyTill anyChar (lookAhead eol <|> eof)

-- | Creates a `String` from a character list.
unfoldToString :: forall f. Foldable f => f Char -> String
unfoldToString = String.fromCharArray <<< Array.fromFoldable

-- | Parses a comment in the form of `# Comment`.
comment :: Parser String String
comment = char '#' *> tillEnd
comment = unfoldToString <$> (char '#' *> tillEnd)

-- | Parses a variable name.
name :: Parser String String
name = unfoldToString <$> (manyTill anyChar $ char '=')

-- | Parses an unquoted variable value.
unquotedValue :: Parser String (List Char)
unquotedValue = manyTill anyChar (lookAhead eol <|> eof)

-- | Parses a quoted variable value enclosed within the specified type of quotation mark.
quotedValue :: Char -> Parser String (List Char)
quotedValue q = char q *> manyTill anyChar (char q)

-- | Parses a variable value.
value :: Parser String String
value = unfoldToString <$> (quotedValue '"' <|> quotedValue '\'') <|> trim <<< unfoldToString <$> unquotedValue

-- | Parses a variable in the form of `KEY=value`.
variable :: Parser String (Tuple String String)
variable = do
name <- unfoldToString <$> manyTill anyChar (char '=')
value <- unfoldToString <$> ((quotedValue '"' <|> quotedValue '\'')) <|> (trim <<< unfoldToString <$> unquotedValue)
pure $ Tuple name value

-- | Creates a `String` from a character list.
unfoldToString :: forall f. Foldable f => f Char -> String
unfoldToString = String.fromCharArray <<< Array.fromFoldable
variable = Tuple <$> name <*> value

0 comments on commit 3df29dc

Please sign in to comment.