Skip to content

Commit

Permalink
[PHP] Add support for shorter attribute syntax "@@"
Browse files Browse the repository at this point in the history
https://wiki.php.net/rfc/shorter_attribute_syntax

Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Jun 22, 2020
1 parent b76caad commit dabc21e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 148 deletions.
79 changes: 31 additions & 48 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ contexts:
captures:
1: storage.type.trait.php
2: entity.name.trait.php
- include: attributes
- include: typed-property
- include: function
- match: '^\s*((?!default|else){{identifier}})\s*(:)(?!:)'
Expand Down Expand Up @@ -112,6 +111,7 @@ contexts:
- include: statements

expressions:
- include: attributes
- include: comments
- match: (?i)\b((?:require|include)(?:_once)?)\b\s*
captures:
Expand Down Expand Up @@ -143,6 +143,7 @@ contexts:
1: keyword.control.exception.catch.php
push:
- meta_scope: meta.catch.php
- include: attributes
- include: comments
- include: identifier-parts-as-path
- match: '(\\)'
Expand Down Expand Up @@ -355,55 +356,23 @@ contexts:

attributes:
# https://wiki.php.net/rfc/attributes_v2
- match: (?=<<(?![<=]))
branch_point: branch-point-attributes
branch:
- is-attributes-branch
- not-attributes-branch

attributes-in-function-parameter:
# https://wiki.php.net/rfc/attributes_v2 (for function parameter)
- match: (?=<<(?![<=]))
branch_point: branch-point-attributes-in-function-parameter
branch:
- is-attributes-in-function-parameter-branch
- not-attributes-branch

is-attributes-branch:
- match: '<<(?={{path}})'
scope: punctuation.definition.attribute.begin.php
# https://wiki.php.net/rfc/attribute_amendments
# https://wiki.php.net/rfc/shorter_attribute_syntax
- match: '@@(?=[\\\w])'
scope: punctuation.definition.attribute.php
push:
- meta_scope: meta.attribute.php
# [</#] is not fully quailified in order to not flicker while typing
- match: '>>(?=\s*(?:$|[</#]|\b(?:const|class|function|fn|final|abstract|{{visibility_modifier}}|static)\b))'
scope: punctuation.definition.attribute.end.php
pop: 2 # branch successful matched
- match: (?=[<>=)\]},;]|\s*\?>)
pop: true
- include: class-name
- include: expressions
- match: ''
fail: branch-point-attributes

is-attributes-in-function-parameter-branch:
- match: '<<(?={{path}})'
scope: punctuation.definition.attribute.begin.php
push:
- meta_scope: meta.attribute.php
- match: '>>'
scope: punctuation.definition.attribute.end.php
pop: 2 # branch successful matched
- match: (?=[<>=)\]},;]|\s*\?>)
- match: \(
scope: punctuation.section.group.begin.php
set:
- meta_scope: meta.attribute.php meta.group.php
- match: \)
scope: punctuation.section.group.end.php
pop: true
- include: expressions
- match: ''
pop: true
- include: class-name
- include: expressions
- match: ''
fail: branch-point-attributes-in-function-parameter

not-attributes-branch:
- match: '<<'
scope: keyword.operator.bitwise.php
pop: true

class-builtin:
- match: |-
Expand Down Expand Up @@ -461,6 +430,7 @@ contexts:
pop: true

use-statement-common:
- include: attributes
- include: comments
- match: (?i)\bas\b
scope: keyword.other.use-as.php
Expand All @@ -470,6 +440,7 @@ contexts:
use-statement-identifier-class-def:
- match: '(?={{path}})'
push:
- include: attributes
- include: comments
# We are going to assume the alias is for a class so that the identifier
# gets put into the index. This will be incorrect sometimes (because it
Expand All @@ -478,6 +449,7 @@ contexts:
- match: '(?i)\b(as)\b'
scope: keyword.other.use-as.php
push:
- include: attributes
- include: comments
- match: '{{identifier}}'
scope: entity.name.class.php
Expand Down Expand Up @@ -514,10 +486,12 @@ contexts:
use-statement-identifier-function-def:
- match: '(?={{path}})'
push:
- include: attributes
- include: comments
- match: '(?i)\b(as)\b'
scope: keyword.other.use-as.php
push:
- include: attributes
- include: comments
- match: '{{identifier}}'
scope: entity.name.function.php
Expand Down Expand Up @@ -581,13 +555,15 @@ contexts:
- match: '\{'
scope: meta.block.php punctuation.section.block.php
set: class-body
- include: attributes
- include: comments
- match: (?i)(extends)\b\s*
captures:
1: storage.modifier.extends.php
push:
- match: '(?=implements)'
pop: true
- include: attributes
- include: comments
- match: '(?={{path}})'
set:
Expand All @@ -606,6 +582,7 @@ contexts:
captures:
1: storage.modifier.implements.php
push:
- include: attributes
- include: comments
- match: '(?={{path}})'
push:
Expand All @@ -625,6 +602,7 @@ contexts:

class-body:
- meta_content_scope: meta.class.php meta.block.php
- include: attributes
- include: comments
- match: '\}'
scope: meta.class.php meta.block.php punctuation.section.block.php
Expand All @@ -643,6 +621,7 @@ contexts:
- match: '}'
scope: punctuation.section.block.php
pop: true
- include: attributes
- include: comments
- match: \b(as)\b
scope: keyword.other.use-as.php
Expand All @@ -660,6 +639,7 @@ contexts:
1: punctuation.separator.namespace.php
- match: ''
pop: true
- include: attributes
- include: comments
- include: class-builtin
- match: (?={{path}})
Expand Down Expand Up @@ -692,6 +672,7 @@ contexts:
- match: (?i)(?=(?:\b(?:final|abstract|{{visibility_modifier}}|static)\s+)*\bfunction\b\s*(&\s*)?{{identifier_start}})
push:
- meta_content_scope: meta.function.php
- include: attributes
- include: comments
- match: '(?i)\b(final|abstract|{{visibility_modifier}}|static)\b'
scope: storage.modifier.php
Expand Down Expand Up @@ -757,6 +738,7 @@ contexts:
- match: '(?=;|,|\)|\s*\?>)'
pop: 2
- include: expressions
- include: attributes
- include: comments
- match: (?=\S)
fail: arrow-function-branch
Expand Down Expand Up @@ -797,11 +779,11 @@ contexts:
# Exit on unexpected content
- match: (?=\S)
pop: true
- include: attributes
- include: comments
- match: \b(array|callable|int|string|bool|float|object)\b
scope: storage.type.php
# Class name type hint
- include: attributes-in-function-parameter
- include: type-hint
- match: '\.\.\.'
scope: keyword.operator.spread.php
Expand All @@ -822,6 +804,7 @@ contexts:

function-use:
- meta_content_scope: meta.function.closure.use.php
- include: attributes
- include: comments
- match: '\)'
scope: punctuation.section.group.end.php
Expand Down Expand Up @@ -1106,7 +1089,7 @@ contexts:
1: punctuation.separator.namespace.php
- match: |-
(\\)?\b(?x:
T_ABSTRACT | T_AND_EQUAL | T_ARRAY | T_ARRAY_CAST | T_AS | T_BAD_CHARACTER | T_BOOLEAN_AND | T_BOOLEAN_OR |
T_ABSTRACT | T_AND_EQUAL | T_ARRAY | T_ARRAY_CAST | T_AS | T_ATTRIBUTE | T_BAD_CHARACTER | T_BOOLEAN_AND | T_BOOLEAN_OR |
T_BOOL_CAST | T_BREAK | T_CASE | T_CATCH | T_CHARACTER | T_CLASS | T_CLASS_C | T_CLONE |
T_CLOSE_TAG | T_COMMENT | T_CONCAT_EQUAL | T_CONST | T_CONSTANT_ENCAPSED_STRING | T_CONTINUE | T_CURLY_OPEN | T_DEC |
T_DECLARE | T_DEFAULT | T_DIR | T_DIV_EQUAL | T_DNUMBER | T_DO | T_DOC_COMMENT | T_DOLLAR_OPEN_CURLY_BRACES |
Expand Down
Loading

0 comments on commit dabc21e

Please sign in to comment.