github.com/BurntSushi/xgb@v0.0.0-20210121224620-deaf085860bc/xgbgen/xml.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/xml"
     5  	"io/ioutil"
     6  	"log"
     7  )
     8  
     9  type XML struct {
    10  	// Root 'xcb' element properties.
    11  	XMLName        xml.Name `xml:"xcb"`
    12  	Header         string   `xml:"header,attr"`
    13  	ExtensionXName string   `xml:"extension-xname,attr"`
    14  	ExtensionName  string   `xml:"extension-name,attr"`
    15  	MajorVersion   string   `xml:"major-version,attr"`
    16  	MinorVersion   string   `xml:"minor-version,attr"`
    17  
    18  	// Types for all top-level elements.
    19  	// First are the simple ones.
    20  	Imports     XMLImports      `xml:"import"`
    21  	Enums       []*XMLEnum      `xml:"enum"`
    22  	Xids        []*XMLXid       `xml:"xidtype"`
    23  	XidUnions   []*XMLXid       `xml:"xidunion"`
    24  	TypeDefs    []*XMLTypeDef   `xml:"typedef"`
    25  	EventCopies []*XMLEventCopy `xml:"eventcopy"`
    26  	ErrorCopies []*XMLErrorCopy `xml:"errorcopy"`
    27  
    28  	// Here are the complex ones, i.e., anything with "structure contents"
    29  	Structs  []*XMLStruct  `xml:"struct"`
    30  	Unions   []*XMLUnion   `xml:"union"`
    31  	Requests []*XMLRequest `xml:"request"`
    32  	Events   []*XMLEvent   `xml:"event"`
    33  	Errors   []*XMLError   `xml:"error"`
    34  }
    35  
    36  type XMLImports []*XMLImport
    37  
    38  func (imports XMLImports) Eval() {
    39  	for _, imp := range imports {
    40  		xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml")
    41  		if err != nil {
    42  			log.Fatalf("Could not read X protocol description for import "+
    43  				"'%s' because: %s", imp.Name, err)
    44  		}
    45  
    46  		imp.xml = &XML{}
    47  		err = xml.Unmarshal(xmlBytes, imp.xml)
    48  		if err != nil {
    49  			log.Fatal("Could not parse X protocol description for import "+
    50  				"'%s' because: %s", imp.Name, err)
    51  		}
    52  
    53  		// recursive imports...
    54  		imp.xml.Imports.Eval()
    55  	}
    56  }
    57  
    58  type XMLImport struct {
    59  	Name string `xml:",chardata"`
    60  	xml  *XML   `xml:"-"`
    61  }
    62  
    63  type XMLEnum struct {
    64  	Name  string         `xml:"name,attr"`
    65  	Items []*XMLEnumItem `xml:"item"`
    66  }
    67  
    68  type XMLEnumItem struct {
    69  	Name string         `xml:"name,attr"`
    70  	Expr *XMLExpression `xml:",any"`
    71  }
    72  
    73  type XMLXid struct {
    74  	XMLName xml.Name
    75  	Name    string `xml:"name,attr"`
    76  }
    77  
    78  type XMLTypeDef struct {
    79  	Old string `xml:"oldname,attr"`
    80  	New string `xml:"newname,attr"`
    81  }
    82  
    83  type XMLEventCopy struct {
    84  	Name   string `xml:"name,attr"`
    85  	Number int    `xml:"number,attr"`
    86  	Ref    string `xml:"ref,attr"`
    87  }
    88  
    89  type XMLErrorCopy struct {
    90  	Name   string `xml:"name,attr"`
    91  	Number int    `xml:"number,attr"`
    92  	Ref    string `xml:"ref,attr"`
    93  }
    94  
    95  type XMLStruct struct {
    96  	Name   string      `xml:"name,attr"`
    97  	Fields []*XMLField `xml:",any"`
    98  }
    99  
   100  type XMLUnion struct {
   101  	Name   string      `xml:"name,attr"`
   102  	Fields []*XMLField `xml:",any"`
   103  }
   104  
   105  type XMLRequest struct {
   106  	Name    string      `xml:"name,attr"`
   107  	Opcode  int         `xml:"opcode,attr"`
   108  	Combine bool        `xml:"combine-adjacent,attr"`
   109  	Fields  []*XMLField `xml:",any"`
   110  	Reply   *XMLReply   `xml:"reply"`
   111  }
   112  
   113  type XMLReply struct {
   114  	Fields []*XMLField `xml:",any"`
   115  }
   116  
   117  type XMLEvent struct {
   118  	Name       string      `xml:"name,attr"`
   119  	Number     int         `xml:"number,attr"`
   120  	NoSequence bool        `xml:"no-sequence-number,attr"`
   121  	Fields     []*XMLField `xml:",any"`
   122  }
   123  
   124  type XMLError struct {
   125  	Name   string      `xml:"name,attr"`
   126  	Number int         `xml:"number,attr"`
   127  	Fields []*XMLField `xml:",any"`
   128  }
   129  
   130  type XMLExpression struct {
   131  	XMLName xml.Name
   132  
   133  	Exprs []*XMLExpression `xml:",any"`
   134  
   135  	Data string `xml:",chardata"`
   136  	Op   string `xml:"op,attr"`
   137  	Ref  string `xml:"ref,attr"`
   138  }