dparkins / language-fortran

Syntax highlighting for FORTRAN for atom
MIT License
35 stars 16 forks source link

Highlighting fail in non-standard declarations #107

Open Edmundod opened 6 years ago

Edmundod commented 6 years ago

Hello,

is it possible to recognize and highlight declarations by looking for ::? I use library which defines it's own variable types and in that case highlighting fails. But if I use any attribute, than at least :: is colored fine.

img

Test code:

program test
  implicit none
  integer         :: a
  real            :: b
  Vec             :: vector
  Vec, intent(in) :: vector_in

end program test
tomedunn commented 6 years ago

It's definitely possible to do this, however I don't know if it's worth supporting non-standard formatting. The standard way of writing this would be

type(Vec) :: vector

which is correctly highlighted

screen shot 2018-09-07 at 9 14 05 am

Also, without a fixed keyword, such as integer, to start off the statement, any rule for doing this would likely fail on statements spanning multiple lines, since Atom's grammar engine can't look ahead across lines.

If this is something you really want just for your own use then here is a rule you can add to the file fortran - free form.cson that should accomplish the basic task your looking for.

  'unknown-specification-statements':
    'name': 'meta.specification.type.unknown.fortran'
    'begin': '(?ix)(?=\\b[^\'";!\\n]*::)'
    'end': '(?=[\\);!\\n])'
    'patterns':[
      {
        'match': '(?i)\\G\\s*\\b([a-z]\\w*)\\b'
        'captures':
          '1': 'name': 'storage.type.unknown.fortran'
      }
      {
        'comment': 'Attribute list.'
        'contentName': 'meta.attribute-list.fortran'
        'begin': '(?=\\s*(,|::))'
        'end': '(::)|(?=[;!\\n])'
        'endCaptures':
          '1': 'name': 'keyword.operator.double-colon.fortran'
        'patterns':[
          {
            'begin': '(,)|^|(?<=&)'
            'beginCaptures':
              '1': 'name': 'punctuation.comma.fortran'
            'end': '(?=::|[,&;!\\n])'
            'patterns':[
              {'include': '#access-attribute'}
              {'include': '#allocatable-attribute'}
              {'include': '#asynchronous-attribute'}
              {'include': '#codimension-attribute'}
              {'include': '#contiguous-attribute'}
              {'include': '#dimension-attribute'}
              {'include': '#external-attribute'}
              {'include': '#intent-attribute'}
              {'include': '#intrinsic-attribute'}
              {'include': '#language-binding-attribute'}
              {'include': '#optional-attribute'}
              {'include': '#parameter-attribute'}
              {'include': '#pointer-attribute'}
              {'include': '#protected-attribute'}
              {'include': '#save-attribute'}
              {'include': '#target-attribute'}
              {'include': '#value-attribute'}
              {'include': '#volatile-attribute'}
              {'include': '#invalid-word'}
            ]
          }
        ]
      }
      {'include': '#name-list'}
    ]

You'll also need to add the following line in the 'patterns' section of the same file.

  {'include': '#unknown-specification-statements'}
Edmundod commented 6 years ago

The solution with type declaration will be the best in this case. Thank you very much!