Skip to content

Commit 6e7eff2

Browse files
committed
Simplify getKeywordArgumentsDescriptor()
1 parent a33da8e commit 6e7eff2

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,65 +3117,54 @@ private RubyNode addNewlineIfNeeded(ParseNode jrubyNode, RubyNode node) {
31173117

31183118
private static ArgumentsDescriptor getKeywordArgumentsDescriptor(RubyLanguage language, ParseNode[] arguments) {
31193119
// A simple empty set of arguments is always an empty descriptor
3120-
31213120
if (arguments.length == 0) {
31223121
return EmptyArgumentsDescriptor.INSTANCE;
31233122
}
31243123

31253124
// Find the keyword argument hash parse node
3125+
var lastArgument = arguments[arguments.length - 1];
3126+
final HashParseNode keywordHashArgumentNode = findLastHashParseNode(lastArgument);
31263127

3127-
int keywordHashArgumentIndex = 0;
3128-
HashParseNode keywordHashArgumentNode = null;
3129-
3130-
if (arguments[0] instanceof ArgsPushParseNode) {
3131-
final ArgsPushParseNode argsPushParseNode = (ArgsPushParseNode) arguments[0];
3132-
3133-
if (argsPushParseNode.getFirstNode() instanceof HashParseNode) {
3134-
keywordHashArgumentNode = (HashParseNode) argsPushParseNode.getFirstNode();
3135-
} else if (argsPushParseNode.getSecondNode() instanceof HashParseNode) {
3136-
keywordHashArgumentNode = (HashParseNode) argsPushParseNode.getSecondNode();
3137-
}
3138-
} else {
3139-
if (arguments[arguments.length - 1] instanceof HashParseNode) {
3140-
keywordHashArgumentIndex = arguments.length - 1;
3141-
keywordHashArgumentNode = (HashParseNode) arguments[keywordHashArgumentIndex];
3142-
}
3143-
}
3144-
3145-
// If there's no hash parse node of any kind, then there are no keyword arguments
3146-
3147-
if (keywordHashArgumentNode == null) {
3128+
if (keywordHashArgumentNode == null || !keywordHashArgumentNode.isKeywordArguments()) {
31483129
return EmptyArgumentsDescriptor.INSTANCE;
31493130
}
31503131

31513132
final List<String> keywords = new ArrayList<>();
31523133
boolean alsoSplat = false;
31533134

31543135
for (ParseNodeTuple pair : keywordHashArgumentNode.getPairs()) {
3155-
if (pair instanceof ParseNodeTuple) {
3156-
final ParseNode key = pair.getKey();
3157-
final ParseNode value = pair.getValue();
3158-
3159-
if (key instanceof SymbolParseNode &&
3160-
((SymbolParseNode) key).getName() != null) {
3161-
if (keywordHashArgumentNode.isKeywordArguments()) {
3162-
keywords.add(((SymbolParseNode) key).getName());
3163-
}
3164-
} else if (key == null && value != null) {
3165-
// A splat keyword hash
3166-
alsoSplat = true;
3167-
} else {
3168-
// For non-symbol keys
3169-
alsoSplat = true;
3170-
}
3136+
final ParseNode key = pair.getKey();
3137+
final ParseNode value = pair.getValue();
3138+
3139+
if (key instanceof SymbolParseNode &&
3140+
((SymbolParseNode) key).getName() != null) {
3141+
keywords.add(((SymbolParseNode) key).getName());
3142+
} else if (key == null && value != null) {
3143+
// A splat keyword hash
3144+
alsoSplat = true;
3145+
} else {
3146+
// For non-symbol keys
3147+
alsoSplat = true;
31713148
}
31723149
}
31733150

3174-
if (keywords.isEmpty() && !alsoSplat) {
3151+
if (!keywords.isEmpty() || alsoSplat) {
3152+
return KeywordArgumentsDescriptor.INSTANCE;
3153+
} else {
31753154
return EmptyArgumentsDescriptor.INSTANCE;
31763155
}
3156+
}
31773157

3178-
return KeywordArgumentsDescriptor.INSTANCE;
3158+
private static HashParseNode findLastHashParseNode(ParseNode node) {
3159+
if (node instanceof HashParseNode) {
3160+
return (HashParseNode) node;
3161+
} else if (node instanceof ArgsPushParseNode) {
3162+
return findLastHashParseNode(((ArgsPushParseNode) node).getSecondNode());
3163+
} else {
3164+
// SplatParseNode: cannot contain kwargs (*array)
3165+
// ArgsCatParseNode: cannot contain kwargs (RHS is *array)
3166+
return null;
3167+
}
31793168
}
31803169

31813170
@Override

0 commit comments

Comments
 (0)