Skip to content

Commit

Permalink
Add tryConsume() helper method (#94)
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 authored Dec 1, 2023
1 parent de89241 commit 3b3dbd0
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 @@ -3126,23 +3126,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 @@ -3298,9 +3306,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 3b3dbd0

Please sign in to comment.