...
1 package main
2
3 import (
4 "encoding/xml"
5 "log"
6 )
7
8 type XMLField struct {
9 XMLName xml.Name
10
11
12 Bytes uint `xml:"bytes,attr"`
13
14
15 Name string `xml:"name,attr"`
16
17
18 Type string `xml:"type,attr"`
19
20
21 Expr *XMLExpression `xml:",any"`
22
23
24 ValueMaskType string `xml:"value-mask-type,attr"`
25 ValueMaskName string `xml:"value-mask-name,attr"`
26 ValueListName string `xml:"value-list-name,attr"`
27
28
29 Bitcases []*XMLBitcase `xml:"bitcase"`
30
31
32
33 OptEnum string `xml:"enum,attr"`
34 OptMask string `xml:"mask,attr"`
35 OptAltEnum string `xml:"altenum,attr"`
36 }
37
38
39
40
41
42
43
44
45
46 type XMLBitcase struct {
47 Fields []*XMLField `xml:",any"`
48
49
50
51 ExprOp *XMLExpression `xml:"op"`
52 ExprUnOp *XMLExpression `xml:"unop"`
53 ExprField *XMLExpression `xml:"fieldref"`
54 ExprValue *XMLExpression `xml:"value"`
55 ExprBit *XMLExpression `xml:"bit"`
56 ExprEnum *XMLExpression `xml:"enumref"`
57 ExprSum *XMLExpression `xml:"sumof"`
58 ExprPop *XMLExpression `xml:"popcount"`
59 }
60
61
62
63 func (b *XMLBitcase) Expr() *XMLExpression {
64 choices := []*XMLExpression{
65 b.ExprOp, b.ExprUnOp, b.ExprField, b.ExprValue,
66 b.ExprBit, b.ExprEnum, b.ExprSum, b.ExprPop,
67 }
68
69 var choice *XMLExpression = nil
70 numNonNil := 0
71 for _, c := range choices {
72 if c != nil {
73 numNonNil++
74 choice = c
75 }
76 }
77
78 if choice == nil {
79 log.Panicf("No top level expression found in a bitcase.")
80 }
81 if numNonNil > 1 {
82 log.Panicf("More than one top-level expression was found in a bitcase.")
83 }
84 return choice
85 }
86
View as plain text