Commit 5a1cbe8b authored by Rob Pike's avatar Rob Pike

trim tags from type strings for structure fields.

R=rsc
DELTA=28  (14 added, 4 deleted, 10 changed)
OCL=18561
CL=18563
parent 7fe34ea0
...@@ -250,4 +250,10 @@ func main() { ...@@ -250,4 +250,10 @@ func main() {
assert(t.String(), "chan<-string"); assert(t.String(), "chan<-string");
ct = t.(reflect.ChanType); ct = t.(reflect.ChanType);
assert(ct.Elem().String(), "string"); assert(ct.Elem().String(), "string");
// make sure tag strings are not part of element type
t = reflect.ParseTypeString("", "struct{d *[]uint32 \"TAG\"}");
st = t.(reflect.StructType);
name, typ, tag, offset = st.Field(0);
assert(typ.String(), "*[]uint32");
} }
...@@ -557,11 +557,19 @@ type Parser struct { ...@@ -557,11 +557,19 @@ type Parser struct {
str string; // string being parsed str string; // string being parsed
token string; // the token being parsed now token string; // the token being parsed now
tokstart int; // starting position of token tokstart int; // starting position of token
prevend int; // (one after) ending position of previous token
index int; // next character position in str index int; // next character position in str
} }
// Return typestring starting at position i.
// Trim trailing blanks.
func (p *Parser) TypeString(i int) string {
return p.str[i:p.prevend];
}
// Load next token into p.token // Load next token into p.token
func (p *Parser) Next() { func (p *Parser) Next() {
p.prevend = p.index;
token := ""; token := "";
for ; p.index < len(p.str) && p.str[p.index] == ' '; p.index++ { for ; p.index < len(p.str) && p.str[p.index] == ' '; p.index++ {
} }
...@@ -643,7 +651,7 @@ func (p *Parser) Array(name string, tokstart int) *StubType { ...@@ -643,7 +651,7 @@ func (p *Parser) Array(name string, tokstart int) *StubType {
} }
p.Next(); p.Next();
elemtype := p.Type(""); elemtype := p.Type("");
return NewStubType(name, NewArrayTypeStruct(name, p.str[tokstart:p.index], open, size, elemtype)); return NewStubType(name, NewArrayTypeStruct(name, p.TypeString(tokstart), open, size, elemtype));
} }
func (p *Parser) Map(name string, tokstart int) *StubType { func (p *Parser) Map(name string, tokstart int) *StubType {
...@@ -657,7 +665,7 @@ func (p *Parser) Map(name string, tokstart int) *StubType { ...@@ -657,7 +665,7 @@ func (p *Parser) Map(name string, tokstart int) *StubType {
} }
p.Next(); p.Next();
elemtype := p.Type(""); elemtype := p.Type("");
return NewStubType(name, NewMapTypeStruct(name, p.str[tokstart:p.index], keytype, elemtype)); return NewStubType(name, NewMapTypeStruct(name, p.TypeString(tokstart), keytype, elemtype));
} }
func (p *Parser) Chan(name string, tokstart, dir int) *StubType { func (p *Parser) Chan(name string, tokstart, dir int) *StubType {
...@@ -669,7 +677,7 @@ func (p *Parser) Chan(name string, tokstart, dir int) *StubType { ...@@ -669,7 +677,7 @@ func (p *Parser) Chan(name string, tokstart, dir int) *StubType {
dir = SendDir; dir = SendDir;
} }
elemtype := p.Type(""); elemtype := p.Type("");
return NewStubType(name, NewChanTypeStruct(name, p.str[tokstart:p.index], dir, elemtype)); return NewStubType(name, NewChanTypeStruct(name, p.TypeString(tokstart), dir, elemtype));
} }
// Parse array of fields for struct, interface, and func arguments // Parse array of fields for struct, interface, and func arguments
...@@ -713,9 +721,8 @@ func (p *Parser) Struct(name string, tokstart int) *StubType { ...@@ -713,9 +721,8 @@ func (p *Parser) Struct(name string, tokstart int) *StubType {
if p.token != "}" { if p.token != "}" {
return MissingStub; return MissingStub;
} }
ts := p.str[tokstart:p.index];
p.Next(); p.Next();
return NewStubType(name, NewStructTypeStruct(name, ts, f)); return NewStubType(name, NewStructTypeStruct(name, p.TypeString(tokstart), f));
} }
func (p *Parser) Interface(name string, tokstart int) *StubType { func (p *Parser) Interface(name string, tokstart int) *StubType {
...@@ -723,9 +730,8 @@ func (p *Parser) Interface(name string, tokstart int) *StubType { ...@@ -723,9 +730,8 @@ func (p *Parser) Interface(name string, tokstart int) *StubType {
if p.token != "}" { if p.token != "}" {
return MissingStub; return MissingStub;
} }
ts := p.str[tokstart:p.index];
p.Next(); p.Next();
return NewStubType(name, NewInterfaceTypeStruct(name, ts, f)); return NewStubType(name, NewInterfaceTypeStruct(name, p.TypeString(tokstart), f));
} }
func (p *Parser) Func(name string, tokstart int) *StubType { func (p *Parser) Func(name string, tokstart int) *StubType {
...@@ -734,16 +740,15 @@ func (p *Parser) Func(name string, tokstart int) *StubType { ...@@ -734,16 +740,15 @@ func (p *Parser) Func(name string, tokstart int) *StubType {
if p.token != ")" { if p.token != ")" {
return MissingStub; return MissingStub;
} }
end := p.index;
p.Next(); p.Next();
if p.token != "(" { if p.token != "(" {
// 1 list: the in parameters are a list. Is there a single out parameter? // 1 list: the in parameters are a list. Is there a single out parameter?
if p.token == "" || p.token == "}" || p.token == "," || p.token == ";" { if p.token == "" || p.token == "}" || p.token == "," || p.token == ";" {
return NewStubType(name, NewFuncTypeStruct(name, p.str[tokstart:end], f1, nil)); return NewStubType(name, NewFuncTypeStruct(name, p.TypeString(tokstart), f1, nil));
} }
// A single out parameter. // A single out parameter.
f2 := NewStructTypeStruct("", "", p.OneField()); f2 := NewStructTypeStruct("", "", p.OneField());
return NewStubType(name, NewFuncTypeStruct(name, p.str[tokstart:end], f1, f2)); return NewStubType(name, NewFuncTypeStruct(name, p.TypeString(tokstart), f1, f2));
} else { } else {
p.Next(); p.Next();
} }
...@@ -751,10 +756,9 @@ func (p *Parser) Func(name string, tokstart int) *StubType { ...@@ -751,10 +756,9 @@ func (p *Parser) Func(name string, tokstart int) *StubType {
if p.token != ")" { if p.token != ")" {
return MissingStub; return MissingStub;
} }
end = p.index;
p.Next(); p.Next();
// 2 lists: the in and out parameters are present // 2 lists: the in and out parameters are present
return NewStubType(name, NewFuncTypeStruct(name, p.str[tokstart:end], f1, f2)); return NewStubType(name, NewFuncTypeStruct(name, p.TypeString(tokstart), f1, f2));
} }
func (p *Parser) Type(name string) *StubType { func (p *Parser) Type(name string) *StubType {
...@@ -766,7 +770,7 @@ func (p *Parser) Type(name string) *StubType { ...@@ -766,7 +770,7 @@ func (p *Parser) Type(name string) *StubType {
case p.token == "*": case p.token == "*":
p.Next(); p.Next();
sub := p.Type(""); sub := p.Type("");
return NewStubType(name, NewPtrTypeStruct(name, p.str[tokstart:p.index], sub)); return NewStubType(name, NewPtrTypeStruct(name, p.TypeString(tokstart), sub));
case p.token == "[": case p.token == "[":
p.Next(); p.Next();
return p.Array(name, tokstart); return p.Array(name, tokstart);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment