Skip to content

Commit

Permalink
restore lost work fom 1.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcooke committed Nov 13, 2015
1 parent b275b4e commit 6024e47
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 28 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,11 @@ Drop stuff you don't need.
Transform things into containers so that your result has nice types. Look at
how the [example](#example) works.

Understand the format you are parsing. What motivated the person who
designed the format? Compare the [GML](src/gml/GML.jl) and
[DOT](src/dot/DOT.jl) parsers - they return different results because
the format authors cared about different things. GML is an elegant,
general data format, while DOT is a sequential description - a
program, almost - that encodes graph layouts.
Understand the format you a parsing. What motivated the person who designed
the format? Compare the [GML](src/gml/GML.jl) and [DOT](src/dot/DOT.jl)
parsers - they return different results because the format authors cared about
different things. GML is an elegant, general data format, while DOT is a
sequential description - a program, almost - that encodes graph layouts.

#### Adding Matchers

Expand Down Expand Up @@ -1085,9 +1084,10 @@ DOT describes a graph using a complex format that resembles a program (with
mutable state) more than a specification (see comments in
[source](src/dot/DOT.jl)).

* `parse_dot` returns a structured AST (see the types in
[DOT.jl](src/dot/DOT.jl)). It has one keyword argument, `debug`, which
takes a `Bool` and enables the usual debugging output.
* `parse_dot` returns a list of structured AST (see the types in
[DOT.jl](src/dot/DOT.jl)), one per graph in the file. It has one keyword
argument, `debug`, which takes a `Bool` and enables the usual debugging
output.

* `nodes(g::Graph)` extracts a set of node names from the structured AST.

Expand Down
8 changes: 4 additions & 4 deletions src/dot/DOT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,18 @@ end
Seq!(~NoCase("graph"), Insert(false)))
graph = Seq!(strict, spc_star, direct, spc_star, Opt!(id), spc_star, stmt_brak) > Graph

dot = Seq!(spc_init, graph, spc_star, Eos())
dot = Seq!(spc_init, Plus!(Seq!(graph, spc_star)), Eos())

end


# the file structured using the types above.
# the file structured using the types above (returns an array of graphs)
function parse_dot(s; debug=false)
try
if debug
parse_one_dbg(s, Trace(dot); debug=true)[1]
parse_one_dbg(s, Trace(dot); debug=true)
else
parse_one(s, dot)[1]
parse_one(s, dot)
end
catch x
if debug
Expand Down
17 changes: 17 additions & 0 deletions test/dot/double.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
graph {
a -- b;
b -- c;
a -- c;
d -- c;
e -- c;
e -- a;
}

graph two {
a -- b;
b -- c;
a -- c;
d -- c;
e -- c;
e -- a;
}
2 changes: 1 addition & 1 deletion test/dot/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ my_graph = "graph {
}
"

root = parse_dot(my_graph)
root = parse_dot(my_graph)[1]

for node in nodes(root)
println("node $(node)")
Expand Down
27 changes: 17 additions & 10 deletions test/dot/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ using ParserCombinator.Parsers.DOT; D = ParserCombinator.Parsers.DOT

# from http://graphs.grevian.org/example

d = parse_dot(open(readall, "dot/simple.dot"))
d = parse_dot(open(readall, "dot/simple.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e"])
@test edges(d) == Set([("a","e"),("c","d"),("c","e"),("b","c"),("a","c"),("a","b")])

d = parse_dot(open(readall, "dot/k6.dot"))
d = parse_dot(open(readall, "dot/k6.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e", "f"])
@test length(edges(d)) == 6*5/2

d = parse_dot(open(readall, "dot/simple-digraph.dot"))
d = parse_dot(open(readall, "dot/simple-digraph.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "d"])
@test edges(d) == Set([("a","b"),("b","c"),("c","d"),("d","a")])

d = parse_dot(open(readall, "dot/full-digraph.dot"))
d = parse_dot(open(readall, "dot/full-digraph.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "e"])
@test edges(d) == Set([("a","b"),("a","c"),("c","b"),("e","b"),("e","e"),("c","e")])
@test length(d.stmts) == 6
Expand All @@ -32,13 +32,13 @@ d = parse_dot(open(readall, "dot/full-digraph.dot"))
@test d.stmts[1].attrs[2].value.id == "0.2"

for f in ("path1.dot", "path2.dot")
d = parse_dot(open(readall, "dot/$f"))
d = parse_dot(open(readall, "dot/$f"))[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e", "f"])
@test edges(d) == Set([("a","b"),("a","d"),("b","c"),("b","d"),("c","d"),("c","f"),("e","f"),("d","e")])
end

# this example also has comments (ignored in parse)
d = parse_dot(open(readall, "dot/simple-subgraph.dot"))
d = parse_dot(open(readall, "dot/simple-subgraph.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "d", "f"])
@test edges(d) == Set([("a","b"),("a","f"),("b","c"),("c","d"),("f","c")])
@test length(d.stmts) == 2
Expand All @@ -48,27 +48,34 @@ d = parse_dot(open(readall, "dot/simple-subgraph.dot"))
@test d.stmts[1].stmts[1].name.id == "label"
@test d.stmts[1].stmts[1].value.id == "Subgraph A"

d = parse_dot(open(readall, "dot/bipartite-subgraph.dot"))
d = parse_dot(open(readall, "dot/bipartite-subgraph.dot"))[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e"])
@test length(edges(d)) == 6

d1 = parse_dot(open(readall, "dot/large1.dot"))
d1 = parse_dot(open(readall, "dot/large1.dot"))[1]
@test length(nodes(d1)) == 21
@test length(edges(d1)) == 42

d2 = parse_dot(open(readall, "dot/large2.dot"))
d2 = parse_dot(open(readall, "dot/large2.dot"))[1]
@test length(nodes(d2)) == 21
@test length(edges(d2)) == 42

@test nodes(d1) == nodes(d2)
@test edges(d1) == edges(d2)

s = open(readall, "dot/tictactoe.dot")
@time d = parse_dot(s)
@time d = parse_dot(s)[1]
@time n = nodes(d)
@time e = edges(d)
# these numbers are not really checks, just what the code returns
@test length(n) == 744
@test length(e) == 4361

d = parse_dot(open(readall, "dot/double.dot"))
d1, d2 = d
@test nodes(d1) == Set(["a", "b", "c", "d", "e"])
@test edges(d1) == Set([("a","e"),("c","d"),("c","e"),("b","c"),("a","c"),("a","b")])
@test nodes(d2) == Set(["a", "b", "c", "d", "e"])
@test edges(d2) == Set([("a","e"),("c","d"),("c","e"),("b","c"),("a","c"),("a","b")])

println("examples ok")
8 changes: 4 additions & 4 deletions test/dot/subgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ graph {
}
d -- e
}
""")
""")[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e"])
@test edges(d) == Set([("d","e"),("a","c"),("a","b")])

Expand All @@ -21,7 +21,7 @@ graph {
}
d; e;
}
""")
""")[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e"])
@test edges(d) == Set([("a","c"),("a","b")])

Expand All @@ -32,15 +32,15 @@ graph {
c -- d
} -- e
}
""")
""")[1]
@test nodes(d) == Set(["a", "b", "c", "d", "e"])
@test edges(d) == Set([("a","b"),("a","c"),("a","d"),("b","c"),("b","e"),("c","d"),("c","e"),("d","e")])

d = parse_dot("""
graph {
subgraph { a -- b } -- subgraph { c -- d }
}
""")
""")[1]
@test nodes(d) == Set(["a", "b", "c", "d"])
@test edges(d) == Set([("a","b"),("a","c"),("a","d"),("b","c"),("b","d"),("c","d")])

Expand Down

0 comments on commit 6024e47

Please sign in to comment.