Skip to content
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

Add support for scoped ref modifier #311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions grammars/csharp.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -4799,9 +4799,12 @@
<key>begin</key>
<string>(?x)
(?:
(?:(\bref)\s+(?:(\breadonly)\s+)?)?(\bvar\b)| # ref local
(?:(\bscoped)\s+(?:(\bref)\s+)?)? # scoped local
(?:(\bref)\s+(?:(\breadonly)\s+)?)? # ref local
(\bvar\b)|
(?&lt;type_name&gt;
(?:
(?:scoped\s+(?:ref\s+)?)? # scoped local
(?:ref\s+(?:readonly\s+)?)? # ref local
(?:
(?:(?&lt;identifier&gt;@?[_[:alpha:]][_[:alnum:]]*)\s*\:\:\s*)? # alias-qualification
Expand Down Expand Up @@ -4832,19 +4835,29 @@
<key>1</key>
<dict>
<key>name</key>
<string>storage.modifier.ref.cs</string>
<string>storage.modifier.scoped.cs</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>storage.modifier.readonly.cs</string>
<string>storage.modifier.ref.cs</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>storage.type.var.cs</string>
<string>storage.modifier.ref.cs</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>storage.modifier.readonly.cs</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>storage.type.var.cs</string>
</dict>
<key>6</key>
<dict>
<key>patterns</key>
<array>
Expand All @@ -4854,7 +4867,7 @@
</dict>
</array>
</dict>
<key>9</key>
<key>11</key>
<dict>
<key>name</key>
<string>entity.name.variable.local.cs</string>
Expand Down Expand Up @@ -7284,7 +7297,7 @@
<dict>
<key>match</key>
<string>(?x)
(?:(?:\b(ref|params|out|in|this)\b)\s+)?
(?:(?:\b(scoped|ref|params|out|in|this)\b)\s+)?
(?&lt;type_name&gt;
(?:
(?:ref\s+)? # ref return
Expand Down Expand Up @@ -8204,6 +8217,10 @@
<key>include</key>
<string>#comment</string>
</dict>
<dict>
<key>include</key>
<string>#scoped-modifier</string>
</dict>
<dict>
<key>include</key>
<string>#ref-modifier</string>
Expand Down Expand Up @@ -8242,6 +8259,13 @@
</dict>
</array>
</dict>
<key>scoped-modifier</key>
<dict>
<key>name</key>
<string>storage.modifier.scoped.cs</string>
<key>match</key>
<string>\bscoped\b</string>
</dict>
<key>ref-modifier</key>
<dict>
<key>name</key>
Expand Down
25 changes: 19 additions & 6 deletions grammars/csharp.tmLanguage.cson
Original file line number Diff line number Diff line change
Expand Up @@ -2931,9 +2931,12 @@ repository:
begin: '''
(?x)
(?:
(?:(\\bref)\\s+(?:(\\breadonly)\\s+)?)?(\\bvar\\b)| # ref local
(?:(\\bscoped)\\s+(?:(\\bref)\\s+)?)? # scoped local
(?:(\\bref)\\s+(?:(\\breadonly)\\s+)?)? # ref local
(\\bvar\\b)|
(?<type_name>
(?:
(?:scoped\\s+(?:ref\\s+)?)? # scoped local
(?:ref\\s+(?:readonly\\s+)?)? # ref local
(?:
(?:(?<identifier>@?[_[:alpha:]][_[:alnum:]]*)\\s*\\:\\:\\s*)? # alias-qualification
Expand Down Expand Up @@ -2962,18 +2965,22 @@ repository:
'''
beginCaptures:
"1":
name: "storage.modifier.ref.cs"
name: "storage.modifier.scoped.cs"
"2":
name: "storage.modifier.readonly.cs"
name: "storage.modifier.ref.cs"
"3":
name: "storage.type.var.cs"
name: "storage.modifier.ref.cs"
"4":
name: "storage.modifier.readonly.cs"
"5":
name: "storage.type.var.cs"
"6":
patterns: [
{
include: "#type"
}
]
"9":
"11":
name: "entity.name.variable.local.cs"
end: "(?=[;)}])"
patterns: [
Expand Down Expand Up @@ -4393,7 +4400,7 @@ repository:
parameter:
match: '''
(?x)
(?:(?:\\b(ref|params|out|in|this)\\b)\\s+)?
(?:(?:\\b(scoped|ref|params|out|in|this)\\b)\\s+)?
(?<type_name>
(?:
(?:ref\\s+)? # ref return
Expand Down Expand Up @@ -4961,6 +4968,9 @@ repository:
{
include: "#comment"
}
{
include: "#scoped-modifier"
}
{
include: "#ref-modifier"
}
Expand Down Expand Up @@ -4989,6 +4999,9 @@ repository:
include: "#type-pointer-suffix"
}
]
"scoped-modifier":
name: "storage.modifier.scoped.cs"
match: "\\bscoped\\b"
"ref-modifier":
name: "storage.modifier.ref.cs"
match: "\\bref\\b"
Expand Down
32 changes: 21 additions & 11 deletions src/csharp.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1739,9 +1739,12 @@ repository:
begin: |-
(?x)
(?:
(?:(\bref)\s+(?:(\breadonly)\s+)?)?(\bvar\b)| # ref local
(?:(\bscoped)\s+(?:(\bref)\s+)?)? # scoped local
(?:(\bref)\s+(?:(\breadonly)\s+)?)? # ref local
(\bvar\b)|
(?<type_name>
(?:
(?:scoped\s+(?:ref\s+)?)? # scoped local
(?:ref\s+(?:readonly\s+)?)? # ref local
(?:
(?:(?<identifier>@?[_[:alpha:]][_[:alnum:]]*)\s*\:\:\s*)? # alias-qualification
Expand All @@ -1768,17 +1771,19 @@ repository:
(?!=>)
(?=,|;|=|\))
beginCaptures:
1: { name: storage.modifier.ref.cs }
2: { name: storage.modifier.readonly.cs }
3: { name: storage.type.var.cs }
4:
1: { name: storage.modifier.scoped.cs }
2: { name: storage.modifier.ref.cs }
3: { name: storage.modifier.ref.cs }
4: { name: storage.modifier.readonly.cs }
5: { name: storage.type.var.cs }
6:
patterns:
- include: '#type'
# 5: ?<identifier> is a sub-expression. It's final value is not considered.
# 6: ?<name_and_type_args> is a sub-expression. It's final value is not considered.
# 7: ?<type_args> is a sub-expression. It's final value is not considered.
# 8: ?<tuple> is a sub-expression. It's final value is not considered.
9: { name: entity.name.variable.local.cs }
# 7: ?<identifier> is a sub-expression. It's final value is not considered.
# 8: ?<name_and_type_args> is a sub-expression. It's final value is not considered.
# 9: ?<type_args> is a sub-expression. It's final value is not considered.
# 10: ?<tuple> is a sub-expression. It's final value is not considered.
11: { name: entity.name.variable.local.cs }
end: (?=[;)}])
patterns:
- name: entity.name.variable.local.cs
Expand Down Expand Up @@ -2829,7 +2834,7 @@ repository:
parameter:
match: |-
(?x)
(?:(?:\b(ref|params|out|in|this)\b)\s+)?
(?:(?:\b(scoped|ref|params|out|in|this)\b)\s+)?
(?<type_name>
(?:
(?:ref\s+)? # ref return
Expand Down Expand Up @@ -3226,6 +3231,7 @@ repository:
type:
patterns:
- include: '#comment'
- include: '#scoped-modifier'
- include: '#ref-modifier'
- include: '#readonly-modifier'
- include: '#tuple-type'
Expand All @@ -3236,6 +3242,10 @@ repository:
- include: '#type-nullable-suffix'
- include: '#type-pointer-suffix'

scoped-modifier:
name: storage.modifier.scoped.cs
match: \bscoped\b

ref-modifier:
name: storage.modifier.ref.cs
match: \bref\b
Expand Down
73 changes: 73 additions & 0 deletions test/local.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,79 @@ describe("Locals", () => {
]);
});

it("scoped local", async () => {
const input = Input.InMethod(`scoped Span<int> x = stackalloc int[42];`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Scoped,
Token.Type("Span"),
Token.Punctuation.TypeParameter.Begin,
Token.PrimitiveType.Int,
Token.Punctuation.TypeParameter.End,
Token.Identifier.LocalName("x"),
Token.Operator.Assignment,
Token.Operator.Expression.StackAlloc,
Token.PrimitiveType.Int,
Token.Punctuation.OpenBracket,
Token.Literal.Numeric.Decimal("42"),
Token.Punctuation.CloseBracket,
Token.Punctuation.Semicolon
]);
});

it("scoped local var", async () => {
const input = Input.InMethod(`scoped var x = y;`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Scoped,
Token.Keyword.Definition.Var,
Token.Identifier.LocalName("x"),
Token.Operator.Assignment,
Token.Variable.ReadWrite("y"),
Token.Punctuation.Semicolon
]);
});

it("scoped ref local", async () => {
const input = Input.InMethod(`scoped ref Span<int> x = stackalloc int[42];`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Scoped,
Token.Keyword.Modifier.Ref,
Token.Type("Span"),
Token.Punctuation.TypeParameter.Begin,
Token.PrimitiveType.Int,
Token.Punctuation.TypeParameter.End,
Token.Identifier.LocalName("x"),
Token.Operator.Assignment,
Token.Operator.Expression.StackAlloc,
Token.PrimitiveType.Int,
Token.Punctuation.OpenBracket,
Token.Literal.Numeric.Decimal("42"),
Token.Punctuation.CloseBracket,
Token.Punctuation.Semicolon
]);
});

it("scoped ref local var", async () => {
const input = Input.InMethod(`scoped ref var x = ref y;`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Scoped,
Token.Keyword.Modifier.Ref,
Token.Keyword.Definition.Var,
Token.Identifier.LocalName("x"),
Token.Operator.Assignment,
Token.Keyword.Modifier.Ref,
Token.Variable.ReadWrite("y"),
Token.Punctuation.Semicolon
]);
});

it("ref local", async () => {
const input = Input.InMethod(`ref int x;`);
const tokens = await tokenize(input);
Expand Down
61 changes: 61 additions & 0 deletions test/method.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,67 @@ public void LinearRegression(double[,] samples, double[] standardDeviations, int
]);
});

it("ref parameter in parameter list", async () => {
const input = Input.InClass(`
public static Span<T> CreateSpan<T>(ref T reference, int length) {}
`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Public,
Token.Keyword.Modifier.Static,
Token.Type("Span"),
Token.Punctuation.TypeParameter.Begin,
Token.Type("T"),
Token.Punctuation.TypeParameter.End,
Token.Identifier.MethodName("CreateSpan"),
Token.Punctuation.TypeParameter.Begin,
Token.Identifier.TypeParameterName("T"),
Token.Punctuation.TypeParameter.End,
Token.Punctuation.OpenParen,
Token.Keyword.Modifier.Ref,
Token.Type("T"),
Token.Identifier.ParameterName("reference"),
Token.Punctuation.Comma,
Token.PrimitiveType.Int,
Token.Identifier.ParameterName("length"),
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Punctuation.CloseBrace
]);
});

it("scoped ref parameter in parameter list (#306)", async () => {
const input = Input.InClass(`
public static Span<T> CreateSpan<T>(scoped ref T reference, int length) {}
`);
const tokens = await tokenize(input);

tokens.should.deep.equal([
Token.Keyword.Modifier.Public,
Token.Keyword.Modifier.Static,
Token.Type("Span"),
Token.Punctuation.TypeParameter.Begin,
Token.Type("T"),
Token.Punctuation.TypeParameter.End,
Token.Identifier.MethodName("CreateSpan"),
Token.Punctuation.TypeParameter.Begin,
Token.Identifier.TypeParameterName("T"),
Token.Punctuation.TypeParameter.End,
Token.Punctuation.OpenParen,
Token.Keyword.Modifier.Scoped,
Token.Keyword.Modifier.Ref,
Token.Type("T"),
Token.Identifier.ParameterName("reference"),
Token.Punctuation.Comma,
Token.PrimitiveType.Int,
Token.Identifier.ParameterName("length"),
Token.Punctuation.CloseParen,
Token.Punctuation.OpenBrace,
Token.Punctuation.CloseBrace
]);
});

it("expression body and type constraint (issue #74)", async () => {
const input = Input.InClass(`
T id1<T>(T a) => a;
Expand Down
1 change: 1 addition & 0 deletions test/utils/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export namespace Token {
export const ReadOnly = createToken('readonly', 'storage.modifier.readonly.cs');
export const Ref = createToken('ref', 'storage.modifier.ref.cs');
export const Required = createToken('required', 'storage.modifier.required.cs');
export const Scoped = createToken('scoped', 'storage.modifier.scoped.cs');
export const Sealed = createToken('sealed', 'storage.modifier.sealed.cs');
export const Static = createToken('static', 'storage.modifier.static.cs');
export const This = createToken('this', 'storage.modifier.this.cs');
Expand Down
Loading