-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.idr
57 lines (47 loc) · 1.73 KB
/
Parser.idr
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
module Parser
import Data.Graph
import Lightyear
import Lightyear.Char
import Lightyear.Strings
%default covering
%access export
listToVect : (xs : List a) -> (p : Nat ** Vect p a)
listToVect [] = (_ ** [])
listToVect (x :: xs) = let (_ ** xs') = listToVect xs in (_ ** x :: xs')
vertex : Parser String
vertex = pack <$> some alphaNum
vertices : (g : ExGraph String String) -> Parser (ExGraph String String)
vertices (_ ** _ ** _ ** _ ** g) = do
(_ ** vs) <- listToVect <$> sepBy vertex (char ' ')
pure (_ ** _ ** _ ** _ ** addVertices vs g)
edge : (n : Nat) -> Parser (String, Fin n, Fin n)
edge n = do
e <- pack <$> some alphaNum
char ':'
Just s <- (\i => integerToFin i n) <$> integer | fail "source must be a valid index"
char ','
Just t <- (\i => integerToFin i n) <$> integer | fail "target must be a valid index"
pure (e, s, t)
edges : ExGraph String String -> Parser (ExGraph String String)
edges (n ** _ ** _ ** _ ** g) = do
(_ ** es') <- listToVect <$> sepBy (edge n) (char ' ')
let es = map fst es'
let ss = map (Basics.fst . snd) es'
let ts = map (Basics.snd . snd) es'
pure (_ ** _ ** _ ** _ ** addEdges es ss ts g)
graph : Parser (ExGraph String String)
graph = do
g <- vertices sigmaEmpty
char ' '
token "-"
edges g
-- a b c d e f b - e1:0,1 e2:0,2 e3:1,2
private
encodeHelper : Graph n m {vertex = String} {edge = String} vs es -> String
encodeHelper {es = []} Empty = ""
encodeHelper {es = (e :: es)} (Edge s t g)
= e ++ ":" ++ show (finToNat s) ++ "," ++ show (finToNat t) ++ " " ++ encodeHelper g
encode : Graph n m {vertex = String} {edge = String} vs es -> String
encode {vs} g = let vs' = unwords . toList $ vs
es' = encodeHelper g in
vs' ++ " - " ++ es'