Skip to content

Commit a50e476

Browse files
authored
php: Support property hooks (#2213)
1 parent bd74428 commit a50e476

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

lib/rouge/lexers/php.rb

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def builtins
325325
rule %r/=/, Operator, :in_assign
326326
rule %r/\b(?:public|protected|private|readonly)(?:\(set\)|\b)/i, Keyword
327327
rule %r/\breadonly\b/i, Keyword
328-
rule %r/\??#{id}/, Keyword::Type, :in_assign
328+
rule %r/\??#{id}/, Keyword::Type, :in_property
329329
mixin :escape
330330
mixin :whitespace
331331
mixin :variables
@@ -389,7 +389,10 @@ def builtins
389389
rule %r/\b(?:public|protected|private)(?:\(set\)|\b)/i, Keyword
390390
rule %r/\b(?:readonly|static)\b/i, Keyword
391391
rule %r/(?=(abstract|const|function)\b)/i, Keyword, :pop!
392-
rule %r/\??#{id}/, Keyword::Type, :pop!
392+
rule %r/\??#{id}/ do
393+
token Keyword::Type
394+
goto :in_property
395+
end
393396
mixin :escape
394397
mixin :whitespace
395398
mixin :return
@@ -417,6 +420,51 @@ def builtins
417420
end
418421
mixin :php
419422
end
423+
424+
state :in_property do
425+
rule %r/\$+#{id}/, Name::Variable
426+
rule %r/\{/ do
427+
token Punctuation
428+
goto :in_property_hooks
429+
end
430+
rule %r/[;,]/, Punctuation, :pop!
431+
rule %r/(?==)/ do
432+
pop!
433+
end
434+
rule %r/[|&]/, Operator
435+
rule %r/\??#{id}/, Keyword::Type
436+
mixin :escape
437+
mixin :whitespace
438+
mixin :return
439+
end
440+
441+
state :in_property_hooks do
442+
rule %r/\}/, Punctuation, :pop!
443+
rule %r/\bfinal\b/i, Keyword
444+
rule %r/&(?=get\b)/, Operator
445+
rule %r/(\bset\b)(\s*)(\()/ do
446+
groups Keyword, Text, Punctuation
447+
push :in_property_hook_params
448+
end
449+
rule %r/\b(?:get|set)\b/, Keyword
450+
rule %r/\{/, Punctuation, :in_function_body
451+
rule %r/[;,\(\)\[\]]/, Punctuation
452+
mixin :escape
453+
mixin :whitespace
454+
mixin :variables
455+
mixin :values
456+
mixin :names
457+
mixin :operators
458+
rule %r/[=?]/, Operator
459+
end
460+
461+
state :in_property_hook_params do
462+
rule %r/\)/, Punctuation, :pop!
463+
rule %r/\??#{id}/, Keyword::Type
464+
mixin :escape
465+
mixin :whitespace
466+
mixin :variables
467+
end
420468
end
421469
end
422470
end

spec/visual/samples/php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,46 @@ class C {
409409
}
410410

411411
?>
412+
413+
/* Property Hooks */
414+
415+
<?php
416+
417+
class A {
418+
public string $x1 {
419+
get {
420+
return $this->x1;
421+
}
422+
set (string $value) {
423+
$this->x1 = trim($value);
424+
}
425+
}
426+
427+
public string $x2 {
428+
get => strtolower($this->x2);
429+
set => trim($value);
430+
}
431+
432+
public array $x3 {
433+
&get => $this->data;
434+
}
435+
436+
public string $x4 {
437+
final get => $this->x6;
438+
set => strtolower($value);
439+
}
440+
441+
public function __construct(
442+
public string $x5 { get => strtoupper($this->x5); },
443+
protected int $x6 {
444+
set {
445+
if ($value < 0) {
446+
throw new \ValueError('Must be non-negative');
447+
}
448+
$this->x6 = $value;
449+
}
450+
},
451+
) {}
452+
}
453+
454+
?>

0 commit comments

Comments
 (0)