github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/formats/dot/dot.go (about)

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