|
1 | 1 | 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, |
17 | 3 | Parser,
|
18 | 4 | )
|
19 | 5 | where
|
20 | 6 |
|
21 |
| -import Data.Functor (void) |
22 | 7 | import Data.Void (Void)
|
23 | 8 | import Text.Megaparsec
|
24 | 9 | ( Parsec,
|
25 |
| - between, |
26 |
| - empty, |
27 |
| - many, |
28 |
| - manyTill, |
29 |
| - notFollowedBy, |
30 |
| - sepBy, |
31 |
| - sepBy1, |
32 |
| - takeWhileP, |
33 |
| - try, |
34 |
| - (<?>), |
35 |
| - (<|>), |
36 | 10 | )
|
37 |
| -import qualified Text.Megaparsec.Char as C |
38 |
| -import qualified Text.Megaparsec.Char.Lexer as L |
39 | 11 |
|
40 | 12 | type Parser = Parsec Void SourceCode
|
41 | 13 |
|
42 | 14 | 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 '/') |
0 commit comments