File tree Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Package printer implements printing of AST nodes.
2
+ //
3
+ // This is WIP package. DO NOT USE.
4
+ package printer
5
+
6
+ import (
7
+ "errors"
8
+ "fmt"
9
+ "io"
10
+
11
+ "github.com/haya14busa/go-vimlparser/ast"
12
+ )
13
+
14
+ // A Config node controls the output of Fprint.
15
+ type Config struct {}
16
+
17
+ // Fprint "pretty-prints" an AST node to output for a given configuration cfg.
18
+ func Fprint (output io.Writer , node ast.Node , cfg * Config ) error {
19
+ var p printer
20
+ p .init (cfg )
21
+ if err := p .printNode (node ); err != nil {
22
+ return err
23
+ }
24
+ if _ , err := output .Write (p .output ); err != nil {
25
+ return err
26
+ }
27
+ return nil
28
+ }
29
+
30
+ type printer struct {
31
+ Config
32
+
33
+ // Current state
34
+ output []byte // raw printer result
35
+ }
36
+
37
+ func (p * printer ) init (cfg * Config ) {
38
+ if cfg == nil {
39
+ cfg = & Config {}
40
+ }
41
+ p .Config = * cfg
42
+ }
43
+
44
+ func (p * printer ) printNode (node ast.Node ) error {
45
+ switch n := node .(type ) {
46
+ case * ast.File :
47
+ return p .file (n )
48
+ case ast.Expr :
49
+ return p .expr (n )
50
+ case ast.Statement :
51
+ return p .stmt (n )
52
+ default :
53
+ return fmt .Errorf ("go-vimlparser/printer: unsupported node type %T" , node )
54
+ }
55
+ }
56
+
57
+ func (p * printer ) file (f * ast.File ) error {
58
+ return errors .New ("Not implemented: printer.file" )
59
+ }
60
+
61
+ func (p * printer ) expr (expr ast.Expr ) error {
62
+ return errors .New ("Not implemented: printer.expr" )
63
+ }
64
+
65
+ func (p * printer ) stmt (node ast.Statement ) error {
66
+ return errors .New ("Not implemented: printer.stmt" )
67
+ }
Original file line number Diff line number Diff line change
1
+ package printer
2
+
3
+ import (
4
+ "bytes"
5
+ "strings"
6
+ "testing"
7
+
8
+ "github.com/haya14busa/go-vimlparser"
9
+ )
10
+
11
+ func TestFprint_file (t * testing.T ) {
12
+ src := `let _ = 1`
13
+ r := strings .NewReader (src )
14
+ node , err := vimlparser .ParseFile (r , "" , nil )
15
+ if err != nil {
16
+ t .Fatal (err )
17
+ }
18
+ buf := new (bytes.Buffer )
19
+ if err := Fprint (buf , node , nil ); err == nil {
20
+ t .Error ("want error" )
21
+ }
22
+ }
23
+
24
+ func TestFprint_expr (t * testing.T ) {
25
+ src := `1 + 2`
26
+ r := strings .NewReader (src )
27
+ node , err := vimlparser .ParseExpr (r )
28
+ if err != nil {
29
+ t .Fatal (err )
30
+ }
31
+ buf := new (bytes.Buffer )
32
+ if err := Fprint (buf , node , nil ); err == nil {
33
+ t .Error ("want error" )
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments