generate: Parse doc comments
This commit is contained in:
parent
e991b5af67
commit
190a89fbb3
@ -1,6 +1,7 @@
|
||||
package generate
|
||||
|
||||
import "io"
|
||||
import "strings"
|
||||
import "strconv"
|
||||
import "git.tebibyte.media/sashakoshka/goparse"
|
||||
|
||||
@ -47,18 +48,28 @@ func (this *parser) parse() error {
|
||||
}
|
||||
|
||||
func (this *parser) parseTopLevel() error {
|
||||
err := this.ExpectDesc("message or typedef", TokenMethod, TokenIdent)
|
||||
if err != nil { return err }
|
||||
if this.EOF() { return nil }
|
||||
doc := ""
|
||||
for {
|
||||
err := this.ExpectDesc("message or typedef", TokenMethod, TokenIdent, TokenComment)
|
||||
if err != nil { return err }
|
||||
if this.EOF() { return nil }
|
||||
if this.Kind() == TokenComment {
|
||||
if doc != "" { doc += "\n" }
|
||||
doc += this.parseComment(this.Value())
|
||||
this.Next()
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
switch this.Kind() {
|
||||
case TokenMethod: return this.parseMessage()
|
||||
case TokenIdent: return this.parseTypedef()
|
||||
case TokenMethod: return this.parseMessage(doc)
|
||||
case TokenIdent: return this.parseTypedef(doc)
|
||||
}
|
||||
panic("bug")
|
||||
}
|
||||
|
||||
func (this *parser) parseMessage() error {
|
||||
func (this *parser) parseMessage(doc string) error {
|
||||
err := this.Expect(TokenMethod)
|
||||
if err != nil { return err }
|
||||
method, err := this.parseHexNumber(this.Value(), 0xFFFF)
|
||||
@ -72,13 +83,13 @@ func (this *parser) parseMessage() error {
|
||||
if err != nil { return err }
|
||||
this.protocol.Messages[uint16(method)] = Message {
|
||||
Name: name,
|
||||
// TODO: doc
|
||||
Doc: doc,
|
||||
Type: typ,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *parser) parseTypedef() error {
|
||||
func (this *parser) parseTypedef(doc string) error {
|
||||
err := this.Expect(TokenIdent)
|
||||
if err != nil { return err }
|
||||
name := this.Value()
|
||||
@ -87,7 +98,7 @@ func (this *parser) parseTypedef() error {
|
||||
typ, err := this.parseType()
|
||||
if err != nil { return err }
|
||||
this.protocol.Types[name] = Typedef {
|
||||
// TODO: doc
|
||||
Doc: doc,
|
||||
Type: typ,
|
||||
}
|
||||
return nil
|
||||
@ -161,12 +172,22 @@ func (this *parser) parseTypeTable() (TypeTableDefined, error) {
|
||||
Fields: make(map[uint16] Field),
|
||||
}
|
||||
for {
|
||||
err := this.ExpectDesc("table field", TokenKey, TokenRBrace)
|
||||
if err != nil { return TypeTableDefined { }, err }
|
||||
doc := ""
|
||||
for {
|
||||
err := this.ExpectDesc("table field", TokenKey, TokenRBrace, TokenComment)
|
||||
if err != nil { return TypeTableDefined { }, err }
|
||||
if this.Kind() == TokenComment {
|
||||
if doc != "" { doc += "\n" }
|
||||
doc += this.parseComment(this.Value())
|
||||
this.Next()
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if this.Is(TokenRBrace) {
|
||||
break
|
||||
}
|
||||
key, field, err := this.parseField()
|
||||
key, field, err := this.parseField(doc)
|
||||
if err != nil { return TypeTableDefined { }, err }
|
||||
typ.Fields[key] = field
|
||||
err = this.Expect(TokenComma, TokenRBrace)
|
||||
@ -182,7 +203,7 @@ func (this *parser) parseTypeTable() (TypeTableDefined, error) {
|
||||
return typ, nil
|
||||
}
|
||||
|
||||
func (this *parser) parseField() (uint16, Field, error) {
|
||||
func (this *parser) parseField(doc string) (uint16, Field, error) {
|
||||
err := this.Expect(TokenKey)
|
||||
if err != nil { return 0, Field { }, err }
|
||||
key, err := this.parseHexNumber(this.Value(), 0xFFFF)
|
||||
@ -196,7 +217,7 @@ func (this *parser) parseField() (uint16, Field, error) {
|
||||
if err != nil { return 0, Field { }, err }
|
||||
return uint16(key), Field {
|
||||
Name: name,
|
||||
// TODO: doc
|
||||
Doc: doc,
|
||||
Type: typ,
|
||||
}, nil
|
||||
}
|
||||
@ -211,3 +232,7 @@ func (this *parser) parseHexNumber(input string, maxValue int64) (int64, error)
|
||||
}
|
||||
return number, nil
|
||||
}
|
||||
|
||||
func (this *parser) parseComment(input string) string {
|
||||
return strings.TrimPrefix(strings.TrimPrefix(input, "//"), " ")
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ func TestParse(test *testing.T) {
|
||||
correct := defaultProtocol()
|
||||
correct.Messages[0x0000] = Message {
|
||||
Name: "Connect",
|
||||
Doc: "Connect is sent from the client to the server as the first message of an\n authenticated transaction.",
|
||||
Doc: "Connect is sent from the client to the server as the first message of an\nauthenticated transaction.",
|
||||
Type: TypeTableDefined {
|
||||
Fields: map[uint16] Field {
|
||||
0x0000: Field { Name: "Name", Type: TypeString { } },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user