Skip to content

Commit

Permalink
Added syntax for Type guards and it's test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal <[email protected]>
  • Loading branch information
Vishalk91-4 committed Jul 21, 2024
1 parent f789f72 commit 173307c
Show file tree
Hide file tree
Showing 5 changed files with 76,255 additions and 75,128 deletions.
9 changes: 8 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ module.exports = grammar({
$.braces_expression,
$.optional_attribute,
$.optional_item,
$.optional_attribute_declaration,
$.null_coalesce,
$.string_literal_expr,
$.config_expr,
Expand Down Expand Up @@ -803,6 +804,12 @@ module.exports = grammar({
field('attribute', $.identifier),
)),

optional_attribute_declaration: $ => prec(2, seq(
field('attribute', $.identifier),
'?:',
field('type', $.type),
)),

optional_item: $ => prec(PREC.call, seq(
field('object', $.primary_expression),
'?[',
Expand Down Expand Up @@ -1082,4 +1089,4 @@ function commaSep1(rule) {
*/
function sep1(rule, separator) {
return seq(rule, repeat(seq(separator, rule)));
}
}
70 changes: 45 additions & 25 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,49 +421,36 @@
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "argument_list"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
"type": "SYMBOL",
"name": "argument_list"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "dict_expr"
"type": "STRING",
"value": ")"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "dict_expr"
}
]
},
Expand Down Expand Up @@ -1679,6 +1666,10 @@
"type": "SYMBOL",
"name": "optional_item"
},
{
"type": "SYMBOL",
"name": "optional_attribute_declaration"
},
{
"type": "SYMBOL",
"name": "null_coalesce"
Expand Down Expand Up @@ -3214,6 +3205,35 @@
]
}
},
"optional_attribute_declaration": {
"type": "PREC",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "attribute",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": "?:"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "type"
}
}
]
}
},
"optional_item": {
"type": "PREC",
"value": 22,
Expand Down
36 changes: 35 additions & 1 deletion src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@
"type": "optional_attribute",
"named": true
},
{
"type": "optional_attribute_declaration",
"named": true
},
{
"type": "optional_item",
"named": true
Expand Down Expand Up @@ -1674,6 +1678,32 @@
}
}
},
{
"type": "optional_attribute_declaration",
"named": true,
"fields": {
"attribute": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "type",
"named": true
}
]
}
}
},
{
"type": "optional_item",
"named": true,
Expand Down Expand Up @@ -1947,7 +1977,7 @@
},
"children": {
"multiple": true,
"required": false,
"required": true,
"types": [
{
"type": "argument_list",
Expand Down Expand Up @@ -2582,6 +2612,10 @@
"type": "?.",
"named": false
},
{
"type": "?:",
"named": false
},
{
"type": "?[",
"named": false
Expand Down
Loading

0 comments on commit 173307c

Please sign in to comment.