Skip to content

Support PHP8 syntax table for supporting #[Attributes] #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions lisp/php-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ In that case set to `NIL'."
:tag "PHP Mode Enable Project Local Variable"
:type 'boolean)

(defcustom php-mode-use-php7-syntax-table nil
"When set to `T', use a syntax table compatible with PHP 7."
:group 'php-mode
:tag "PHP Mode Enable Project Local Variable"
:type 'boolean)

(defconst php-mode-cc-vertion
(eval-when-compile c-version))

Expand Down Expand Up @@ -979,18 +985,45 @@ this ^ lineup"
(string-match "\\_<.+?\\_>" heredoc-start)
(concat "^\\s-*\\(" (match-string 0 heredoc-start) "\\)\\W"))

(defvar php-syntax-propertize-functions
'(php-syntax-propertize-heredoc
php-syntax-propertize-hash-line-comment
php-syntax-propertize-quotes-in-comment)
"Syntax propertize functions for PHP script.")

(defun php-syntax-propertize-function (start end)
"Apply propertize rules from START to END."
(goto-char start)
(dolist (propertizer php-syntax-propertize-functions)
(goto-char start)
(funcall propertizer start end)))

(defun php-syntax-propertize-heredoc (_start end)
"Apply propertize Heredoc and Nowdoc from START to END."
(while (and (< (point) end)
(re-search-forward php-heredoc-start-re end t))
(php-heredoc-syntax))
(goto-char start)
(php-heredoc-syntax)))

(defun php-syntax-propertize-quotes-in-comment (_start end)
"Apply propertize quotes (' and \") from START to END."
(while (re-search-forward "['\"]" end t)
(when (php-in-comment-p)
(c-put-char-property (match-beginning 0)
'syntax-table (string-to-syntax "_")))))

(defun php-syntax-propertize-hash-line-comment (_start end)
"Apply propertize # comment (without PHP8 Attributes) from START to END."
(unless php-mode-use-php7-syntax-table
(let (line-end in-last-line)
(while (and (< (point) (min end (point-max)))
(not in-last-line))
(setq line-end (line-end-position))
(when (and (search-forward "#" line-end t)
(not (php-in-string-or-comment-p))
(not (looking-at "[[]")))
(c-put-char-property (1- (point)) 'syntax-table (string-to-syntax "< b")))
(move-beginning-of-line 2)
(setq in-last-line (>= line-end (point)))))))

(defun php-heredoc-syntax ()
"Mark the boundaries of searched heredoc."
(goto-char (match-beginning 0))
Expand Down Expand Up @@ -1121,7 +1154,6 @@ After setting the stylevars run hooks according to STYLENAME
(modify-syntax-entry ?_ "_" table)
(modify-syntax-entry ?` "\"" table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?# "< b" table)
(modify-syntax-entry ?\n "> b" table)
(modify-syntax-entry ?$ "_" table)
table))
Expand Down Expand Up @@ -1151,7 +1183,7 @@ After setting the stylevars run hooks according to STYLENAME
(setq-local comment-start "// ")
(setq-local comment-start-skip
(eval-when-compile
(rx (group (or (: "#")
(rx (group (or (: "#" (not (any "[")))
(: "/" (+ "/"))
(: "/*")))
(* (syntax whitespace)))))
Expand All @@ -1175,6 +1207,9 @@ After setting the stylevars run hooks according to STYLENAME
;; PHP vars are case-sensitive
(setq case-fold-search t)

(when php-mode-use-php7-syntax-table
(modify-syntax-entry ?# "< b" php-mode-syntax-table))

(when php-mode-enable-project-local-variable
(add-hook 'hack-local-variables-hook #'php-mode-set-local-variable-delay t t))

Expand Down
22 changes: 22 additions & 0 deletions tests/8.0/attribute/class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

# Comment

#[ExampleAttribute]
#[Attr1, Attr2]
class Klass
{
#[ExampleAttribute]
function f1() { }

#[WithoutArgument]
#[SingleArgument(0)]
#[FewArguments('Hello', 'World')]
function foo() {}

#[WithoutArgument] #[SingleArgument(0)] #[FewArguments('Hello', 'World')]
function bar() {}

#[Attr2("foo"), Attr2("bar")]
function buz() {}
}
72 changes: 72 additions & 0 deletions tests/8.0/attribute/class.php.faces
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
;; -*- mode: emacs-lisp -*-
(("<?php" . php-php-tag)
("

")
("# " . font-lock-comment-delimiter-face)
("Comment
" . font-lock-comment-face)
("
#[ExampleAttribute]
#[Attr1, Attr2]
")
("class" . php-class-declaration)
(" ")
("Klass" . font-lock-type-face)
("
{
#[ExampleAttribute]
")
("function" . php-keyword)
(" ")
("f1" . php-function-name)
("() { }

#[WithoutArgument]
#[")
("SingleArgument" . php-function-call)
("(0)]
#[")
("FewArguments" . php-function-call)
("(")
("'Hello'" . php-string)
(", ")
("'World'" . php-string)
(")]
")
("function" . php-keyword)
(" ")
("foo" . php-function-name)
("() {}

#[WithoutArgument] #[")
("SingleArgument" . php-function-call)
("(0)] #[")
("FewArguments" . php-function-call)
("(")
("'Hello'" . php-string)
(", ")
("'World'" . php-string)
(")]
")
("function" . php-keyword)
(" ")
("bar" . php-function-name)
("() {}

#[")
("Attr2" . php-function-call)
("(")
("\"foo\"" . php-string)
("), ")
("Attr2" . php-function-call)
("(")
("\"bar\"" . php-string)
(")]
")
("function" . php-keyword)
(" ")
("buz" . php-function-name)
("() {}
}
"))
10 changes: 5 additions & 5 deletions tests/8.0/attribute/function.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

<<ExampleAttribute>>
#[ExampleAttribute]
function f1() { }

<<WithoutArgument>>
<<SingleArgument(0)>>
<<FewArguments('Hello', 'World')>>
#[WithoutArgument]
#[SingleArgument(0)]
#[FewArguments('Hello', 'World')]
function foo() {}

<<WithoutArgument>><<SingleArgument(0)>><<FewArguments('Hello', 'World')>>
#[WithoutArgument]#[SingleArgument(0)]#[FewArguments('Hello', 'World')]
function bar() {}
43 changes: 43 additions & 0 deletions tests/8.0/attribute/function.php.faces
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;; -*- mode: emacs-lisp -*-
(("<?php" . php-php-tag)
("

#[ExampleAttribute]
")
("function" . php-keyword)
(" ")
("f1" . php-function-name)
("() { }

#[WithoutArgument]
#[")
("SingleArgument" . php-function-call)
("(0)]
#[")
("FewArguments" . php-function-call)
("(")
("'Hello'" . php-string)
(", ")
("'World'" . php-string)
(")]
")
("function" . php-keyword)
(" ")
("foo" . php-function-name)
("() {}

#[WithoutArgument]#[")
("SingleArgument" . php-function-call)
("(0)]#[")
("FewArguments" . php-function-call)
("(")
("'Hello'" . php-string)
(", ")
("'World'" . php-string)
(")]
")
("function" . php-keyword)
(" ")
("bar" . php-function-name)
("() {}
"))
8 changes: 8 additions & 0 deletions tests/8.0/attribute/function2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

#[
Deprecated
]
function f($arg1,
#[Deprecated] $arg2){}
#Deprecated
23 changes: 23 additions & 0 deletions tests/8.0/attribute/function2.php.faces
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;; -*- mode: emacs-lisp -*-
(("<?php" . php-php-tag)
("

#[
Deprecated
]
")
("function" . php-keyword)
(" ")
("f" . php-function-name)
("(")
("$" . php-variable-sigil)
("arg1" . php-variable-name)
(",
#[Deprecated] ")
("$" . php-variable-sigil)
("arg2" . php-variable-name)
("){}
")
("#D" . font-lock-comment-delimiter-face)
("eprecated
" . font-lock-comment-face))
2 changes: 1 addition & 1 deletion tests/8.1/enum.php.faces
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@
};
}
}
"))
"))
17 changes: 17 additions & 0 deletions tests/php-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ Meant for `php-mode-test-issue-503'."
(with-php-mode-test ("7.4/arrow-function.php" :faces t))
(with-php-mode-test ("7.4/typed-property.php" :faces t)))

(ert-deftest php-mode-test-php80 ()
"Test highlighting language constructs added in PHP 8.0."
(with-php-mode-test ("8.0/attribute/class.php" :faces t))
(with-php-mode-test ("8.0/attribute/function.php" :faces t))
(with-php-mode-test ("8.0/attribute/function2.php" :faces t)))

(ert-deftest php-mode-test-php81 ()
"Test highlighting language constructs added in PHP 8.1."
(with-php-mode-test ("8.1/enum.php" :faces t)))
Expand All @@ -678,4 +684,15 @@ Meant for `php-mode-test-issue-503'."
(with-php-mode-test ("lang/errorcontrol.php" :faces t))
(with-php-mode-test ("lang/magical-constants/echo.php" :faces t)))

;; For developers: How to make .faces list file.
;;
;; 1. Press `M-x eval-buffer' in this file bufffer.
;; 2. Copy follows code snippet:
;; (setq x (php-mode-test--buffer-face-list (current-buffer)))
;; 3. Visit target buffer of testing PHP file.
;; 4. Press `M-:' (or `M-x eval-expression') and yank killed the code snippet.
;; 5. Press `M-x ielm' and input `x' and RET key.
;; 6. Kill output list and yank list to .faces file.
;; 7. Execute `make test' in shell.

;;; php-mode-test.el ends here