Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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 2 commits into from
Apr 23, 2025

Conversation

hashar
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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
@hashar
Copy link
Contributor Author

hashar commented Apr 14, 2025

I have addressed the issues pointed by @wkania .

Looks like this is now pending on @joshgoebel to acknowledge my answer to their remark at #4230 (review) ?

Copy link

Build Size Report

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

9 files changed

Total change +10 B

View Changes
file base pr diff
es/core.min.js 8.19 KB 8.19 KB +1 B
es/highlight.min.js 8.19 KB 8.19 KB +1 B
es/languages/javascript.min.js 2.74 KB 2.74 KB -2 B
es/languages/php.min.js 2.65 KB 2.66 KB +9 B
es/languages/typescript.min.js 3.21 KB 3.21 KB -3 B
highlight.min.js 8.23 KB 8.23 KB +2 B
languages/javascript.min.js 2.75 KB 2.74 KB -3 B
languages/php.min.js 2.66 KB 2.66 KB +8 B
languages/typescript.min.js 3.22 KB 3.21 KB -3 B

Copy link

Build Size Report

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

2 files changed

Total change +17 B

View Changes
file base pr diff
es/languages/php.min.js 2.65 KB 2.66 KB +9 B
languages/php.min.js 2.66 KB 2.66 KB +8 B

@joshgoebel joshgoebel merged commit d864744 into highlightjs:main Apr 23, 2025
19 checks passed
ewwwin pushed a commit to ewwwin/highlight.js that referenced this pull request Apr 24, 2025
…js#4230)

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

Co-authored-by: Josh Goebel <[email protected]>
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