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

Add support for ternary and null coalescing operators #285

Merged
merged 8 commits into from Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions grammars/php.cson
Expand Up @@ -2041,6 +2041,15 @@
'match': ','
'name': 'punctuation.separator.delimiter.php'
}
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason for the includes? Could those simply be just inlined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference. Never know when they might be needed to compose some larger expression in the future. Ran into that problem a couple times with inlined matches in language-css.

'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 @@ -3724,3 +3733,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