tree-sitter/tree-sitter-php

feature: field names for for

akimd opened this issue · 1 comments

Did you check the tree-sitter docs?

Is your feature request related to a problem? Please describe.

A for-loop has several optional components. To find each element, one currently needs to count the semi-colons, which is quite inconvenient. Field names are most useful in that case, as demonstrated below with the Java grammar.

$ cat /tmp/for.java
for (init; cond; incr) {}
for (    ; cond; incr) {}
for (init;     ; incr) {}
for (init; cond;     ) {}
$ tree-sitter parse /tmp/for.java
(program [0, 0] - [4, 0]
  (for_statement [0, 0] - [0, 25]
    init: (identifier [0, 5] - [0, 9])
    condition: (identifier [0, 11] - [0, 15])
    update: (identifier [0, 17] - [0, 21])
    body: (block [0, 23] - [0, 25]))
  (for_statement [1, 0] - [1, 25]
    condition: (identifier [1, 11] - [1, 15])
    update: (identifier [1, 17] - [1, 21])
    body: (block [1, 23] - [1, 25]))
  (for_statement [2, 0] - [2, 25]
    init: (identifier [2, 5] - [2, 9])
    update: (identifier [2, 17] - [2, 21])
    body: (block [2, 23] - [2, 25]))
  (for_statement [3, 0] - [3, 25]
    init: (identifier [3, 5] - [3, 9])
    condition: (identifier [3, 11] - [3, 15])
    body: (block [3, 23] - [3, 25])))
$ cat /tmp/for.php
<?php
for (init; cond; incr) {}
for (    ; cond; incr) {}
for (init;     ; incr) {}
for (init; cond;     ) {}
?>
$ tree-sitter parse /tmp/for.php
(program [0, 0] - [6, 0]
  (php_tag [0, 0] - [0, 5])
  (for_statement [1, 0] - [1, 25]
    (name [1, 5] - [1, 9])
    (name [1, 11] - [1, 15])
    (name [1, 17] - [1, 21])
    (compound_statement [1, 23] - [1, 25]))
  (for_statement [2, 0] - [2, 25]
    (name [2, 11] - [2, 15])
    (name [2, 17] - [2, 21])
    (compound_statement [2, 23] - [2, 25]))
  (for_statement [3, 0] - [3, 25]
    (name [3, 5] - [3, 9])
    (name [3, 17] - [3, 21])
    (compound_statement [3, 23] - [3, 25]))
  (for_statement [4, 0] - [4, 25]
    (name [4, 5] - [4, 9])
    (name [4, 11] - [4, 15])
    (compound_statement [4, 23] - [4, 25]))
  (text_interpolation [5, 0] - [5, 2]))

Describe the solution you'd like

Have field names as in Java.

Describe alternatives you've considered

Count semi-colons.

Additional context

Cheers!

Thanks!