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