2012-04-30 22:18:17 +02:00
|
|
|
package main
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2012-04-30 22:18:17 +02:00
|
|
|
// Error types
|
|
|
|
func (e *Error) Define(c *Context) {
|
|
|
|
c.Putln("// Error definition %s (%d)", e.SrcName(), e.Number)
|
2012-05-02 07:46:30 +02:00
|
|
|
c.Putln("// Size: %s", e.Size())
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("const %s = %d", e.ErrConst(), e.Number)
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("type %s struct {", e.ErrType())
|
|
|
|
c.Putln("Sequence uint16")
|
|
|
|
c.Putln("NiceName string")
|
|
|
|
for _, field := range e.Fields {
|
|
|
|
field.Define(c)
|
|
|
|
}
|
|
|
|
c.Putln("}")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("")
|
|
|
|
|
2012-05-02 07:46:30 +02:00
|
|
|
// Read defines a function that transforms a byte slice into this
|
|
|
|
// error struct.
|
|
|
|
e.Read(c)
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// Makes sure this error type implements the xgb.Error interface.
|
|
|
|
e.ImplementsError(c)
|
2012-05-02 07:46:30 +02:00
|
|
|
|
|
|
|
// Let's the XGB event loop read this error.
|
|
|
|
c.Putln("func init() {")
|
|
|
|
c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.ErrType())
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-02 07:46:30 +02:00
|
|
|
func (e *Error) Read(c *Context) {
|
|
|
|
c.Putln("// Error read %s", e.SrcName())
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("func New%s(buf []byte) Error {", e.ErrType())
|
2012-05-02 07:46:30 +02:00
|
|
|
c.Putln("v := %s{}", e.ErrType())
|
|
|
|
c.Putln("v.NiceName = \"%s\"", e.SrcName())
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("b := 1 // skip error determinant")
|
|
|
|
c.Putln("b += 1 // don't read error number")
|
|
|
|
c.Putln("")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("v.Sequence = Get16(buf[b:])")
|
2012-05-02 07:46:30 +02:00
|
|
|
c.Putln("b += 2")
|
|
|
|
c.Putln("")
|
|
|
|
for _, field := range e.Fields {
|
2012-05-03 07:00:01 +02:00
|
|
|
field.Read(c, "v.")
|
2012-05-02 07:46:30 +02:00
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
c.Putln("return v")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// ImplementsError writes functions to implement the XGB Error interface.
|
|
|
|
func (e *Error) ImplementsError(c *Context) {
|
|
|
|
c.Putln("func (err %s) ImplementsError() { }", e.ErrType())
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("func (err %s) SequenceId() uint16 {", e.ErrType())
|
|
|
|
c.Putln("return err.Sequence")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("func (err %s) BadId() Id {", e.ErrType())
|
|
|
|
c.Putln("return Id(err.BadValue)")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("func (err %s) Error() string {", e.ErrType())
|
|
|
|
FieldString(c, e.Fields, e.ErrConst())
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:18:17 +02:00
|
|
|
// ErrorCopy types
|
|
|
|
func (e *ErrorCopy) Define(c *Context) {
|
|
|
|
c.Putln("// ErrorCopy definition %s (%d)", e.SrcName(), e.Number)
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("const %s = %d", e.ErrConst(), e.Number)
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType())
|
|
|
|
c.Putln("")
|
2012-05-01 07:08:03 +02:00
|
|
|
|
|
|
|
// Read defines a function that transforms a byte slice into this
|
|
|
|
// error struct.
|
|
|
|
e.Read(c)
|
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// Makes sure this error type implements the xgb.Error interface.
|
|
|
|
e.ImplementsError(c)
|
2012-05-01 07:08:03 +02:00
|
|
|
|
|
|
|
// Let's the XGB know how to read this error.
|
|
|
|
c.Putln("func init() {")
|
2012-05-02 07:46:30 +02:00
|
|
|
c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.ErrType())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorCopy) Read(c *Context) {
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("func New%s(buf []byte) Error {", e.ErrType())
|
|
|
|
c.Putln("v := %s(New%s(buf).(%s))",
|
|
|
|
e.ErrType(), e.Old.(*Error).ErrType(), e.Old.(*Error).ErrType())
|
|
|
|
c.Putln("v.NiceName = \"%s\"", e.SrcName())
|
|
|
|
c.Putln("return v")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImplementsError writes functions to implement the XGB Error interface.
|
|
|
|
func (e *ErrorCopy) ImplementsError(c *Context) {
|
|
|
|
c.Putln("func (err %s) ImplementsError() { }", e.ErrType())
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("func (err %s) SequenceId() uint16 {", e.ErrType())
|
|
|
|
c.Putln("return err.Sequence")
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("func (err %s) BadId() Id {", e.ErrType())
|
|
|
|
c.Putln("return Id(err.BadValue)")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("func (err %s) Error() string {", e.ErrType())
|
|
|
|
FieldString(c, e.Old.(*Error).Fields, e.ErrConst())
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldString works for both Error and ErrorCopy. It assembles all of the
|
|
|
|
// fields in an error and formats them into a single string.
|
|
|
|
func FieldString(c *Context, fields []Field, errName string) {
|
|
|
|
c.Putln("fieldVals := make([]string, 0, %d)", len(fields))
|
|
|
|
c.Putln("fieldVals = append(fieldVals, \"NiceName: \" + err.NiceName)")
|
|
|
|
c.Putln("fieldVals = append(fieldVals, " +
|
|
|
|
"sprintf(\"Sequence: %s\", err.Sequence))", "%d")
|
|
|
|
for _, field := range fields {
|
|
|
|
switch field.(type) {
|
|
|
|
case *PadField:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
if field.SrcType() == "string" {
|
|
|
|
c.Putln("fieldVals = append(fieldVals, \"%s: \" + err.%s)",
|
|
|
|
field.SrcName(), field.SrcName())
|
|
|
|
} else {
|
|
|
|
format := fmt.Sprintf("sprintf(\"%s: %s\", err.%s)",
|
|
|
|
field.SrcName(), "%d", field.SrcName())
|
|
|
|
c.Putln("fieldVals = append(fieldVals, %s)", format)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Putln("return \"%s {\" + stringsJoin(fieldVals, \", \") + \"}\"", errName)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|