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

Fails to parse classes with typed constants #1133

Closed
davidrans opened this issue May 9, 2024 · 6 comments · Fixed by #1136
Closed

Fails to parse classes with typed constants #1133

davidrans opened this issue May 9, 2024 · 6 comments · Fixed by #1136

Comments

@davidrans
Copy link

It looks like files that have typed constants are not parseable by the latest version (3.1.5).

Input file:

<?php

class Test {
   private const int MY_CONST = 3;
}

Error:

SyntaxError: Parse Error : syntax error, unexpected 'MY_CONST' (T_STRING), expecting '=' on line 4
    at Parser.raiseError (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser.js:349:17)
    at Parser.error (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser.js:397:15)
    at Parser.expect (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser.js:577:10)
    at Parser.read_constant_declaration (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/class.js:261:18)
    at Parser.read_list (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/utils.js:76:33)
    at Parser.read_constant_list (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/class.js:237:24)
    at Parser.read_class_body (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/class.js:117:32)
    at Parser.read_class_declaration_statement (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/class.js:32:30)
    at Parser.read_top_statement (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/statement.js:52:21)
    at Parser.read_start (/home/user/Code/node_modules/.pnpm/[email protected]/node_modules/php-parser/src/parser/main.js:18:19) {
  lineNumber: 4,
  fileName: 'eval',
  columnNumber: 21
}
@Kenneth-Sills
Copy link
Contributor

Kenneth-Sills commented Jun 24, 2024

At a glance, this conditional needs to be updated to support type hint tokens proceeded by a constant list, then read_constant_declaration needs to be updated to use read_optional_type.1 Finally, we'll need to throw that information into the ClassConstant constructor.

Traits, interfaces, and enums go through this path, so should be supported implicitly. So as long as there's no complications, that should be all that's necessary.

@czosel Are y'all currently accepting PRs? It's been a bit since an update, so want to confirm before embarking on any work.

Footnotes

  1. I'm not too familiar with this library and what it would consider a parser error vs runtime error, but it's worth noting that there are limitations on allowed types.

@cseufert
Copy link
Collaborator

PR would be great

@genintho
Copy link
Collaborator

The code merged is not working if the class constant does not contain a type.

Example 'class Foo { public const CONSTANT = "Hello world!"; }'

classconstant › type hinted (supported)
SyntaxError: Parse Error : syntax error, unexpected '=' on line 1
347 | message += " on line " + this.lexer.yylloc.first_line;
348 | if (!this.suppressErrors) {
> 349 | const err = new SyntaxError(
| ^
350 | message,
351 | this.filename,
352 | this.lexer.yylloc.first_line,

  at Parser.Object.<anonymous>.Parser.raiseError (src/parser.js:349:17)
  at Parser.raiseError [as error] (src/parser.js:397:15)
  at Parser.error [as expect] (src/parser.js:577:10)

Working on a fix

@genintho
Copy link
Collaborator

PR opened

@Kenneth-Sills
Copy link
Contributor

Thank you, @genintho! Apologies for not adding more test cases; 100% should have caught this. Let me know if you need any help with the fixes!

@genintho
Copy link
Collaborator

genintho commented Dec 4, 2024

@Kenneth-Sills No worries, I think this PR fixes it. #1147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants