Skip to content

Commit 6fb5ef4

Browse files
committed
WIP: Trailing comment support
1 parent 1a1f5a3 commit 6fb5ef4

File tree

13 files changed

+266
-141
lines changed

13 files changed

+266
-141
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 & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +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 Data.Functor (void)
227
import Data.Void (Void)
238
import Text.Megaparsec
249
( Parsec,
25-
between,
26-
empty,
27-
many,
28-
manyTill,
29-
notFollowedBy,
30-
sepBy,
31-
sepBy1,
32-
takeWhileP,
33-
try,
34-
(<?>),
35-
(<|>),
3610
)
37-
import qualified Text.Megaparsec.Char as C
38-
import qualified Text.Megaparsec.Char.Lexer as L
3911

4012
type Parser = Parsec Void SourceCode
4113

4214
type SourceCode = String
43-
44-
reserved :: String -> Parser ()
45-
reserved name =
46-
lexeme $
47-
try $
48-
do
49-
_ <- C.string name
50-
notFollowedBy identLetter <?> ("end of " ++ show name)
51-
52-
identifier :: Parser String
53-
identifier = lexeme $ try ident
54-
where
55-
ident =
56-
do
57-
c <- identStart
58-
cs <- many identLetter
59-
return (c : cs)
60-
<?> "identifier"
61-
62-
identStart :: Parser Char
63-
identStart = C.letterChar
64-
65-
identLetter :: Parser Char
66-
identLetter = C.alphaNumChar <|> C.char '_'
67-
68-
braces :: Parser a -> Parser a
69-
braces = between (symbol "{") (symbol "}")
70-
71-
parens :: Parser a -> Parser a
72-
parens = between (symbol "(") (symbol ")")
73-
74-
stringLiteral :: Parser String
75-
stringLiteral = lexeme $ quote >> manyTill L.charLiteral quote
76-
where
77-
quote = C.char '"'
78-
79-
brackets :: Parser a -> Parser a
80-
brackets = between (symbol "[") (symbol "]")
81-
82-
commaSep1 :: Parser a -> Parser [a]
83-
commaSep1 = flip sepBy1 comma
84-
85-
commaSep :: Parser a -> Parser [a]
86-
commaSep = flip sepBy comma
87-
88-
comma :: Parser String
89-
comma = symbol ","
90-
91-
colon :: Parser String
92-
colon = symbol ":"
93-
94-
symbol :: String -> Parser String
95-
symbol = L.symbol whiteSpace
96-
97-
float :: Parser Double
98-
float = L.signed whiteSpace unsignedFloat
99-
where
100-
unsignedFloat = lexeme L.float
101-
102-
integer :: Parser Integer
103-
integer = L.signed whiteSpace unsignedInteger
104-
where
105-
unsignedInteger = lexeme L.decimal
106-
107-
lexeme :: Parser a -> Parser a
108-
lexeme = L.lexeme whiteSpace
109-
110-
whiteSpace :: Parser ()
111-
whiteSpace =
112-
L.space (void C.spaceChar) (void lineComment) empty
113-
114-
lineComment :: Parser String
115-
lineComment =
116-
try doubleSlashSymbolParser
117-
>> takeWhileP (Just "character") (/= '\n')
118-
where
119-
doubleSlashSymbolParser = 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,9 +27,9 @@ 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+
spaceConsumerNL
3031
elements <-
31-
many $
32+
(`sepEndBy` compulsoryNewline) $
3233
choice
3334
[ Psl.Schema.ModelBlock <$> withCtx model,
3435
Psl.Schema.ViewBlock <$> withCtx view,

0 commit comments

Comments
 (0)