Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unix shebangs #2175

Merged
merged 1 commit into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions dhall/src/Dhall/Parser/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Dhall.Parser.Expression where
import Control.Applicative (Alternative (..), liftA2, optional)
import Data.ByteArray.Encoding (Base (..))
import Data.Foldable (foldl')
import Data.Functor (void)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import Dhall.Src (Src (..))
Expand Down Expand Up @@ -113,7 +112,17 @@ data Parsers a = Parsers
parsers :: forall a. Parser a -> Parsers a
parsers embedded = Parsers {..}
where
completeExpression_ = whitespace *> expression <* whitespace
completeExpression_ =
optional shebang *> whitespace *> expression <* whitespace

shebang = do
_ <- text "#!"

let predicate c = ('\x20' <= c && c <= '\x10FFFF') || c == '\t'

_ <- Dhall.Parser.Combinators.takeWhile predicate

endOfLine

expression =
noted
Expand Down Expand Up @@ -718,7 +727,7 @@ parsers embedded = Parsers {..}
, unescapedCharacterFast
, unescapedCharacterSlow
, tab
, endOfLine
, endOfLine_
]
where
escapeSingleQuotes = do
Expand Down Expand Up @@ -757,7 +766,7 @@ parsers embedded = Parsers {..}
where
predicate c = c == '$' || c == '\''

endOfLine = do
endOfLine_ = do
a <- "\n" <|> "\r\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could this be replaced by a <- endOfLine ?

b <- singleQuoteContinue
return (Chunks [] a <> b)
Expand All @@ -769,12 +778,12 @@ parsers embedded = Parsers {..}

singleQuoteLiteral = do
_ <- text "''"

_ <- endOfLine

a <- singleQuoteContinue

return (Dhall.Syntax.toDoubleQuoted a)
where
endOfLine = (void (char '\n') <|> void (text "\r\n")) <?> "newline"

textLiteral = (do
literal <- doubleQuotedLiteral <|> singleQuoteLiteral
Expand Down
16 changes: 8 additions & 8 deletions dhall/src/Dhall/Parser/Token.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-- | Parse Dhall tokens. Even though we don't have a tokenizer per-se this
--- module is useful for keeping some small parsing utilities.
module Dhall.Parser.Token (
endOfLine,
validCodepoint,
whitespace,
lineComment,
Expand Down Expand Up @@ -135,6 +136,12 @@ import qualified Text.Parser.Token

import Numeric.Natural (Natural)

-- | Match an end-of-line character sequence
endOfLine :: Parser Text
endOfLine =
( Text.Parser.Char.text "\n"
<|> Text.Parser.Char.text "\r\n"
) <?> "newline"

-- | Returns `True` if the given `Int` is a valid Unicode codepoint
validCodepoint :: Int -> Bool
Expand Down Expand Up @@ -360,14 +367,9 @@ lineComment = do

commentText <- Dhall.Parser.Combinators.takeWhile predicate

endOfLine
_ <- endOfLine

return ("--" <> commentText)
where
endOfLine =
( void (Text.Parser.Char.char '\n' )
<|> void (Text.Parser.Char.text "\r\n")
) <?> "newline"

-- | Parsed text doesn't include opening braces
blockComment :: Parser Text
Expand Down Expand Up @@ -396,8 +398,6 @@ blockCommentChunk =
where
predicate c = '\x20' <= c && c <= '\x10FFFF' || c == '\n' || c == '\t'

endOfLine = (Text.Parser.Char.text "\r\n" <?> "newline")

blockCommentContinue :: Parser Text
blockCommentContinue = endOfComment <|> continue
where
Expand Down