Skip to content

Commit 3be8010

Browse files
committed
More gracefully degrading string formatting syntax translation
1 parent 6a8ded2 commit 3be8010

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

java2python/mod/transform.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,25 @@ def formatString(node, config):
117117
method = dot.parent
118118
arg_list = method.firstChildOfType(tokens.ARGUMENT_LIST)
119119
call_args = [arg for arg in arg_list.childrenOfType(tokens.EXPR)]
120-
121-
format = call_args[0].firstChildOfType(tokens.STRING_LITERAL)
122120
args = [arg.firstChildOfType(tokens.IDENT) for arg in call_args[1:]]
123121

124-
# Translate format syntax
125-
format.token.text = re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefg])',
122+
# Translate format syntax (if format == string_literal)
123+
format = call_args[0].firstChildOfType(tokens.STRING_LITERAL)
124+
if format:
125+
format.token.text = \
126+
re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefg])',
126127
formatSyntaxTransf,
127128
format.token.text,
128129
flags=re.IGNORECASE)
130+
else:
131+
# Translation should happen at runtime
132+
format = call_args[0].firstChild()
133+
if format.type == tokens.IDENT:
134+
# String variable
135+
pass
136+
else:
137+
# Function that returns String
138+
pass
129139

130140
left_ident = dot.children[0]
131141
right_ident = dot.children[1]

0 commit comments

Comments
 (0)