Skip to content

Fix parsing ambiguity around adjacent > and = tokens #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Parsing errors where adjacent `>` and `=` tokens were wrongly interpreted as the `>=` operator.

## [1.13.0] - 2025-02-05

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ tokens {
TkArgument;
TkAnonymousMethod;
TkAnonymousMethodHeading;
TkLessThanEqual;
TkGreaterThanEqual;
}

@header
Expand Down Expand Up @@ -328,13 +330,19 @@ import org.apache.commons.lang3.StringUtils;
return t;
}

private Token combineLastNTokens(int count) {
private Token combineLastNTokens(int type, int count) {
CommonToken firstToken = (CommonToken) input.LT(-count);
CommonToken lastToken = (CommonToken) input.LT(-1);
lastToken.setType(type);
lastToken.setStartIndex(firstToken.getStartIndex());
return lastToken;
}

private BinaryExpressionNodeImpl createBinaryExpression(Object operator) {
Token token = adaptor.getToken(operator);
return new BinaryExpressionNodeImpl(token);
}

@Override
public void reportError(RecognitionException e) {
String hdr = this.getErrorHeader(e);
Expand Down Expand Up @@ -965,12 +973,21 @@ unaryOperator : NOT<UnaryExpressionNodeImpl>
relationalOperator : '='<BinaryExpressionNodeImpl>
| '>'<BinaryExpressionNodeImpl>
| '<'<BinaryExpressionNodeImpl>
| '<='<BinaryExpressionNodeImpl>
| '>='<BinaryExpressionNodeImpl>
| op=lessThanEqualOperator -> {createBinaryExpression(op.getTree())}
| op=greaterThanEqualOperator -> {createBinaryExpression(op.getTree())}
| '<>'<BinaryExpressionNodeImpl>
| IN<BinaryExpressionNodeImpl>
| IS<BinaryExpressionNodeImpl>
;
// We're only doing this for symmetry with greaterThanEqualOperator. (see comment below)
lessThanEqualOperator : '<' '=' -> ^({combineLastNTokens(TkLessThanEqual, 2)})
;
// We construct the "greater than equal" tokens while parsing binary expressions to preserve the
// individual '>' and '=' tokens in other cases like `const Foo: TArray<Byte>=[1, 2, 3];`, which
// we otherwise couldn't parse since the `>=` token would consume the closing angle bracket of the
// generic type arguments and the const assignment operator.
greaterThanEqualOperator : '>' '=' -> ^({combineLastNTokens(TkGreaterThanEqual, 2)})
;
constExpression : expression
| recordExpression
| arrayExpression
Expand Down Expand Up @@ -1372,8 +1389,6 @@ COLON : ':' ;
EQUAL : '=' ;
NOT_EQUAL : '<>' ;
LESS_THAN : '<' ;
LESS_THAN_EQUAL : '<=' ;
GREATER_THAN_EQUAL : '>=' ;
GREATER_THAN : '>' ;
SQUARE_BRACKET_LEFT : '[' ;
SQUARE_BRACKET_RIGHT : ']' ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,9 @@ void testConditionalAsm() {
void testSemicolonSeparatedGenericArguments() {
assertParsed("SemicolonSeparatedGenericArguments.pas");
}

@Test
void testGreaterThanEqualAmbiguity() {
assertParsed("GreaterThanEqualAmbiguity.pas");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
unit GreaterThanEqualAmbiguity;

interface

type
TFoo = class(TObject)
procedure Bar(Baz: IBaz<string>=nil);
end;

implementation

procedure TFoo.Bar(Baz: IBaz<string>=nil);
begin
// do nothing
end;

function Flarp: TArray<Byte>;
const
Bytes: TArray<Byte>=[1, 2, 3];
begin
Result := Bytes;
end;

function FlimFlam: TArray<Byte>;
begin
const Bytes: TArray<Byte>=[1, 2, 3];
Result := Bytes;
end;

end.