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
|
module Grasp.Parser (
parseGrasp,
dup
) where
import Control.Applicative( some )
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Error
import Text.Parsec.Pos
import Text.Read( readMaybe )
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
import Data.Char
import qualified Data.Map as Map
import Grasp.Types
type StrLNode a = (String,a)
type StrLEdge a = (String,String,a)
type GraspData = ([StrLNode String],[StrLEdge String])
parseGrasp :: String -> Either ParseError GraspProgram
parseGrasp input =
parse grasp "error" input >>= sanityCheck >>= nameCheck >>= return . constructGraph
sanityCheck :: GraspData -> Either ParseError GraspData
sanityCheck (nodeList, edgeList) =
let n = map fst nodeList
a = multiCheck nodeList
b = filter (\(x,y,_) -> x `notElem` n || y `notElem` n) edgeList
in case (a,b) of
(Just x,_) -> Left (newErrorMessage (Message ("multiple declaration of node " ++ (show x))) (newPos "" 0 0))
(_,(x:_)) -> Left (newErrorMessage (Message ("edge " ++ (show x) ++ " is unconnected")) (newPos "" 0 0))
_ -> Right (nodeList, edgeList)
nameCheck :: GraspData -> Either ParseError GraspData
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 = multiCheck namedNodes
b = nonStringNames namedNodes
c = graspMainPresent namedNodes
in case (a,b,c) of
(Just x,_,_) -> Left (newErrorMessage (Message ("node " ++ (show x) ++ " has multiple names")) (newPos "" 0 0))
(_,Just x,_) -> Left (newErrorMessage (Message ("node " ++ (show x) ++ " has a numeric name")) (newPos "" 0 0))
(_,_,False) -> Left (newErrorMessage (Message "could not find grasp:main") (newPos "" 0 0))
_ -> Right (nodeList, edgeList)
multiCheck :: (Eq a) => [(a, b)] -> Maybe a
multiCheck = dup . (map fst)
nonStringNames :: (Eq a) => [(a, String)] -> Maybe a
nonStringNames nodeList =
let f x = readMaybe x :: Maybe Float
nonStringNames = filter (isJust . f . snd) nodeList
in if (nonStringNames == []) then Nothing else Just (fst . head $ nonStringNames)
graspMainPresent :: [(a, String)] -> Bool
graspMainPresent = any (\x -> snd x == "grasp:main")
constructGraph :: GraspData -> GraspProgram
constructGraph (sn, se) =
let strNodeList = map fst sn
nmap = Map.fromList (zip strNodeList [1..])
change x = fromJust (Map.lookup x nmap)
n = map (\(x,y) -> (change x, y)) sn
e = map (\(x,y,z) -> (change x, change y, z)) se
in Graph.mkGraph n e
dup :: (Eq a) => [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 {"
whiteSpac'
(n,e) <- stmtLis' ([],[])
string "}"
eo'
eof
return (n,e)
stmtLis' (n,e) =
try (nod' >>= (\x -> stmtLis' (x:n,e)) )
<|> try (edg' >>= (\x -> stmtLis' (n,x:e)) )
<|> return (reverse n, reverse e)
nod' = do
i <- iden'
l <- labelAttri'
whiteSpac'
return (i,l)
edg' = do
a <- iden'
directedEdg'
b <- iden'
l <- labelAttri'
whiteSpac'
return (a,b,l)
iden' = do
d <- some (noneOf " \t\r\n")
inLineWhS'
return d
labelAttri' = do
char '['
inLineWhS'
string "label=\""
l <- labelI'
char '\"'
inLineWhS'
string "];"
return l
labelI' = some (noneOf "\"\r\n\\" <|> escapedCha')
escapedCha' = try (string "\\\"" >> return '\"')
<|> try (string "\\\\" >> return '\\')
directedEdg' = string "->" >> inLineWhS'
inLineWhS' = many (oneOf "\t ")
whiteSpac' = many (oneOf "\n\r\t ")
eo' = try (string "\r\n")
<|> try (string "\n\r")
<|> try (string "\n")
<|> try (string "\r")
<?> "end of line"
-- work in progress more complete DOT language parser below this point
graspDOT = do
optional strict
graphType
ident
openBrace
(n,e) <- stmtList ([],[])
closeBrace
eof
return (n,e)
strict = caseInsensitiveString "strict"
graphType = try (caseInsensitiveString "digraph") <?> "digraph"
ident = (try alphaNumString)
<|> (try numeral)
<|> (try quotedString)
<?> "ID"
stmtList (n,e) =
try (node >>= (\x -> stmtList (x:n,e)) )
<|> try (edge >>= (\x -> stmtList (n,x:e)) )
<|> try (attr >> stmtList (n,e))
<|> try (subgraph >>= (\(x,y) -> stmtList (x ++ n, y ++ e)) )
<|> return (reverse n, reverse e)
-- todo
alphaNumString = return "a"
-- todo
numeral = return "0"
-- todo
quotedString = return "\""
-- todo
node = return ("1","a")
-- todo
edge = return ("1","2","b")
-- todo
attr = return ""
-- todo
subgraph = return ([],[])
openBrace = char '{'
closeBrace = char '}'
caseInsensitiveChar c = char (toLower c) <|> char (toUpper c)
caseInsensitiveString s = mapM caseInsensitiveChar s
|