ivan-m / graphviz

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

Parsing issue #58

Open CGenie opened 1 year ago

CGenie commented 1 year ago

Hello,

I have this simple code:

runParser' Data.GraphViz.Parsing.parseUnqt $ printDotGraph $ digraph (Str "x") $ do "a0" --> "a1"

however it results in this error:

*** Exception: Error when parsing Dot code:
Expected one or more digits
    Parse.many1Satisfy: failed

Remaining input:
    "digraph x {\n    a0 -> a1;\n}"
CGenie commented 1 year ago

Same if one changes digraph to graph.

ShrykeWindgrace commented 1 year ago

@CGenie I'd guess that you doing this in ghci; there runParser' :: Parse a -> Text -> a has no idea which a to pick, defaults to a ~ Int and then bails out on invalid input. If that's indeed the case, try specifying the expected type explicitly. Something like DotGraph String should do the trick.

ghci> (runParser' Data.GraphViz.Parsing.parseUnqt $ printDotGraph $ digraph (Str "x") $ do "a0" --> "a1") :: DotGraph String
fromCanonical DotGraph {strictGraph = False, directedGraph = True, graphID = Just (Str "x"), graphStatements = DotStmts {attrStmts = [], subGraphs = [], nodeStmts = [DotNode {nodeID = "a0", nodeAttributes = []},DotNode {nodeID = "a1", nodeAttributes = []}], edgeStmts = [DotEdge {fromNode = "a0", toNode = "a1", edgeAttributes = []}]}}
CGenie commented 1 year ago

Indeed!

runParser' (Data.GraphViz.Parsing.parse :: Parse (Data.GraphViz.DotGraph Text)) $ printDotGraph $ graph (Str "x") $ do "a0" --> "a1"

returns

DotGraph {strictGraph = False, directedGraph = False, graphID = Just (Str "x"), graphStatements = DotStmts {attrStmts = [], subGraphs = [], nodeStmts = [], edgeStmts = [DotEdge {fromNode = "a0", toNode = "a1", edgeAttributes = []}]}}

Thanks!