aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/xml.go
blob: df214332279f8b33ef64a0659fac337d54598045 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main

import (
	"encoding/xml"
	"io/ioutil"
	"log"
)

type XML struct {
	// Root 'xcb' element properties.
	XMLName        xml.Name `xml:"xcb"`
	Header         string   `xml:"header,attr"`
	ExtensionXName string   `xml:"extension-xname,attr"`
	ExtensionName  string   `xml:"extension-name,attr"`
	MajorVersion   string   `xml:"major-version,attr"`
	MinorVersion   string   `xml:"minor-version,attr"`

	// Types for all top-level elements.
	// First are the simple ones.
	Imports     XMLImports     `xml:"import"`
	Enums       []*XMLEnum       `xml:"enum"`
	Xids        []*XMLXid        `xml:"xidtype"`
	XidUnions   []*XMLXid        `xml:"xidunion"`
	TypeDefs    []*XMLTypeDef    `xml:"typedef"`
	EventCopies []*XMLEventCopy `xml:"eventcopy"`
	ErrorCopies []*XMLErrorCopy `xml:"errorcopy"`

	// Here are the complex ones, i.e., anything with "structure contents"
	Structs  []*XMLStruct  `xml:"struct"`
	Unions   []*XMLUnion   `xml:"union"`
	Requests []*XMLRequest `xml:"request"`
	Events   []*XMLEvent   `xml:"event"`
	Errors   []*XMLError   `xml:"error"`
}

type XMLImports []*XMLImport

func (imports XMLImports) Eval() {
	for _, imp := range imports {
		xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml")
		if err != nil {
			log.Fatalf("Could not read X protocol description for import "+
				"'%s' because: %s", imp.Name, err)
		}

		imp.xml = &XML{}
		err = xml.Unmarshal(xmlBytes, imp.xml)
		if err != nil {
			log.Fatal("Could not parse X protocol description for import "+
				"'%s' because: %s", imp.Name, err)
		}

		// recursive imports...
		imp.xml.Imports.Eval()
	}
}

type XMLImport struct {
	Name string `xml:",chardata"`
	xml  *XML   `xml:"-"`
}

type XMLEnum struct {
	Name  string         `xml:"name,attr"`
	Items []*XMLEnumItem `xml:"item"`
}

type XMLEnumItem struct {
	Name string         `xml:"name,attr"`
	Expr *XMLExpression `xml:",any"`
}

type XMLXid struct {
	XMLName xml.Name
	Name    string `xml:"name,attr"`
}

type XMLTypeDef struct {
	Old string `xml:"oldname,attr"`
	New string `xml:"newname,attr"`
}

type XMLEventCopy struct {
	Name   string `xml:"name,attr"`
	Number int    `xml:"number,attr"`
	Ref    string `xml:"ref,attr"`
}

type XMLErrorCopy struct {
	Name   string `xml:"name,attr"`
	Number int    `xml:"number,attr"`
	Ref    string `xml:"ref,attr"`
}

type XMLStruct struct {
	Name   string    `xml:"name,attr"`
	Fields []*XMLField `xml:",any"`
}

type XMLUnion struct {
	Name   string    `xml:"name,attr"`
	Fields []*XMLField `xml:",any"`
}

type XMLRequest struct {
	Name    string    `xml:"name,attr"`
	Opcode  int       `xml:"opcode,attr"`
	Combine bool      `xml:"combine-adjacent,attr"`
	Fields  []*XMLField `xml:",any"`
	Reply   *XMLReply `xml:"reply"`
}

type XMLReply struct {
	Fields []*XMLField `xml:",any"`
}

type XMLEvent struct {
	Name       string    `xml:"name,attr"`
	Number     int       `xml:"number,attr"`
	NoSequence bool      `xml:"no-sequence-number,attr"`
	Fields     []*XMLField `xml:",any"`
}

type XMLError struct {
	Name   string    `xml:"name,attr"`
	Number int       `xml:"number,attr"`
	Fields []*XMLField `xml:",any"`
}

type XMLExpression struct {
	XMLName xml.Name

	Exprs []*XMLExpression `xml:",any"`

	Data string `xml:",chardata"`
	Op   string `xml:"op,attr"`
	Ref  string `xml:"ref,attr"`
}