Skip to content

Commit

Permalink
feat: support typed class constants
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jan 5, 2024
1 parent 657546d commit 333495e
Show file tree
Hide file tree
Showing 8 changed files with 138,216 additions and 135,791 deletions.
1 change: 1 addition & 0 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ module.exports = function defineGrammar(dialect) {
_const_declaration: $ => seq(
optional($.visibility_modifier),
keyword('const'),
optional(field('type', $._type)),
commaSep1($.const_element),
$._semicolon,
),
Expand Down
37 changes: 37 additions & 0 deletions common/test/corpus/class.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,43 @@ class Foo {
)
)

=========================================
Typed class constants
=========================================

<?php

class Foo {
public const int A = 1;
public const ?array A = [];
public const bool D = false;
}

---

(program
(php_tag)
(class_declaration
(name)
(declaration_list
(const_declaration
(visibility_modifier)
(union_type (primitive_type))
(const_element (name) (integer))
)
(const_declaration
(visibility_modifier)
(union_type (optional_type (primitive_type)))
(const_element (name) (array_creation_expression))
)
(const_declaration
(visibility_modifier)
(union_type (primitive_type))
(const_element (name) (boolean))
)
)
)
)

=========================================
Final class
Expand Down
16 changes: 16 additions & 0 deletions php/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,22 @@
"named": false,
"value": "const"
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "_type"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "SEQ",
"members": [
Expand Down
10 changes: 10 additions & 0 deletions php/src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,16 @@
"named": true
}
]
},
"type": {
"multiple": false,
"required": false,
"types": [
{
"type": "_type",
"named": true
}
]
}
},
"children": {
Expand Down
Loading

0 comments on commit 333495e

Please sign in to comment.