gographviz.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //Copyright 2013 GoGraphviz Authors
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License");
  4. //you may not use this file except in compliance with the License.
  5. //You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. //Unless required by applicable law or agreed to in writing, software
  10. //distributed under the License is distributed on an "AS IS" BASIS,
  11. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //See the License for the specific language governing permissions and
  13. //limitations under the License.
  14. //Package gographviz provides parsing for the DOT grammar into
  15. //an abstract syntax tree representing a graph,
  16. //analysis of the abstract syntax tree into a more usable structure,
  17. //and writing back of this structure into the DOT format.
  18. package gographviz
  19. import (
  20. "github.com/awalterschulze/gographviz/ast"
  21. "github.com/awalterschulze/gographviz/internal/parser"
  22. )
  23. var _ Interface = NewGraph()
  24. //Interface allows you to parse the graph into your own structure.
  25. type Interface interface {
  26. SetStrict(strict bool) error
  27. SetDir(directed bool) error
  28. SetName(name string) error
  29. AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string) error
  30. AddEdge(src, dst string, directed bool, attrs map[string]string) error
  31. AddNode(parentGraph string, name string, attrs map[string]string) error
  32. AddAttr(parentGraph string, field, value string) error
  33. AddSubGraph(parentGraph string, name string, attrs map[string]string) error
  34. String() string
  35. }
  36. //Parse parses the buffer into a abstract syntax tree representing the graph.
  37. func Parse(buf []byte) (*ast.Graph, error) {
  38. return parser.ParseBytes(buf)
  39. }
  40. //ParseString parses the buffer into a abstract syntax tree representing the graph.
  41. func ParseString(buf string) (*ast.Graph, error) {
  42. return parser.ParseBytes([]byte(buf))
  43. }
  44. //Read parses and creates a new Graph from the data.
  45. func Read(buf []byte) (*Graph, error) {
  46. st, err := Parse(buf)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return NewAnalysedGraph(st)
  51. }