summaryrefslogtreecommitdiff
path: root/src/Grasp/Parser.hs
blob: 72749198192b2085696916e58f70ad80cad88b11 (plain)
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
module Grasp.Parser (
    parseGrasp
    ) where




import Control.Applicative( some )
import Data.Char( toLower, toUpper )
import Text.ParserCombinators.Parsec

import Grasp.Types.GNode( GNode )
import qualified Grasp.Types.GNode as GN

import Grasp.Types.GEdge( GEdge )
import qualified Grasp.Types.GEdge as GE

import Grasp.Types.Instruction( Instruction )
import qualified Grasp.Types.Instruction as IN

import Grasp.Types.EdgeLabel( EdgeLabel )
import qualified Grasp.Types.EdgeLabel as EL




parseGrasp :: String -> Either ParseError ([GNode],[GEdge])
parseGrasp input =
    parse removeComments "error" input >>=
    parse graspDOT "error"




-- removes comments but otherwise leaves input unchanged

removeComments = gline `sepEndBy` eol >>= return . concat


eol  =  try (string "\r\n")
    <|> try (string "\n\r")
    <|> try (string "\n")
    <|> try (string "\r")
    <?> "end of line"


gline = many thing >>= return . concat


thing = try (some (noneOf "\r\n\"/#"))
    <|> try (quotedString >>= return . ("\"" ++) . (++ "\""))
    <|> try singleLineComment
    <|> try multiLineComment
    <|> (anyChar >>= return . (:[]) )


singleLineComment =
        (string "//" >> many (noneOf "\r\n") >> return "")
    <|> (string "#" >> many (noneOf "\r\n") >> return "")


multiLineComment = do
    string "/*"
    many (noneOf "*" <|> (char '*' >> notFollowedBy (char '/') >> return '*'))
    string "*/"
    return ""



-- parses a DOT graph language file into the data for a grasp program

graspDOT = do
    optional strict
    graphType
    optional ident
    openBrace
    (n,e) <- stmtList ([],[])
    closeBrace
    many blankLine
    eof
    return (n,e)


strict = caseInsensitiveString "strict" >>= (\x -> whiteSpace >> return x)


graphType = try (caseInsensitiveString "digraph" >>= (\x -> whiteSpace >> return x)) <?> "digraph"


ident = ((try alphaNumString)
    <|>  (try numeral)
    <|>  (try quotedString)) >>= (\x -> whiteSpace >> return x)
    <?> "ID"


stmtList (n,e) =
        try (whiteSpace >> node >>= (\x -> stmtList (x:n,e)) )
    <|> try (whiteSpace >> edge >>= (\x -> stmtList (n,x:e)) )
    <|> try (whiteSpace >> attr >> stmtList (n,e))
    <|> try (whiteSpace >> subgraph >>= (\(x,y) -> stmtList ((reverse x) ++ n, (reverse y) ++ e)) )
    <|> try (blankLine >> stmtList (n,e))
    <|> return (reverse n, reverse e)


blankLine = whiteSpace >> eol


alphaNumString = do
    a <- nonDigitChar
    b <- many alphaNumChar
    return (a:b)


nonDigitChar = letter <|> char '_'
alphaNumChar = alphaNum <|> char '_'


numeral = try negativeNum <|> positiveNum
negativeNum = char '-' >> positiveNum >>= return . ('-':)
positiveNum = try pointNum <|> try floatNum <|> wholeNum
pointNum = char '.' >> some digit >>= return . ('.':)
wholeNum = some digit
floatNum = do { a <- some digit; char '.'; b <- many digit; return (a ++ "." ++ b)}


quotedString = do
    char '\"'
    s <- some quotedChar
    char '\"'
    return s


quotedChar = noneOf "\"\r\n" <|> try (char '\\' >> char '\"')


node = do
    n <- ident
    a <- attrList
    optional (char ';')
    whiteSpace
    return (GN.mk (n, IN.mk a))


edge = do
    a <- ident
    edgeOp
    b <- ident
    c <- attrList
    optional (char ';')
    whiteSpace
    return (GE.mk (a,b, EL.mk c))


edgeOp = string "->" >> whiteSpace >> return "->"


attr = attrType >> attrList >> optional (char ';') >> whiteSpace
attrType = (caseInsensitiveString "graph"
        <|> caseInsensitiveString "node"
        <|> caseInsensitiveString "edge") >>= (\x -> whiteSpace >> return x)


attrList = do
    a <- many aList
    let r = filter (\x -> fst x == "label") (concat a)
    case (length r) of
        0 -> fail "expected node/edge label"
        1 -> return . snd . head $ r
        _ -> fail "unexpected multiple labels for single node/edge"


aList = do
    openBracket
    a <- many equAttr
    closeBracket
    whiteSpace
    return a


equAttr = do
    e <- equ
    optional (char ';' <|> char ',')
    whiteSpace
    return e


equ = do
    a <- ident
    equalsChar
    b <- ident
    return (a,b)


subgraph = do
    optional (caseInsensitiveString "subgraph" >> optional ident)
    openBrace
    (n,e) <- stmtList ([],[])
    closeBrace
    optional (char ';')
    whiteSpace
    return (n,e)


openBrace = char '{' >> whiteSpace >> return '{'
closeBrace = char '}' >> whiteSpace >> return '}'
openBracket = char '[' >> whiteSpace >> return '['
closeBracket = char ']' >> whiteSpace >> return ']'
equalsChar = char '='


caseInsensitiveChar c = char (toLower c) <|> char (toUpper c)
caseInsensitiveString s = mapM caseInsensitiveChar s


whiteSpace = many (oneOf " \t")