tree-sitter / tree-sitter/tree-sitter-php

bug: constant declarations with names matching certain reserved words result in parsing errors

Open
#295 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
235
Forks
74
PR merge metrics
No merged PRs in 30d

Description

Did you check existing issues?
  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-php
Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.26.3

Describe the bug

Constant declarations with names matching certain reserved words result in parsing errors:

After briefly checking the members of BASE_RESERVED_KEYWORD_SET and OTHER_RESERVED_KEYWORD_SET from common/define-grammar.js, I found the following names to be problematic:

callable
namespace
void
false
mixed
true

I suspect this is due to the reserved words being created as regexes with the case insensitive "i" flag here:

https://github.com/tree-sitter/tree-sitter-php/blob/7d07b41ce2d442ca9a90ed85d0075eccc17ae315/common/define-grammar.js#L1665-L1670

It's not clear to me why some reserved words conflict and others don't.

Steps To Reproduce/Bad Parse Tree

input:

<?php
const CALLABLE = 'callable';
const NAMESPACE = 'namespace';

const VOID = 'void';
const FALSE = 'false';
const MIXED = 'mixed';
const TRUE = 'true';

Resulting s-expression/cst:

(program [0, 0] - [8, 0]
  (php_tag [0, 0] - [0, 5])
  (ERROR [1, 0] - [1, 16])
  (expression_statement [1, 17] - [1, 28]
    (string [1, 17] - [1, 27]
      (string_content [1, 18] - [1, 26])))
  (ERROR [2, 0] - [2, 17])
  (expression_statement [2, 18] - [2, 30]
    (string [2, 18] - [2, 29]
      (string_content [2, 19] - [2, 28])))
  (ERROR [4, 0] - [4, 12])
  (expression_statement [4, 13] - [4, 20]
    (string [4, 13] - [4, 19]
      (string_content [4, 14] - [4, 18])))
  (ERROR [5, 0] - [5, 13])
  (expression_statement [5, 14] - [5, 22]
    (string [5, 14] - [5, 21]
      (string_content [5, 15] - [5, 20])))
  (ERROR [6, 0] - [6, 13])
  (expression_statement [6, 14] - [6, 22]
    (string [6, 14] - [6, 21]
      (string_content [6, 15] - [6, 20])))
  (ERROR [7, 0] - [7, 12])
  (expression_statement [7, 13] - [7, 20]
    (string [7, 13] - [7, 19]
      (string_content [7, 14] - [7, 18]))))
0:0  - 8:0    •program
0:0  - 0:5      php_tag `<?php`
1:0  - 1:16     •ERROR
1:0  - 1:5        "const"
1:15 - 1:16       "="
1:17 - 1:28     expression_statement
1:17 - 1:27       string
1:17 - 1:18         "'"
1:18 - 1:26         string_content `callable`
1:26 - 1:27         "'"
1:27 - 1:28       ";"
2:0  - 2:17     •ERROR
2:0  - 2:5        "const"
2:6  - 2:15       "namespace"
2:16 - 2:17       "="
2:18 - 2:30     expression_statement
2:18 - 2:29       string
2:18 - 2:19         "'"
2:19 - 2:28         string_content `namespace`
2:28 - 2:29         "'"
2:29 - 2:30       ";"
4:0  - 4:12     •ERROR
4:0  - 4:5        "const"
4:11 - 4:12       "="
4:13 - 4:20     expression_statement
4:13 - 4:19       string
4:13 - 4:14         "'"
4:14 - 4:18         string_content `void`
4:18 - 4:19         "'"
4:19 - 4:20       ";"
5:0  - 5:13     •ERROR
5:0  - 5:5        "const"
5:12 - 5:13       "="
5:14 - 5:22     expression_statement
5:14 - 5:21       string
5:14 - 5:15         "'"
5:15 - 5:20         string_content `false`
5:20 - 5:21         "'"
5:21 - 5:22       ";"
6:0  - 6:13     •ERROR
6:0  - 6:5        "const"
6:12 - 6:13       "="
6:14 - 6:22     expression_statement
6:14 - 6:21       string
6:14 - 6:15         "'"
6:15 - 6:20         string_content `mixed`
6:20 - 6:21         "'"
6:21 - 6:22       ";"
7:0  - 7:12     •ERROR
7:0  - 7:5        "const"
7:11 - 7:12       "="
7:13 - 7:20     expression_statement
7:13 - 7:19       string
7:13 - 7:14         "'"
7:14 - 7:18         string_content `true`
7:18 - 7:19         "'"
7:19 - 7:20       ";"

Originally discussed here: https://github.com/tree-sitter/tree-sitter-php/pull/293#discussion_r2704961190

Expected Behavior/Parse Tree

The code should parse correctly as a const_declaration node.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied PHP input with tree-sitter 0.26.3, then inspect the reserved-word definitions in common/define-grammar.js around lines 1665-1670. Compare the affected names with other reserved words and verify that each declaration parses as a const_declaration node without ERROR nodes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, php
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.