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

feat: resources can implement interfaces #1755

Merged
merged 17 commits into from
Mar 13, 2023
Merged
14 changes: 14 additions & 0 deletions examples/tests/invalid/impl_interface.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bring cloud;

resource A impl cloud.IQueueOnMessageHandler {
// Error: A does not implement "handle" method of cloud.IQueueOnMessageHandler
init() {}
}

resource B impl cloud.IQueueOnMessageHandler {
init() {}
inflight handle() {
// Error: Expected type to be "inflight (str): void", but got "inflight (): void" instead
return;
}
}
15 changes: 15 additions & 0 deletions examples/tests/valid/impl_interface.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bring cloud;

resource A impl cloud.IQueueOnMessageHandler {
init() {}
inflight handle(msg: str) {
return;
}
}

resource B impl cloud.IQueueOnMessageHandler {
init() {}
inflight handle(msg: str): num { // its okay to return a more specific type
return 5;
}
}
45 changes: 45 additions & 0 deletions libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = grammar({
$.return_statement,
$.class_definition,
$.resource_definition,
$.interface_definition,
$.for_in_loop,
$.while_statement,
$.if_statement,
Expand Down Expand Up @@ -143,6 +144,7 @@ module.exports = grammar({
"class",
field("name", $.identifier),
optional(seq("extends", field("parent", $.custom_type))),
optional(seq("impl", field("implements", commaSep1($.custom_type)))),
field("implementation", $.class_implementation)
),
class_implementation: ($) =>
Expand Down Expand Up @@ -175,6 +177,7 @@ module.exports = grammar({
"resource",
field("name", $.identifier),
optional(seq("extends", field("parent", $.custom_type))),
optional(seq("impl", field("implements", commaSep1($.custom_type)))),
field("implementation", $.resource_implementation)
),
resource_implementation: ($) =>
Expand All @@ -191,6 +194,26 @@ module.exports = grammar({
"}"
),

interface_definition: ($) =>
seq(
"interface",
field("name", $.identifier),
optional(seq("extends", field("implements", commaSep1($.custom_type)))),
field("implementation", $.interface_implementation)
),
interface_implementation: ($) =>
seq(
"{",
repeat(
choice(
$.method_signature,
$.inflight_method_signature,
$.class_field,
)
),
"}"
),

for_in_loop: ($) =>
seq(
"for",
Expand Down Expand Up @@ -370,6 +393,17 @@ module.exports = grammar({
field("block", $.block)
),

method_signature: ($) =>
seq(
optional(field("access_modifier", $.access_modifier)),
optional(field("static", $.static)),
optional(field("async", $.async_modifier)),
field("name", $.identifier),
field("parameter_list", $.parameter_list),
optional(field("return_type", $._type_annotation)),
";"
),

method_definition: ($) =>
seq(
optional(field("access_modifier", $.access_modifier)),
Expand All @@ -381,6 +415,17 @@ module.exports = grammar({
field("block", $.block)
),

inflight_method_signature: ($) =>
seq(
optional(field("access_modifier", $.access_modifier)),
optional(field("static", $.static)),
field("phase_modifier", $._inflight_specifier),
field("name", $.identifier),
field("parameter_list", $.parameter_list),
optional(field("return_type", $._type_annotation)),
";"
),

inflight_method_definition: ($) =>
seq(
optional(field("access_modifier", $.access_modifier)),
Expand Down
1 change: 1 addition & 0 deletions libs/tree-sitter-wing/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"targets": {
"test": {
"dependsOn": ["build"],
"executor": "nx:run-commands",
"options": {
"command": "npm run test",
Expand Down
Loading