Skip to content

Commit

Permalink
feat: more c# improvements (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
urbit-pilled authored Dec 5, 2024
1 parent 19b572d commit 2caaaf6
Show file tree
Hide file tree
Showing 8 changed files with 2,028 additions and 1,847 deletions.
44 changes: 44 additions & 0 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16300,3 +16300,47 @@ fn csharp_attribute_swap() {
})
.unwrap();
}

#[test]
fn csharp_throw_exception() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`throw $a;` => .
|
|"#
.trim_margin()
.unwrap(),
source: r#"
|try
|{
| result = SafeDivision(a, b);
| Console.WriteLine("{0} divided by {1} = {2}", a, b, result);
|}
|catch (DivideByZeroException)
|{
| Console.WriteLine("Attempted divide by zero.");
| throw new Exception("Something went wrong.");
|}
|"#
.trim_margin()
.unwrap(),
expected: r#"
|try
|{
| result = SafeDivision(a, b);
| Console.WriteLine("{0} divided by {1} = {2}", a, b, result);
|}
|catch (DivideByZeroException)
|{
| Console.WriteLine("Attempted divide by zero.");
|}
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-c-sharp.wasm
Binary file not shown.
18 changes: 9 additions & 9 deletions resources/language-metavariables/tree-sitter-c-sharp/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ module.exports = grammar({
field("attributes", repeat($._attribute_list)),
optional($._parameter_type_with_modifiers),
field("name", $.identifier),
optional(seq("=", $.expression)),
optional(seq("=", field("value", $.expression))),
),

_parameter_array: ($) =>
Expand Down Expand Up @@ -723,11 +723,11 @@ module.exports = grammar({
seq(
choice(field("name", $.identifier), $.tuple_pattern),
optional($.bracketed_argument_list),
optional(seq("=", $.expression)),
optional(seq("=", field("value", $.expression))),
),

using_variable_declarator: ($) =>
seq(field("name", $.identifier), optional(seq("=", $.expression))),
seq(field("name", $.identifier), optional(seq("=", field("value", $.expression)))),

bracketed_argument_list: ($) =>
seq("[", commaSep1(field("argumment", $.argument)), optional(","), "]"),
Expand Down Expand Up @@ -969,10 +969,10 @@ module.exports = grammar({

return_statement: ($) => seq("return", field("expression", optional($.expression)), ";"),

lock_statement: ($) => seq("lock", "(", $.expression, ")", $.statement),
lock_statement: ($) => seq("lock", "(", field("expression", $.expression), ")", field("statement", $.statement)),

yield_statement: ($) =>
seq("yield", choice(seq("return", $.expression), "break"), ";"),
seq("yield", choice(seq("return", field("expression", $.expression)), "break"), ";"),

switch_statement: ($) =>
seq(
Expand Down Expand Up @@ -1001,7 +1001,7 @@ module.exports = grammar({
),
),

throw_statement: ($) => seq("throw", optional($.expression), ";"),
throw_statement: ($) => seq("throw", optional(field("expression", $.expression)), ";"),

try_statement: ($) =>
seq(
Expand Down Expand Up @@ -1554,11 +1554,11 @@ module.exports = grammar({
switch_expression_arm: ($) =>
seq(field("pattern", $.pattern), field("when", optional($.when_clause)), $.switch_arrow, field("expression", $.expression)),

when_clause: ($) => seq("when", $.expression),
when_clause: ($) => seq("when", field("expression", $.expression)),

await_expression: ($) => prec.right(PREC.UNARY, seq("await", $.expression)),
await_expression: ($) => prec.right(PREC.UNARY, seq("await", field("expression", $.expression))),

throw_expression: ($) => seq("throw", $.expression),
throw_expression: ($) => seq("throw", field("exception", $.expression)),

element_access_expression: ($) =>
prec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3029,8 +3029,12 @@
"value": "="
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand Down Expand Up @@ -3534,8 +3538,12 @@
"value": "="
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand Down Expand Up @@ -3568,8 +3576,12 @@
"value": "="
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand Down Expand Up @@ -4970,16 +4982,24 @@
"value": "("
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "STRING",
"value": ")"
},
{
"type": "SYMBOL",
"name": "statement"
"type": "FIELD",
"name": "statement",
"content": {
"type": "SYMBOL",
"name": "statement"
}
}
]
},
Expand All @@ -5001,8 +5021,12 @@
"value": "return"
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand Down Expand Up @@ -5184,8 +5208,12 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
},
{
"type": "BLANK"
Expand Down Expand Up @@ -8575,8 +8603,12 @@
"value": "when"
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand All @@ -8591,8 +8623,12 @@
"value": "await"
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "expression",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
}
Expand All @@ -8605,8 +8641,12 @@
"value": "throw"
},
{
"type": "SYMBOL",
"name": "expression"
"type": "FIELD",
"name": "exception",
"content": {
"type": "SYMBOL",
"name": "expression"
}
}
]
},
Expand Down
Loading

0 comments on commit 2caaaf6

Please sign in to comment.