Skip to content

Commit

Permalink
Use show to render string and character literals (fixes #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrosso committed Oct 31, 2022
1 parent 8da784a commit 921f00e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 20 deletions.
11 changes: 3 additions & 8 deletions src/Axel/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import Axel.Haskell.Language (isOperator)
import Axel.Haskell.Macros (hygenisizeMacroName)
import qualified Axel.Parse.AST as Parse
import Axel.Sourcemap
( Bracket(CurlyBraces, DoubleQuotes, Parentheses, SingleQuotes,
SquareBrackets)
( Bracket(CurlyBraces, Parentheses, SquareBrackets)
, Delimiter(Commas, Newlines, Pipes, Spaces)
)
import qualified Axel.Sourcemap as SM
Expand All @@ -23,7 +22,6 @@ import qualified Axel.Sourcemap as SM
, surround
)
import qualified Axel.Utils.Display as Display (delimit, renderPragma, surround)
import Axel.Utils.Text (handleCharEscapes)
import Axel.Utils.Tuple (annotate, unannotated)

import Control.Lens.Combinators (_head, _last)
Expand Down Expand Up @@ -452,13 +450,10 @@ instance ToHaskell (FunctionApplication (Maybe SM.Expression)) where

instance ToHaskell (Literal (Maybe SM.Expression)) where
toHaskell :: Literal (Maybe SM.Expression) -> SM.Output
toHaskell literal@(LChar _ x) =
mkHaskell literal $
Display.surround SingleQuotes (handleCharEscapes (T.singleton x))
toHaskell literal@(LChar _ x) = mkHaskell literal $ showText x
toHaskell literal@(LFloat _ x) = mkHaskell literal $ showText x
toHaskell literal@(LInt _ x) = mkHaskell literal $ showText x
toHaskell literal@(LString _ x) =
mkHaskell literal $ Display.surround DoubleQuotes (handleCharEscapes x)
toHaskell literal@(LString _ x) = mkHaskell literal $ showText x

instance ToHaskell (TypeSignature (Maybe SM.Expression)) where
toHaskell :: TypeSignature (Maybe SM.Expression) -> SM.Output
Expand Down
3 changes: 1 addition & 2 deletions src/Axel/Parse/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Axel.Utils.Recursion
, ZipperRecursive(zipperBottomUpTraverse, zipperTopDownTraverse)
, bottomUpFmap
)
import Axel.Utils.Text (handleCharEscapes)
import Axel.Utils.Zipper (unsafeDown, unsafeUp)

import Control.Lens ((<|))
Expand Down Expand Up @@ -125,7 +124,7 @@ toAxel :: Expression ann -> Text
toAxel (LiteralChar _ x) = "#\\" <> T.singleton x
toAxel (LiteralFloat _ x) = showText x
toAxel (LiteralInt _ x) = showText x
toAxel (LiteralString _ xs) = "\"" <> handleCharEscapes (T.pack xs) <> "\""
toAxel (LiteralString _ xs) = showText xs
toAxel (SExpression _ (Symbol _ "applyInfix":xs)) =
"{" <> T.unwords (map toAxel xs) <> "}"
toAxel (SExpression _ (Symbol _ "list":xs)) =
Expand Down
3 changes: 1 addition & 2 deletions src/Axel/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Axel.Parse.AST
import qualified Axel.Sourcemap as SM
import Axel.Utils.Foldable (mapWithPrev)
import Axel.Utils.Recursion (bottomUpFmap)
import Axel.Utils.Text (handleCharEscapes)

import Control.Lens (ala, under)

Expand Down Expand Up @@ -79,7 +78,7 @@ toAxelPretty (LiteralChar _ x) = "#\\" <> P.pretty x
toAxelPretty (LiteralFloat _ x) = P.pretty x
toAxelPretty (LiteralInt _ x) = P.pretty x
toAxelPretty (LiteralString _ x) =
P.dquotes $ P.pretty (under unpacked handleCharEscapes x)
P.dquotes $ P.pretty (under unpacked showText x)
toAxelPretty (SExpression _ (Symbol _ "applyInfix":xs)) =
P.braces $ sexp True (map toAxelPretty xs)
toAxelPretty (SExpression _ (Symbol _ "list":xs)) =
Expand Down
7 changes: 0 additions & 7 deletions src/Axel/Utils/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ s =
(error "Cannot use s as a type")
(error "Cannot use s as a dec")

handleCharEscapes :: Text -> Text
handleCharEscapes =
T.concatMap $ \case
'\\' -> "\\\\"
'\n' -> "\\n"
c -> T.singleton c

-- TODO This renders very poorly in e.g. Fira Code Mono.
bold :: Text -> Text
bold = T.map boldCharacter
Expand Down
18 changes: 18 additions & 0 deletions test/Axel/Test/ParseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ spec_Parse = do
it "can parse a string literal" $ do
let result = LiteralString () "a \x1000 \"b"
parseSingle "\"a \x1000 \\\"b\"" `shouldBe` result
it
"can parse string literals with escaped double quotes at the boundaries (regression: #79)" $ do
let result = LiteralString () "a \x1000 \""
parseSingle "\"a \x1000 \\\"\"" `shouldBe` result
let result = LiteralString () "a \x1000 \"\""
parseSingle "\"a \x1000 \\\"\\\"\"" `shouldBe` result
let result = LiteralString () "\""
parseSingle "\"\\\"\"" `shouldBe` result
let result = LiteralString () "\"\""
parseSingle "\"\\\"\\\"\"" `shouldBe` result
let result = LiteralString () "\"\" foo"
parseSingle "\"\\\"\\\" foo\"" `shouldBe` result
let result = LiteralString () "\"\"\""
parseSingle "\"\\\"\\\"\\\"\"" `shouldBe` result
it
"can parse a string literal with a double quote at the end (regression: #79)" $ do
let result = LiteralString () "a \x1000 \""
parseSingle "\"a \x1000 \\\"\"" `shouldBe` result
it "can parse a quasiquoted expression" $ do
let result =
SExpression
Expand Down
5 changes: 5 additions & 0 deletions test/Axel/Test/Transpilation/regression/Issue79.axel_golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module Main)

(= example "This should be a quotation mark: \"")

(= main (print example))
6 changes: 6 additions & 0 deletions test/Axel/Test/Transpilation/regression/Issue79.hs_golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where
import Axel
import qualified Prelude as GHCPrelude
import qualified Axel.Parse.AST as AST
example = "This should be a quotation mark: \""
main = (print example)
2 changes: 1 addition & 1 deletion test/Axel/Test/Transpilation/syntax/StringSyntax.hs_golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module StringSyntax where
import Axel
import qualified Prelude as GHCPrelude
import qualified Axel.Parse.AST as AST
foo = "some\nstring\n multiline {- this should be in the string -} -- and this"
foo = "some\nstring\n multiline\t{- this should be in the string -} -- and this"

0 comments on commit 921f00e

Please sign in to comment.