Skip to content

Commit d66b773

Browse files
Merge pull request dotnet#66 from DustinCampbell/issue-60
Ensure that comments are allowed after try, finally, catch, and exception filters
2 parents 37f8a9a + f863f7f commit d66b773

File tree

6 files changed

+166
-4
lines changed

6 files changed

+166
-4
lines changed

grammars/csharp.tmLanguage

+16
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,10 @@
30163016
<string>(?&lt;=\})</string>
30173017
<key>patterns</key>
30183018
<array>
3019+
<dict>
3020+
<key>include</key>
3021+
<string>#comment</string>
3022+
</dict>
30193023
<dict>
30203024
<key>include</key>
30213025
<string>#block</string>
@@ -3038,6 +3042,10 @@
30383042
<string>(?&lt;=\})</string>
30393043
<key>patterns</key>
30403044
<array>
3045+
<dict>
3046+
<key>include</key>
3047+
<string>#comment</string>
3048+
</dict>
30413049
<dict>
30423050
<key>include</key>
30433051
<string>#block</string>
@@ -3128,6 +3136,10 @@
31283136
<key>include</key>
31293137
<string>#when-clause</string>
31303138
</dict>
3139+
<dict>
3140+
<key>include</key>
3141+
<string>#comment</string>
3142+
</dict>
31313143
<dict>
31323144
<key>include</key>
31333145
<string>#block</string>
@@ -3167,6 +3179,10 @@
31673179
<key>include</key>
31683180
<string>#expression</string>
31693181
</dict>
3182+
<dict>
3183+
<key>include</key>
3184+
<string>#comment</string>
3185+
</dict>
31703186
</array>
31713187
</dict>
31723188
<key>checked-unchecked-statement</key>

grammars/csharp.tmLanguage.cson

+12
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ repository:
18481848
name: "keyword.control.try.cs"
18491849
end: "(?<=\\})"
18501850
patterns: [
1851+
{
1852+
include: "#comment"
1853+
}
18511854
{
18521855
include: "#block"
18531856
}
@@ -1859,6 +1862,9 @@ repository:
18591862
name: "keyword.control.try.finally.cs"
18601863
end: "(?<=\\})"
18611864
patterns: [
1865+
{
1866+
include: "#comment"
1867+
}
18621868
{
18631869
include: "#block"
18641870
}
@@ -1916,6 +1922,9 @@ repository:
19161922
{
19171923
include: "#when-clause"
19181924
}
1925+
{
1926+
include: "#comment"
1927+
}
19191928
{
19201929
include: "#block"
19211930
}
@@ -1935,6 +1944,9 @@ repository:
19351944
{
19361945
include: "#expression"
19371946
}
1947+
{
1948+
include: "#comment"
1949+
}
19381950
]
19391951
"checked-unchecked-statement":
19401952
begin: "(?<!\\.)\\b(?:(checked)|(unchecked))\\b\\s*(?!\\()"

src/csharp.tmLanguage.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ repository:
11031103
'1': { name: keyword.control.try.cs }
11041104
end: (?<=\})
11051105
patterns:
1106+
- include: '#comment'
11061107
- include: '#block'
11071108

11081109
finally-clause:
@@ -1111,6 +1112,7 @@ repository:
11111112
'1': { name: keyword.control.try.finally.cs }
11121113
end: (?<=\})
11131114
patterns:
1115+
- include: '#comment'
11141116
- include: '#block'
11151117

11161118
catch-clause:
@@ -1155,6 +1157,7 @@ repository:
11551157
# '5': ?<tuple> is a sub-expression. It's final value is not considered.
11561158
'6': { name: entity.name.variable.local.cs }
11571159
- include: '#when-clause'
1160+
- include: '#comment'
11581161
- include: '#block'
11591162

11601163
when-clause:
@@ -1167,6 +1170,7 @@ repository:
11671170
'0': { name: punctuation.parenthesis.close.cs }
11681171
patterns:
11691172
- include: '#expression'
1173+
- include: '#comment'
11701174

11711175
checked-unchecked-statement:
11721176
begin: (?<!\.)\b(?:(checked)|(unchecked))\b\s*(?!\()

test/comment.tests.ts

+130
Original file line numberDiff line numberDiff line change
@@ -310,5 +310,135 @@ event EventHandler E {
310310
Token.Comment.SingleLine.Text(" comment2"),
311311
Token.Punctuation.CloseBrace]);
312312
});
313+
314+
it("after try (issue #60)", () => {
315+
const input = Input.InMethod(`
316+
try //comment
317+
{
318+
}
319+
finally
320+
{
321+
}
322+
`);
323+
const tokens = tokenize(input);
324+
325+
tokens.should.deep.equal([
326+
Token.Keywords.Control.Try,
327+
Token.Comment.SingleLine.Start,
328+
Token.Comment.SingleLine.Text("comment"),
329+
Token.Punctuation.OpenBrace,
330+
Token.Punctuation.CloseBrace,
331+
Token.Keywords.Control.Finally,
332+
Token.Punctuation.OpenBrace,
333+
Token.Punctuation.CloseBrace]);
334+
});
335+
336+
it("after finally (issue #60)", () => {
337+
const input = Input.InMethod(`
338+
try
339+
{
340+
}
341+
finally //comment
342+
{
343+
}
344+
`);
345+
const tokens = tokenize(input);
346+
347+
tokens.should.deep.equal([
348+
Token.Keywords.Control.Try,
349+
Token.Punctuation.OpenBrace,
350+
Token.Punctuation.CloseBrace,
351+
Token.Keywords.Control.Finally,
352+
Token.Comment.SingleLine.Start,
353+
Token.Comment.SingleLine.Text("comment"),
354+
Token.Punctuation.OpenBrace,
355+
Token.Punctuation.CloseBrace]);
356+
});
357+
358+
it("after catch (issue #60)", () => {
359+
const input = Input.InMethod(`
360+
try
361+
{
362+
}
363+
catch //comment
364+
{
365+
}
366+
`);
367+
const tokens = tokenize(input);
368+
369+
tokens.should.deep.equal([
370+
Token.Keywords.Control.Try,
371+
Token.Punctuation.OpenBrace,
372+
Token.Punctuation.CloseBrace,
373+
Token.Keywords.Control.Catch,
374+
Token.Comment.SingleLine.Start,
375+
Token.Comment.SingleLine.Text("comment"),
376+
Token.Punctuation.OpenBrace,
377+
Token.Punctuation.CloseBrace]);
378+
});
379+
380+
it("after catch with exception (issue #60)", () => {
381+
const input = Input.InMethod(`
382+
try
383+
{
384+
}
385+
catch (Exception) //comment
386+
{
387+
}
388+
`);
389+
const tokens = tokenize(input);
390+
391+
tokens.should.deep.equal([
392+
Token.Keywords.Control.Try,
393+
Token.Punctuation.OpenBrace,
394+
Token.Punctuation.CloseBrace,
395+
Token.Keywords.Control.Catch,
396+
Token.Punctuation.OpenParen,
397+
Token.Type("Exception"),
398+
Token.Punctuation.CloseParen,
399+
Token.Comment.SingleLine.Start,
400+
Token.Comment.SingleLine.Text("comment"),
401+
Token.Punctuation.OpenBrace,
402+
Token.Punctuation.CloseBrace]);
403+
});
404+
405+
it("after exception filter (issue #60)", () => {
406+
const input = Input.InMethod(`
407+
try
408+
{
409+
}
410+
catch (DataNotFoundException dnfe) when (dnfe.GetType() == typeof(DataNotFoundException)) //Only catch exceptions that are distinctly DataNotFoundException
411+
{
412+
}
413+
`);
414+
const tokens = tokenize(input);
415+
416+
tokens.should.deep.equal([
417+
Token.Keywords.Control.Try,
418+
Token.Punctuation.OpenBrace,
419+
Token.Punctuation.CloseBrace,
420+
Token.Keywords.Control.Catch,
421+
Token.Punctuation.OpenParen,
422+
Token.Type("DataNotFoundException"),
423+
Token.Identifiers.LocalName("dnfe"),
424+
Token.Punctuation.CloseParen,
425+
Token.Keywords.Control.When,
426+
Token.Punctuation.OpenParen,
427+
Token.Variables.Object("dnfe"),
428+
Token.Punctuation.Accessor,
429+
Token.Identifiers.MethodName("GetType"),
430+
Token.Punctuation.OpenParen,
431+
Token.Punctuation.CloseParen,
432+
Token.Operators.Relational.Equals,
433+
Token.Keywords.TypeOf,
434+
Token.Punctuation.OpenParen,
435+
Token.Type("DataNotFoundException"),
436+
Token.Punctuation.CloseParen,
437+
Token.Punctuation.CloseParen,
438+
Token.Comment.SingleLine.Start,
439+
Token.Comment.SingleLine.Text("Only catch exceptions that are distinctly DataNotFoundException"),
440+
Token.Punctuation.OpenBrace,
441+
Token.Punctuation.CloseBrace]);
442+
});
313443
});
314444
});

test/statements.tests.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ finally
645645
Token.Keywords.Control.Try,
646646
Token.Punctuation.OpenBrace,
647647
Token.Punctuation.CloseBrace,
648-
Token.Keywords.Finally,
648+
Token.Keywords.Control.Finally,
649649
Token.Punctuation.OpenBrace,
650650
Token.Punctuation.CloseBrace
651651
]);
@@ -691,7 +691,7 @@ finally
691691
Token.Keywords.Control.Catch,
692692
Token.Punctuation.OpenBrace,
693693
Token.Punctuation.CloseBrace,
694-
Token.Keywords.Finally,
694+
Token.Keywords.Control.Finally,
695695
Token.Punctuation.OpenBrace,
696696
Token.Punctuation.CloseBrace
697697
]);
@@ -817,7 +817,7 @@ int x;`);
817817
Token.Keywords.Control.Try,
818818
Token.Punctuation.OpenBrace,
819819
Token.Punctuation.CloseBrace,
820-
Token.Keywords.Finally,
820+
Token.Keywords.Control.Finally,
821821
Token.Punctuation.OpenBrace,
822822
Token.Punctuation.CloseBrace,
823823
Token.PrimitiveType.Int,

test/utils/tokenize.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export namespace Token {
204204
export const Default = createToken('default', 'keyword.control.default.cs');
205205
export const Do = createToken('do', 'keyword.control.loop.do.cs');
206206
export const Else = createToken('else', 'keyword.control.conditional.else.cs');
207+
export const Finally = createToken('finally', 'keyword.control.try.finally.cs');
207208
export const For = createToken('for', 'keyword.control.loop.for.cs');
208209
export const ForEach = createToken('foreach', 'keyword.control.loop.foreach.cs');
209210
export const Goto = createToken('goto', 'keyword.control.goto.cs');
@@ -292,7 +293,6 @@ export namespace Token {
292293
export const Event = createToken('event', 'keyword.other.event.cs');
293294
export const Explicit = createToken('explicit', 'keyword.other.explicit.cs');
294295
export const Extern = createToken('extern', 'keyword.other.extern.cs');
295-
export const Finally = createToken('finally', 'keyword.control.try.finally.cs');
296296
export const Get = createToken('get', 'keyword.other.get.cs');
297297
export const Implicit = createToken('implicit', 'keyword.other.implicit.cs');
298298
export const Interface = createToken('interface', 'keyword.other.interface.cs');

0 commit comments

Comments
 (0)