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 "discard" keyword #35

Open
wants to merge 1 commit into
base: master
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
19 changes: 19 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ module.exports = grammar(C, {
...original.members.filter((member) => member.content?.name != '_old_style_function_definition'),
),

_top_level_statement: (_, original) => choice(
Copy link
Member

@theHamsta theHamsta Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this was the exact problem that got a similar change failing in HLSL. Should we really allow discard as a top-level statement? It actually only makes sense within a shader. This was one of the reasons why I didn't integrate the change myself.

What's your opinion on this? Should discard be allowed top-level for improved testing and error-recovery or only within shader functions?

...original.members.concat({
type: "SYMBOL",
name: "discard_statement",
})
),

_non_case_statement: (_, original) => choice(
...original.members.concat({
type: "SYMBOL",
name: "discard_statement",
})
),

function_definition: ($, original) => seq(
optional(
seq(
Expand All @@ -29,6 +43,11 @@ module.exports = grammar(C, {
, original
),

discard_statement: () => seq(
"discard",
";"
),

declaration: ($, original) =>
choice(seq(choice("invariant", "precise"), $.identifier, ";"), seq(
repeat(
Expand Down
21 changes: 21 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,10 @@
{
"type": "SYMBOL",
"name": "goto_statement"
},
{
"type": "SYMBOL",
"name": "discard_statement"
}
]
},
Expand Down Expand Up @@ -5050,6 +5054,10 @@
{
"type": "SYMBOL",
"name": "goto_statement"
},
{
"type": "SYMBOL",
"name": "discard_statement"
}
]
},
Expand Down Expand Up @@ -8497,6 +8505,19 @@
]
}
},
"discard_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "discard"
},
{
"type": "STRING",
"value": ";"
}
]
},
"extension_storage_class": {
"type": "CHOICE",
"members": [
Expand Down
21 changes: 21 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@
"type": "continue_statement",
"named": true
},
{
"type": "discard_statement",
"named": true
},
{
"type": "do_statement",
"named": true
Expand Down Expand Up @@ -916,6 +920,10 @@
"type": "declaration",
"named": true
},
{
"type": "discard_statement",
"named": true
},
{
"type": "do_statement",
"named": true
Expand Down Expand Up @@ -1314,6 +1322,11 @@
]
}
},
{
"type": "discard_statement",
"named": true,
"fields": {}
},
{
"type": "do_statement",
"named": true,
Expand Down Expand Up @@ -3448,6 +3461,10 @@
"type": "declaration",
"named": true
},
{
"type": "discard_statement",
"named": true
},
{
"type": "do_statement",
"named": true
Expand Down Expand Up @@ -4171,6 +4188,10 @@
"type": "defined",
"named": false
},
{
"type": "discard",
"named": false
},
{
"type": "do",
"named": false
Expand Down
24 changes: 24 additions & 0 deletions test/corpus/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,27 @@ layout (input_attachment_index = 1, set = 1, binding = 1) uniform subpassInput i
(number_literal))))
(type_identifier)
(identifier)))

================================================================================
Discard
================================================================================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to run tree-sitter test -u because there seems to be another test using discard, currently parsed as an expression_statement.

void main() {
if (true) return;
if (true) discard;
}
--------------------------------------------------------------------------------
(translation_unit
(function_definition
(primitive_type)
(function_declarator
(identifier)
(parameter_list))
(compound_statement
(if_statement
(parenthesized_expression
(true))
(return_statement))
(if_statement
(parenthesized_expression
(true))
(discard_statement)))))