Skip to content
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

enh(php) support C style and hash prefix single line comments in functions #4230

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hashar
Copy link

@hashar hashar commented Mar 21, 2025

enh(php) add // and # comments in functions and attributes

functions, arrow functions parameters can contain nested comments such as:

header( /* cache age */ 'Age: 60' );
fn($x /* something */) => $x;

Those are C blocks comments. However PHP also support single line comments either C-style (//) or using hash (#):

header(
    # Set caching header
    'Age: 60' // seconds
);
fn(
    # We like magic
    $x // something
) => $x;

PHP 8 attributes can also have such comments:

#[\Deprecated(
# A comment
since: "2025-03-25"
// C line comment
#)]

Parameters of functions, arrow functions and PHP 8 attributes solely had C_BLOCK_COMMENT_MODE.

Add C_LINE_COMMENT_MODE and HASH_COMMENT_MODE to recognize the single line comments.

Fixes: #3655
Fixes: https://phabricator.wikimedia.org/T372404

@hashar
Copy link
Author

hashar commented Mar 21, 2025

Looks like the single lines PHP comments that starts with # suffer from the same issue (#3655) and this patch should fix them as well while at it.

@hashar hashar force-pushed the php-c-line-comment-in-functions branch from 80f3fa7 to 6060d5c Compare March 21, 2025 14:33
@hashar hashar changed the title enh(php) support C line comments in functions enh(php) support C style and hash prefix single line comments in functions Mar 21, 2025
@@ -49,3 +49,15 @@ setAlarm(
label: 'foo',
time:time() + array(5)[0] + Foo::HOUR,
);

header(
Copy link
Member

Choose a reason for hiding this comment

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

Is header a PHP thing?

Copy link
Contributor

Choose a reason for hiding this comment

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

In this context (functions.txt), he calling user definer function:

header()

Copy link
Author

Choose a reason for hiding this comment

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

Is header a PHP thing?

Yes! That is used to add a HTTP response header to the output. PHP is pretty much a language to generate web pages and their responses and header() is I guess known to anyone practicing PHP. https://www.php.net/manual/en/function.header.php

The function name should not matter to highlight.js, as long as it is recognized as a function. If header() is confusing, I do not mind changing it to another built-in function that is common to other languages such as printf().

Copy link
Member

@joshgoebel joshgoebel left a comment

Choose a reason for hiding this comment

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

Please add an entry to CHANGES.md as well! Looks good!

Copy link

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +16 B

View Changes
file base pr diff
es/core.min.js 8.19 KB 8.19 KB -2 B
es/highlight.min.js 8.19 KB 8.19 KB -2 B
es/languages/php.min.js 2.65 KB 2.66 KB +12 B
highlight.min.js 8.23 KB 8.23 KB -2 B
languages/php.min.js 2.66 KB 2.67 KB +10 B

functions, arrow functions parameters can contain nested comments such
as:

    header( /* cache age */ 'Age: 60' );
    fn($x /* something */) => $x;

Those are C blocks comments. However PHP also support single line
comments either C-style (`//`) or using hash (`#`):

    header(
        # Set caching header
        'Age: 60' // seconds
    );
    fn(
        # We like magic
        $x // something
    ) => $x;

PHP 8 attributes can also have such comments:

    #[\Deprecated(
    # A comment
    since: "2025-03-25"
    // C line comment
    #)]

Parameters of functions, arrow functions and PHP 8 attributes solely had
`C_BLOCK_COMMENT_MODE`.

Add `C_LINE_COMMENT_MODE` and `HASH_COMMENT_MODE` to recognize the
single line comments.

Fixes: highlightjs#3655
Fixes: https://phabricator.wikimedia.org/T372404
@hashar hashar force-pushed the php-c-line-comment-in-functions branch from 6060d5c to 7a6a1f2 Compare March 25, 2025 10:17
@hashar
Copy link
Author

hashar commented Mar 25, 2025

I have made adjustments to properly support attributes, thanks to @wkania

Please add an entry to CHANGES.md as well! Looks good!

I have added an entry in the change log, thank you!

@hashar hashar requested review from wkania and joshgoebel March 25, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(PHP) Syntax parsing breaks inside parameters of some function calls
4 participants