2012-04-29 05:25:57 +02:00
|
|
|
package main
|
2012-04-30 08:44:31 +02:00
|
|
|
|
2012-04-29 05:25:57 +02:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
type XMLField struct {
|
2012-04-29 05:25:57 +02:00
|
|
|
XMLName xml.Name
|
|
|
|
|
|
|
|
// For 'pad' element
|
2018-09-21 08:35:18 +02:00
|
|
|
Bytes uint `xml:"bytes,attr"`
|
2017-01-18 09:53:26 +01:00
|
|
|
Align uint16 `xml:"align,attr"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// For 'field', 'list', 'localfield', 'exprfield' and 'switch' elements.
|
2012-04-30 08:40:55 +02:00
|
|
|
Name string `xml:"name,attr"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// For 'field', 'list', 'localfield', and 'exprfield' elements.
|
2012-04-30 08:40:55 +02:00
|
|
|
Type string `xml:"type,attr"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// For 'list', 'exprfield' and 'switch' elements.
|
2012-04-30 08:40:55 +02:00
|
|
|
Expr *XMLExpression `xml:",any"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// For 'valueparm' element.
|
2012-04-30 08:40:55 +02:00
|
|
|
ValueMaskType string `xml:"value-mask-type,attr"`
|
|
|
|
ValueMaskName string `xml:"value-mask-name,attr"`
|
|
|
|
ValueListName string `xml:"value-list-name,attr"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// For 'switch' element.
|
2012-05-06 08:21:31 +02:00
|
|
|
Bitcases []*XMLBitcase `xml:"bitcase"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// I don't know which elements these are for. The documentation is vague.
|
|
|
|
// They also seem to be completely optional.
|
2012-04-30 08:44:31 +02:00
|
|
|
OptEnum string `xml:"enum,attr"`
|
|
|
|
OptMask string `xml:"mask,attr"`
|
2012-04-30 08:40:55 +02:00
|
|
|
OptAltEnum string `xml:"altenum,attr"`
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bitcase represents a single expression followed by any number of fields.
|
|
|
|
// Namely, if the switch's expression (all bitcases are inside a switch),
|
|
|
|
// and'd with the bitcase's expression is equal to the bitcase expression,
|
|
|
|
// then the fields should be included in its parent structure.
|
|
|
|
// Note that since a bitcase is unique in that expressions and fields are
|
|
|
|
// siblings, we must exhaustively search for one of them. Essentially,
|
|
|
|
// it's the closest thing to a Union I can get to in Go without interfaces.
|
|
|
|
// Would an '<expression>' tag have been too much to ask? :-(
|
2012-04-30 08:40:55 +02:00
|
|
|
type XMLBitcase struct {
|
2012-05-06 08:21:31 +02:00
|
|
|
Fields []*XMLField `xml:",any"`
|
2012-04-29 05:25:57 +02:00
|
|
|
|
|
|
|
// All the different expressions.
|
|
|
|
// When it comes time to choose one, use the 'Expr' method.
|
2012-04-30 08:44:31 +02:00
|
|
|
ExprOp *XMLExpression `xml:"op"`
|
|
|
|
ExprUnOp *XMLExpression `xml:"unop"`
|
2012-04-30 08:40:55 +02:00
|
|
|
ExprField *XMLExpression `xml:"fieldref"`
|
|
|
|
ExprValue *XMLExpression `xml:"value"`
|
2012-04-30 08:44:31 +02:00
|
|
|
ExprBit *XMLExpression `xml:"bit"`
|
|
|
|
ExprEnum *XMLExpression `xml:"enumref"`
|
|
|
|
ExprSum *XMLExpression `xml:"sumof"`
|
|
|
|
ExprPop *XMLExpression `xml:"popcount"`
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expr chooses the only non-nil Expr* field from Bitcase.
|
|
|
|
// Panic if there is more than one non-nil expression.
|
2012-04-30 08:40:55 +02:00
|
|
|
func (b *XMLBitcase) Expr() *XMLExpression {
|
|
|
|
choices := []*XMLExpression{
|
2012-04-29 05:25:57 +02:00
|
|
|
b.ExprOp, b.ExprUnOp, b.ExprField, b.ExprValue,
|
|
|
|
b.ExprBit, b.ExprEnum, b.ExprSum, b.ExprPop,
|
|
|
|
}
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
var choice *XMLExpression = nil
|
2012-04-29 05:25:57 +02:00
|
|
|
numNonNil := 0
|
|
|
|
for _, c := range choices {
|
|
|
|
if c != nil {
|
|
|
|
numNonNil++
|
|
|
|
choice = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if choice == nil {
|
|
|
|
log.Panicf("No top level expression found in a bitcase.")
|
|
|
|
}
|
|
|
|
if numNonNil > 1 {
|
|
|
|
log.Panicf("More than one top-level expression was found in a bitcase.")
|
|
|
|
}
|
|
|
|
return choice
|
|
|
|
}
|