Skip to content

Commit be24caf

Browse files
authored
Merge pull request #300 from dotnet/jorobich/for-statement
Support for loop statements with empty sections
2 parents 238e285 + fada6e2 commit be24caf

File tree

4 files changed

+119
-4
lines changed

4 files changed

+119
-4
lines changed

grammars/csharp.tmLanguage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4155,7 +4155,7 @@
41554155
<array>
41564156
<dict>
41574157
<key>begin</key>
4158-
<string>\G</string>
4158+
<string>(?=[^;\)])</string>
41594159
<key>end</key>
41604160
<string>(?=;|\))</string>
41614161
<key>patterns</key>

grammars/csharp.tmLanguage.cson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ repository:
25442544
name: "punctuation.parenthesis.close.cs"
25452545
patterns: [
25462546
{
2547-
begin: "\\G"
2547+
begin: "(?=[^;\\)])"
25482548
end: "(?=;|\\))"
25492549
patterns: [
25502550
{

src/csharp.tmLanguage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ repository:
14901490
endCaptures:
14911491
0: { name: punctuation.parenthesis.close.cs }
14921492
patterns:
1493-
- begin: \G
1493+
- begin: (?=[^;\)])
14941494
end: (?=;|\))
14951495
patterns:
14961496
- include: '#intrusive'

test/statements.tests.ts

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,121 @@ for (int i = 0; i < 42; i++)
164164
]);
165165
});
166166

167+
it("for loop with continue (no initializer)", async () => {
168+
const input = Input.InMethod(`
169+
int i = 0;
170+
for (; i < 10; i++)
171+
{
172+
continue;
173+
}
174+
`);
175+
const tokens = await tokenize(input);
176+
177+
tokens.should.deep.equal([
178+
Token.PrimitiveType.Int,
179+
Token.Identifier.LocalName("i"),
180+
Token.Operator.Assignment,
181+
Token.Literal.Numeric.Decimal("0"),
182+
Token.Punctuation.Semicolon,
183+
Token.Keyword.Loop.For,
184+
Token.Punctuation.OpenParen,
185+
Token.Punctuation.Semicolon,
186+
Token.Variable.ReadWrite("i"),
187+
Token.Operator.Relational.LessThan,
188+
Token.Literal.Numeric.Decimal("10"),
189+
Token.Punctuation.Semicolon,
190+
Token.Variable.ReadWrite("i"),
191+
Token.Operator.Increment,
192+
Token.Punctuation.CloseParen,
193+
Token.Punctuation.OpenBrace,
194+
Token.Keyword.Flow.Continue,
195+
Token.Punctuation.Semicolon,
196+
Token.Punctuation.CloseBrace,
197+
]);
198+
});
199+
200+
it("for loop with continue (no initializer, no condition)", async () => {
201+
const input = Input.InMethod(`
202+
int i = 0;
203+
for (;; i++)
204+
{
205+
if (i < 10)
206+
continue;
207+
break;
208+
}
209+
`);
210+
const tokens = await tokenize(input);
211+
212+
tokens.should.deep.equal([
213+
Token.PrimitiveType.Int,
214+
Token.Identifier.LocalName("i"),
215+
Token.Operator.Assignment,
216+
Token.Literal.Numeric.Decimal("0"),
217+
Token.Punctuation.Semicolon,
218+
Token.Keyword.Loop.For,
219+
Token.Punctuation.OpenParen,
220+
Token.Punctuation.Semicolon,
221+
Token.Punctuation.Semicolon,
222+
Token.Variable.ReadWrite("i"),
223+
Token.Operator.Increment,
224+
Token.Punctuation.CloseParen,
225+
Token.Punctuation.OpenBrace,
226+
Token.Keyword.Conditional.If,
227+
Token.Punctuation.OpenParen,
228+
Token.Variable.ReadWrite("i"),
229+
Token.Operator.Relational.LessThan,
230+
Token.Literal.Numeric.Decimal("10"),
231+
Token.Punctuation.CloseParen,
232+
Token.Keyword.Flow.Continue,
233+
Token.Punctuation.Semicolon,
234+
Token.Keyword.Flow.Break,
235+
Token.Punctuation.Semicolon,
236+
Token.Punctuation.CloseBrace,
237+
]);
238+
});
239+
240+
it("for loop with continue (no initializer, no condition, no iterator)", async () => {
241+
const input = Input.InMethod(`
242+
int i = 0;
243+
for (;;)
244+
{
245+
i++;
246+
if (i < 10)
247+
continue;
248+
break;
249+
}
250+
`);
251+
const tokens = await tokenize(input);
252+
253+
tokens.should.deep.equal([
254+
Token.PrimitiveType.Int,
255+
Token.Identifier.LocalName("i"),
256+
Token.Operator.Assignment,
257+
Token.Literal.Numeric.Decimal("0"),
258+
Token.Punctuation.Semicolon,
259+
Token.Keyword.Loop.For,
260+
Token.Punctuation.OpenParen,
261+
Token.Punctuation.Semicolon,
262+
Token.Punctuation.Semicolon,
263+
Token.Punctuation.CloseParen,
264+
Token.Punctuation.OpenBrace,
265+
Token.Variable.ReadWrite("i"),
266+
Token.Operator.Increment,
267+
Token.Punctuation.Semicolon,
268+
Token.Keyword.Conditional.If,
269+
Token.Punctuation.OpenParen,
270+
Token.Variable.ReadWrite("i"),
271+
Token.Operator.Relational.LessThan,
272+
Token.Literal.Numeric.Decimal("10"),
273+
Token.Punctuation.CloseParen,
274+
Token.Keyword.Flow.Continue,
275+
Token.Punctuation.Semicolon,
276+
Token.Keyword.Flow.Break,
277+
Token.Punctuation.Semicolon,
278+
Token.Punctuation.CloseBrace,
279+
]);
280+
});
281+
167282
it("for loop with argument multiplication (issue #99)", async () => {
168283
const input = Input.InMethod(`
169284
for (int i = 0; i < n1 * n2; i++)
@@ -678,7 +793,7 @@ switch (i) {
678793
Token.Punctuation.CloseBrace
679794
]);
680795
});
681-
796+
682797
it("switch statement with comment (issue #145)", async () => {
683798
const input = Input.InMethod(`
684799
switch (i) /* comment */ {

0 commit comments

Comments
 (0)