Skip to content

Commit 9372e18

Browse files
committed
printer: initial support of BasicLit and Ident
1 parent 215470b commit 9372e18

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

printer/printer.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"log"
1011

1112
"github.com/haya14busa/go-vimlparser/ast"
1213
)
@@ -41,6 +42,23 @@ func (p *printer) init(cfg *Config) {
4142
p.Config = *cfg
4243
}
4344

45+
func (p *printer) print(args ...interface{}) {
46+
for _, arg := range args {
47+
switch arg := arg.(type) {
48+
case *ast.BasicLit:
49+
p.writeString(arg.Value)
50+
case *ast.Ident:
51+
p.writeString(arg.Name)
52+
default:
53+
log.Fatal(fmt.Errorf("print: unsupported type %T", arg))
54+
}
55+
}
56+
}
57+
58+
func (p *printer) writeString(s string) {
59+
p.output = append(p.output, s...)
60+
}
61+
4462
func (p *printer) printNode(node ast.Node) error {
4563
switch n := node.(type) {
4664
case *ast.File:
@@ -59,7 +77,27 @@ func (p *printer) file(f *ast.File) error {
5977
}
6078

6179
func (p *printer) expr(expr ast.Expr) error {
62-
return errors.New("Not implemented: printer.expr")
80+
switch n := expr.(type) {
81+
// case *ast.TernaryExpr:
82+
// case *ast.BinaryExpr:
83+
// case *ast.UnaryExpr:
84+
// case *ast.SubscriptExpr:
85+
// case *ast.SliceExpr:
86+
// case *ast.CallExpr:
87+
// case *ast.DotExpr:
88+
// case *ast.List:
89+
// case *ast.Dict:
90+
// case *ast.CurlyName:
91+
// case *ast.CurlyNameLit:
92+
// case *ast.CurlyNameExpr:
93+
case *ast.BasicLit, *ast.Ident:
94+
p.print(n)
95+
// case *ast.LambdaExpr:
96+
// case *ast.ParenExpr:
97+
default:
98+
return fmt.Errorf("unsupported expr type %T", n)
99+
}
100+
return nil
63101
}
64102

65103
func (p *printer) stmt(node ast.Statement) error {

printer/printer_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,32 @@ func TestFprint_file(t *testing.T) {
2222
}
2323

2424
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)
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},
3034
}
31-
buf := new(bytes.Buffer)
32-
if err := Fprint(buf, node, nil); err == nil {
33-
t.Error("want error")
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+
}
3452
}
3553
}

0 commit comments

Comments
 (0)