Skip to content

fix PHP templates#144

Merged
DmitrySoshnikov merged 1 commit into
DmitrySoshnikov:masterfrom
rodrigopedra:master
Jan 3, 2024
Merged

fix PHP templates#144
DmitrySoshnikov merged 1 commit into
DmitrySoshnikov:masterfrom
rodrigopedra:master

Conversation

@rodrigopedra

@rodrigopedra rodrigopedra commented Oct 11, 2023

Copy link
Copy Markdown
Contributor

This PR fixes some errors in PHP templates.

On src/plugins/php/templates/tokenizer.template.php

  • Adds a missing $ to the $state variable on method Tokenizer::begin()

On src/plugins/php/templates/ll.template.php

  • Uses call_user_func() to call static::$on_parse_begin hook, instead of assigning it to a temporary variable
  • Also changes the check to verify if static::$on_parse_begin is a callable using is_callable (available since PHP 5.3)
  • Changes the exception message thrown on line 71 (of the original code) to use single quotes instead of backticks. Backticks are used in PHP to execute shell code, and can be disabled through a php.ini directive

On src/plugins/php/templates/lr.template.php

  • Fixes line 134 to return $start instead of a non-existent $static variable, within the check to see if either $start or $end are valid values
  • Uses call_user_func() to call static::$on_parse_begin hook, instead of assigning it to a temporary variable
  • Also changes the check to verify if static::$on_parse_begin is a callable using is_callable (available since PHP 4.0.6)
  • Changes the exception message thrown on line 176 (of the original code) to use single quotes instead of backticks. Backticks are used in PHP to execute shell code, and can be disabled through a php.ini directive
  • Uses get_class() instead of the 'self' string when calling forward_static_call_array() on line 270 (of original code). Using the string 'self' and 'static' on callable array syntax is deprecated as of PHP 8.2, and generates a warning. get_class() is available since PHP 4
  • Uses call_user_func() to call static::$on_parse_end hook, instead of assigning it to a temporary variable
  • Also changes the check to verify if static::$on_parse_end is a callable using is_callable (available since PHP 4.0.6)

Notes:

I wasn't sure which PHP version the parser generator targets, so I tried to keep the most backwards-compatible approach possible.

From the built-in functions used, forward_static_call() was introduced in PHP 5.3, that is why I replaced array('self', $production[2]), by array(get_class(), $production[2]) - to avoid a deprecation warning on PHP 8.2 - instead of replacing it by array(self::class, $production[2]), as ::class notation was introduced in PHP 5.5.

I had to skip git hooks when committing and pushing, as tests were failing. But they were already failing when I ran npm ci to install dependencies, and none of the failures were related to the PHP template changes I made.

I can try fixing those errors, if you'd like me to, but I prefer to do it on a separate PR, as none of them are related to the changes on this PR.

References:

Epilogue

By the way, thanks for this tool. =)

I discovered it when watching your "Building a Parser from Scratch" course, and was very surprised to discover it supported PHP.

Greetings from Brazil.

@DmitrySoshnikov DmitrySoshnikov merged commit b330831 into DmitrySoshnikov:master Jan 3, 2024
@DmitrySoshnikov

Copy link
Copy Markdown
Owner

Thanks for the contributions!

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.

2 participants