Skip to content

Commit

Permalink
Add tryConsume() helper method
Browse files Browse the repository at this point in the history
To avoid issues with unexpected tokens, it would be better if we would try to avoid using `sourceStartsWithAtCursor()`. As an alternative I added `tryConsume()` and `tryConsumeWithPrefix()`.
  • Loading branch information
knutwannheden committed Nov 29, 2023
1 parent 6ab4caf commit 54b8d52
Showing 1 changed file with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ private void visitBinaryUpdateExpression(TSCNode incrementor, List<JRightPadded<
}
Statement r = (Statement) visitNode(incrementor.getNodeProperty("right"));
Space after = whitespace();
if (sourceStartsWithAtCursor(",")) {
consumeToken(TSCSyntaxKind.CommaToken);
} else if (sourceStartsWithAtCursor(")")) {
consumeToken(TSCSyntaxKind.CloseParenToken);
if (tryConsume(TSCSyntaxKind.CommaToken)) {
// TODO check where to add this to LST
} else if (tryConsume(TSCSyntaxKind.CloseParenToken)) {
// TODO check where to add this to LST
}
updates.add(padRight(r, after));
}
Expand Down Expand Up @@ -3110,23 +3110,31 @@ private <T> JRightPadded<T> padRight(T tree, @Nullable Space right, Markers mark
}

private <K2 extends J> JRightPadded<K2> maybeSemicolon(K2 k) {
return tryConsumeWithPrefix(
TSCSyntaxKind.SemicolonToken,
prefix -> new JRightPadded<>(k, prefix, Markers.EMPTY.add(new Semicolon(randomId())))
).orElseGet(() -> JRightPadded.build(k));
}

private boolean tryConsume(TSCSyntaxKind kind) {
int saveCursor = getCursor();
Space beforeSemi = whitespace();
Semicolon semicolon = null;
if (sourceStartsWithAtCursor(";")) {
semicolon = new Semicolon(randomId());
consumeToken(TSCSyntaxKind.SemicolonToken);
if (scan() == kind) {
return true;
} else {
beforeSemi = EMPTY;
cursor(saveCursor);
cursorContext.resetScanner(saveCursor);
return false;
}
}

JRightPadded<K2> padded = JRightPadded.build(k).withAfter(beforeSemi);
if (semicolon != null) {
padded = padded.withMarkers(padded.getMarkers().add(semicolon));
private <T> Optional<T> tryConsumeWithPrefix(TSCSyntaxKind kind, Function<Space, T> whenMatched) {
int saveCursor = getCursor();
Space prefix = whitespace();
if (scan() == kind) {
return Optional.of(whenMatched.apply(prefix));
} else {
cursorContext.resetScanner(saveCursor);
return Optional.empty();
}

return padded;
}

private TSCSyntaxKind scan() {
Expand Down Expand Up @@ -3282,9 +3290,8 @@ private <T extends J> JContainer<T> mapContainer(TSCSyntaxKind open, List<TSCNod
after = sourceBefore(delimiter);
} else if (delimiter == TSCSyntaxKind.CommaToken) {
after = whitespace();
if (sourceStartsWithAtCursor(",")) {
consumeToken(delimiter);
markers = markers.addIfAbsent(new TrailingComma(randomId(), whitespace()));
if (tryConsume(TSCSyntaxKind.CommaToken)) {
markers = markers.add(new TrailingComma(randomId(), whitespace()));
}
} else {
after = whitespace();
Expand Down

0 comments on commit 54b8d52

Please sign in to comment.