Skip to content

Commit

Permalink
Merge pull request #265 from wise0704/with
Browse files Browse the repository at this point in the history
Allow comment before initializer
  • Loading branch information
JoeRobich authored Aug 2, 2023
2 parents 7163532 + d830c0f commit 9439e96
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 16 deletions.
18 changes: 13 additions & 5 deletions grammars/csharp.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@
)
(?<interface_name>\g<type_name>\s*\.\s*)?
(?<property_name>\g<identifier>)\s*
(?=\{|=&gt;|$)</string>
(?=\{|=&gt;|//|/\*|$)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -1899,7 +1899,7 @@
)
(?&lt;interface_name&gt;\g&lt;type_name&gt;\s*\.\s*)?
(?&lt;event_names&gt;\g&lt;identifier&gt;(?:\s*,\s*\g&lt;identifier&gt;)*)\s*
(?=\{|;|$)</string>
(?=\{|;|//|/\*|$)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -3024,7 +3024,7 @@
<key>with-expression</key>
<dict>
<key>begin</key>
<string>(?&lt;!\.)\b(with)\b\s*(?=\{|$)</string>
<string>(?&lt;!\.)\b(with)\b\s*(?=\{|//|/\*|$)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -3037,6 +3037,10 @@
<string>(?&lt;=\})</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#comment</string>
</dict>
<dict>
<key>include</key>
<string>#initializer-expression</string>
Expand Down Expand Up @@ -6252,7 +6256,7 @@
)*
)
)\s*
(?=\{|$)</string>
(?=\{|//|/\*|$)</string>
<key>captures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -6331,7 +6335,7 @@
<key>anonymous-object-creation-expression</key>
<dict>
<key>begin</key>
<string>\b(new)\b\s*(?=\{|$)</string>
<string>\b(new)\b\s*(?=\{|//|/\*|$)</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand All @@ -6344,6 +6348,10 @@
<string>(?&lt;=\})</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#comment</string>
</dict>
<dict>
<key>include</key>
<string>#initializer-expression</string>
Expand Down
16 changes: 11 additions & 5 deletions grammars/csharp.tmLanguage.cson
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ repository:
)
(?<interface_name>\\g<type_name>\\s*\\.\\s*)?
(?<property_name>\\g<identifier>)\\s*
(?=\\{|=>|$)
(?=\\{|=>|//|/\\*|$)
'''
beginCaptures:
"1":
Expand Down Expand Up @@ -1227,7 +1227,7 @@ repository:
)
(?<interface_name>\\g<type_name>\\s*\\.\\s*)?
(?<event_names>\\g<identifier>(?:\\s*,\\s*\\g<identifier>)*)\\s*
(?=\\{|;|$)
(?=\\{|;|//|/\\*|$)
'''
beginCaptures:
"1":
Expand Down Expand Up @@ -1888,12 +1888,15 @@ repository:
}
]
"with-expression":
begin: "(?<!\\.)\\b(with)\\b\\s*(?=\\{|$)"
begin: "(?<!\\.)\\b(with)\\b\\s*(?=\\{|//|/\\*|$)"
beginCaptures:
"1":
name: "keyword.other.with.cs"
end: "(?<=\\})"
patterns: [
{
include: "#comment"
}
{
include: "#initializer-expression"
}
Expand Down Expand Up @@ -3763,7 +3766,7 @@ repository:
)*
)
)\\s*
(?=\\{|$)
(?=\\{|//|/\\*|$)
'''
captures:
"1":
Expand Down Expand Up @@ -3818,12 +3821,15 @@ repository:
}
]
"anonymous-object-creation-expression":
begin: "\\b(new)\\b\\s*(?=\\{|$)"
begin: "\\b(new)\\b\\s*(?=\\{|//|/\\*|$)"
beginCaptures:
"1":
name: "keyword.other.new.cs"
end: "(?<=\\})"
patterns: [
{
include: "#comment"
}
{
include: "#initializer-expression"
}
Expand Down
12 changes: 7 additions & 5 deletions src/csharp.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ repository:
)
(?<interface_name>\g<type_name>\s*\.\s*)?
(?<property_name>\g<identifier>)\s*
(?=\{|=>|$)
(?=\{|=>|//|/\*|$)
beginCaptures:
'1':
patterns:
Expand Down Expand Up @@ -691,7 +691,7 @@ repository:
)
(?<interface_name>\g<type_name>\s*\.\s*)?
(?<event_names>\g<identifier>(?:\s*,\s*\g<identifier>)*)\s*
(?=\{|;|$)
(?=\{|;|//|/\*|$)
beginCaptures:
'1': { name: keyword.other.event.cs }
'2':
Expand Down Expand Up @@ -1122,11 +1122,12 @@ repository:
- include: '#punctuation-comma'

with-expression:
begin: (?<!\.)\b(with)\b\s*(?=\{|$)
begin: (?<!\.)\b(with)\b\s*(?=\{|//|/\*|$)
beginCaptures:
'1': { name: keyword.other.with.cs }
end: (?<=\})
patterns:
- include: '#comment'
- include: '#initializer-expression'

switch-pattern:
Expand Down Expand Up @@ -2501,7 +2502,7 @@ repository:
)*
)
)\s*
(?=\{|$)
(?=\{|//|/\*|$)
captures:
'1': { name: keyword.other.new.cs }
'2':
Expand Down Expand Up @@ -2545,11 +2546,12 @@ repository:
- include: '#bracketed-argument-list'

anonymous-object-creation-expression:
begin: \b(new)\b\s*(?=\{|$)
begin: \b(new)\b\s*(?=\{|//|/\*|$)
beginCaptures:
'1': { name: keyword.other.new.cs }
end: (?<=\})
patterns:
- include: '#comment'
- include: '#initializer-expression'

bracketed-parameter-list:
Expand Down
46 changes: 45 additions & 1 deletion test/event.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,49 @@ event EventHandler E
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace]);
});

it("comment before initializer - single line (issue #264)", async () => {
const input = Input.InClass(`event EventHandler Event /* comment */ { add; remove; }`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keywords.Event,
Token.Type("EventHandler"),
Token.Identifiers.EventName("Event"),
Token.Comment.MultiLine.Start,
Token.Comment.MultiLine.Text(" comment "),
Token.Comment.MultiLine.End,
Token.Punctuation.OpenBrace,
Token.Keywords.Add,
Token.Punctuation.Semicolon,
Token.Keywords.Remove,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});

it("comment before initializer - multiple lines (issue #264)", async () => {
const input = Input.InClass(`
event EventHandler Event // comment
{
add;
remove;
}`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keywords.Event,
Token.Type("EventHandler"),
Token.Identifiers.EventName("Event"),
Token.Comment.SingleLine.Start,
Token.Comment.SingleLine.Text(" comment"),
Token.Punctuation.OpenBrace,
Token.Keywords.Add,
Token.Punctuation.Semicolon,
Token.Keywords.Remove,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});
});
});
});
74 changes: 74 additions & 0 deletions test/expressions.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ describe("Expressions", () => {
Token.Punctuation.Semicolon
]);
});

it("comment before initializer (issue #264)", async () => {
const input = Input.InMethod(`
var a = new A // comment
{
X = 1
};`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keywords.Var,
Token.Identifiers.LocalName("a"),
Token.Operators.Assignment,
Token.Keywords.New,
Token.Type("A"),
Token.Comment.SingleLine.Start,
Token.Comment.SingleLine.Text(" comment"),
Token.Punctuation.OpenBrace,
Token.Variables.ReadWrite("X"),
Token.Operators.Assignment,
Token.Literals.Numeric.Decimal("1"),
Token.Punctuation.CloseBrace,
Token.Punctuation.Semicolon,
]);
});
});

describe("Anonymous Methods", () => {
Expand Down Expand Up @@ -1149,6 +1174,30 @@ var x = new
Token.Punctuation.Semicolon
]);
});

it("comment before initializer (issue #264)", async () => {
const input = Input.InMethod(`
var x = new // comment
{
ID = 42
};`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keywords.Var,
Token.Identifiers.LocalName("x"),
Token.Operators.Assignment,
Token.Keywords.New,
Token.Comment.SingleLine.Start,
Token.Comment.SingleLine.Text(" comment"),
Token.Punctuation.OpenBrace,
Token.Variables.ReadWrite("ID"),
Token.Operators.Assignment,
Token.Literals.Numeric.Decimal("42"),
Token.Punctuation.CloseBrace,
Token.Punctuation.Semicolon,
]);
});
});

describe("Arithmetic", () => {
Expand Down Expand Up @@ -4964,6 +5013,31 @@ var p2 = p1 with
Token.Punctuation.Semicolon,
]);
});

it("comment before initializer (issue #264)", async () => {
const input = Input.InMethod(`
var p2 = p1 with // comment
{
X = 5
};`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keywords.Var,
Token.Identifiers.LocalName("p2"),
Token.Operators.Assignment,
Token.Variables.ReadWrite("p1"),
Token.Keywords.With,
Token.Comment.SingleLine.Start,
Token.Comment.SingleLine.Text(" comment"),
Token.Punctuation.OpenBrace,
Token.Variables.ReadWrite("X"),
Token.Operators.Assignment,
Token.Literals.Numeric.Decimal("5"),
Token.Punctuation.CloseBrace,
Token.Punctuation.Semicolon,
]);
});
});
});
});
42 changes: 42 additions & 0 deletions test/property.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,47 @@ public int Timeout
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace]);
});

it("comment before initializer - multiple lines (issue #264)", async () => {
const input = Input.InClass(`
int Property // comment
{
get;
set;
}`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.PrimitiveType.Int,
Token.Identifiers.PropertyName("Property"),
Token.Comment.SingleLine.Start,
Token.Comment.SingleLine.Text(" comment"),
Token.Punctuation.OpenBrace,
Token.Keywords.Get,
Token.Punctuation.Semicolon,
Token.Keywords.Set,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});

it("comment before initializer - single line (issue #264)", async () => {
const input = Input.InClass(`int Property /* comment */ { get; set; }`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.PrimitiveType.Int,
Token.Identifiers.PropertyName("Property"),
Token.Comment.MultiLine.Start,
Token.Comment.MultiLine.Text(" comment "),
Token.Comment.MultiLine.End,
Token.Punctuation.OpenBrace,
Token.Keywords.Get,
Token.Punctuation.Semicolon,
Token.Keywords.Set,
Token.Punctuation.Semicolon,
Token.Punctuation.CloseBrace,
]);
});
});
});

0 comments on commit 9439e96

Please sign in to comment.