Fortran syntax highlighting: distinguish free format and fixed format

Review Request #129569 - Created Nov. 28, 2016 and updated

Information
Janus Weil
syntax-highlighting
372950
Reviewers
kate
See https://bugs.kde.org/show_bug.cgi?id=372950 for bug description and https://github.com/janusw/syntax-highlighting/commits/master for three separate commits implementing this.

Description copied from bugzilla:

Kate currently seems to have only one syntax-highlighting mode for Fortran. However, in the Fortran language there are two different source formats:
* fixed format (from the old Fortran77 standard, extension .f)
* free format (introduced with Fortran90, extension .f90)

The keywords etc are the same, but one thing that differs is commenting: In fixed format every line with a 'c' in the first column is a comment, while in free format comments start with an '!' (in any column).

Both of these mechanisms seem to be supported in Kate 16.04.3 on Ubuntu 16.10 (with Fortran highlighting file v2). The problem is that lines starting with 'c' are also displayed like a comment in f90 files (where only '!' indicates a comment).

Simple example ('test.f90'):


! this is a test program that does absolutely nothing
program test
contains
  subroutine s
  end subroutine
end program


The first line is greyed out, which is correct, but also the third line ('contains') is greyed out, which is wrong. It is displayed normally if a space is inserted before 'contains.'

Opened a few different files (.f, .f90, .f03, .f08) in Kate and verified that the comments show up as expected and all artefacts disappear. A good source for test files is the testsuite of the gfortran compiler ( see e.g. https://github.com/gcc-mirror/gcc/tree/master/gcc/testsuite/gfortran.dg ).

Janus Weil
Review request changed

Testing Done:

~  

Opened a few different files (.f, .f90, .f03, .f08) in Kate and verified that the comments show up as expected and all artefacts disappear. A good source for test files is the testsuite of the gfortran compiler (see e.g. https://github.com/gcc-mirror/gcc/tree/master/gcc/testsuite/gfortran.dg).

  ~

Opened a few different files (.f, .f90, .f03, .f08) in Kate and verified that the comments show up as expected and all artefacts disappear. A good source for test files is the testsuite of the gfortran compiler ( see e.g. https://github.com/gcc-mirror/gcc/tree/master/gcc/testsuite/gfortran.dg ).

Dominik Haumann

Hi Janus, sorry for the long delay.

In general, the patch looks good.

I have some comments, though, maybe we can improve the following:
1. automated tests: Running make test, the highlight.f90 does not not work anymore. This is easily fixed by running in the build folder ./autotests/update-reference-data.sh. However, I think since contents is for .f90 and for .f, the test itself is not correct anymore everywhere. --> Could you copy the highligh.f90 to highlight.f and adapt both files so that the contents makes sense again? You can verify the output by looking at the generated .html files.
2. The file contents now are almost identical, so we have quite some duplicated code. In theorey, it should be possible to use IncludeRules to include highlighting rules from a different highlighting .xml file. Could you maybe try this as well in order to avoid the code duplication? Just grep for IncludeRules containing a '##' or similar, you should also be able to include just include rules from a dedicated context. See also IncludeRules at https://docs.kde.org/stable5/en/applications/katepart/highlight.html

If we cannot fix 2. properly, I'd also accept the patch as is. But please have a look into 1. again, since the unit tests are really important. Thanks!

  1. ping? :-)

  2. Hi Dominik,
    thanks for the comments. I agree about the code-duplication problem, but I'm just not very familiar with the XML structure of the highlighting files. If I get it right, the idea would be to have one XML with the "common" Fortran rules (which apply both to fixed and free format), and then import this into two new XML files (for fixed and free format, respectively) and add more specific rules there (e.g. for the comments)? If you could help me get started with this, that would be a great help. If not I'll try it myself, but it might take a week or two until I get to it. At least I can promise that, after this problem is solved, I can contribute more additions regarding modern Fortran dialects, like Fortran 2003 and 2008 keywords etc. I have already implemented a few such things on my github branch: https://github.com/janusw/syntax-highlighting/commits/Fortran_2003_2008
    Cheers,
    Janus

  3. Ok, there is no need to hurry. I just would like to avoid that this patch gets lost over time :-)

    And yes, the idea is to avoid code duplication. And yes, you get it right that you'd just include the rules of a context from another file. There are plenty of examples in the other highlighting files. Just search the other files, and read the docs of the link above.

Gregor Mi

How is this patch coming along? Please consider moving it to Phabricator since Reviewboard is closed down soon.

Loading...