stadelmanma / tree-sitter-fortran

Fortran grammar for tree-sitter
MIT License
30 stars 15 forks source link

Parser error when assigning to a keyword #53

Closed stadelmanma closed 1 year ago

stadelmanma commented 3 years ago

Fortran allows any variable to be assigned to and we get an error when that occurs with a keyword. See the pared down snippet from the Fire Models FDS code base (file: pois.f90, line 6115).

SUBROUTINE FSH08S(AN,CN,B,COEF,T)

IF = 2**KAPPA

end subroutine
ZedThree commented 1 year ago

This also seems to apply to identifiers like double which form part of white-spaced keywords like double precision:

elemental integer function double(x)
  integer, intent(in) :: x
  double = 2 * x
end function double
ZedThree commented 1 year ago

I asked about this in the tree-sitter discussion forum, and the solution is to basically add all the keywords to identifier:

    identifier: $ => choice(
      /[a-zA-Z_]\w*/,
      caseInsensitive('data'),
      caseInsensitive('double'),
      caseInsensitive('elseif'),
      caseInsensitive('end'),
      caseInsensitive('endif'),
      caseInsensitive('error'),
      caseInsensitive('exit'),
      caseInsensitive('if'),
      caseInsensitive('select'),
      caseInsensitive('stop'),
      caseInsensitive('value'),
    )

Quite a few of these then needed the rules also adding to conflicts, but I have been able to get this to work. It's a bit of a pain if we have to list all the keywords, though I'm not certain we do need everything.