Skip to content

Commit 9609644

Browse files
committed
WIP: Triple slash comments
1 parent 932feea commit 9609644

File tree

12 files changed

+207
-133
lines changed

12 files changed

+207
-133
lines changed

waspc/src/Wasp/Psl/Parser/Argument.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ where
99
import Text.Megaparsec (choice, optional, try)
1010
import qualified Text.Megaparsec.Char as C
1111
import qualified Wasp.Psl.Ast.Argument as Psl.Argument
12-
import Wasp.Psl.Parser.Common
13-
( Parser,
14-
brackets,
12+
import Wasp.Psl.Parser.Common (Parser)
13+
import Wasp.Psl.Parser.Tokens
14+
( brackets,
1515
colon,
1616
commaSep,
1717
float,
Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,14 @@
11
module Wasp.Psl.Parser.Common
2-
( whiteSpace,
3-
reserved,
4-
identifier,
5-
braces,
6-
symbol,
7-
parens,
8-
stringLiteral,
9-
brackets,
10-
commaSep1,
11-
commaSep,
12-
colon,
13-
float,
14-
integer,
15-
lexeme,
16-
SourceCode,
2+
( SourceCode,
173
Parser,
184
)
195
where
206

21-
import Control.Applicative (liftA2)
22-
import Data.Functor (void)
237
import Data.Void (Void)
248
import Text.Megaparsec
259
( Parsec,
26-
between,
27-
empty,
28-
many,
29-
manyTill,
30-
notFollowedBy,
31-
sepBy,
32-
sepBy1,
33-
takeWhileP,
34-
try,
35-
(<?>),
36-
(<|>),
3710
)
38-
import qualified Text.Megaparsec.Char as C
39-
import qualified Text.Megaparsec.Char.Lexer as L
4011

4112
type Parser = Parsec Void SourceCode
4213

4314
type SourceCode = String
44-
45-
reserved :: String -> Parser ()
46-
reserved name =
47-
lexeme $ try $ C.string name >> (notFollowedBy identLetter <?> ("end of " ++ show name))
48-
49-
identifier :: Parser String
50-
identifier =
51-
lexeme $ try (ident <?> "identifier")
52-
where
53-
ident = liftA2 (:) identHead identTail
54-
identHead = C.letterChar
55-
identTail = many identLetter
56-
57-
identLetter :: Parser Char
58-
identLetter = C.alphaNumChar <|> C.char '_'
59-
60-
braces :: Parser a -> Parser a
61-
braces = between (symbol "{") (symbol "}")
62-
63-
parens :: Parser a -> Parser a
64-
parens = between (symbol "(") (symbol ")")
65-
66-
stringLiteral :: Parser String
67-
stringLiteral = lexeme $ quote >> manyTill L.charLiteral quote
68-
where
69-
quote = C.char '"'
70-
71-
brackets :: Parser a -> Parser a
72-
brackets = between (symbol "[") (symbol "]")
73-
74-
commaSep1 :: Parser a -> Parser [a]
75-
commaSep1 = flip sepBy1 comma
76-
77-
commaSep :: Parser a -> Parser [a]
78-
commaSep = flip sepBy comma
79-
80-
comma :: Parser String
81-
comma = symbol ","
82-
83-
colon :: Parser String
84-
colon = symbol ":"
85-
86-
symbol :: String -> Parser String
87-
symbol = L.symbol whiteSpace
88-
89-
float :: Parser Double
90-
float = L.signed whiteSpace unsignedFloat
91-
where
92-
unsignedFloat = lexeme L.float
93-
94-
integer :: Parser Integer
95-
integer = L.signed whiteSpace unsignedInteger
96-
where
97-
unsignedInteger = lexeme L.decimal
98-
99-
lexeme :: Parser a -> Parser a
100-
lexeme = L.lexeme whiteSpace
101-
102-
whiteSpace :: Parser ()
103-
whiteSpace =
104-
L.space (void C.spaceChar) (void lineComment) empty
105-
106-
lineComment :: Parser String
107-
lineComment =
108-
try doubleSlashSymbol
109-
>> takeWhileP (Just "character") (/= '\n')
110-
where
111-
doubleSlashSymbol = C.string "//" >> notFollowedBy (C.char '/')

waspc/src/Wasp/Psl/Parser/ConfigBlock.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ module Wasp.Psl.Parser.ConfigBlock
66
)
77
where
88

9-
import Text.Megaparsec (choice, many, try)
9+
import Text.Megaparsec (choice, sepEndBy, try)
1010
import qualified Wasp.Psl.Ast.ConfigBlock as Psl.ConfigBlock
1111
import Wasp.Psl.Parser.Argument (expression)
12-
import Wasp.Psl.Parser.Common
13-
( Parser,
14-
braces,
12+
import Wasp.Psl.Parser.Common (Parser)
13+
import Wasp.Psl.Parser.Lexer (compulsoryNewline)
14+
import Wasp.Psl.Parser.Tokens
15+
( braces,
1516
identifier,
1617
reserved,
1718
symbol,
@@ -35,7 +36,7 @@ configBlock = do
3536
Psl.ConfigBlock.ConfigBlock configBlockType name <$> configBlockBody
3637

3738
configBlockBody :: Parser [Psl.ConfigBlock.KeyValuePair]
38-
configBlockBody = braces (many keyValuePair)
39+
configBlockBody = braces $ keyValuePair `sepEndBy` compulsoryNewline
3940

4041
-- | Parses a key-value pair.
4142
-- Example of PSL key-value pair:

waspc/src/Wasp/Psl/Parser/Enum.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Wasp.Psl.Parser.Enum
33
)
44
where
55

6-
import Text.Megaparsec (choice, many, some, try)
6+
import Text.Megaparsec (choice, many, sepEndBy1, try)
77
import qualified Wasp.Psl.Ast.Enum as Psl.Enum
88
import Wasp.Psl.Parser.Attribute (attribute, blockAttribute)
9-
import Wasp.Psl.Parser.Common
10-
( Parser,
11-
braces,
9+
import Wasp.Psl.Parser.Common (Parser)
10+
import Wasp.Psl.Parser.Lexer (compulsoryNewline)
11+
import Wasp.Psl.Parser.Tokens
12+
( braces,
1213
identifier,
1314
reserved,
1415
)
@@ -25,7 +26,7 @@ enum :: Parser Psl.Enum.Enum
2526
enum = do
2627
reserved "enum"
2728
enumName <- identifier
28-
Psl.Enum.Enum enumName <$> braces (some $ withCtx enumField)
29+
Psl.Enum.Enum enumName <$> braces (withCtx enumField `sepEndBy1` compulsoryNewline)
2930

3031
enumField :: Parser Psl.Enum.Element
3132
enumField =

waspc/src/Wasp/Psl/Parser/Lexer.hs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module Wasp.Psl.Parser.Lexer
2+
( compulsoryNewline,
3+
lexeme,
4+
spaceConsumer,
5+
spaceConsumerNL,
6+
)
7+
where
8+
9+
import qualified Data.Char as Char
10+
import Data.Functor (void)
11+
import Text.Megaparsec
12+
( MonadParsec (label),
13+
empty,
14+
notFollowedBy,
15+
satisfy,
16+
takeWhileP,
17+
try,
18+
(<?>),
19+
(<|>),
20+
)
21+
import qualified Text.Megaparsec.Char as C
22+
import qualified Text.Megaparsec.Char.Lexer as L
23+
import Wasp.Psl.Parser.Common (Parser)
24+
25+
lexeme :: Parser a -> Parser a
26+
lexeme = L.lexeme spaceConsumer
27+
28+
compulsoryNewline :: Parser ()
29+
compulsoryNewline = newline >> spaceConsumerNL
30+
31+
spaceConsumerNL :: Parser ()
32+
spaceConsumerNL =
33+
L.space
34+
whiteSpaceNL
35+
(void lineComment)
36+
empty
37+
38+
spaceConsumer :: Parser ()
39+
spaceConsumer =
40+
L.space
41+
whiteSpace
42+
lineComment
43+
empty
44+
45+
lineComment :: Parser ()
46+
lineComment =
47+
void $
48+
label "line comment" $
49+
try commentSymbol
50+
>> takeWhileP (Just "character") (/= '\n')
51+
where
52+
commentSymbol = C.string "//" >> notFollowedBy newline
53+
54+
whiteSpaceNL :: Parser ()
55+
whiteSpaceNL = whiteSpace <|> newline
56+
57+
newline :: Parser ()
58+
newline = void C.newline <|> void C.crlf
59+
60+
whiteSpace :: Parser ()
61+
whiteSpace = void (satisfy isNonNewlineSpace <?> "white space")
62+
where
63+
isNonNewlineSpace c = Char.Space == Char.generalCategory c

waspc/src/Wasp/Psl/Parser/Model.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ where
77

88
import Control.Arrow (left)
99
import Data.Maybe (maybeToList)
10-
import Text.Megaparsec (choice, errorBundlePretty, many, optional, some, try)
10+
import Text.Megaparsec (choice, errorBundlePretty, many, optional, sepEndBy1, try)
1111
import qualified Text.Megaparsec as Megaparsec
1212
import qualified Wasp.Psl.Ast.Model as Psl.Model
1313
import Wasp.Psl.Parser.Attribute (attribute, blockAttribute)
1414
import Wasp.Psl.Parser.Common
1515
( Parser,
1616
SourceCode,
17-
braces,
17+
)
18+
import Wasp.Psl.Parser.Lexer (compulsoryNewline, spaceConsumerNL)
19+
import Wasp.Psl.Parser.Tokens
20+
( braces,
1821
identifier,
1922
parens,
2023
reserved,
2124
stringLiteral,
2225
symbol,
23-
whiteSpace,
2426
)
2527
import Wasp.Psl.Parser.WithCtx (withCtx)
2628

@@ -29,7 +31,7 @@ import Wasp.Psl.Parser.WithCtx (withCtx)
2931
-- parser directly (meaning not as part of parsing the whole schema) which means that the
3032
-- leading whitespace is not consumed by the `schema` parser.
3133
parseBody :: SourceCode -> Either String Psl.Model.Body
32-
parseBody = left errorBundlePretty . Megaparsec.parse (whiteSpace >> body) ""
34+
parseBody = left errorBundlePretty . Megaparsec.parse (spaceConsumerNL >> body) ""
3335

3436
-- | Parses PSL (Prisma Schema Language model).
3537
-- Example of PSL model:
@@ -48,7 +50,7 @@ model = do
4850
-- which is everything besides model keyword, name and braces:
4951
-- `model User { <body> }`.
5052
body :: Parser Psl.Model.Body
51-
body = Psl.Model.Body <$> some (withCtx element)
53+
body = Psl.Model.Body <$> (withCtx element `sepEndBy1` compulsoryNewline)
5254

5355
element :: Parser Psl.Model.Element
5456
element =

waspc/src/Wasp/Psl/Parser/Schema.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ module Wasp.Psl.Parser.Schema
66
where
77

88
import Control.Arrow (left)
9-
import Text.Megaparsec (choice, eof, errorBundlePretty, many)
9+
import Text.Megaparsec (choice, eof, errorBundlePretty, sepEndBy)
1010
import qualified Text.Megaparsec as Megaparsec
1111
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
12-
import Wasp.Psl.Parser.Common (Parser, SourceCode, whiteSpace)
12+
import Wasp.Psl.Parser.Common (Parser, SourceCode)
1313
import Wasp.Psl.Parser.ConfigBlock (configBlock)
1414
import Wasp.Psl.Parser.Enum (enum)
15+
import Wasp.Psl.Parser.Lexer (compulsoryNewline, spaceConsumerNL)
1516
import Wasp.Psl.Parser.Model (model)
1617
import Wasp.Psl.Parser.Type (typeBlock)
1718
import Wasp.Psl.Parser.View (view)
@@ -26,8 +27,8 @@ schema = do
2627
-- Megaparsec's lexeme parsers in the sub-parsers (model, enum, configBlock)
2728
-- which consume the (trailing) whitespace themselves. It's a bit of an
2829
-- implict behaviour that we need to be aware of.
29-
whiteSpace
30-
elements <- many $ withCtx block
30+
spaceConsumerNL
31+
elements <- withCtx block `sepEndBy` compulsoryNewline
3132
-- We want to throw and if there is any source code left after parsing the schema.
3233
-- If we don't do this, the parser sometimes returns an empty schema when there
3334
-- are some syntax errors in the schema.

0 commit comments

Comments
 (0)