forked from natural/java2python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_case.py
More file actions
47 lines (39 loc) · 1.45 KB
/
snake_case.py
File metadata and controls
47 lines (39 loc) · 1.45 KB
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
from java2python.lang.selector import *
astTransforms = [
(Type('NULL'), transform.null2None),
(Type('FALSE'), transform.false2False),
(Type('TRUE'), transform.true2True),
#(Type('IDENT'), transform.keywordSafeIdent),
(Type('IDENT'),camel_to_snake),
(Type('FLOATING_POINT_LITERAL'), transform.syntaxSafeFloatLiteral),
(Type('TYPE') > Type('BOOLEAN'), transform.typeSub),
(Type('TYPE') > Type('BYTE'), transform.typeSub),
(Type('TYPE') > Type('CHAR'), transform.typeSub),
(Type('TYPE') > Type('FLOAT'), transform.typeSub),
(Type('TYPE') > Type('INT'), transform.typeSub),
(Type('TYPE') > Type('SHORT'), transform.typeSub),
(Type('TYPE') > Type('LONG'), transform.typeSub),
(Type('TYPE') > Type('DOUBLE'), transform.typeSub),
(Type('METHOD_CALL') > Type('DOT') > Type('IDENT', 'length'),
transform.lengthToLen),
(Type('METHOD_CALL') > Type('DOT') > (
Type('IDENT', 'String') +
Type('IDENT', 'format')
),
transform.formatString),
(Type('TYPE') > Type('QUALIFIED_TYPE_IDENT') > Type('IDENT'),
transform.typeSub),
(Type('FUNCTIION_METHOD_DECL') > Type('IDENT'),camel_to_snake),
(Type('VOID_METHOD_DECL') > Type('IDENT'),camel_to_snake)
]
def camel_to_snake(node,config):
rawstring = re.findall('[a-z]+|[A-Z][a-z]*',node.token.text)
snake_string = ""
index = 0
for word in rawstring:
if index == 0:
snake_string += word
index = index + 1
else:
snake_string += "_" + word
node.token.text = snake_string