Switch type argument syntax (#1940)
This changes type argument syntax to "standard" style, and away from turbofish. The motivation here is that turbofish-style type arguments are hard to read, and developers who are not already deeply knowledgeable about Pkl might struggle to understand such code. In most languages, the grammar for "method call with type args" don't introduce any additional symbols between the identifier and the type argument list (e.g. C++, TypeScript, C#, Scala, Go).
This commit is contained in:
1 parent
6ad50eb589
commit
a9b7fe3a33
96 files changed
+1448
-376
No files matched your search
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser;
|
||||
|
||||
final class Constants {
|
||||
private Constants() {}
|
||||
|
||||
/**
|
||||
* Max number of tokens consumed during speculative parsing before failure.
|
||||
*
|
||||
* <p>5 million token budget takes about 300ms before reaching the error case (tested on a MacBook
|
||||
* Pro M4 Max).
|
||||
*/
|
||||
static final int MAX_SPECULATIVE_TOKEN_COUNT = 5_000_000;
|
||||
}
|
||||
@@ -22,12 +22,21 @@ public final class GenericParser {
|
||||
var parser = new GenericParserImpl(source);
|
||||
try {
|
||||
return parser.parseModule();
|
||||
} catch (StackOverflowError e) {
|
||||
} catch (MaxBacktrackCountError err) {
|
||||
throw err.toGenericParserError();
|
||||
} catch (StackOverflowError err) {
|
||||
throw parser.tooDeeplyNested();
|
||||
}
|
||||
}
|
||||
|
||||
public Node parseExpressionInput(String source) {
|
||||
return new GenericParserImpl(source).parseExpressionInput();
|
||||
var parser = new GenericParserImpl(source);
|
||||
try {
|
||||
return parser.parseExpressionInput();
|
||||
} catch (MaxBacktrackCountError err) {
|
||||
throw err.toGenericParserError();
|
||||
} catch (StackOverflowError err) {
|
||||
throw parser.tooDeeplyNested();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,13 @@
|
||||
*/
|
||||
package org.pkl.parser;
|
||||
|
||||
import static org.pkl.parser.Constants.MAX_SPECULATIVE_TOKEN_COUNT;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.pkl.parser.syntax.Operator;
|
||||
@@ -25,6 +29,7 @@ import org.pkl.parser.syntax.generic.FullSpan;
|
||||
import org.pkl.parser.syntax.generic.Node;
|
||||
import org.pkl.parser.syntax.generic.NodeType;
|
||||
import org.pkl.parser.util.ErrorMessages;
|
||||
import org.pkl.parser.util.SortedIntArray;
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
class GenericParserImpl {
|
||||
@@ -35,6 +40,11 @@ class GenericParserImpl {
|
||||
private FullToken _lookahead;
|
||||
private int cursor = 0;
|
||||
private final List<FullToken> tokens = new ArrayList<>();
|
||||
private int speculativeTokenCount = 0;
|
||||
private boolean isSpeculativeParse = false;
|
||||
private final Set<Integer> failedTypeArgAttempts = new HashSet<>();
|
||||
// char indices of `<` that might start a type argument list
|
||||
private @Nullable SortedIntArray typeArgCandidates;
|
||||
|
||||
GenericParserImpl(String source) {
|
||||
this.lexer = new Lexer(source);
|
||||
@@ -745,18 +755,12 @@ class GenericParserImpl {
|
||||
|
||||
private void parseAccess(List<Node> children) {
|
||||
children.add(parseIdentifier());
|
||||
if (lookahead() == Token.DCOLON && noSemicolonInbetween() && _lookahead.newLinesBetween == 0) {
|
||||
children.add(makeTerminal(next()));
|
||||
var typeArgs = tryParseTypeArgumentList();
|
||||
if (typeArgs != null) {
|
||||
// `tryParseTypeArgumentList` guarantees that an argument list follows
|
||||
children.add(typeArgs);
|
||||
ff(children);
|
||||
children.add(parseTypeArgumentList());
|
||||
if (lookahead() == Token.LPAREN
|
||||
&& noSemicolonInbetween()
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
ff(children);
|
||||
children.add(parseArgumentList());
|
||||
} else {
|
||||
throw parserError(ErrorMessages.create("unexpectedToken", "("), spanLookahead);
|
||||
}
|
||||
children.add(parseArgumentList());
|
||||
} else if (lookahead() == Token.LPAREN
|
||||
&& noSemicolonInbetween()
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
@@ -765,6 +769,48 @@ class GenericParserImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable Node tryParseTypeArgumentList() {
|
||||
if (lookahead() != Token.LT
|
||||
|| !noSemicolonInbetween()
|
||||
|| _lookahead.newLinesBetween != 0
|
||||
|| failedTypeArgAttempts.contains(spanLookahead.charIndex())
|
||||
|| !isTypeArgCandidate(spanLookahead.charIndex())) {
|
||||
return null;
|
||||
}
|
||||
var savedCursor = cursor;
|
||||
var savedCharIndex = spanLookahead.charIndex();
|
||||
var savedIsSpeculativeParse = isSpeculativeParse;
|
||||
isSpeculativeParse = true;
|
||||
try {
|
||||
var typeArgs = parseTypeArgumentList();
|
||||
if (lookahead() == Token.LPAREN
|
||||
&& noSemicolonInbetween()
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
return typeArgs;
|
||||
}
|
||||
} catch (GenericParserError ignored) {
|
||||
// not a type argument list
|
||||
} finally {
|
||||
isSpeculativeParse = savedIsSpeculativeParse;
|
||||
}
|
||||
failedTypeArgAttempts.add(savedCharIndex);
|
||||
backtrackTo(savedCursor);
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isTypeArgCandidate(int charIndex) {
|
||||
if (typeArgCandidates == null) {
|
||||
var scanner = new TypeArgumentCandidatesScanner();
|
||||
scanner.addPending(charIndex);
|
||||
for (var i = cursor + 1; i < tokens.size(); i++) {
|
||||
var tk = tokens.get(i);
|
||||
scanner.accept(tk.token, tk.span.charIndex());
|
||||
}
|
||||
typeArgCandidates = scanner.candidates();
|
||||
}
|
||||
return typeArgCandidates.contains(charIndex);
|
||||
}
|
||||
|
||||
private Node parseExprAtom(@Nullable String expectation) {
|
||||
var expr =
|
||||
switch (lookahead) {
|
||||
@@ -1506,6 +1552,12 @@ class GenericParserImpl {
|
||||
}
|
||||
|
||||
private FullToken next() {
|
||||
if (isSpeculativeParse) {
|
||||
if (speculativeTokenCount > MAX_SPECULATIVE_TOKEN_COUNT) {
|
||||
throw new MaxBacktrackCountError(spanLookahead);
|
||||
}
|
||||
speculativeTokenCount++;
|
||||
}
|
||||
var tmp = tokens.get(cursor++);
|
||||
_lookahead = tokens.get(cursor);
|
||||
lookahead = _lookahead.token;
|
||||
@@ -1517,15 +1569,10 @@ class GenericParserImpl {
|
||||
return tokens.get(cursor - 1).token != Token.SEMICOLON;
|
||||
}
|
||||
|
||||
private void backtrack() {
|
||||
var tmp = tokens.get(--cursor);
|
||||
lookahead = tmp.token;
|
||||
spanLookahead = tmp.span;
|
||||
}
|
||||
|
||||
private void backtrackTo(int point) {
|
||||
cursor = point;
|
||||
var tmp = tokens.get(cursor);
|
||||
_lookahead = tmp;
|
||||
lookahead = tmp.token;
|
||||
spanLookahead = tmp.span;
|
||||
}
|
||||
@@ -1611,7 +1658,6 @@ class GenericParserImpl {
|
||||
case ASSIGN -> NodeType.ASSIGN;
|
||||
case AT -> NodeType.AT;
|
||||
case COLON -> NodeType.COLON;
|
||||
case DCOLON -> NodeType.DCOLON;
|
||||
case COMMA -> NodeType.COMMA;
|
||||
case DOT -> NodeType.DOT;
|
||||
case GT -> NodeType.GT;
|
||||
|
||||
@@ -85,6 +85,49 @@ public final class Lexer {
|
||||
return new String(source, offset, size);
|
||||
}
|
||||
|
||||
/** Captures the lexer's current state so that it can later be rewound via {@link #restore}. */
|
||||
public Snapshot snapshot() {
|
||||
var scopes = new ArrayDeque<InterpolationScope>();
|
||||
// `ArrayDeque.push` adds to the front, so iterate in reverse to preserve order.
|
||||
var it = interpolationStack.descendingIterator();
|
||||
while (it.hasNext()) {
|
||||
scopes.push(it.next().copy());
|
||||
}
|
||||
return new Snapshot(
|
||||
cursor,
|
||||
sCursor,
|
||||
line,
|
||||
sLine,
|
||||
col,
|
||||
sCol,
|
||||
lookahead,
|
||||
state,
|
||||
scopes,
|
||||
stringEnded,
|
||||
isEscape,
|
||||
newLinesBetween);
|
||||
}
|
||||
|
||||
public void restore(Snapshot snapshot) {
|
||||
cursor = snapshot.cursor;
|
||||
sCursor = snapshot.sCursor;
|
||||
line = snapshot.line;
|
||||
sLine = snapshot.sLine;
|
||||
col = snapshot.col;
|
||||
sCol = snapshot.sCol;
|
||||
lookahead = snapshot.lookahead;
|
||||
state = snapshot.state;
|
||||
interpolationStack.clear();
|
||||
// copy again so that the snapshot can be restored more than once
|
||||
var it = snapshot.interpolationStack.descendingIterator();
|
||||
while (it.hasNext()) {
|
||||
interpolationStack.push(it.next().copy());
|
||||
}
|
||||
stringEnded = snapshot.stringEnded;
|
||||
isEscape = snapshot.isEscape;
|
||||
newLinesBetween = snapshot.newLinesBetween;
|
||||
}
|
||||
|
||||
public Token next() {
|
||||
sCursor = cursor;
|
||||
sLine = line;
|
||||
@@ -137,12 +180,7 @@ public final class Lexer {
|
||||
case '}' -> Token.RBRACE;
|
||||
case ',' -> Token.COMMA;
|
||||
case '@' -> Token.AT;
|
||||
case ':' -> {
|
||||
if (lookahead == ':') {
|
||||
nextChar();
|
||||
yield Token.DCOLON;
|
||||
} else yield Token.COLON;
|
||||
}
|
||||
case ':' -> Token.COLON;
|
||||
case '+' -> Token.PLUS;
|
||||
case '%' -> Token.MOD;
|
||||
case '[' -> {
|
||||
@@ -864,8 +902,28 @@ public final class Lexer {
|
||||
this.quotes = quotes;
|
||||
this.pounds = pounds;
|
||||
}
|
||||
|
||||
InterpolationScope copy() {
|
||||
var res = new InterpolationScope(quotes, pounds);
|
||||
res.parens = parens;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public record Snapshot(
|
||||
int cursor,
|
||||
int sCursor,
|
||||
int line,
|
||||
int sLine,
|
||||
int col,
|
||||
int sCol,
|
||||
int lookahead,
|
||||
State state,
|
||||
Deque<InterpolationScope> interpolationStack,
|
||||
boolean stringEnded,
|
||||
boolean isEscape,
|
||||
int newLinesBetween) {}
|
||||
|
||||
private enum State {
|
||||
DEFAULT,
|
||||
STRING
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.pkl.parser.syntax.generic.FullSpan;
|
||||
import org.pkl.parser.util.ErrorMessages;
|
||||
|
||||
final class MaxBacktrackCountError extends RuntimeException {
|
||||
|
||||
private final @Nullable Span span;
|
||||
private final @Nullable FullSpan fullSpan;
|
||||
|
||||
public MaxBacktrackCountError(Span span) {
|
||||
this.span = span;
|
||||
this.fullSpan = null;
|
||||
}
|
||||
|
||||
public MaxBacktrackCountError(FullSpan span) {
|
||||
this.span = null;
|
||||
this.fullSpan = span;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return ErrorMessages.create("maxBacktrackCount");
|
||||
}
|
||||
|
||||
public ParserError toParserError() {
|
||||
assert span != null;
|
||||
return new ParserError(getMessage(), span);
|
||||
}
|
||||
|
||||
public GenericParserError toGenericParserError() {
|
||||
assert fullSpan != null;
|
||||
return new GenericParserError(getMessage(), fullSpan);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,9 @@ public final class Parser {
|
||||
var parser = new ParserImpl(source);
|
||||
try {
|
||||
return parser.parseModule();
|
||||
} catch (StackOverflowError e) {
|
||||
} catch (MaxBacktrackCountError err) {
|
||||
throw err.toParserError();
|
||||
} catch (StackOverflowError err) {
|
||||
throw parser.tooDeeplyNested();
|
||||
}
|
||||
}
|
||||
@@ -35,7 +37,9 @@ public final class Parser {
|
||||
var parser = new ParserImpl(source);
|
||||
try {
|
||||
return parser.parseExpressionInput();
|
||||
} catch (StackOverflowError e) {
|
||||
} catch (MaxBacktrackCountError err) {
|
||||
throw err.toParserError();
|
||||
} catch (StackOverflowError err) {
|
||||
throw parser.tooDeeplyNested();
|
||||
}
|
||||
}
|
||||
@@ -44,7 +48,9 @@ public final class Parser {
|
||||
var parser = new ParserImpl(source);
|
||||
try {
|
||||
return parser.parseReplInput();
|
||||
} catch (StackOverflowError e) {
|
||||
} catch (MaxBacktrackCountError err) {
|
||||
throw err.toParserError();
|
||||
} catch (StackOverflowError err) {
|
||||
throw parser.tooDeeplyNested();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,14 @@
|
||||
*/
|
||||
package org.pkl.parser;
|
||||
|
||||
import static org.pkl.parser.Constants.MAX_SPECULATIVE_TOKEN_COUNT;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.pkl.parser.syntax.Annotation;
|
||||
@@ -86,10 +90,10 @@ import org.pkl.parser.syntax.TypeArgumentList;
|
||||
import org.pkl.parser.syntax.TypeParameter;
|
||||
import org.pkl.parser.syntax.TypeParameterList;
|
||||
import org.pkl.parser.util.ErrorMessages;
|
||||
import org.pkl.parser.util.SortedIntArray;
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
final class ParserImpl {
|
||||
|
||||
private final Lexer lexer;
|
||||
private Token lookahead;
|
||||
private Span spanLookahead;
|
||||
@@ -97,8 +101,15 @@ final class ParserImpl {
|
||||
private FullToken prev = new FullToken(Token.SOF, new Span(0, 0), 0);
|
||||
private FullToken _lookahead;
|
||||
private boolean precededBySemicolon = false;
|
||||
private int speculativeTokenCount = 0;
|
||||
private final Set<Integer> failedTypeArgAttempts = new HashSet<>();
|
||||
private boolean isSpeculativeParse = false;
|
||||
private final String source;
|
||||
// char indices of `<` that might start a type argument list
|
||||
private @Nullable SortedIntArray typeArgCandidates;
|
||||
|
||||
ParserImpl(String source) {
|
||||
this.source = source;
|
||||
this.lexer = new Lexer(source);
|
||||
_lookahead = forceNext();
|
||||
lookahead = _lookahead.token;
|
||||
@@ -831,21 +842,12 @@ final class ParserImpl {
|
||||
case DOT, QDOT -> {
|
||||
var rhs = parseIdentifier();
|
||||
var isNullable = op == Operator.QDOT;
|
||||
TypeArgumentList typeArgumentList = null;
|
||||
if (lookahead == Token.DCOLON
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
next();
|
||||
typeArgumentList = parseTypeArgumentList();
|
||||
}
|
||||
var typeArgumentList = tryParseTypeArgumentList();
|
||||
ArgumentList argumentList = null;
|
||||
if (lookahead == Token.LPAREN
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
argumentList = parseArgumentList();
|
||||
} else if (typeArgumentList != null) {
|
||||
// type args but no args
|
||||
throw new ParserError(ErrorMessages.create("unexpectedToken", "("), spanLookahead);
|
||||
}
|
||||
var lastSpan = argumentList != null ? argumentList.span() : rhs.span();
|
||||
expr =
|
||||
@@ -992,25 +994,14 @@ final class ParserImpl {
|
||||
if (lookahead == Token.DOT) {
|
||||
next();
|
||||
var identifier = parseIdentifier();
|
||||
TypeArgumentList typeArgs = null;
|
||||
if (lookahead == Token.DCOLON
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
next();
|
||||
typeArgs = parseTypeArgumentList();
|
||||
}
|
||||
var typeArgs = tryParseTypeArgumentList();
|
||||
if (lookahead == Token.LPAREN
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
var args = parseArgumentList();
|
||||
yield new SuperAccessExpr(identifier, typeArgs, args, start.endWith(args.span()));
|
||||
}
|
||||
if (typeArgs != null) {
|
||||
// type args but no args
|
||||
throw new ParserError(ErrorMessages.create("unexpectedToken", "("), spanLookahead);
|
||||
} else {
|
||||
yield new SuperAccessExpr(identifier, null, null, start.endWith(identifier.span()));
|
||||
}
|
||||
yield new SuperAccessExpr(identifier, null, null, start.endWith(identifier.span()));
|
||||
} else {
|
||||
expect(Token.LBRACK, "unexpectedToken", "[");
|
||||
var exp = parseExpr("]");
|
||||
@@ -1052,13 +1043,7 @@ final class ParserImpl {
|
||||
case STRING_MULTI_START -> parseMultiLineStringLiteralExpr();
|
||||
case IDENTIFIER -> {
|
||||
var identifier = parseIdentifier();
|
||||
TypeArgumentList typeArgs = null;
|
||||
if (lookahead == Token.DCOLON
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
next();
|
||||
typeArgs = parseTypeArgumentList();
|
||||
}
|
||||
var typeArgs = tryParseTypeArgumentList();
|
||||
if (lookahead == Token.LPAREN
|
||||
&& !precededBySemicolon
|
||||
&& _lookahead.newLinesBetween == 0) {
|
||||
@@ -1066,12 +1051,7 @@ final class ParserImpl {
|
||||
yield new UnqualifiedAccessExpr(
|
||||
identifier, typeArgs, args, identifier.span().endWith(args.span()));
|
||||
}
|
||||
if (typeArgs != null) {
|
||||
// type args but no args
|
||||
throw new ParserError(ErrorMessages.create("unexpectedToken", "("), spanLookahead);
|
||||
} else {
|
||||
yield new UnqualifiedAccessExpr(identifier, null, null, identifier.span());
|
||||
}
|
||||
yield new UnqualifiedAccessExpr(identifier, null, null, identifier.span());
|
||||
}
|
||||
case EOF ->
|
||||
throw new ParserError(
|
||||
@@ -1111,17 +1091,10 @@ final class ParserImpl {
|
||||
else if (lookahead == Token.DOT || lookahead == Token.QDOT) {
|
||||
var isNullable = next().token == Token.QDOT;
|
||||
var identifier = parseIdentifier();
|
||||
TypeArgumentList typeArgumentList = null;
|
||||
if (lookahead == Token.DCOLON && !precededBySemicolon && _lookahead.newLinesBetween == 0) {
|
||||
next();
|
||||
typeArgumentList = parseTypeArgumentList();
|
||||
}
|
||||
var typeArgumentList = tryParseTypeArgumentList();
|
||||
ArgumentList argumentList = null;
|
||||
if (lookahead == Token.LPAREN && !precededBySemicolon && _lookahead.newLinesBetween == 0) {
|
||||
argumentList = parseArgumentList();
|
||||
} else if (typeArgumentList != null) {
|
||||
// type args but no args
|
||||
throw new ParserError(ErrorMessages.create("unexpectedToken", "("), spanLookahead);
|
||||
}
|
||||
var lastSpan = argumentList != null ? argumentList.span() : identifier.span();
|
||||
expr =
|
||||
@@ -1603,6 +1576,53 @@ final class ParserImpl {
|
||||
return new TypeArgumentList(pars, start.endWith(end));
|
||||
}
|
||||
|
||||
private @Nullable TypeArgumentList tryParseTypeArgumentList() {
|
||||
if (lookahead != Token.LT
|
||||
|| precededBySemicolon
|
||||
|| _lookahead.newLinesBetween != 0
|
||||
|| failedTypeArgAttempts.contains(spanLookahead.charIndex())
|
||||
|| !isTypeArgCandidate(spanLookahead.charIndex())) {
|
||||
return null;
|
||||
}
|
||||
var lexerSnapshot = lexer.snapshot();
|
||||
var savedLookahead = lookahead;
|
||||
var savedSpanLookahead = spanLookahead;
|
||||
var savedBacktracking = backtracking;
|
||||
var savedPrev = prev;
|
||||
var savedFullLookahead = _lookahead;
|
||||
var savedPrecededBySemicolon = precededBySemicolon;
|
||||
var savedIsSpeculativeParse = isSpeculativeParse;
|
||||
isSpeculativeParse = true;
|
||||
try {
|
||||
var typeArgs = parseTypeArgumentList();
|
||||
if (lookahead == Token.LPAREN && !precededBySemicolon && _lookahead.newLinesBetween == 0) {
|
||||
return typeArgs;
|
||||
}
|
||||
} catch (ParserError ignored) {
|
||||
// not a type argument list
|
||||
} finally {
|
||||
isSpeculativeParse = savedIsSpeculativeParse;
|
||||
}
|
||||
failedTypeArgAttempts.add(savedSpanLookahead.charIndex());
|
||||
lexer.restore(lexerSnapshot);
|
||||
lookahead = savedLookahead;
|
||||
spanLookahead = savedSpanLookahead;
|
||||
backtracking = savedBacktracking;
|
||||
prev = savedPrev;
|
||||
_lookahead = savedFullLookahead;
|
||||
precededBySemicolon = savedPrecededBySemicolon;
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isTypeArgCandidate(int charIndex) {
|
||||
if (typeArgCandidates == null) {
|
||||
var snapshot = lexer.snapshot();
|
||||
typeArgCandidates = TypeArgumentCandidatesScanner.scanFromPending(lexer);
|
||||
lexer.restore(snapshot);
|
||||
}
|
||||
return typeArgCandidates.contains(charIndex);
|
||||
}
|
||||
|
||||
private ArgumentList parseArgumentList() {
|
||||
var start = expect(Token.LPAREN, "unexpectedToken", "(").span;
|
||||
if (lookahead == Token.RPAREN) {
|
||||
@@ -1877,6 +1897,12 @@ final class ParserImpl {
|
||||
}
|
||||
|
||||
private FullToken next() {
|
||||
if (isSpeculativeParse) {
|
||||
if (speculativeTokenCount > MAX_SPECULATIVE_TOKEN_COUNT) {
|
||||
throw new MaxBacktrackCountError(spanLookahead);
|
||||
}
|
||||
speculativeTokenCount++;
|
||||
}
|
||||
if (backtracking) {
|
||||
backtracking = false;
|
||||
lookahead = _lookahead.token;
|
||||
|
||||
@@ -89,7 +89,6 @@ public enum Token {
|
||||
NOT,
|
||||
QUESTION,
|
||||
COLON,
|
||||
DCOLON,
|
||||
ARROW,
|
||||
EQUAL,
|
||||
NOT_EQUAL,
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser;
|
||||
|
||||
import org.pkl.parser.util.IntArray;
|
||||
import org.pkl.parser.util.SortedIntArray;
|
||||
|
||||
/**
|
||||
* Finds all {@code <} tokens might be the start of a method call's type argument list, e.g. the
|
||||
* {@code <Int>} in {@code foo<Int>(5)}.
|
||||
*
|
||||
* <p>A candidate must have balanced {@code <} and {@code >} pairs and followed by {@code (}.
|
||||
*
|
||||
* <p>Candidates are not <i>necessarily</i> starts of type argument lists. For example, {@code
|
||||
* foo<5>()} is not one because {@code 5} is not a type.
|
||||
*
|
||||
* <p>However, this class doesn't know what a type is; this is meant to be a quick scan of the whole
|
||||
* file to help minimize speculative parsing in the underlying parser.
|
||||
*/
|
||||
final class TypeArgumentCandidatesScanner {
|
||||
// marks a `(`, `[`, `{` or string interpolation on the stack; `<` is stored as its char index
|
||||
private static final int BRACKET = -100;
|
||||
|
||||
// char index of every candidate, in the order they were discovered
|
||||
private final IntArray found = new IntArray();
|
||||
// `<` that are still open, and the brackets that enclose them
|
||||
private final IntArray pending = new IntArray();
|
||||
// char index of the `<` matching the most recent `>`, if we haven't seen the next token yet
|
||||
private int currentPending = -1;
|
||||
|
||||
static SortedIntArray scan(Lexer lexer) {
|
||||
var scanner = new TypeArgumentCandidatesScanner();
|
||||
try {
|
||||
while (true) {
|
||||
var token = lexer.next();
|
||||
if (token == Token.EOF) break;
|
||||
scanner.accept(token, lexer.getStartCursor());
|
||||
}
|
||||
} catch (ParserError ignored) {
|
||||
// Lexing error. The parser will report it when it gets there; `<` that are still open at
|
||||
// this point are simply not candidates.
|
||||
}
|
||||
return scanner.candidates();
|
||||
}
|
||||
|
||||
static SortedIntArray scanFromPending(Lexer lexer) {
|
||||
var scanner = new TypeArgumentCandidatesScanner();
|
||||
scanner.pending.push(lexer.getStartCursor());
|
||||
try {
|
||||
while (true) {
|
||||
var token = lexer.next();
|
||||
if (token == Token.EOF) break;
|
||||
scanner.accept(token, lexer.getStartCursor());
|
||||
}
|
||||
} catch (ParserError ignored) {
|
||||
// Lexing error. The parser will report it when it gets there; `<` that are still open at
|
||||
// this point are simply not candidates.
|
||||
}
|
||||
return scanner.candidates();
|
||||
}
|
||||
|
||||
/**
|
||||
* The indices of the {@code <} tokens that might be type arg calls.
|
||||
*
|
||||
* <p>Must only call this after feeding all tokens.
|
||||
*/
|
||||
SortedIntArray candidates() {
|
||||
return new SortedIntArray(found);
|
||||
}
|
||||
|
||||
/** Feeds the next token. Call in source order, for every token including affixes. */
|
||||
void accept(Token token, int charIndex) {
|
||||
if (token.isAffix()) return;
|
||||
if (currentPending >= 0) {
|
||||
if (token == Token.LPAREN) {
|
||||
found.push(currentPending);
|
||||
}
|
||||
currentPending = -1;
|
||||
}
|
||||
switch (token) {
|
||||
case LT -> pending.push(charIndex);
|
||||
case GT -> {
|
||||
// a `>` without a matching `<` at the same level is a comparison operator
|
||||
if (pending.isNotEmpty() && pending.get(pending.size() - 1) != BRACKET) {
|
||||
currentPending = pending.pop();
|
||||
}
|
||||
}
|
||||
case LPAREN, LBRACK, LBRACE, INTERPOLATION_START -> pending.push(BRACKET);
|
||||
case RPAREN, RBRACK, RBRACE -> {
|
||||
// discard unmatched `<` (comparison operators) down to the enclosing bracket
|
||||
while (true) {
|
||||
if (pending.isEmpty() || pending.pop() == BRACKET) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
public void addPending(int charIndex) {
|
||||
pending.push(charIndex);
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,6 @@ public enum NodeType {
|
||||
ASSIGN(NodeKind.TERMINAL),
|
||||
AT(NodeKind.TERMINAL),
|
||||
COLON(NodeKind.TERMINAL),
|
||||
DCOLON(NodeKind.TERMINAL),
|
||||
COMMA(NodeKind.TERMINAL),
|
||||
DOT(NodeKind.TERMINAL),
|
||||
GT(NodeKind.TERMINAL),
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class IntArray {
|
||||
int[] store = new int[16];
|
||||
private int size = 0;
|
||||
|
||||
public void push(int value) {
|
||||
if (size == store.length) {
|
||||
store = Arrays.copyOf(store, size * 2);
|
||||
}
|
||||
store[size++] = value;
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
if (size == 0) return -1;
|
||||
size--;
|
||||
return store[size];
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
public void sort() {
|
||||
Arrays.sort(store, 0, size);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public boolean isNotEmpty() {
|
||||
return size != 0;
|
||||
}
|
||||
|
||||
public int get(int index) {
|
||||
assert index < size;
|
||||
return store[index];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SortedIntArray {
|
||||
private final int[] store;
|
||||
private final int size;
|
||||
|
||||
public SortedIntArray(IntArray array) {
|
||||
array.sort();
|
||||
this.store = array.store;
|
||||
this.size = array.size();
|
||||
}
|
||||
|
||||
public boolean contains(int value) {
|
||||
if (size == 0) return false;
|
||||
if (value < store[0]) return false;
|
||||
if (value > store[size - 1]) return false;
|
||||
return Arrays.binarySearch(store, 0, size, value) >= 0;
|
||||
}
|
||||
|
||||
// for testing only
|
||||
public int[] toArray() {
|
||||
return Arrays.copyOf(store, size);
|
||||
}
|
||||
}
|
||||
@@ -114,3 +114,6 @@ Shebangs are only allowed at the very beginning of a file.
|
||||
|
||||
typeParameterVarianceNotAllowed=\
|
||||
Variance modifiers may only be applied to type parameters on classes or typealiases.
|
||||
|
||||
maxBacktrackCount=\
|
||||
Too much backtracking occurred during parsing.
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.time.Duration
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.assertTimeoutPreemptively
|
||||
|
||||
class GenericParserTest {
|
||||
@Test
|
||||
fun `parser fails if exceeds max backtracking count`() {
|
||||
val code =
|
||||
javaClass
|
||||
.getResourceAsStream("large_backtracking.pkl.txt")!!
|
||||
.readAllBytes()
|
||||
.toString(StandardCharsets.UTF_8)
|
||||
val err = assertThrows<GenericParserError> { GenericParser().parseModule(code) }
|
||||
assertThat(err).hasMessageContaining("Too much backtracking occurred during parsing.")
|
||||
}
|
||||
|
||||
// this is kind of a smoke test to make sure that we parse these cases reasonably quickly.
|
||||
@Test
|
||||
fun `parser succeeds if nested backtracking stays within max depth`() {
|
||||
val code =
|
||||
"""
|
||||
result = a(a < a(a < a(a < a(a < a(a < a(a < a(a < a(a < a(a < 1)))))))))
|
||||
"""
|
||||
.trimIndent()
|
||||
assertTimeoutPreemptively(Duration.ofMillis(10)) { GenericParser().parseModule(code) }
|
||||
}
|
||||
}
|
||||
@@ -21,58 +21,42 @@ import kotlin.io.path.extension
|
||||
import kotlin.io.path.pathString
|
||||
import kotlin.io.path.readText
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.SoftAssertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.DynamicTest
|
||||
import org.junit.jupiter.api.TestFactory
|
||||
import org.junit.jupiter.api.parallel.Execution
|
||||
import org.junit.jupiter.api.parallel.ExecutionMode
|
||||
import org.pkl.commons.walk
|
||||
|
||||
@Execution(ExecutionMode.CONCURRENT)
|
||||
class ParserComparisonTest {
|
||||
|
||||
@Test
|
||||
fun compareSnippetTests() {
|
||||
SoftAssertions.assertSoftly { softly ->
|
||||
getSnippets()
|
||||
.parallelStream()
|
||||
.map { Pair(it.pathString, it.readText()) }
|
||||
.forEach { (path, snippet) ->
|
||||
try {
|
||||
compare(snippet, path, softly)
|
||||
} catch (e: GenericParserError) {
|
||||
softly.fail("path: $path. Message: ${e.message}", e)
|
||||
} catch (e: ParserError) {
|
||||
softly.fail("path: $path. Message: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getSnippets(): List<Path> {
|
||||
return Path("../pkl-core/src/test/files/LanguageSnippetTests/input")
|
||||
.walk()
|
||||
.filter { path ->
|
||||
val pathStr = path.toString().replace("\\", "/")
|
||||
path.extension == "pkl" &&
|
||||
!exceptions.any { pathStr.endsWith(it) } &&
|
||||
!regexExceptions.any { it.matches(pathStr) }
|
||||
@TestFactory
|
||||
fun compareSnippetTests(): List<DynamicTest> {
|
||||
val root = Path("../pkl-core/src/test/files/LanguageSnippetTests/input")
|
||||
return getSnippets().map { path ->
|
||||
DynamicTest.dynamicTest(root.relativize(path).pathString, path.toUri()) {
|
||||
val (sexp, genSexp) = renderBoth(path.readText())
|
||||
assertThat(genSexp).isEqualTo(sexp)
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun compare(code: String, path: String? = null, softly: SoftAssertions? = null) {
|
||||
val (sexp, genSexp) = renderBoth(code)
|
||||
when {
|
||||
(path != null && softly != null) ->
|
||||
softly.assertThat(genSexp).`as`("path: $path").isEqualTo(sexp)
|
||||
else -> assertThat(genSexp).`as`("path: $path").isEqualTo(sexp)
|
||||
}
|
||||
}
|
||||
|
||||
fun renderBoth(code: String): Pair<String, String> =
|
||||
private fun renderBoth(code: String): Pair<String, String> =
|
||||
Pair(renderCode(code), renderGenericCode(code))
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun getSnippets(): List<Path> {
|
||||
return Path("../pkl-core/src/test/files/LanguageSnippetTests/input")
|
||||
.walk()
|
||||
.filter { path ->
|
||||
val pathStr = path.toString().replace("\\", "/")
|
||||
path.extension == "pkl" &&
|
||||
!exceptions.any { pathStr.endsWith(it) } &&
|
||||
!regexExceptions.any { it.matches(pathStr) }
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
private fun renderCode(code: String): String {
|
||||
val parser = Parser()
|
||||
val mod = parser.parseModule(code)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.time.Duration
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.assertTimeoutPreemptively
|
||||
|
||||
class ParserTest {
|
||||
@Test
|
||||
fun `parser fails if exceeds max backtracking count`() {
|
||||
val code =
|
||||
javaClass
|
||||
.getResourceAsStream("large_backtracking.pkl.txt")!!
|
||||
.readAllBytes()
|
||||
.toString(StandardCharsets.UTF_8)
|
||||
val err = assertThrows<ParserError> { Parser().parseModule(code) }
|
||||
assertThat(err).hasMessageContaining("Too much backtracking occurred during parsing.")
|
||||
}
|
||||
|
||||
// this is kind of a smoke test to make sure that we parse these cases reasonably quickly.
|
||||
@Test
|
||||
fun `parser succeeds if nested backtracking stays within max depth`() {
|
||||
val code =
|
||||
"""
|
||||
result = a(a < a(a < a(a < a(a < a(a < a(a < a(a < a(a < a(a < 1)))))))))
|
||||
"""
|
||||
.trimIndent()
|
||||
// 1 second should still catch if we see exponential regressions
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(1)) { Parser().parseModule(code) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pkl.parser
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class TypeArgumentCandidatesScannerTest {
|
||||
private fun candidates(source: String): IntArray =
|
||||
TypeArgumentCandidatesScanner.scan(Lexer(source)).toArray()
|
||||
|
||||
@Test
|
||||
fun `empty input`() {
|
||||
assertThat(candidates("")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `simple method call with type arguments`() {
|
||||
assertThat(candidates("x = foo<Int>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `comparison without closing angle bracket`() {
|
||||
assertThat(candidates("x = a < b")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `closing angle bracket not followed by open paren`() {
|
||||
assertThat(candidates("x = foo<Int> bar")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `less-than-or-equal and greater-than-or-equal are not angle brackets`() {
|
||||
assertThat(candidates("x = a <= b > (c)")).isEmpty()
|
||||
assertThat(candidates("x = a < b >= (c)")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stray greater-than is ignored`() {
|
||||
assertThat(candidates("x = a > (b)")).isEmpty()
|
||||
// the first `>` has no matching `<`; the second one matches
|
||||
assertThat(candidates("x = a > b < c > (d)")).isEqualTo(intArrayOf(10))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nested generics - only the outermost is followed by open paren`() {
|
||||
val source = "x = foo<Map<String, List<Int>>>(5)"
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `multiple candidates`() {
|
||||
assertThat(candidates("x = foo<Int>(bar<String>(1))")).isEqualTo(intArrayOf(7, 16))
|
||||
assertThat(candidates("x = foo<Int>(1)\ny = bar<String>(2)")).isEqualTo(intArrayOf(7, 23))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `function types - arrow is not a closing angle bracket`() {
|
||||
assertThat(candidates("x = foo<(Int) -> String>(f)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `union, nullable and default types`() {
|
||||
assertThat(candidates("x = foo<*Int|String?>(f)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
// constrained types
|
||||
|
||||
@Test
|
||||
fun `greater-than inside a constraint does not close the type argument list`() {
|
||||
assertThat(candidates("x = foo<Int(this > 3)>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `less-than inside a constraint is not a candidate`() {
|
||||
// only the outer `<` is
|
||||
assertThat(candidates("x = foo<Int(this < 3)>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `comparisons in both directions inside a constraint`() {
|
||||
assertThat(candidates("x = foo<Int(this < 3 && this > 1)>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `constraint with a call followed by open paren`() {
|
||||
val source = "x = foo<Int(bar<Int>(this))>(5)"
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(7, 15))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `constraint in a nested type argument`() {
|
||||
assertThat(candidates("x = foo<List<Int(this > 3)>>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `multiple constrained type arguments`() {
|
||||
assertThat(candidates("x = foo<Int(this > 3), String(length < 5)>(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `comparison inside parens does not match a greater-than outside`() {
|
||||
// the `>` after `)` is at a different nesting level than the `<`
|
||||
assertThat(candidates("x = (a < b) > (c)")).isEmpty()
|
||||
}
|
||||
|
||||
// brackets and braces
|
||||
|
||||
@Test
|
||||
fun `comparison inside subscript does not match a greater-than outside`() {
|
||||
assertThat(candidates("x = list[a < b] > (c)")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `comparison inside object body does not match a greater-than outside`() {
|
||||
assertThat(candidates("x = new Foo { y = a < b } > (c)")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `greater-than inside object body does not close an outer less-than`() {
|
||||
assertThat(candidates("x = foo<Foo(new Listing { 1 > 0 }.length > 0)>(5)"))
|
||||
.isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unmatched less-than inside parens is discarded when the paren closes`() {
|
||||
// `<` pushed after `(` must not be left on the stack for later `>`
|
||||
assertThat(candidates("x = (a < b) c > (d)")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stray closing bracket does not break the scan`() {
|
||||
assertThat(candidates(") x = foo<Int>(5)")).isEqualTo(intArrayOf(9))
|
||||
}
|
||||
|
||||
// string interpolation
|
||||
|
||||
@Test
|
||||
fun `comparison inside string interpolation is not a candidate`() {
|
||||
assertThat(candidates("""x = "\(a < b)" > (c)""")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `method call inside string interpolation`() {
|
||||
assertThat(candidates("""x = "hello \(foo<Int>(1)) world"""")).isEqualTo(intArrayOf(16))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `greater-than inside string interpolation does not close an outer less-than`() {
|
||||
assertThat(candidates("""x = foo<"\(a > b)">(5)""")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nested string interpolation`() {
|
||||
assertThat(candidates("""x = "\("\(foo<Int>(1))")"""")).isEqualTo(intArrayOf(13))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `multiline string with interpolation`() {
|
||||
val source = "x = \"\"\"\n \\(foo<Int>(1))\n \"\"\""
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(15))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `angle brackets in string literal text are ignored`() {
|
||||
assertThat(candidates("""x = "a < b > (c)"""")).isEmpty()
|
||||
}
|
||||
|
||||
// affixes between the closing angle bracket and open paren
|
||||
|
||||
@Test
|
||||
fun `comments between closing angle bracket and open paren`() {
|
||||
assertThat(candidates("x = foo<Int> /* c */ (5)")).isEqualTo(intArrayOf(7))
|
||||
assertThat(candidates("x = foo<Int> // c\n(5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `newline and semicolon before open paren - parser re-checks these`() {
|
||||
// these are intentionally lenient; `ParserImpl` and `GenericParserImpl` reject them.
|
||||
assertThat(candidates("x = foo<Int>\n(5)")).isEqualTo(intArrayOf(7))
|
||||
assertThat(candidates("x = foo<Int>; (5)")).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
// robustness
|
||||
|
||||
@Test
|
||||
fun `lexing error ends scan without throwing`() {
|
||||
// candidates found before the error are retained
|
||||
assertThat(candidates("x = foo<Int>(1)\ny = \u0001")).isEqualTo(intArrayOf(7))
|
||||
// `<` still open when the error occurs is not a candidate
|
||||
assertThat(candidates("x = foo<Int \u0001 >(1)")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deeply nested angle brackets`() {
|
||||
val depth = 1000
|
||||
val source = "x = foo" + "<A".repeat(depth) + ">".repeat(depth) + "(1)"
|
||||
// only the outermost `<` has a `>` that is followed by `(`
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(7))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `many nested brackets`() {
|
||||
val depth = 1000
|
||||
val source = "x = " + "(".repeat(depth) + "a < b" + ")".repeat(depth) + " > (c)"
|
||||
assertThat(candidates(source)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `triply nested brackets`() {
|
||||
val source = "x = foo < bar < baz < biz >() >() >()"
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(8, 14, 20))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `triply nested brackets followed by more brackets later`() {
|
||||
val source =
|
||||
"""
|
||||
x = foo < bar < baz < biz > () > () > ()
|
||||
y = foo < bar > ()
|
||||
"""
|
||||
.trimIndent()
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(8, 14, 20, 49))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nested candidate within a non-candidate`() {
|
||||
val source =
|
||||
"""
|
||||
x = a < (foo < A > (1)) > b
|
||||
// ^ this is the candidate
|
||||
y = bar<B>(2)
|
||||
"""
|
||||
.trimIndent()
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(13, 77))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sibling candidates within brackets`() {
|
||||
val source =
|
||||
"""
|
||||
x = a < foo < A > (), bar < B > () > b
|
||||
// ^ this is the candidate
|
||||
y = bar<B>(2)
|
||||
"""
|
||||
.trimIndent()
|
||||
assertThat(candidates(source)).isEqualTo(intArrayOf(12, 26, 88))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result = a<a<a<a<a<a<a<a<a<a<a<a<a<a<a<a<a<a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