diff options
author | Jed Barber <jjbarber@y7mail.com> | 2014-04-26 21:58:17 +1000 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2014-04-26 21:58:17 +1000 |
commit | 5faa918bcbb4a2a7c255b789ab76e68748e57f1a (patch) | |
tree | daacf0ee154af638fe170f95ffcefe597e01f476 | |
parent | 4a631e9f9d4394ab9edfe2f4976e949470667b79 (diff) |
Parser for Grasp begun, using a subset of the graphviz DOT language
-rw-r--r-- | src/Grasp/Parser.hs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/Grasp/Parser.hs b/src/Grasp/Parser.hs new file mode 100644 index 0000000..3a71fdf --- /dev/null +++ b/src/Grasp/Parser.hs @@ -0,0 +1,99 @@ +module Grasp.Parser ( + GraspProgram(..), + parseGrasp + ) where + + +import Control.Applicative( some ) +import Text.ParserCombinators.Parsec +import Data.Graph.Inductive.Graph( Node, LNode, LEdge, (&) ) +import Data.Graph.Inductive.Graph as Graph +import Data.Graph.Inductive.Tree + + + + +data GraspProgram = Gr String String + + + + +parseGrasp :: String -> Either ParseError ([LNode String],[LEdge String]) +parseGrasp = parse grasp "error" +--parseGrasp input = +-- let firstPass = parse grasp "error" input +-- in case firstPass of +-- Left e -> Left e +-- Right (n,e) -> validate n e + + + +--validate :: [LNode String] -> [LEdge String] -> Either ParseError GraspProgram + + + + +grasp = do + string "digraph {" + whiteSpace + n <- many node + e <- many edge + string "}" + eol + eof + return (n,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" + |