Skip to content
This repository was archived by the owner on May 22, 2019. It is now read-only.

Commit c0fffa9

Browse files
committed
Use PSString for literals and labels
1 parent 433c6e6 commit c0fffa9

File tree

7 files changed

+40
-26
lines changed

7 files changed

+40
-26
lines changed

src/Language/PureScript/CST/Convert.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Language.PureScript.CST.Convert
66
, convertBinder
77
, convertDeclaration
88
, convertModule
9+
, sourcePos
10+
, sourceSpan
11+
, comment
12+
, comments
913
) where
1014

1115
import Prelude
@@ -115,7 +119,7 @@ convertType fileName = go
115119
Nothing -> T.REmpty $ sourceAnnCommented fileName b b
116120
rowCons (Labeled a _ ty) c = do
117121
let ann = sourceAnnCommented fileName (lblTok a) (snd $ typeRange ty)
118-
T.RCons ann (L.Label . mkString $ lblName a) (go ty) c
122+
T.RCons ann (L.Label $ lblName a) (go ty) c
119123
case labels of
120124
Just (Separated h t) ->
121125
rowCons h $ foldr (rowCons . snd) rowTail t
@@ -132,7 +136,7 @@ convertType fileName = go
132136
TypeHole _ a ->
133137
T.TypeWildcard (sourceName fileName a) . Just . getIdent $ nameValue a
134138
TypeString _ a b ->
135-
T.TypeLevelString (sourceAnnCommented fileName a a) $ mkString b
139+
T.TypeLevelString (sourceAnnCommented fileName a a) $ b
136140
TypeRow _ (Wrapped _ row b) ->
137141
goRow row b
138142
TypeRecord _ (Wrapped a row b) -> do
@@ -272,7 +276,7 @@ convertExpr fileName = go
272276
positioned ann . AST.Literal (fst ann) $ AST.CharLiteral b
273277
ExprString _ a b -> do
274278
let ann = sourceAnnCommented fileName a a
275-
positioned ann . AST.Literal (fst ann) . AST.StringLiteral $ mkString b
279+
positioned ann . AST.Literal (fst ann) . AST.StringLiteral $ b
276280
ExprNumber _ a b -> do
277281
let ann = sourceAnnCommented fileName a a
278282
positioned ann . AST.Literal (fst ann) $ AST.NumericLiteral b
@@ -288,7 +292,7 @@ convertExpr fileName = go
288292
ann = sourceAnnCommented fileName a c
289293
lbl = \case
290294
RecordPun f -> (mkString . getIdent $ nameValue f, go . ExprIdent z $ QualifiedName (nameTok f) Nothing (nameValue f))
291-
RecordField f _ v -> (mkString $ lblName f, go v)
295+
RecordField f _ v -> (lblName f, go v)
292296
vals = case bs of
293297
Just (Separated x xs) -> lbl x : (lbl . snd <$> xs)
294298
Nothing -> []
@@ -325,13 +329,13 @@ convertExpr fileName = go
325329
expr@(ExprRecordAccessor _ (RecordAccessor a _ (Separated h t))) -> do
326330
let
327331
ann = uncurry (sourceAnnCommented fileName) $ exprRange expr
328-
field x f = AST.Accessor (mkString $ lblName f) x
332+
field x f = AST.Accessor (lblName f) x
329333
positioned ann $ foldl' (\x (_, f) -> field x f) (field (go a) h) t
330334
expr@(ExprRecordUpdate _ a b) -> do
331335
let
332336
ann = uncurry (sourceAnnCommented fileName) $ exprRange expr
333-
k (RecordUpdateLeaf f _ x) = (mkString $ lblName f, AST.Leaf $ go x)
334-
k (RecordUpdateBranch f xs) = (mkString $ lblName f, AST.Branch $ toTree xs)
337+
k (RecordUpdateLeaf f _ x) = (lblName f, AST.Leaf $ go x)
338+
k (RecordUpdateBranch f xs) = (lblName f, AST.Branch $ toTree xs)
335339
toTree (Wrapped _ xs _) = AST.PathTree . AST.AssocList . map k $ toList xs
336340
positioned ann . AST.ObjectUpdateNested (go a) $ toTree b
337341
expr@(ExprApp _ a b) -> do
@@ -391,7 +395,7 @@ convertBinder fileName = go
391395
positioned ann . AST.LiteralBinder (fst ann) $ AST.CharLiteral b
392396
BinderString _ a b -> do
393397
let ann = sourceAnnCommented fileName a a
394-
positioned ann . AST.LiteralBinder (fst ann) . AST.StringLiteral $ mkString b
398+
positioned ann . AST.LiteralBinder (fst ann) . AST.StringLiteral $ b
395399
BinderNumber _ n a b -> do
396400
let
397401
ann = sourceAnnCommented fileName a a
@@ -411,7 +415,7 @@ convertBinder fileName = go
411415
ann = sourceAnnCommented fileName a c
412416
lbl = \case
413417
RecordPun f -> (mkString . getIdent $ nameValue f, go $ BinderVar z f)
414-
RecordField f _ v -> (mkString $ lblName f, go v)
418+
RecordField f _ v -> (lblName f, go v)
415419
vals = case bs of
416420
Just (Separated x xs) -> lbl x : (lbl . snd <$> xs)
417421
Nothing -> []

src/Language/PureScript/CST/Errors.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
12
module Language.PureScript.CST.Errors
23
( ParserError(..)
34
, ParserErrorType(..)
45
, prettyPrintError
6+
, prettyPrintErrorMessage
57
) where
68

79
import Prelude

src/Language/PureScript/CST/Lexer.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
module Language.PureScript.CST.Lexer
23
( lex
34
, munch
@@ -7,9 +8,11 @@ import Prelude hiding (lex, exp, exponent, lines)
78

89
import Control.Monad (join)
910
import qualified Data.Char as Char
11+
import qualified Data.DList as DList
1012
import Data.Foldable (foldl')
1113
import Data.Functor (($>))
1214
import qualified Data.Scientific as Sci
15+
import Data.String (fromString)
1316
import Data.Text (Text)
1417
import qualified Data.Text as Text
1518
import Language.PureScript.CST.Errors
@@ -418,9 +421,9 @@ token = peek >>= maybe (pure TokEof) k0
418421
chs <- nextWhile isNormalStringChar
419422
let
420423
raw' = raw <> chs
421-
acc' = acc <> chs
424+
acc' = acc <> DList.fromList (Text.unpack chs)
422425
peek >>= \case
423-
Just '"' -> next $> TokString raw' acc'
426+
Just '"' -> next $> TokString raw' (fromString (DList.toList acc'))
424427
Just '\\' -> next *> goEscape (raw' <> "\\") acc'
425428
Just _ -> throw ErrLineFeedInString
426429
Nothing -> throw ErrEof
@@ -431,19 +434,19 @@ token = peek >>= maybe (pure TokEof) k0
431434
Just ch1 | isStringGapChar ch1 -> do
432435
gap <- nextWhile isStringGapChar
433436
peek >>= \case
434-
Just '"' -> next $> TokString (raw <> gap) acc
437+
Just '"' -> next $> TokString (raw <> gap) (fromString (DList.toList acc))
435438
Just '\\' -> next *> go (raw <> gap <> "\\") acc
436439
Just ch -> throw $ ErrCharInGap ch
437440
Nothing -> throw ErrEof
438441
_ -> do
439442
(raw', ch) <- escape
440-
go (raw <> raw') (acc <> Text.singleton ch)
441-
go "" ""
443+
go (raw <> raw') (acc <> DList.singleton ch)
444+
go "" mempty
442445
1 ->
443446
pure $ TokString "" ""
444447
n | n >= 5 -> do
445448
let str = Text.take 5 quotes1
446-
pure $ TokString str str
449+
pure $ TokString str (fromString (Text.unpack str))
447450
_ -> do
448451
let
449452
go acc = do

src/Language/PureScript/CST/Parser.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Language.PureScript.CST.Positions
2222
import Language.PureScript.CST.Types
2323
import Language.PureScript.CST.Utils
2424
import qualified Language.PureScript.Names as N
25+
import Language.PureScript.PSString (PSString)
2526
}
2627
2728
%expect 98
@@ -236,7 +237,7 @@ label :: { Label }
236237
hole :: { Name Ident }
237238
: LIT_HOLE {% toName Ident $1 }
238239
239-
string :: { (SourceToken, Text) }
240+
string :: { (SourceToken, PSString) }
240241
: LIT_STRING { toString $1 }
241242
| LIT_RAW_STRING { toString $1 }
242243

src/Language/PureScript/CST/Positions.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE BangPatterns #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
13
module Language.PureScript.CST.Positions where
24

35
import Prelude

src/Language/PureScript/CST/Types.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Data.Text (Text)
77
import Data.Void (Void)
88
import GHC.Generics (Generic)
99
import qualified Language.PureScript.Names as N
10+
import Language.PureScript.PSString (PSString)
1011

1112
data SourcePos = SourcePos
1213
{ srcLine :: {-# UNPACK #-} !Int
@@ -62,7 +63,7 @@ data Token
6263
| TokSymbolArr !SourceStyle
6364
| TokHole !Text
6465
| TokChar !Text !Char
65-
| TokString !Text !Text
66+
| TokString !Text !PSString
6667
| TokRawString !Text
6768
| TokInt !Text !Integer
6869
| TokNumber !Text !Double
@@ -94,7 +95,7 @@ data QualifiedName a = QualifiedName
9495

9596
data Label = Label
9697
{ lblTok :: SourceToken
97-
, lblName :: Text
98+
, lblName :: PSString
9899
} deriving (Show, Eq, Ord, Generic)
99100

100101
data Wrapped a = Wrapped
@@ -134,7 +135,7 @@ data Type a
134135
| TypeConstructor a (QualifiedName (N.ProperName 'N.TypeName))
135136
| TypeWildcard a SourceToken
136137
| TypeHole a (Name Ident)
137-
| TypeString a SourceToken Text
138+
| TypeString a SourceToken PSString
138139
| TypeRow a (Wrapped (Row a))
139140
| TypeRecord a (Wrapped (Row a))
140141
| TypeForall a SourceToken (NonEmpty (TypeVarBinding a)) SourceToken (Type a)
@@ -316,7 +317,7 @@ data Expr a
316317
| ExprConstructor a (QualifiedName (N.ProperName 'N.ConstructorName))
317318
| ExprBoolean a SourceToken Bool
318319
| ExprChar a SourceToken Char
319-
| ExprString a SourceToken Text
320+
| ExprString a SourceToken PSString
320321
| ExprNumber a SourceToken (Either Integer Double)
321322
| ExprArray a (Delimited (Expr a))
322323
| ExprRecord a (Delimited (RecordLabeled (Expr a)))
@@ -419,7 +420,7 @@ data Binder a
419420
| BinderConstructor a (QualifiedName (N.ProperName 'N.ConstructorName)) [Binder a]
420421
| BinderBoolean a SourceToken Bool
421422
| BinderChar a SourceToken Char
422-
| BinderString a SourceToken Text
423+
| BinderString a SourceToken PSString
423424
| BinderNumber a (Maybe SourceToken) SourceToken (Either Integer Double)
424425
| BinderArray a (Delimited (Binder a))
425426
| BinderRecord a (Delimited (RecordLabeled (Binder a)))

src/Language/PureScript/CST/Utils.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Language.PureScript.CST.Positions
1818
import Language.PureScript.CST.Traversals.Type
1919
import Language.PureScript.CST.Types
2020
import qualified Language.PureScript.Names as N
21+
import Language.PureScript.PSString (PSString, mkString)
2122

2223
placeholder :: SourceToken
2324
placeholder = SourceToken
@@ -114,19 +115,19 @@ toName k tok = case tokValue tok of
114115

115116
toLabel :: SourceToken -> Label
116117
toLabel tok = case tokValue tok of
117-
TokLowerName [] a -> Label tok a
118+
TokLowerName [] a -> Label tok $ mkString a
118119
TokString _ a -> Label tok a
119-
TokRawString a -> Label tok a
120-
TokForall ASCII -> Label tok "forall"
120+
TokRawString a -> Label tok $ mkString a
121+
TokForall ASCII -> Label tok $ mkString "forall"
121122
_ -> internalError $ "Invalid label: " <> show tok
122123

123124
labelToIdent :: Label -> Parser (Name Ident)
124125
labelToIdent (Label tok _) = toName Ident tok
125126

126-
toString :: SourceToken -> (SourceToken, Text)
127+
toString :: SourceToken -> (SourceToken, PSString)
127128
toString tok = case tokValue tok of
128129
TokString _ a -> (tok, a)
129-
TokRawString a -> (tok, a)
130+
TokRawString a -> (tok, mkString a)
130131
_ -> internalError $ "Invalid string literal: " <> show tok
131132

132133
toChar :: SourceToken -> (SourceToken, Char)

0 commit comments

Comments
 (0)