Skip to content

Commit

Permalink
Updated to work with new xproto XML files.
Browse files Browse the repository at this point in the history
Namely, the "doc" element is ignored. Also, I've sorted everything
before output so that diff isn't completely useless.
  • Loading branch information
BurntSushi committed Aug 12, 2013
1 parent 7725850 commit a95df02
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
5 changes: 5 additions & 0 deletions xgbgen/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/xml"
"fmt"
"log"
"sort"
"time"
)

Expand Down Expand Up @@ -70,6 +71,8 @@ func (c *Context) Morph(xmlBytes []byte) {
if c.protocol.isExt() {
c.Putln("\"github.com/BurntSushi/xgb/xproto\"")
}

sort.Sort(Protocols(c.protocol.Imports))
for _, imp := range c.protocol.Imports {
// We always import xproto, so skip it if it's explicitly imported
if imp.Name == "xproto" {
Expand Down Expand Up @@ -142,6 +145,8 @@ func (c *Context) Morph(xmlBytes []byte) {
}

// Now write Go source code
sort.Sort(Types(c.protocol.Types))
sort.Sort(Requests(c.protocol.Requests))
for _, typ := range c.protocol.Types {
typ.Define(c)
}
Expand Down
6 changes: 6 additions & 0 deletions xgbgen/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type Protocol struct {
Requests []*Request
}

type Protocols []*Protocol

func (ps Protocols) Len() int { return len(ps) }
func (ps Protocols) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
func (ps Protocols) Less(i, j int) bool { return ps[i].ExtName < ps[j].ExtName }

// Initialize traverses all structures, looks for 'Translation' type,
// and looks up the real type in the namespace. It also sets the source
// name for all relevant fields/structures.
Expand Down
6 changes: 6 additions & 0 deletions xgbgen/request_reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type Request struct {
Reply *Reply // A reply, if one exists for this request.
}

type Requests []*Request

func (rs Requests) Len() int { return len(rs) }
func (rs Requests) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs Requests) Less(i, j int) bool { return rs[i].xmlName < rs[j].xmlName }

// Initialize creates the proper Go source name for this request.
// It also initializes the reply if one exists, and all fields in this request.
func (r *Request) Initialize(p *Protocol) {
Expand Down
28 changes: 18 additions & 10 deletions xgbgen/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ func (x *XMLEvent) Translate() *Event {
xmlName: x.Name,
Number: x.Number,
NoSequence: x.NoSequence,
Fields: make([]Field, len(x.Fields)),
Fields: make([]Field, 0, len(x.Fields)),
}
for i, field := range x.Fields {
ev.Fields[i] = field.Translate(ev)
for _, field := range x.Fields {
if field.XMLName.Local == "doc" {
continue
}
ev.Fields = append(ev.Fields, field.Translate(ev))
}
return ev
}
Expand Down Expand Up @@ -200,11 +203,14 @@ func (x *XMLRequest) Translate() *Request {
xmlName: x.Name,
Opcode: x.Opcode,
Combine: x.Combine,
Fields: make([]Field, len(x.Fields)),
Fields: make([]Field, 0, len(x.Fields)),
Reply: x.Reply.Translate(),
}
for i, field := range x.Fields {
r.Fields[i] = field.Translate(r)
for _, field := range x.Fields {
if field.XMLName.Local == "doc" {
continue
}
r.Fields = append(r.Fields, field.Translate(r))
}

// Address bug (or legacy code) in QueryTextExtents.
Expand All @@ -229,10 +235,13 @@ func (x *XMLReply) Translate() *Reply {
}

r := &Reply{
Fields: make([]Field, len(x.Fields)),
Fields: make([]Field, 0, len(x.Fields)),
}
for i, field := range x.Fields {
r.Fields[i] = field.Translate(r)
for _, field := range x.Fields {
if field.XMLName.Local == "doc" {
continue
}
r.Fields = append(r.Fields, field.Translate(r))
}
return r
}
Expand Down Expand Up @@ -380,7 +389,6 @@ func SrcName(p *Protocol, name string) string {
if newn, ok := NameMap[name]; ok {
return newn
}

return splitAndTitle(name)
}

Expand Down
10 changes: 10 additions & 0 deletions xgbgen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ type Type interface {
Define(c *Context)
}

type Types []Type

func (ts Types) Len() int { return len(ts) }
func (ts Types) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
func (ts Types) Less(i, j int) bool {
x1, x2 := ts[i].XmlName(), ts[j].XmlName()
s1, s2 := ts[i].SrcName(), ts[j].SrcName()
return (s1 == s2 && x1 < x2) || s1 < s2
}

// Translation is used *only* when transitioning from XML types to
// our better representation. They are placeholders for the real types (below)
// that will replace them.
Expand Down

0 comments on commit a95df02

Please sign in to comment.