Skip to content

Commit

Permalink
full TLO generation support (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
DedAzaMarks authored Oct 17, 2023
1 parent 686b3af commit e2b49b3
Show file tree
Hide file tree
Showing 21 changed files with 359 additions and 116 deletions.
28 changes: 26 additions & 2 deletions cmd/tlo2json/tlo2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"os"
"strings"

"github.com/vkcom/tl/internal/tlcodegen/gen_tlo"
tls "github.com/vkcom/tl/internal/tlcodegen/gentlo/tltls"
"github.com/vkcom/tl/internal/utils"

"golang.org/x/exp/slices"
)

func main() {
Expand All @@ -24,10 +26,32 @@ func main() {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error on reading file %s: %v", os.Args[1], err)
}
var v4 gen_tlo.TlsSchemaV4
var v4 tls.SchemaV4
if _, err := v4.ReadBoxed(buf); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error on reading schema: %v", err)
}
slices.SortFunc(v4.Constructors, func(a, b tls.CombinatorUnion) int {
valAV4, okA := a.AsV4()
if !okA {
panic("invalid union interpretation for tls.combinator_v4: " + valAV4.String())
}
valBV4, okB := b.AsV4()
if !okB {
panic("invalid union interpretation for tls.combinator_v4: " + valAV4.String())
}
return strings.Compare(valAV4.Id, valBV4.Id)
})
slices.SortFunc(v4.Functions, func(a, b tls.CombinatorUnion) int {
valAV4, okA := a.AsV4()
if !okA {
panic("invalid union interpretation for tls.combinator_v4: " + valAV4.String())
}
valBV4, okB := b.AsV4()
if !okB {
panic("invalid union interpretation for tls.combinator_v4: " + valAV4.String())
}
return strings.Compare(valAV4.Id, valBV4.Id)
})
out, err := v4.WriteJSON(nil)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error on creating json: %v", err)
Expand Down
22 changes: 14 additions & 8 deletions internal/tlast/tlcrc32.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func fieldToCrc32(t TypeRef) string {
}
s.WriteString(t.Type.String())
for _, x := range t.Args {
s.WriteString(" " + aotToCrc32(x))
s.WriteByte(' ')
s.WriteString(aotToCrc32(x))
}
return s.String()
}
Expand Down Expand Up @@ -69,20 +70,24 @@ func (descriptor *Combinator) Crc32() uint32 {
return *descriptor.Construct.ID
}
var s strings.Builder
s.WriteString(descriptor.Construct.Name.String() + " ")
s.WriteString(descriptor.Construct.Name.String())
s.WriteByte(' ')
for _, x := range descriptor.TemplateArguments {
if x.IsNat {
s.WriteString(x.FieldName + ":# ")
s.WriteString(x.FieldName)
s.WriteString(":# ")
} else {
s.WriteString(x.FieldName + ":Type ")
s.WriteString(x.FieldName)
s.WriteString(":Type ")
}
}
if descriptor.Builtin {
s.WriteString("? ")
}
for _, x := range descriptor.Fields {
if x.FieldName != "" {
s.WriteString(x.FieldName + ":")
s.WriteString(x.FieldName)
s.WriteByte(':')
}
if x.Mask != nil {
s.WriteString(x.Mask.String())
Expand All @@ -94,10 +99,11 @@ func (descriptor *Combinator) Crc32() uint32 {
}
s.WriteByte(' ')
}
if descriptor.Modifiers == nil {
s.WriteString("= " + descriptor.TypeDecl.String())
s.WriteString("= ")
if descriptor.IsFunction {
s.WriteString(fieldToCrc32(descriptor.FuncDecl))
} else {
s.WriteString("= " + fieldToCrc32(descriptor.FuncDecl))
s.WriteString(descriptor.TypeDecl.String())
}
// _, err := fmt.Fprintf(os.Stderr, "%s\n%x\n", s.String(), crc32.ChecksumIEEE([]byte(s.String())))
// if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions internal/tlast/tlcrc32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import (
)

func TestCRC32TL(t *testing.T) {
t.Run("Primitives", func(t *testing.T) {
test := []struct {
input string
output uint32
}{
{input: "int#a8509bda ? = Int;", output: 0xa8509bda},
{input: "long#22076cba ? = Long;", output: 0x22076cba},
{input: "float#824dab22 ? = Float;", output: 0x824dab22},
{input: "double#2210c154 ? = Double;", output: 0x2210c154},
{input: "string#b5286e24 ? = String;", output: 0xb5286e24},
}
for _, tst := range test {
tl, err := ParseTL(tst.input)
require.NoError(t, err)
require.Equal(t, tst.output, tl[0].Crc32())
}
})
tests := []struct {
tlText string
tlTag uint32
Expand Down
22 changes: 10 additions & 12 deletions internal/tlast/tlparser_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,18 @@ func parseConstructor(tokens tokenIterator, outer Position, allowBuiltin bool) (
rest := tokens
res := Constructor{NamePR: rest.skipWS(outer)}
var err error
if allowBuiltin {
if rest.checkToken(numberSign) {
res.Name.Name = rest.front().val
rest.expectOrPanic(numberSign)
res.NamePR.End = rest.front().pos
res.IDPR = res.NamePR // wish to highlight name, if tag absent
return res, rest, nil
if allowBuiltin && rest.checkToken(numberSign) {
res.Name.Name = rest.front().val
rest.expectOrPanic(numberSign)
res.NamePR.End = rest.front().pos
res.IDPR = res.NamePR // wish to highlight name, if tag absent
} else {
if res.Name, rest, err = parseLCIdentNS(rest, outer); err != nil {
return Constructor{}, tokens, err // parseErrToken(fmt.Errorf("constructor name expected"), rest.front())
}
res.NamePR.End = rest.front().pos
res.IDPR = res.NamePR // wish to highlight name, if tag absent
}
if res.Name, rest, err = parseLCIdentNS(rest, outer); err != nil {
return Constructor{}, tokens, err // parseErrToken(fmt.Errorf("constructor name expected"), rest.front())
}
res.NamePR.End = rest.front().pos
res.IDPR = res.NamePR // wish to highlight name, if tag absent
if rest.checkToken(crc32hash) {
res.IDPR.Begin = rest.front().pos
i, err := strconv.ParseUint(rest.front().val[1:], 16, 32)
Expand Down
6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/basictl/basictl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/constants/constants.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/factory/factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/a_tlgen_helpers_code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.Combinator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.CombinatorLeft.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.Expr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.NatExpr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.Schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.TypeExpr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.arg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.combinatorRight.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/internal/tls.type.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/meta/meta.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/tlcodegen/gentlo/tlgen2_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tlgen2 version 2022.06.30, hash of source code -
tlgen2 version 2022.06.30, hash of source code - 9aaba04f997419ca831c1bae3b50dfb59c99029226ecd9a6ad6c03826a60014f
6 changes: 6 additions & 0 deletions internal/tlcodegen/gentlo/tltls/tltls.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e2b49b3

Please sign in to comment.