-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLGrammar.hs
385 lines (332 loc) · 8.97 KB
/
PLGrammar.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
{-# LANGUAGE
GADTs
, FlexibleContexts
, KindSignatures
, OverloadedStrings
, RankNTypes
, RankNTypes
, LambdaCase
, ScopedTypeVariables
, TypeOperators
#-}
{-|
Module : PLGrammar
Copyright : (c) Samuel A. Yallop, 2017
Maintainer : syallop@gmail.com
Stability : experimental
A description of a grammar. The intent is that it can be used as either a
parser or a printer depending on context. This could prevent round-trip
properties from being accidentally violated for example, by adjusting a parser
but not the printer.
Note: WIP. Some constructors may be missing/ some may be unneccessary and in
general the entire type is likely to change.
Correspondance to invertable-syntax:
- <$>: rmap: \$/
- <*>: rap: \*/
- <|>: ralt: \|/
- empty: rempty
- pure: rpure
- token: anyChar
-}
module PLGrammar
( Grammar ()
, GrammarInstr (..)
, charIs
, textIs
, anyChar
, charWhen
, try
, label
, upper
, lower
, digit
, arrow
, bar
, star
, plus
, comma
, upArrow
, lambda
, langle
, rangle
, lparen
, rparen
, underscore
, union
, question
, at
, bigLambda
, bigAt
, between
, betweenParens
, longestMatching
, longestMatching1
, natural
, alternatives
, rap
, (\$/)
, (\*/)
, (\|/)
, (\*)
, (*/)
, rmany
, rmany1
, spaceAllowed
, spaceRequired
, spacePreferred
, sepBy
, sepBy1
, permissive
, token
, tokenThenMany1ThenSomething
)
where
import Data.Text (Text)
import Data.Char
import Data.Kind
import qualified Data.Text as T
{-import PLGrammar.Iso-}
import PLLabel
import Reversible
import Reversible.Iso
import DSL.Instruction
import Prelude hiding ((.),id)
import Control.Category
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NE
-- | Instructions which specify core aspects of a Grammar, minus higher level
-- features such as alternatives, mapping, sequencing, etc.
--
-- - 'a' is the result type the Grammar should parse/ print/ act upon.
-- - 'p' is the type of program the Grammar is part of.
data GrammarInstr (p :: Type -> Type) (a :: Type) where
-- Any character
GAnyChar
:: GrammarInstr p Char
GLabel
:: Label
-> p a
-> GrammarInstr p a
GTry
:: p a
-> GrammarInstr p a
-- | A Grammar is a Reversible description of some grammar.
-- - 'a' is the result type the Grammar shold parse/ print/ act upon.
--
-- 'Reversible' means a Grammar has access to reversible functions
-- 'rpure', 'rempty', 'ralt', 'rmap', 'rap' as well as the core Grammar defined
-- in 'GrammarInstr'.
type Grammar a = Reversible GrammarInstr a
-- | A 'Reversible' thing can be 'Label'led to describe itself, often for the
-- purpose of debugging.
label
:: GrammarInstr :<- i
=> Label
-> Reversible i a
-> Reversible i a
label l gr = reversible $ GLabel l gr
-- | A single character.
charIs :: Char -> Grammar ()
charIs c =
let txt = T.singleton c
in label (descriptiveLabel txt) . textIs $ txt
-- | Any single character.
-- ANY including spaces, newlines, etc.
anyChar
:: GrammarInstr :<- i
=> Reversible i Char
anyChar = reversible GAnyChar
-- | A character that matches a predicate
charWhen :: (Char -> Bool) -> Grammar Char
charWhen p = try $ predI \$/ anyChar
where
predI = Iso
(\c -> if p c then Just c else Nothing)
(\c -> if p c then Just c else Nothing)
-- | A single upper case character.
upper :: Grammar Char
upper = label (descriptiveLabel "upper") $ charWhen isUpper
-- | A single lower case character.
lower :: Grammar Char
lower = label (descriptiveLabel "lower") $ charWhen isLower
-- | A single digit character.
digit :: Grammar Char
digit = label (descriptiveLabel "digit") $ charWhen isDigit
arrow, bar, star, plus, comma, upArrow, lambda, langle, rangle, lparen, rparen, underscore, union, question, at, bigLambda, bigAt, spaceLike :: Grammar ()
arrow = charIs '→' \|/ textIs "->"
bar = charIs '|'
star = charIs '*'
plus = charIs '+'
comma = charIs ','
upArrow = charIs '^'
lambda = charIs 'λ' \|/ charIs '\\'
langle = charIs '<'
rangle = charIs '>'
lparen = charIs '('
rparen = charIs ')'
underscore = charIs '_'
union = charIs '∪' \|/ charIs 'U'
question = charIs '?'
at = charIs '@'
bigLambda = charIs 'Λ' \|/ textIs "/\\"
bigAt = charIs '#'
spaceLike = label (descriptiveLabel "spaceLike") $ alternatives . map textIs $ [" ","\t","\n","\r"]
-- | A string of Text with try semantics.
--
-- E.G.
-- Given:
-- input = "abz"
--
-- And gramamrs:
-- charSequenceGrammar = charIs 'a' */ charIs 'b' */ charIs 'c'
-- textSequenceGrammar = textIs "abc"
--
-- Then interpreting with a backtracking parser would result in:
-- - leftovers of 'z' for charSequenceGrammar
-- - leftovers of 'abz' for textSequenceGrammar
textIs :: Text -> Grammar ()
textIs txt = label (descriptiveLabel txt) . try . textIs' $ txt
textIs' :: Text -> Grammar ()
textIs' txt = case T.uncons txt of
Nothing
-> rpure ()
Just (c,cs)
-> inverse (elementIso ((), ()))
\$/ (inverse (elementIso c) \$/ anyChar)
\*/ textIs' cs
-- | Try should backtrack any notion of consumed input upon failure.
try
:: GrammarInstr :<- i
=> Reversible i a
-> Reversible i a
try = reversible . GTry
-- | A Grammar between parentheses.
betweenParens :: Show a => Grammar a -> Grammar a
betweenParens a = between (token lparen) a (token rparen)
-- | Longest matching text.
longestMatching :: (Char -> Bool) -> Grammar Text
longestMatching p = label (enhancingLabel "longestMatching") $ concatI \$/ rmany (charWhen p)
where
concatI :: Iso String Text
concatI = Iso
(Just . T.pack)
(Just . T.unpack)
-- | Longest matching text. At least one character.
longestMatching1 :: (Char -> Bool) -> Grammar Text
longestMatching1 p = label (enhancingLabel "longestMatching1") $ concatI \$/ rmany1 (charWhen p)
where
concatI :: Iso String Text
concatI = Iso
(Just . T.pack)
(Just . T.unpack)
-- | A natural number: zero and positive integers
natural :: Grammar Int
natural = label (descriptiveLabel "natural") $ naturalI \$/ longestMatching1 isDigit
where
naturalI :: Iso Text Int
naturalI = Iso
(Just . read . T.unpack) -- TODO: partial
(Just . T.pack . show)
-- | Space is permitted to parse but none printed.
-- 0-n parsed, 0 printed.
spaceAllowed :: Grammar ()
spaceAllowed = label (descriptiveLabel "spaceAllowed") $ allowed spaceLike
-- | Space is required to parse, one is printed.
-- 1-n parsed, 1 printed.
spaceRequired :: Grammar ()
spaceRequired = label (descriptiveLabel "spaceRequired") $ required spaceLike
-- | Space preferred to parse, one is printed.
-- 0-n parsed, 1 printed.
spacePreferred :: Grammar ()
spacePreferred = label (descriptiveLabel "spacePreferred") $ ignoreIso [()] \$/ rmany spaceLike
-- | Separate at least one Grammar by a separator.
-- E.G.
-- sepBy1 (textIs ',') digit
-- would parse:
-- "1"
-- "1,2"
-- "1,2,3"
sepBy1
:: (Show a, Eq a)
=> Grammar ()
-> Grammar a
-> Grammar (NonEmpty a)
sepBy1 sepG g = iso \$/ sepBy1' sepG g
where
iso :: Iso [a] (NonEmpty a)
iso = Iso
{_forwards = \case
[]
-> Nothing
(a:as)
-> Just $ a NE.:| as
,_backwards = \ne -> Just $ NE.toList ne
}
-- Implementation of sepBy1 that uses a weaker list type rather than a NonEmpty
sepBy1'
:: (Show a, Eq a)
=> Grammar ()
-> Grammar a
-> Grammar [a]
sepBy1' sepG g = iso \$/ g \*/ alternatives [ try $ sepG */ sepBy1' sepG g
, rpure []
]
where
iso :: Iso (a,[a]) [a]
iso = Iso
{ _forwards = \(a,as) -> Just $ a:as
, _backwards = \case
a:as
-> Just (a,as)
_ -> Nothing
}
-- | Separate 0 or many Grammars by a separator.
-- E.G.
-- sepBy (textIs ',') digig
-- would parse nothing or:
-- ""
-- "1"
-- "1,2"
-- "1,2,3"
sepBy
:: (Show a, Eq a)
=> Grammar ()
-> Grammar a
-> Grammar [a]
sepBy sepG g = alternatives [ try $ sepBy1' sepG g
, rpure []
]
-- | A permissive grammar is itself and also may have:
-- - Any number of spaces preceeding
-- - Any number of enclosing (matched) parentheses
-- - Which themselves may have internal spacing.
permissive
:: Show a
=> Grammar a
-> Grammar a
permissive a =
token a \|/ betweenMany1 (token lparen) (token a) (token rparen)
-- | A token allows 0-n preceeding spaces
token
:: Show a
=> Grammar a
-> Grammar a
token gr = spaceAllowed */ gr
{- TODO: These functions should be replaced with a chain or removed -}
tokenThenMany1ThenSomething
:: ( Eq xs
, Show xs
, Show r
, Show a
)
=> Grammar ()
-> Grammar xs
-> Grammar a
-> Iso ([xs],a) r
-> Grammar r
tokenThenMany1ThenSomething t many something iso
= t
*/ (iso \$/ rmany1 ((betweenParens many \|/ many) \* spaceRequired)
\*/ (betweenParens something \|/ something)
)