ivan-m / graphviz

Haskell bindings to the Graphviz toolkit
Other
64 stars 24 forks source link

Edges before nodes #14

Closed joncol closed 7 years ago

joncol commented 8 years ago

I ran into trouble when trying to parse graphs that has edges referring to nodes that has not yet been encountered in the dot file.

The dot command (when run from a shell) has no problems with this, so I was wondering if it was something that I was missing, or if it's an issue with the library?

I constructed the following minimal example:

{-# LANGUAGE OverloadedStrings #-}

module Main where

import           Data.Monoid ((<>))
import qualified Data.GraphViz as G
import qualified Data.GraphViz.Types.Generalised as GG
import qualified Data.Text.Lazy as T

type DG = GG.DotGraph String

c1 :: T.Text
c1 = "digraph \"sm0\" {\n" <>
     "A\n" <>
     "B\n" <>
     "A -> B\n" <>
     "}\n"

c2 :: T.Text
c2 = "digraph \"sm0\" {\n" <>
     "A\n" <>
     "A -> B\n" <>
     "B\n" <>
     "}\n"

main :: IO ()
main = do
  putStrLn $ show (G.parseDotGraph c1 :: DG) -- works
  putStrLn $ show (G.parseDotGraph c2 :: DG) -- doesn't work

-- Parsing c2 leads to:
--
-- *** Exception: Error when parsing Dot code:
-- Unable to parse the Dot graph; usually this is because of either:
--   * Wrong choice of representation: try the Generalised one
--   * Wrong choice of node type; try with `DotGraph String`.

-- The actual parsing error was:
--         Not a valid generalised DotGraph
--         Not a valid value wrapped in braces.
--         Missing closing bracket:
--         Not the expected character: }
ivan-m commented 7 years ago

OK, so the actual issue is how I try and handle the special case of A -> B -> C. It seems to be because it expects there to be attributes afterwards and then failing.