Skip to content

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

@WillLillis

Description

@WillLillis

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:

function keyword(word, aliasAsWord = true) {
/** @type {RegExp|AliasRule} */
let result = new RegExp(word, 'i');
if (aliasAsWord) result = alias(result, word);
return result;
}

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: #293 (comment)

Expected Behavior/Parse Tree

The code should parse correctly as a const_declaration node.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions