Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 8909c50

Browse files
Pure Intersection types (#436)
1 parent 19b82e0 commit 8909c50

2 files changed

Lines changed: 86 additions & 12 deletions

File tree

grammars/php.cson

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@
479479
(:)\\s*
480480
(
481481
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
482-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
482+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
483483
)
484484
(?=\\s*(?:{|/[/*]|\\#|$))
485485
'''
@@ -529,7 +529,7 @@
529529
(:)\\s*
530530
(
531531
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
532-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
532+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
533533
)
534534
(?=\\s*(?:=>|/[/*]|\\#|$))
535535
'''
@@ -569,7 +569,7 @@
569569
'contentName': 'meta.function.parameters.php'
570570
'end': '''(?xi)
571571
(\\)) \\s* ( : \\s*
572-
(?:\\?\\s*)? (?!\\s) [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\\\s\\|]+ (?<!\\s)
572+
(?:\\?\\s*)? (?!\\s) [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\\\s\\|&]+ (?<!\\s)
573573
)?
574574
(?=\\s*(?:{|/[/*]|\\#|$|;))
575575
'''
@@ -593,7 +593,7 @@
593593
(public|private|protected) \\s+
594594
(?: (
595595
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
596-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
596+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
597597
) \\s+ )?
598598
((?:(&)\\s*)?(\\$)[a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*) # Variable name with possible reference
599599
'''
@@ -668,7 +668,7 @@
668668
'end': '''(?xi)
669669
(\\)) (?: \\s* (:) \\s* (
670670
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
671-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
671+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
672672
) )?
673673
(?=\\s*(?:{|/[/*]|\\#|$|;))
674674
'''
@@ -700,7 +700,7 @@
700700
((?:(?:public|private|protected|static)(?:\\s+|(?=\\?)))++) # At least one modifier
701701
(
702702
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
703-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
703+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
704704
)?
705705
\\s+ ((\\$)[a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*) # Variable name
706706
'''
@@ -1432,7 +1432,7 @@
14321432
'match': '''(?xi)
14331433
(?: (
14341434
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
1435-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
1435+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
14361436
) \\s+ )?
14371437
((?:(&)\\s*)?(\\.\\.\\.)(\\$)[a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*) # Variable name with possible reference
14381438
(?=\\s*(?:,|\\)|/[/*]|\\#|$)) # A closing parentheses (end of argument list) or a comma or a comment
@@ -1459,7 +1459,7 @@
14591459
'begin': '''(?xi)
14601460
(
14611461
(?:\\?\\s*)? [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ | # nullable type
1462-
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*\\|\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
1462+
[a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+ (?: \\s*[|&]\\s* [a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]+)+ # union type
14631463
)
14641464
\\s+ ((?:(&)\\s*)?(\\$)[a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*) # Variable name with possible reference
14651465
'''
@@ -2315,7 +2315,7 @@
23152315
'name': 'keyword.operator.nullable-type.php'
23162316
}
23172317
{
2318-
'match': '\\|'
2318+
'match': '[|&]'
23192319
'name': 'punctuation.separator.delimiter.php'
23202320
}
23212321
{
@@ -2482,7 +2482,7 @@
24822482
}
24832483
]
24842484
'php_doc_types':
2485-
'match': '(?i)[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*(\\|[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*)*'
2485+
'match': '(?i)[a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*([|&][a-z_\\x{7f}-\\x{7fffffff}\\\\][a-z0-9_\\x{7f}-\\x{7fffffff}\\\\]*)*'
24862486
'captures':
24872487
'0':
24882488
'patterns': [
@@ -2497,7 +2497,7 @@
24972497
'include': '#class-name'
24982498
}
24992499
{
2500-
'match': '\\|'
2500+
'match': '[|&]'
25012501
'name': 'punctuation.separator.delimiter.php'
25022502
}
25032503
]
@@ -2524,7 +2524,7 @@
25242524
'include': '#php_doc_types'
25252525
}
25262526
{
2527-
'match': '\\|'
2527+
'match': '[|&]'
25282528
'name': 'punctuation.separator.delimiter.php'
25292529
}
25302530
]

spec/php-spec.coffee

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,34 @@ describe 'PHP grammar', ->
765765
expect(lines[1][2]).toEqual value: '?', scopes: ["source.php", "meta.class.php", "meta.class.body.php", "keyword.operator.nullable-type.php"]
766766
expect(lines[1][4]).toEqual value: 'string', scopes: ["source.php", "meta.class.php", "meta.class.body.php", "keyword.other.type.php"]
767767

768+
it 'tokenizes union types', ->
769+
lines = grammar.tokenizeLines '''
770+
class A {
771+
public int|string $id;
772+
}
773+
'''
774+
775+
expect(lines[1][1]).toEqual value: 'public', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
776+
expect(lines[1][3]).toEqual value: 'int', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'keyword.other.type.php']
777+
expect(lines[1][4]).toEqual value: '|', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'punctuation.separator.delimiter.php']
778+
expect(lines[1][5]).toEqual value: 'string', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'keyword.other.type.php']
779+
expect(lines[1][7]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php', 'punctuation.definition.variable.php']
780+
expect(lines[1][8]).toEqual value: 'id', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php']
781+
782+
it 'tokenizes intersection types', ->
783+
lines = grammar.tokenizeLines '''
784+
class A {
785+
public FooInterface & BarInterface $foobar;
786+
}
787+
'''
788+
789+
expect(lines[1][1]).toEqual value: 'public', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'storage.modifier.php']
790+
expect(lines[1][3]).toEqual value: 'FooInterface', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'support.class.php']
791+
expect(lines[1][5]).toEqual value: '&', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'punctuation.separator.delimiter.php']
792+
expect(lines[1][7]).toEqual value: 'BarInterface', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'support.class.php']
793+
expect(lines[1][9]).toEqual value: '$', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php', 'punctuation.definition.variable.php']
794+
expect(lines[1][10]).toEqual value: 'foobar', scopes: ['source.php', 'meta.class.php', 'meta.class.body.php', 'variable.other.php']
795+
768796
it 'tokenizes 2 modifiers correctly', ->
769797
lines = grammar.tokenizeLines '''
770798
class Foo {
@@ -1293,6 +1321,18 @@ describe 'PHP grammar', ->
12931321
expect(tokens[14]).toEqual value: 'a', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'variable.other.php']
12941322
expect(tokens[15]).toEqual value: ')', scopes: ['source.php', 'meta.function.php', 'punctuation.definition.parameters.end.bracket.round.php']
12951323

1324+
it 'tokenizes intersection types in function parameters', ->
1325+
{tokens} = grammar.tokenizeLine 'function test(FooInterface&BarInterface $foobar){}'
1326+
1327+
expect(tokens[0]).toEqual value: 'function', scopes: ['source.php', 'meta.function.php', 'storage.type.function.php']
1328+
expect(tokens[2]).toEqual value: 'test', scopes: ['source.php', 'meta.function.php', 'entity.name.function.php']
1329+
expect(tokens[3]).toEqual value: '(', scopes: ['source.php', 'meta.function.php', 'punctuation.definition.parameters.begin.bracket.round.php']
1330+
expect(tokens[4]).toEqual value: 'FooInterface', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'support.class.php']
1331+
expect(tokens[5]).toEqual value: '&', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'punctuation.separator.delimiter.php']
1332+
expect(tokens[6]).toEqual value: 'BarInterface', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'support.class.php']
1333+
expect(tokens[8]).toEqual value: '$', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'variable.other.php', 'punctuation.definition.variable.php']
1334+
expect(tokens[9]).toEqual value: 'foobar', scopes: ['source.php', 'meta.function.php', 'meta.function.parameters.php', 'meta.function.parameter.typehinted.php', 'variable.other.php']
1335+
12961336
it 'tokenizes trailing comma in function parameters', ->
12971337
{tokens} = grammar.tokenizeLine 'function abc($a, $b,){}'
12981338

@@ -1348,6 +1388,16 @@ describe 'PHP grammar', ->
13481388
expect(tokens[12]).toEqual value: ' ', scopes: ['source.php', 'meta.function.php']
13491389
expect(tokens[13]).toEqual value: 'null', scopes: ['source.php', 'meta.function.php', 'keyword.other.type.php']
13501390

1391+
it 'tokenizes intersection return types', ->
1392+
{tokens} = grammar.tokenizeLine 'function foobar() : FooInterface & BarInterface {}'
1393+
1394+
expect(tokens[0]).toEqual value: 'function', scopes: ['source.php', 'meta.function.php', 'storage.type.function.php']
1395+
expect(tokens[2]).toEqual value: 'foobar', scopes: ['source.php', 'meta.function.php', 'entity.name.function.php']
1396+
expect(tokens[6]).toEqual value: ':', scopes: ['source.php', 'meta.function.php', 'keyword.operator.return-value.php']
1397+
expect(tokens[8]).toEqual value: 'FooInterface', scopes: ['source.php', 'meta.function.php', 'support.class.php']
1398+
expect(tokens[10]).toEqual value: '&', scopes: ['source.php', 'meta.function.php', 'punctuation.separator.delimiter.php']
1399+
expect(tokens[12]).toEqual value: 'BarInterface', scopes: ['source.php', 'meta.function.php', 'support.class.php']
1400+
13511401
it 'tokenizes function names with characters other than letters or numbers', ->
13521402
# Char 160 is hex 0xA0, which is between 0x7F and 0xFF, making it a valid PHP identifier
13531403
functionName = "foo#{String.fromCharCode(160)}bar"
@@ -2475,6 +2525,17 @@ describe 'PHP grammar', ->
24752525
expect(lines[1][5]).toEqual value: 'Class', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
24762526
expect(lines[1][6]).toEqual value: ' description', scopes: ['source.php', 'comment.block.documentation.phpdoc.php']
24772527

2528+
it 'should tokenize intersection types', ->
2529+
lines = grammar.tokenizeLines '''
2530+
/**
2531+
*@param Foo&Bar description
2532+
'''
2533+
2534+
expect(lines[1][1]).toEqual value: '@param', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'keyword.other.phpdoc.php']
2535+
expect(lines[1][3]).toEqual value: 'Foo', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2536+
expect(lines[1][4]).toEqual value: '&', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.separator.delimiter.php']
2537+
expect(lines[1][5]).toEqual value: 'Bar', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2538+
24782539
it 'should tokenize multiple namespaced types', ->
24792540
lines = grammar.tokenizeLines '''
24802541
/**
@@ -2545,6 +2606,19 @@ describe 'PHP grammar', ->
25452606
expect(lines[1][7]).toEqual value: ')', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.definition.type.end.bracket.round.phpdoc.php']
25462607
expect(lines[1][8]).toEqual value: '[]', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.other.array.phpdoc.php']
25472608

2609+
lines = grammar.tokenizeLines '''
2610+
/**
2611+
*@param (Foo&Bar)[] description
2612+
'''
2613+
2614+
expect(lines[1][1]).toEqual value: '@param', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'keyword.other.phpdoc.php']
2615+
expect(lines[1][3]).toEqual value: '(', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.definition.type.begin.bracket.round.phpdoc.php']
2616+
expect(lines[1][4]).toEqual value: 'Foo', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2617+
expect(lines[1][5]).toEqual value: '&', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.separator.delimiter.php']
2618+
expect(lines[1][6]).toEqual value: 'Bar', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'support.class.php']
2619+
expect(lines[1][7]).toEqual value: ')', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'punctuation.definition.type.end.bracket.round.phpdoc.php']
2620+
expect(lines[1][8]).toEqual value: '[]', scopes: ['source.php', 'comment.block.documentation.phpdoc.php', 'meta.other.type.phpdoc.php', 'keyword.other.array.phpdoc.php']
2621+
25482622
lines = grammar.tokenizeLines '''
25492623
/**
25502624
*@param ((Test|int)[]|Test\\Type[]|string[]|resource)[] description

0 commit comments

Comments
 (0)