aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-04-13 13:05:16 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-04-19 10:26:57 +0200
commita116470dc3f3852a062312c93ebc8f2452027133 (patch)
tree02d734adc15cd1f6c930c7ac6ba3a3f0ac055c72 /pkg/ast
parent365fba2440cee3aed74c774867a1f43e3e2f7aac (diff)
pkg/ast: add call attributes
Diffstat (limited to 'pkg/ast')
-rw-r--r--pkg/ast/ast.go1
-rw-r--r--pkg/ast/clone.go1
-rw-r--r--pkg/ast/format.go7
-rw-r--r--pkg/ast/parser.go9
-rw-r--r--pkg/ast/testdata/all.txt6
-rw-r--r--pkg/ast/walk.go3
6 files changed, 26 insertions, 1 deletions
diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go
index f1f74e61b..8cd7bef6e 100644
--- a/pkg/ast/ast.go
+++ b/pkg/ast/ast.go
@@ -92,6 +92,7 @@ type Call struct {
NR uint64
Args []*Field
Ret *Type
+ Attrs []*Type
}
func (n *Call) Info() (Pos, string, string) {
diff --git a/pkg/ast/clone.go b/pkg/ast/clone.go
index 54bf8250c..ece4ba594 100644
--- a/pkg/ast/clone.go
+++ b/pkg/ast/clone.go
@@ -89,6 +89,7 @@ func (n *Call) Clone() Node {
NR: n.NR,
Args: cloneFields(n.Args),
Ret: ret,
+ Attrs: cloneTypes(n.Attrs),
}
}
diff --git a/pkg/ast/format.go b/pkg/ast/format.go
index a2ead06f2..422478e97 100644
--- a/pkg/ast/format.go
+++ b/pkg/ast/format.go
@@ -112,6 +112,13 @@ func (c *Call) serialize(w io.Writer) {
if c.Ret != nil {
fmt.Fprintf(w, " %v", fmtType(c.Ret))
}
+ if len(c.Attrs) != 0 {
+ fmt.Fprintf(w, " (")
+ for i, t := range c.Attrs {
+ fmt.Fprintf(w, "%v%v", comma(i, ""), fmtType(t))
+ }
+ fmt.Fprintf(w, ")")
+ }
fmt.Fprintf(w, "\n")
}
diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go
index 7b46f6611..3f3feda9e 100644
--- a/pkg/ast/parser.go
+++ b/pkg/ast/parser.go
@@ -295,9 +295,16 @@ func (p *parser) parseCall(name *Ident) *Call {
p.tryConsume(tokComma)
}
p.consume(tokRParen)
- if p.tok != tokNewLine {
+ if p.tok != tokNewLine && p.tok != tokLParen {
c.Ret = p.parseType()
}
+ if p.tryConsume(tokLParen) {
+ c.Attrs = append(c.Attrs, p.parseType())
+ for p.tryConsume(tokComma) {
+ c.Attrs = append(c.Attrs, p.parseType())
+ }
+ p.consume(tokRParen)
+ }
return c
}
diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt
index 122fa514b..eccdda712 100644
--- a/pkg/ast/testdata/all.txt
+++ b/pkg/ast/testdata/all.txt
@@ -23,6 +23,12 @@ str_flags4 = "string", 42 ### unexpected int, expecting string, hex string
call(foo ,int32 , bar int32) ### unexpected ',', expecting int, identifier, string
call(foo int32:"bar") ### unexpected string, expecting int, identifier
call(a int32, b len[a:"bar"]) ### unexpected string, expecting int, identifier
+call() (attr)
+call() (attr1, attr2[arg1, "arg2"])
+call() ("attr1")
+call() (42)
+call() ( ### unexpected '\n', expecting int, identifier, string
+call() () ### unexpected ')', expecting int, identifier, string
define FOO bar
diff --git a/pkg/ast/walk.go b/pkg/ast/walk.go
index 142befaaf..fe64b0676 100644
--- a/pkg/ast/walk.go
+++ b/pkg/ast/walk.go
@@ -77,6 +77,9 @@ func (n *Call) walk(cb func(Node)) {
if n.Ret != nil {
cb(n.Ret)
}
+ for _, a := range n.Attrs {
+ cb(a)
+ }
}
func (n *Struct) walk(cb func(Node)) {