Skip to content

Commit cb5c6dd

Browse files
authored
Merge pull request #34 from haya14busa/printer
printer: add printer package
2 parents c568e29 + 6ff15c9 commit cb5c6dd

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

printer/printer.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
p.Config = *cfg
40+
}
41+
}
42+
43+
func (p *printer) writeString(s string) {
44+
p.output = append(p.output, s...)
45+
}
46+
47+
func (p *printer) printNode(node ast.Node) error {
48+
switch n := node.(type) {
49+
case *ast.File:
50+
return p.file(n)
51+
case ast.Expr:
52+
return p.expr(n)
53+
case ast.Statement:
54+
return p.stmt(n)
55+
default:
56+
return fmt.Errorf("go-vimlparser/printer: unsupported node type %T", node)
57+
}
58+
}
59+
60+
func (p *printer) file(f *ast.File) error {
61+
return errors.New("Not implemented: printer.file")
62+
}
63+
64+
func (p *printer) expr(expr ast.Expr) error {
65+
switch n := expr.(type) {
66+
// case *ast.TernaryExpr:
67+
// case *ast.BinaryExpr:
68+
// case *ast.UnaryExpr:
69+
// case *ast.SubscriptExpr:
70+
// case *ast.SliceExpr:
71+
// case *ast.CallExpr:
72+
// case *ast.DotExpr:
73+
// case *ast.List:
74+
// case *ast.Dict:
75+
// case *ast.CurlyName:
76+
// case *ast.CurlyNameLit:
77+
// case *ast.CurlyNameExpr:
78+
case *ast.BasicLit:
79+
p.writeString(n.Value)
80+
case *ast.Ident:
81+
p.writeString(n.Name)
82+
// case *ast.LambdaExpr:
83+
// case *ast.ParenExpr:
84+
default:
85+
return fmt.Errorf("unsupported expr type %T", n)
86+
}
87+
return nil
88+
}
89+
90+
func (p *printer) stmt(node ast.Statement) error {
91+
return errors.New("Not implemented: printer.stmt")
92+
}

printer/printer_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
tests := []struct {
26+
in string
27+
want string
28+
wantErr bool
29+
}{
30+
{in: `xyz`, want: `xyz`}, // Ident
31+
{in: `"double quote"`, want: `"double quote"`}, // BasicLit
32+
{in: `14`, want: `14`}, // BasicLit
33+
{in: `x+1`, want: `x + 1`, wantErr: true},
34+
}
35+
36+
for _, tt := range tests {
37+
r := strings.NewReader(tt.in)
38+
node, err := vimlparser.ParseExpr(r)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
buf := new(bytes.Buffer)
43+
if err := Fprint(buf, node, nil); err != nil {
44+
if !tt.wantErr {
45+
t.Errorf("got unexpected error: %v", err)
46+
}
47+
continue
48+
}
49+
if got := buf.String(); got != tt.want {
50+
t.Errorf("got: %v, want: %v", got, tt.want)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)