FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
fklang/fkl_cli/src/deconstruct/java_construct.rs at master · feakin/fklang · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
feakin
/
fklang
Public
Notifications
You must be signed in to change notification settings
Fork
8
Star
115
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
fklang
/
fkl_cli
/
src
/
deconstruct
/
java_construct.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
290 lines (249 loc) · 7.68 KB
Breadcrumbs
fklang
/
fkl_cli
/
src
/
deconstruct
/
java_construct.rs
Copy path
File metadata and controls
290 lines (249 loc) · 7.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use
tree_sitter
::
{
Node
,
Parser
,
Query
,
QueryCursor
}
;
use
crate
::
code_meta
::
{
CodeClass
,
CodeFile
}
;
use
crate
::
deconstruct
::
code_construct
::
CodeConstruct
;
const
JAVA_QUERY
:
&
'
static
str
=
"
(package_declaration
(scoped_identifier) @package-name)
(import_declaration
(scoped_identifier) @import-name)
(program
(class_declaration
name: (identifier) @class-name
interfaces: (super_interfaces (interface_type_list (type_identifier) @impl-name))?
body: (class_body (method_declaration
(modifiers
(annotation
name: (identifier) @annotation.name
arguments: (annotation_argument_list)? @annotation.key_values
)
)?
type: (type_identifier) @return-type
name: (identifier) @function-name
parameters: (formal_parameters (formal_parameter
type: (type_identifier) @param-type
name: (identifier) @param-name
))?
))?
)
)"
;
pub
struct
JavaConstruct
{
parser
:
Parser
,
query
:
Query
,
}
impl
JavaConstruct
{
pub
fn
new
(
)
->
JavaConstruct
{
let
mut
parser =
Parser
::
new
(
)
;
let
language = tree_sitter_java
::
language
(
)
;
parser
.
set_language
(
language
)
.
unwrap
(
)
;
let
query =
Query
::
new
(
language
,
&
JAVA_QUERY
)
.
map_err
(
|e|
println
!
(
"{}"
,
format!
(
"Query compilation failed: {:?}"
,
e
)
)
)
.
unwrap
(
)
;
JavaConstruct
{
parser
,
query
,
}
}
}
impl
CodeConstruct
for
JavaConstruct
{
fn
parse
(
code
:
&
str
)
->
CodeFile
{
let
mut
ident =
JavaConstruct
::
new
(
)
;
JavaConstruct
::
do_parse
(
&
code
,
&
mut
ident
)
}
}
impl
JavaConstruct
{
fn
do_parse
(
code
:
&
&
str
,
ident
:
&
mut
JavaConstruct
)
->
CodeFile
{
let
tree = ident
.
parser
.
parse
(
code
,
None
)
.
unwrap
(
)
;
let
text_callback = |
n
:
Node
|
&
code
[
n
.
byte_range
(
)
]
;
let
mut
query_cursor =
QueryCursor
::
new
(
)
;
let
captures = query_cursor
.
captures
(
&
ident
.
query
,
tree
.
root_node
(
)
,
text_callback
)
;
let
mut
code_file =
CodeFile
::
default
(
)
;
let
mut
is_last_node =
false
;
let
mut
last_class =
CodeClass
::
default
(
)
;
let
capture_names = ident
.
query
.
capture_names
(
)
;
let
mut
last_type =
"void"
.
to_string
(
)
;
for
(
mat
,
capture_index
)
in
captures
{
let
capture = mat
.
captures
[
capture_index
]
;
let
capture_name =
&
capture_names
[
capture
.
index
as
usize
]
;
let
text = capture
.
node
.
utf8_text
(
(
&
code
)
.
as_ref
(
)
)
.
unwrap_or
(
""
)
;
match
capture_name
.
as_str
(
)
{
"package-name"
=>
{
code_file
.
package
= text
.
to_string
(
)
;
}
"import-name"
=>
{
code_file
.
imports
.
push
(
text
.
to_string
(
)
)
;
}
"class-name"
=>
{
if
!last_class
.
name
.
is_empty
(
)
{
last_class =
CodeClass
::
default
(
)
;
}
last_class
.
name
= text
.
to_string
(
)
;
last_class
.
package
= code_file
.
package
.
clone
(
)
;
let
class_node = capture
.
node
.
parent
(
)
.
unwrap
(
)
;
JavaConstruct
::
insert_location
(
&
mut
last_class
,
class_node
)
;
if
!is_last_node
{
is_last_node =
true
;
}
}
"impl-name"
=>
{
last_class
.
implements
.
push
(
text
.
to_string
(
)
)
;
}
"function-name"
=>
{
let
mut
function =
JavaConstruct
::
insert_function
(
capture
,
text
)
;
function
.
return_type
= last_type
.
clone
(
)
;
last_class
.
functions
.
push
(
function
)
;
}
"return-type"
=>
{
last_type = text
.
to_string
(
)
;
}
"parameter"
=>
{
}
&
_ =>
{
// println!(
// " pattern: {}, capture: {}, row: {}, text: {:?}",
// mat.pattern_index,
// capture_name,
// capture.node.start_position().row,
// capture.node.utf8_text((&code).as_ref()).unwrap_or("")
// );
}
}
}
if
is_last_node
{
code_file
.
classes
.
push
(
last_class
.
clone
(
)
)
;
}
code_file
}
}
#
[
cfg
(
test
)
]
mod
tests
{
use
crate
::
code_meta
::
{
CodeClass
,
CodeFunction
,
CodePoint
}
;
use
crate
::
deconstruct
::
code_construct
::
CodeConstruct
;
use
crate
::
deconstruct
::
java_construct
::
JavaConstruct
;
#
[
test
]
fn
should_parse_import
(
)
{
let
source_code =
"import java.lang.System;
import java.io.InputStream;
import payroll.Employee;
"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
3
,
file
.
imports
.
len
(
)
)
;
}
#
[
test
]
fn
should_parse_impl_java_class
(
)
{
let
source_code =
"class DateTimeImpl implements DateTime {
@Override
public Date getDate() {
return new Date();
}
}"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
1
,
file
.
classes
.
len
(
)
)
;
assert_eq
!
(
"DateTimeImpl"
,
file
.
classes
[
0
]
.
name
)
;
}
#
[
test
]
fn
should_parse_normal_java_class
(
)
{
let
source_code =
"class DateTimeImpl {
public Date getDate() {
return new Date();
}
}"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
1
,
file
.
classes
.
len
(
)
)
;
assert_eq
!
(
"DateTimeImpl"
,
file
.
classes
[
0
]
.
name
)
;
}
#
[
test
]
fn
should_parse_multiple_java_class
(
)
{
let
source_code =
"class DateTimeImpl {
}
"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
1
,
file
.
classes
.
len
(
)
)
;
assert_eq
!
(
"DateTimeImpl"
,
file
.
classes
[
0
]
.
name
)
;
}
#
[
test
]
fn
should_support_package_name
(
)
{
let
source_code =
"package com.phodal.pepper.powermock;
"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
"com.phodal.pepper.powermock"
,
file
.
package
)
;
}
#
[
test
]
fn
should_support_inner_class
(
)
{
let
source_code =
"class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
1
,
file
.
classes
.
len
(
)
)
;
}
#
[
test
]
fn
get_location_from_class
(
)
{
let
source_code =
"class DateTimeImpl {
public Date getDate() {
return new Date();
}
}"
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
file
.
classes
[
0
]
,
CodeClass
{
name
:
"DateTimeImpl"
.
to_string
(
)
,
path
:
""
.
to_string
(
)
,
module
:
""
.
to_string
(
)
,
package
:
""
.
to_string
(
)
,
implements
:
vec!
[
]
,
functions
:
vec!
[
CodeFunction
{
name
:
"getDate"
.
to_string
(
)
,
return_type
:
"Date"
.
to_string
(
)
,
variable
:
vec!
[
]
,
start
:
CodePoint
{
row
:
1
,
column
:
4
}
,
end
:
CodePoint
{
row
:
3
,
column
:
5
}
,
}
]
,
start
:
CodePoint
{
row
:
0
,
column
:
0
}
,
end
:
CodePoint
{
row
:
4
,
column
:
1
}
,
}
)
;
}
#
[
test
]
fn
hello_world_controller
(
)
{
let
source_code =
r#"@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
"#
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
file
.
classes
.
len
(
)
,
1
)
;
assert_eq
!
(
file
.
classes
[
0
]
,
CodeClass
{
name
:
"HelloController"
.
to_string
(
)
,
path
:
""
.
to_string
(
)
,
module
:
""
.
to_string
(
)
,
package
:
""
.
to_string
(
)
,
implements
:
vec!
[
]
,
functions
:
vec!
[
CodeFunction
{
name
:
"index"
.
to_string
(
)
,
return_type
:
"String"
.
to_string
(
)
,
variable
:
vec!
[
]
,
start
:
CodePoint
{
row
:
3
,
column
:
1
}
,
end
:
CodePoint
{
row
:
6
,
column
:
2
}
,
}
]
,
start
:
CodePoint
{
row
:
0
,
column
:
0
}
,
end
:
CodePoint
{
row
:
8
,
column
:
1
}
,
}
)
;
}
#
[
test
]
fn
two_methods
(
)
{
let
source_code =
r#"@RestController
public class HelloController {
@GetMapping("/")
public String index() { return "Greetings from Spring Boot!"; }
@GetMapping("/")
public String second() { return "Greetings from Spring Boot!"; }
}
"#
;
let
file =
JavaConstruct
::
parse
(
source_code
)
;
assert_eq
!
(
file
.
classes
.
len
(
)
,
1
)
;
let
first_class =
&
file
.
classes
[
0
]
;
assert
!
(
first_class
.
is_contain_method
(
"index"
)
)
;
assert
!
(
first_class
.
is_contain_method
(
"second"
)
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL