aboutsummaryrefslogtreecommitdiff
path: root/syntax/walk_test.go
blob: 00d97843e1dc065cdb0d1777f2dbc5c9b8ab314a (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package syntax_test

import (
	"bytes"
	"fmt"
	"log"
	"reflect"
	"strings"
	"testing"

	"go.starlark.net/syntax"
)

func TestWalk(t *testing.T) {
	const src = `
for x in y:
  if x:
    pass
  else:
    f([2*x for x in "abc"])
`
	// TODO(adonovan): test that it finds all syntax.Nodes
	// (compare against a reflect-based implementation).
	// TODO(adonovan): test that the result of f is used to prune
	// the descent.
	f, err := syntax.Parse("hello.go", src, 0)
	if err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	var depth int
	syntax.Walk(f, func(n syntax.Node) bool {
		if n == nil {
			depth--
			return true
		}
		fmt.Fprintf(&buf, "%s%s\n",
			strings.Repeat("  ", depth),
			strings.TrimPrefix(reflect.TypeOf(n).String(), "*syntax."))
		depth++
		return true
	})
	got := buf.String()
	want := `
File
  ForStmt
    Ident
    Ident
    IfStmt
      Ident
      BranchStmt
      ExprStmt
        CallExpr
          Ident
          Comprehension
            BinaryExpr
              Literal
              Ident
            ForClause
              Ident
              Literal`
	got = strings.TrimSpace(got)
	want = strings.TrimSpace(want)
	if got != want {
		t.Errorf("got %s, want %s", got, want)
	}
}

// ExampleWalk demonstrates the use of Walk to
// enumerate the identifiers in a Starlark source file
// containing a nonsense program with varied grammar.
func ExampleWalk() {
	const src = `
load("library", "a")

def b(c, *, d=e):
    f += {g: h}
    i = -(j)
    return k.l[m + n]

for o in [p for q, r in s if t]:
    u(lambda: v, w[x:y:z])
`
	f, err := syntax.Parse("hello.star", src, 0)
	if err != nil {
		log.Fatal(err)
	}

	var idents []string
	syntax.Walk(f, func(n syntax.Node) bool {
		if id, ok := n.(*syntax.Ident); ok {
			idents = append(idents, id.Name)
		}
		return true
	})
	fmt.Println(strings.Join(idents, " "))

	// The identifer 'a' appears in both LoadStmt.From[0] and LoadStmt.To[0].

	// Output:
	// a a b c d e f g h i j k l m n o p q r s t u v w x y z
}