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

Commit

Permalink
Merge pull request #285 from dsifford/dsifford-ternary-operators
Browse files Browse the repository at this point in the history
Add support for ternary and null coalescing operators
  • Loading branch information
50Wliu committed Oct 24, 2017
2 parents 28fb4f8 + 05f4d9d commit 4993090
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
29 changes: 29 additions & 0 deletions grammars/php.cson
Expand Up @@ -2010,6 +2010,15 @@
'match': ','
'name': 'punctuation.separator.delimiter.php'
}
{
'include': '#ternary_shorthand'
}
{
'include': '#null_coalescing'
}
{
'include': '#ternary_expression'
}
]
'namespace':
'begin': '(?i)(?:(namespace)|[a-z_\\x{7f}-\\x{ff}][a-z0-9_\\x{7f}-\\x{ff}]*)?(\\\\)'
Expand Down Expand Up @@ -3693,3 +3702,23 @@
]
}
]
'ternary_shorthand':
'match': '\\?:'
'name': 'keyword.operator.ternary.php'
'ternary_expression':
'begin': '\\?'
'beginCaptures':
'0':
'name': 'keyword.operator.ternary.php'
'end': ':'
'endCaptures':
'0':
'name': 'keyword.operator.ternary.php'
'patterns': [
{
'include': '#language'
}
]
'null_coalescing':
'match': '\\?\\?'
'name': 'keyword.operator.null-coalescing.php'
46 changes: 46 additions & 0 deletions spec/php-spec.coffee
Expand Up @@ -209,6 +209,52 @@ describe 'PHP grammar', ->
expect(tokens[1][4]).toEqual value: ' ', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php']
expect(tokens[1][5]).toEqual value: '2', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'constant.numeric.decimal.php']
expect(tokens[1][6]).toEqual value: ';', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'punctuation.terminator.expression.php']
it 'should tokenize ?? correctly', ->
tokens = grammar.tokenizeLines """<?php
$foo = $bar ?? 'bar';
"""
expect(tokens[1][8]).toEqual value: '??', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.null-coalescing.php']
describe 'ternaries', ->
it 'should tokenize ternary expressions', ->
tokens = grammar.tokenizeLines """<?php
$foo = 1 == 3 ? true : false;
"""
expect(tokens[1][11]).toEqual value: '?', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[1][15]).toEqual value: ':', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
tokens = grammar.tokenizeLines """<?php
$foo = 1 == 3
? true
: false;
"""
expect(tokens[2][0]).toEqual value: '?', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[3][0]).toEqual value: ':', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
tokens = grammar.tokenizeLines """<?php
$foo=1==3?true:false;
"""
expect(tokens[1][6]).toEqual value: '?', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[1][8]).toEqual value: ':', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
it 'should tokenize shorthand ternaries', ->
tokens = grammar.tokenizeLines """<?php
$foo = false ?: false ?: true ?: false;
"""
expect(tokens[1][7]).toEqual value: '?:', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[1][11]).toEqual tokens[1][7]
expect(tokens[1][15]).toEqual tokens[1][7]
it 'should tokenize a combination of ternaries', ->
tokens = grammar.tokenizeLines """<?php
$foo = false ?: true == 1
? true : false ?: false;
"""
expect(tokens[1][7]).toEqual value: '?:', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[2][0]).toEqual value: '?', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[2][4]).toEqual value: ':', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
expect(tokens[2][8]).toEqual value: '?:', scopes: ['text.html.php', 'meta.embedded.block.php', 'source.php', 'keyword.operator.ternary.php']
it 'should tokenize $this', ->
tokens = grammar.tokenizeLines "<?php $this"
Expand Down

0 comments on commit 4993090

Please sign in to comment.