go-tdlib/tlparser/code.go

75 lines
1.4 KiB
Go
Raw Normal View History

2018-10-19 18:06:07 +00:00
package tlparser
import (
2018-10-23 12:49:10 +00:00
"bufio"
"fmt"
"io"
"strings"
2018-10-19 18:06:07 +00:00
)
func ParseCode(reader io.Reader, schema *Schema) error {
2018-10-23 12:49:10 +00:00
var prevLine string
var curLine string
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
userMethods := map[string]bool{}
botMethods := map[string]bool{}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
prevLine = curLine
curLine = scanner.Text()
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
if strings.Contains(curLine, "CHECK_IS_USER();") {
fields := strings.Fields(prevLine)
for _, field := range fields {
var methodName string
n, err := fmt.Sscanf(field, "td_api::%s", &methodName)
if err == nil && n > 0 {
userMethods[methodName] = true
}
}
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
if strings.Contains(curLine, "CHECK_IS_BOT();") {
fields := strings.Fields(prevLine)
for _, field := range fields {
var methodName string
n, err := fmt.Sscanf(field, "td_api::%s", &methodName)
if err == nil && n > 0 {
botMethods[methodName] = true
}
}
}
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
err := scanner.Err()
if err != nil {
return err
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
var ok bool
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
for index, _ := range schema.Functions {
hasType := false
_, ok = userMethods[schema.Functions[index].Name]
if ok {
schema.Functions[index].Type = FUNCTION_TYPE_USER
hasType = true
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
_, ok = botMethods[schema.Functions[index].Name]
if ok {
schema.Functions[index].Type = FUNCTION_TYPE_BOT
hasType = true
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
if !hasType {
schema.Functions[index].Type = FUNCTION_TYPE_COMMON
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
ok = false
}
2018-10-19 18:06:07 +00:00
2018-10-23 12:49:10 +00:00
return nil
2018-10-19 18:06:07 +00:00
}