forked from ClarkGuan/jni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
164 lines (137 loc) · 3.51 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package tool
import (
"bytes"
"fmt"
"os"
"regexp"
"strings"
"unicode"
)
var identifier = regexp.MustCompile(`^\w+`)
var constr = []byte("const")
var methodPtr = regexp.MustCompile(`^\(JNICALL\s+\*(\w+)\)`)
func parseIdentifier(code []byte) (id *cType, next []byte, err error) {
if code[0] == ',' || unicode.IsSpace(rune(code[0])) {
next = code[1:]
next = bytes.TrimLeftFunc(next, unicode.IsSpace)
} else {
next = code
}
// 判断是否是 varArgs
if next[0] == '.' && next[1] == '.' && next[2] == '.' {
id = &cType{isVarArgs: true}
next = bytes.TrimLeftFunc(next[3:], unicode.IsSpace)
return
}
loc := identifier.FindIndex(next)
if len(loc) == 0 {
err = fmt.Errorf("%q 找不到标识符1", code)
return
}
id = new(cType)
// 判断是否以 const 开头
if bytes.Equal(next[loc[0]:loc[1]], constr) {
id.isConst = true
next = bytes.TrimLeftFunc(next[loc[1]:], unicode.IsSpace)
if next[0] == '*' {
err = fmt.Errorf("%q 处理不了 const * xxx 的情况", code)
id = nil
return
}
loc = identifier.FindIndex(next)
if len(loc) == 0 {
err = fmt.Errorf("%q 找不到标识符2", code)
return
}
}
id.typeName = string(next[loc[0]:loc[1]])
next = bytes.TrimLeftFunc(next[loc[1]:], unicode.IsSpace)
if next[0] == '*' {
id.isPtr = true
next = bytes.TrimLeftFunc(next[1:], unicode.IsSpace)
}
if next[0] == '*' {
id = nil
err = fmt.Errorf("%q 处理不了多重指针的情况", code)
}
return
}
func parseFuncPointerName(code []byte) (name string, next []byte, err error) {
loc := methodPtr.FindSubmatchIndex(code)
if len(loc) == 0 {
err = fmt.Errorf("%q 找不到函数指针名称", code)
return
} else if len(loc) < 4 {
err = fmt.Errorf("%q 找不到函数指针名称分组", code)
} else {
name = string(code[loc[2]:loc[3]])
}
next = bytes.TrimLeftFunc(code[loc[1]:], unicode.IsSpace)
return
}
func parseFuncParamList(code []byte) (params []*param, next []byte, err error) {
if code[0] == '(' {
next = code[1:]
} else {
next = code
}
var t *cType
var loc []int
for next[0] != ')' {
if t, next, err = parseIdentifier(next); err != nil {
return
}
loc = identifier.FindIndex(next)
// varargs 的情况
if len(loc) == 0 {
if t.isVarArgs && next[0] == ')' {
params = append(params, ¶m{cType: *t})
} else {
err = fmt.Errorf("%q 解析时出现异常", next)
}
break
}
idName := string(next[loc[0]:loc[1]])
// 避免形参名称和 Go 保留字冲突
if idName == "string" {
idName = "str"
}
params = append(params, ¶m{cType: *t, idName: idName})
next = bytes.TrimLeftFunc(next[loc[1]:], unicode.IsSpace)
}
return
}
func parseSingleMethod(code []byte) (m *method, err error) {
// 找返回值
var ret *cType
if ret, code, err = parseIdentifier(code); err != nil {
return
}
// 找函数指针名称
var name string
if name, code, err = parseFuncPointerName(code); err != nil {
return
}
// 找函数参数列表
var params []*param
if params, code, err = parseFuncParamList(code); err != nil {
return
}
return &method{ret: ret, name: name, params: params}, nil
}
func parseJniMethodList(code string) (list []*method) {
lines := strings.Split(code, ";")
for i := range lines {
lines[i] = strings.TrimSpace(lines[i])
if len(lines[i]) > 0 {
if m, err := parseSingleMethod([]byte(lines[i])); err != nil {
fmt.Fprintf(os.Stderr, "// // 匹配 %q\n", lines[i])
fmt.Fprintln(os.Stderr, "// //", err)
} else {
//fmt.Printf("%s\n", m)
list = append(list, m)
}
}
}
return
}