Skip to content

Commit fca743a

Browse files
committed
Add php-keywords.el with PHP language keyword tables
Extracted from the cc-mode independent php-mode work (#812) so that it can be reviewed and land independently of the Emacs 28.1 requirement that blocks that branch until the August release (#811). The file holds the PHP vocabulary (control structures, declarations and modifiers, statements, primitive types, constants, magic constants and magic methods) as plain defconst tables plus a symbol-bounded regexp-opt for each. The provenance of every entry is documented in the commentary: they were derived from the c-lang-defconst tables of the CC Mode based implementation, supplemented where PHP has gained keywords since. This is purely additive: nothing requires php-keywords.el yet, so the tables have no effect on php-mode until the cc-mode independent mode lands.
1 parent 5229244 commit fca743a

1 file changed

Lines changed: 301 additions & 0 deletions

File tree

lisp/php-keywords.el

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
;;; php-keywords.el --- PHP language keyword tables (cc-mode independent) -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2026 Friends of Emacs-PHP development
4+
5+
;; Author: USAMI Kenta <tadsan@zonu.me>
6+
;; Maintainer: USAMI Kenta <tadsan@zonu.me>
7+
;; URL: https://github.com/emacs-php/php-mode
8+
;; Keywords: languages php
9+
;; License: GPL-3.0-or-later
10+
11+
;; This program is free software; you can redistribute it and/or modify
12+
;; it under the terms of the GNU General Public License as published by
13+
;; the Free Software Foundation, either version 3 of the License, or
14+
;; (at your option) any later version.
15+
16+
;; This program is distributed in the hope that it will be useful,
17+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
;; GNU General Public License for more details.
20+
21+
;; You should have received a copy of the GNU General Public License
22+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
23+
24+
;;; Commentary:
25+
26+
;; This file extracts the PHP vocabulary that used to be scattered across
27+
;; the `c-lang-defconst' forms in lisp/php-cc-mode.el (roughly lines
28+
;; 367-609 as of the `php-cc-mode' freeze) into plain, cc-mode independent
29+
;; `defconst' lists and pre-compiled `regexp-opt' regexps.
30+
;;
31+
;; The purpose is to give the upcoming cc-free `php-mode' font-lock
32+
;; implementation (see lisp/php-mode.el, not yet written) a single source
33+
;; of truth for PHP keywords, without requiring `cc-mode', `cc-langs' or
34+
;; `cc-fonts'.
35+
;;
36+
;; Naming convention: for every category NAME there is
37+
;; - `php-keywords--NAME' a list of keyword strings (lower-case)
38+
;; - `php-keywords--NAME-re' the same list compiled with
39+
;; `regexp-opt' using the 'symbols
40+
;; boundary, so it only matches whole
41+
;; tokens (e.g. "if" but not "iffy").
42+
;;
43+
;; Case folding: PHP keywords (control structures, declarations,
44+
;; statements, type names, `true'/`false'/`null') are case-insensitive
45+
;; in the PHP language itself (e.g. "IF", "If", "if" are equivalent).
46+
;; The lists below are kept lower-case only; callers that build
47+
;; `font-lock-keywords' entries from the `-re' regexps MUST bind
48+
;; `case-fold-search' to non-nil (or use `font-lock-defaults' with the
49+
;; KEYWORDS-CASE-FOLD slot set to t) while matching against them.
50+
;;
51+
;; Magic constants (`__LINE__' etc.) are the sole exception: by PHP
52+
;; convention they are always written in upper-case, and are therefore
53+
;; stored upper-case and are not meant to be matched case-insensitively.
54+
;; See `php-keywords--constants'.
55+
;;
56+
;; This file intentionally does NOT duplicate lisp/php-defs.el, which
57+
;; holds the (large) table of built-in *function* names. Predefined
58+
;; runtime constants other than `true'/`false'/`null' and the magic
59+
;; constants (e.g. `PHP_INT_MAX') are likewise out of scope here; only
60+
;; `PHP_EOL' is included below because it was explicitly called out in
61+
;; the implementation blueprint as a commonly fontified constant.
62+
63+
;;; Code:
64+
65+
;; No dependency on `php' or any other package is required: these are
66+
;; plain data tables. `regexp-opt' is autoloaded from `regexp-opt.el'
67+
;; which is always preloaded in stock Emacs.
68+
69+
70+
;;; 1. Control structures
71+
;;
72+
;; Source: php-cc-mode.el `c-lang-defconst' forms, php-cc branch:
73+
;; - `c-block-stmt-2-kwds' (catch declare elseif for foreach if switch
74+
;; while) -- "elseif" "for" "foreach" "if" "switch" "while" taken
75+
;; from here; "catch" and "declare" are exception-handling /
76+
;; declaration keywords and are filed under
77+
;; `php-keywords--statements' / `php-keywords--declarations'
78+
;; instead.
79+
;; - `c-simple-stmt-kwds' (break continue die echo exit goto return
80+
;; throw include include_once print require require_once) --
81+
;; "break" "continue" "goto" "return" taken from here.
82+
;; - `c-modifier-kwds' (abstract final static case readonly) -- "case"
83+
;; is listed there because php-cc-mode.el also uses it for
84+
;; `enum ... { case Foo; }' declarations, but as a keyword it is
85+
;; overwhelmingly a `switch' label, so it is filed here.
86+
;; - `c-other-kwds' -- "default" "endfor" "endforeach" "endif"
87+
;; "endswitch" "endwhile" taken from here ("enddeclare" is filed
88+
;; under `php-keywords--declarations' next to "declare").
89+
;; - `c-inexpr-block-kwds' (match) -- "match" (PHP 8.0) taken from
90+
;; here.
91+
;;
92+
;; Supplemented (not present as an explicit php-cc `c-lang-defconst'
93+
;; override; inherited by cc-mode from its java-mode/c base language
94+
;; and therefore invisible in the php-cc-specific forms, but required
95+
;; for a self-contained keyword table):
96+
;; - "else" and "do" -- part of C-like `c-block-stmt-1-kwds' defaults
97+
;; that php-cc-mode.el never overrides for PHP.
98+
(defconst php-keywords--control-structures
99+
'("break" "case" "continue" "default" "do" "else" "elseif" "endfor"
100+
"endforeach" "endif" "endswitch" "endwhile" "for" "foreach" "goto"
101+
"if" "match" "return" "switch" "while")
102+
"PHP control-structure keywords (case-insensitive).
103+
See the commentary in php-keywords.el for provenance.")
104+
105+
(defconst php-keywords--control-structures-re
106+
(regexp-opt php-keywords--control-structures 'symbols)
107+
"`regexp-opt' of `php-keywords--control-structures', symbol-bounded.
108+
Match against buffer text with `case-fold-search' bound to non-nil.")
109+
110+
111+
;;; 2. Declarations and modifiers
112+
;;
113+
;; Source:
114+
;; - `c-class-decl-kwds' -- "class" "trait" "interface" "enum" (PHP
115+
;; 8.1 backed enums).
116+
;; - `c-typeless-decl-kwds' -- adds "function" "const" on top of
117+
;; `c-class-decl-kwds'.
118+
;; - `c-modifier-kwds' -- "abstract" "final" "static" "readonly" (PHP
119+
;; 8.1 readonly properties); "case" excluded, see above.
120+
;; - `c-protection-kwds' -- "private" "protected" "public".
121+
;; - `c-postfix-decl-spec-kwds' -- "implements" "extends".
122+
;; - `c-type-list-kwds' -- adds "use" "namespace" "insteadof" (the
123+
;; "@new" and "instanceof" entries in that list are pseudo-tokens /
124+
;; operators, not declaration keywords, and are excluded; "new" is
125+
;; filed under `php-keywords--statements').
126+
;; - `c-other-block-decl-kwds' -- "namespace" (duplicate of above,
127+
;; kept for provenance).
128+
;; - `c-other-kwds' -- "global" "var", plus the declare-directive
129+
;; pseudo-keywords "encoding" "ticks" "strict_types"; "declare"
130+
;; itself comes from `c-block-stmt-2-kwds' and is filed here
131+
;; (declarations), together with "enddeclare" which sits next to it
132+
;; in `c-other-kwds'.
133+
;;
134+
;; Supplemented: none required -- PHP 8.1's "enum"/"readonly" and PHP
135+
;; 8.0's "match" (filed under control-structures) were already present
136+
;; in php-cc-mode.el's tables.
137+
(defconst php-keywords--declarations
138+
'("abstract" "class" "const" "declare" "enddeclare" "encoding" "enum"
139+
"extends" "final" "function" "global" "implements" "insteadof"
140+
"interface" "namespace" "private" "protected" "public" "readonly"
141+
"static" "strict_types" "ticks" "trait" "use" "var")
142+
"PHP declaration and modifier keywords (case-insensitive).
143+
See the commentary in php-keywords.el for provenance.")
144+
145+
(defconst php-keywords--declarations-re
146+
(regexp-opt php-keywords--declarations 'symbols)
147+
"`regexp-opt' of `php-keywords--declarations', symbol-bounded.
148+
Match against buffer text with `case-fold-search' bound to non-nil.")
149+
150+
151+
;;; 3. Statements and expression-level keywords
152+
;;
153+
;; Source:
154+
;; - `c-simple-stmt-kwds' -- "die" "echo" "exit" "include"
155+
;; "include_once" "print" "require" "require_once" "throw" taken
156+
;; from here ("break" "continue" "goto" "return" filed under
157+
;; control-structures instead).
158+
;; - `c-lambda-kwds' -- "function" "use" (already covered by
159+
;; declarations); nothing new added here.
160+
;; - `c-operators' `(prefix "new" "clone")' entry -- "new" "clone".
161+
;; - `c-operators' `(prefix "instanceof")' entry -- "instanceof".
162+
;; - `c-other-kwds' -- "array" "as" "catch" "empty" "eval" "fn" (PHP
163+
;; 7.4 arrow functions) "isset" "list" "unset" "yield" "yield from"
164+
;; "and" "or" "xor" taken from here.
165+
;; - "finally" -- like "else"/"do" above, this is inherited from
166+
;; cc-mode's java-mode/c base `c-block-stmt-1-kwds' and never
167+
;; overridden by php-cc-mode.el, so it is supplemented here for
168+
;; completeness of the try/catch/finally triple.
169+
;;
170+
;; Supplemented: none beyond "finally"; PHP 7.4's "fn" and the
171+
;; "yield from" two-word form were already present in php-cc-mode.el's
172+
;; `c-other-kwds'.
173+
(defconst php-keywords--statements
174+
'("and" "array" "as" "catch" "clone" "die" "echo" "empty" "eval"
175+
"exit" "finally" "fn" "include" "include_once" "instanceof" "isset"
176+
"list" "new" "or" "print" "require" "require_once" "throw" "try"
177+
"unset" "xor" "yield")
178+
"PHP statement and expression keywords (case-insensitive).
179+
See the commentary in php-keywords.el for provenance.
180+
Note: the two-word form \"yield from\" is not representable as a single
181+
`regexp-opt' symbol entry; match it separately as
182+
\"\\\\_<yield\\\\_>\\\\s-+\\\\_<from\\\\_>\" if needed, or rely on
183+
\"yield\" alone matching the common one-word case.")
184+
185+
(defconst php-keywords--statements-re
186+
(regexp-opt php-keywords--statements 'symbols)
187+
"`regexp-opt' of `php-keywords--statements', symbol-bounded.
188+
Match against buffer text with `case-fold-search' bound to non-nil.")
189+
190+
191+
;;; 4. Type names
192+
;;
193+
;; Source: `c-primitive-type-kwds' -- "int" "integer" "bool" "boolean"
194+
;; "float" "double" "real" "string" "object" "void" "mixed" "never"
195+
;; (PHP 8.1 "never" return type).
196+
;;
197+
;; Supplemented (PHP union/pseudo types that are commonly fontified as
198+
;; types but were not present in php-cc-mode.el's `c-primitive-type-kwds'):
199+
;; - "array" "callable" "iterable" -- long-standing PHP pseudo-types
200+
;; usable in type declarations, missing from the cc table (that
201+
;; table only covered scalar/primitive-ish names; "array" itself
202+
;; was instead reachable in php-cc-mode.el only indirectly via
203+
;; `c-other-kwds').
204+
;; - "false" "null" "true" -- PHP 8.0 allows "false"/"null" as
205+
;; standalone return/param types and PHP 8.2 allows "true"; also
206+
;; present in `c-constant-kwds' (see `php-keywords--constants').
207+
;; - "self" "parent" "static" -- class-relative pseudo-types, valid in
208+
;; return-type position since PHP 7.4/8.0; "self" and "static" were
209+
;; present in php-cc-mode.el's `c-other-kwds', "parent" likewise.
210+
(defconst php-keywords--types
211+
'("array" "bool" "boolean" "callable" "double" "false" "float" "int"
212+
"integer" "iterable" "mixed" "never" "null" "object" "parent"
213+
"real" "self" "static" "string" "true" "void")
214+
"PHP type-hint / pseudo-type keywords (case-insensitive).
215+
See the commentary in php-keywords.el for provenance.")
216+
217+
(defconst php-keywords--types-re
218+
(regexp-opt php-keywords--types 'symbols)
219+
"`regexp-opt' of `php-keywords--types', symbol-bounded.
220+
Match against buffer text with `case-fold-search' bound to non-nil.")
221+
222+
223+
;;; 5. Language constants and magic constants
224+
;;
225+
;; Source:
226+
;; - `c-constant-kwds' -- "true" "false" "null" (case-insensitive
227+
;; PHP language constants; stored lower-case here, same convention
228+
;; as every other list in this file).
229+
;; - php.el's existing `php-magical-constants' -- "__CLASS__"
230+
;; "__DIR__" "__FILE__" "__FUNCTION__" "__LINE__" "__METHOD__"
231+
;; "__NAMESPACE__" "__TRAIT__". These are NOT re-derived from
232+
;; php-cc-mode.el (they are not part of the 367-609 c-lang-defconst
233+
;; block; php-cc-mode.el fontifies them by referring directly to
234+
;; `php-magical-constants' from php.el, see the
235+
;; `php-magical-constant' font-lock rule). They are duplicated
236+
;; into this file's `php-keywords--constants' so this module is
237+
;; self-contained and does not need to `require' php.el; keep the
238+
;; two lists in sync if `php-magical-constants' ever changes.
239+
;;
240+
;; Unlike every other category in this file, magic constants are
241+
;; conventionally written in upper-case only and are matched
242+
;; case-sensitively (that is how php-cc-mode.el's existing font-lock
243+
;; rule for `php-magical-constants' behaves). "true"/"false"/"null"
244+
;; remain lower-case/case-insensitive like the rest of the file.
245+
;;
246+
;; Supplemented: "PHP_EOL", a predefined runtime constant (not a
247+
;; syntactic keyword) called out explicitly in the implementation
248+
;; blueprint (§6) as worth fontifying alongside the magic constants.
249+
;; It is the only predefined library constant included here -- the
250+
;; rest of PHP's predefined constants (PHP_INT_MAX, PHP_VERSION, ...)
251+
;; belong with built-in symbol tables such as lisp/php-defs.el, not in
252+
;; this "language keyword" module.
253+
(defconst php-keywords--constants
254+
'("true" "false" "null")
255+
"PHP literal keywords `true', `false' and `null' (case-insensitive).
256+
See `php-keywords--magic-constants' for the separate, case-sensitive
257+
upper-case magic constants (`__LINE__' and friends) and `PHP_EOL'.")
258+
259+
(defconst php-keywords--constants-re
260+
(regexp-opt php-keywords--constants 'symbols)
261+
"`regexp-opt' of `php-keywords--constants', symbol-bounded.
262+
Match against buffer text with `case-fold-search' bound to non-nil.")
263+
264+
(defconst php-keywords--magic-constants
265+
'("__CLASS__" "__DIR__" "__FILE__" "__FUNCTION__" "__LINE__"
266+
"__METHOD__" "__NAMESPACE__" "__TRAIT__" "PHP_EOL")
267+
"PHP magic constants and `PHP_EOL', kept upper-case and matched
268+
case-sensitively (do NOT bind `case-fold-search' to non-nil when using
269+
`php-keywords--magic-constants-re'). Mirrors php.el's
270+
`php-magical-constants' plus \"PHP_EOL\"; see the commentary above.")
271+
272+
(defconst php-keywords--magic-constants-re
273+
(regexp-opt php-keywords--magic-constants 'symbols)
274+
"`regexp-opt' of `php-keywords--magic-constants', symbol-bounded.
275+
Match case-sensitively (do not fold case).")
276+
277+
278+
;;; 6. Magic methods
279+
;;
280+
;; Source: none -- php-cc-mode.el's 367-609 `c-lang-defconst' block
281+
;; does not enumerate magic method names anywhere (grepped for
282+
;; "__construct", "__call", "__get", "__set", "magic" etc.: no hits).
283+
;; This whole category is supplemented from the PHP manual's list of
284+
;; magic methods (https://www.php.net/manual/language.oop5.magic.php),
285+
;; current through PHP 8.4.
286+
(defconst php-keywords--magic-methods
287+
'("__call" "__callstatic" "__clone" "__construct" "__debuginfo"
288+
"__destruct" "__get" "__invoke" "__isset" "__serialize" "__set"
289+
"__set_state" "__sleep" "__tostring" "__unserialize" "__unset"
290+
"__wakeup")
291+
"PHP magic method names, stored lower-case (case-insensitive, like
292+
ordinary PHP identifiers). Supplemented in full; not derived from
293+
php-cc-mode.el, see the commentary above.")
294+
295+
(defconst php-keywords--magic-methods-re
296+
(regexp-opt php-keywords--magic-methods 'symbols)
297+
"`regexp-opt' of `php-keywords--magic-methods', symbol-bounded.
298+
Match against buffer text with `case-fold-search' bound to non-nil.")
299+
300+
(provide 'php-keywords)
301+
;;; php-keywords.el ends here

0 commit comments

Comments
 (0)