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
|
module Grasp.Parser (
GraspProgram(..),
parseGrasp,
) where
import Control.Applicative( some )
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Error
import Text.Parsec.Pos
import Data.Graph.Inductive.Graph( Node, LNode, LEdge, (&) )
import Data.Graph.Inductive.Graph as Graph
import Data.Graph.Inductive.Tree
import Data.List
import Data.Maybe
type GraspProgram = Gr String String
type StrLNode a = (String,a)
type StrLEdge a = (String,String,a)
parseGrasp :: String -> Either ParseError GraspProgram
parseGrasp input =
parse grasp "error" input >>= connected >>= nameCheck
connected :: ([LNode String],[LEdge String]) -> Either ParseError ([LNode String],[LEdge String])
connected (nodeList, edgeList) = Right (nodeList, edgeList)
nameCheck :: ([LNode String],[LEdge String]) -> Either ParseError GraspProgram
nameCheck (nodeList, edgeList) =
let nameEdges = filter (\(_,_,z) -> z == "name") edgeList
-- designed to convert the edges into (lnode,name) pairs
findNode n l = find (\(x,_) -> x == n) l
mapFunc (x,y,_) = (fromJust (findNode x nodeList), snd . fromJust $ (findNode y nodeList))
namedNodes = map mapFunc nameEdges
a = multiNames namedNodes
b = nonStringNames namedNodes
c = graspMainPresent namedNodes
in case (a,b,c) of
(Nothing,Nothing,True) -> Right (constructGraph nodeList edgeList)
(Nothing,Nothing,False) -> Left (newErrorMessage (Message "could not find grasp:main") (newPos "" 0 0))
(Nothing,Just x,_) -> Left (newErrorMessage (Message ("node " ++ (show x) ++ " has a numeric name")) (newPos "" 0 0))
(Just x,_,_) -> Left (newErrorMessage (Message ("node " ++ (show x) ++ " has multiple names")) (newPos "" 0 0))
multiNames :: [(LNode String, String)] -> Maybe (LNode String)
multiNames = dup . (map fst)
nonStringNames :: [(LNode String, String)] -> Maybe (LNode String)
nonStringNames x = Nothing
graspMainPresent :: [(LNode String, String)] -> Bool
graspMainPresent = any (\x -> snd x == "grasp:main")
constructGraph :: [LNode String] -> [LEdge String] -> GraspProgram
constructGraph = Graph.mkGraph
dup :: [a] -> Maybe a
dup x =
let dup' [] _ = Nothing
dup' (x:xs) s = if (x `elem` s) then Just x else dup' xs (x:s)
in dup' x []
grasp = do
string "digraph {"
whiteSpace
(n,e) <- stmtList ([],[])
string "}"
eol
eof
return (n,e)
stmtList (n,e) =
try (node >>= (\x -> stmtList (x:n,e)) )
<|> try (edge >>= (\x -> stmtList (n,x:e)) )
<|> return (reverse n, reverse e)
node = do
i <- ident
l <- labelAttrib
whiteSpace
return (i,l)
edge = do
a <- ident
directedEdge
b <- ident
l <- labelAttrib
whiteSpace
return (a,b,l)
ident = do
d <- some digit
inLineWhSp
return (read d)
labelAttrib = do
char '['
inLineWhSp
string "label=\""
l <- labelID
char '\"'
inLineWhSp
string "];"
return l
labelID = some (noneOf "\"\r\n\\" <|> escapedChar)
escapedChar = try (string "\\\"" >> return '\"')
<|> try (string "\\\\" >> return '\\')
directedEdge = string "->" >> inLineWhSp
inLineWhSp = many (oneOf "\t ")
whiteSpace = many (oneOf "\n\r\t ")
eol = try (string "\r\n")
<|> try (string "\n\r")
<|> try (string "\n")
<|> try (string "\r")
<?> "end of line"
|