1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
// This file is dual licensed under CC0 and The Gonum License.
//
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Copyright ©2017 Robin Eklind.
// This file is made available under a Creative Commons CC0 1.0
// Universal Public Domain Dedication.
//go:generate ./makeinternal.bash
package dot
import (
"fmt"
"io"
"os"
"gonum.org/v1/gonum/graph/formats/dot/ast"
"gonum.org/v1/gonum/graph/formats/dot/internal/lexer"
"gonum.org/v1/gonum/graph/formats/dot/internal/parser"
)
// ParseFile parses the given Graphviz DOT file into an AST.
func ParseFile(path string) (*ast.File, error) {
buf, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return ParseBytes(buf)
}
// Parse parses the given Graphviz DOT file into an AST, reading from r.
func Parse(r io.Reader) (*ast.File, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return ParseBytes(buf)
}
// ParseBytes parses the given Graphviz DOT file into an AST, reading from b.
func ParseBytes(b []byte) (*ast.File, error) {
l := lexer.NewLexer(b)
p := parser.NewParser()
file, err := p.Parse(l)
if err != nil {
return nil, err
}
f, ok := file.(*ast.File)
if !ok {
return nil, fmt.Errorf("invalid file type; expected *ast.File, got %T", file)
}
if err := check(f); err != nil {
return nil, err
}
return f, nil
}
// ParseString parses the given Graphviz DOT file into an AST, reading from s.
func ParseString(s string) (*ast.File, error) {
return ParseBytes([]byte(s))
}
|