Skip to content

Commit

Permalink
Support for statements without initializer section
Browse files Browse the repository at this point in the history
Fixes #299
  • Loading branch information
JoeRobich committed Dec 20, 2023
1 parent 0febdc7 commit fada6e2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
2 changes: 1 addition & 1 deletion grammars/csharp.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -4137,7 +4137,7 @@
<array>
<dict>
<key>begin</key>
<string>\G</string>
<string>(?=[^;\)])</string>
<key>end</key>
<string>(?=;|\))</string>
<key>patterns</key>
Expand Down
2 changes: 1 addition & 1 deletion grammars/csharp.tmLanguage.cson
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ repository:
name: "punctuation.parenthesis.close.cs"
patterns: [
{
begin: "\\G"
begin: "(?=[^;\\)])"
end: "(?=;|\\))"
patterns: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/csharp.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ repository:
endCaptures:
0: { name: punctuation.parenthesis.close.cs }
patterns:
- begin: \G
- begin: (?=[^;\)])
end: (?=;|\))
patterns:
- include: '#intrusive'
Expand Down
117 changes: 116 additions & 1 deletion test/statements.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,121 @@ for (int i = 0; i < 42; i++)
]);
});

it("for loop with continue (no initializer)", async () => {
const input = Input.InMethod(`
int i = 0;
for (; i < 10; i++)
{
continue;
}
`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.PrimitiveType.Int,
Token.Identifier.LocalName("i"),
Token.Operator.Assignment,
Token.Literal.Numeric.Decimal("0"),
Token.Punctuation.Semicolon,
Token.Keyword.Loop.For,
Token.Punctuation.OpenParen,
Token.Punctuation.Semicolon,
Token.Variable.ReadWrite("i"),
Token.Operator.Relational.LessThan,
Token.Literal.Numeric.Decimal("10"),
Token.Punctuation.Semicolon,
Token.Variable.ReadWrite("i"),
Token.Operator.Increment,
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Keyword.Flow.Continue,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});

it("for loop with continue (no initializer, no condition)", async () => {
const input = Input.InMethod(`
int i = 0;
for (;; i++)
{
if (i < 10)
continue;
break;
}
`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.PrimitiveType.Int,
Token.Identifier.LocalName("i"),
Token.Operator.Assignment,
Token.Literal.Numeric.Decimal("0"),
Token.Punctuation.Semicolon,
Token.Keyword.Loop.For,
Token.Punctuation.OpenParen,
Token.Punctuation.Semicolon,
Token.Punctuation.Semicolon,
Token.Variable.ReadWrite("i"),
Token.Operator.Increment,
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Keyword.Conditional.If,
Token.Punctuation.OpenParen,
Token.Variable.ReadWrite("i"),
Token.Operator.Relational.LessThan,
Token.Literal.Numeric.Decimal("10"),
Token.Punctuation.CloseParen,
Token.Keyword.Flow.Continue,
Token.Punctuation.Semicolon,
Token.Keyword.Flow.Break,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});

it("for loop with continue (no initializer, no condition, no iterator)", async () => {
const input = Input.InMethod(`
int i = 0;
for (;;)
{
i++;
if (i < 10)
continue;
break;
}
`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.PrimitiveType.Int,
Token.Identifier.LocalName("i"),
Token.Operator.Assignment,
Token.Literal.Numeric.Decimal("0"),
Token.Punctuation.Semicolon,
Token.Keyword.Loop.For,
Token.Punctuation.OpenParen,
Token.Punctuation.Semicolon,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Variable.ReadWrite("i"),
Token.Operator.Increment,
Token.Punctuation.Semicolon,
Token.Keyword.Conditional.If,
Token.Punctuation.OpenParen,
Token.Variable.ReadWrite("i"),
Token.Operator.Relational.LessThan,
Token.Literal.Numeric.Decimal("10"),
Token.Punctuation.CloseParen,
Token.Keyword.Flow.Continue,
Token.Punctuation.Semicolon,
Token.Keyword.Flow.Break,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});

it("for loop with argument multiplication (issue #99)", async () => {
const input = Input.InMethod(`
for (int i = 0; i < n1 * n2; i++)
Expand Down Expand Up @@ -678,7 +793,7 @@ switch (i) {
Token.Punctuation.CloseBrace
]);
});

it("switch statement with comment (issue #145)", async () => {
const input = Input.InMethod(`
switch (i) /* comment */ {
Expand Down

0 comments on commit fada6e2

Please sign in to comment.