Skip to content

Commit

Permalink
enh(fortran) Add support for FORTRAN 77 style comments (#2416)
Browse files Browse the repository at this point in the history
Closes #2310.
  • Loading branch information
joshgoebel committed Feb 23, 2020
1 parent 83996aa commit d2e7493
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -16,6 +16,7 @@ Core Changes:

Language Improvements:

- enh(fortran) Support Fortran 77 style comments (#2416) [Josh Goebel][]
- (csharp) add support for `@identifier` style identifiers (#2414) [Josh Goebel][]
- fix(elixir) Support function names with a slash (#2406) [Josh Goebel][]
- fix(javascript) comma is allowed in a "value container" (#2403) [Josh Goebel][]
Expand Down
60 changes: 43 additions & 17 deletions src/languages/fortran.js
Expand Up @@ -6,12 +6,43 @@ Category: scientific
*/

export default function(hljs) {
var PARAMS = {
const PARAMS = {
className: 'params',
begin: '\\(', end: '\\)'
};

var F_KEYWORDS = {
const COMMENT = {
variants: [
hljs.COMMENT('!', '$', {relevance: 0}),
// allow Fortran 77 style comments
hljs.COMMENT('^C', '$', {relevance: 0})
]
};

const NUMBER = {
className: 'number',
// regex in both fortran and irpf90 should match
begin: '(?=\\b|\\+|\\-|\\.)(?:\\.|\\d+\\.?)\\d*([de][+-]?\\d+)?(_[a-z_\\d]+)?',
relevance: 0
};

const FUNCTION_DEF = {
className: 'function',
beginKeywords: 'subroutine function program',
illegal: '[${=\\n]',
contains: [hljs.UNDERSCORE_TITLE_MODE, PARAMS]
};

const STRING = {
className: 'string',
relevance: 0,
variants: [
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE
]
};

const KEYWORDS = {
literal: '.False. .True.',
keyword: 'kind do concurrent local shared while private call intrinsic where elsewhere ' +
'type endtype endmodule endselect endinterface end enddo endif if forall endforall only contains default return stop then block endblock endassociate ' +
Expand Down Expand Up @@ -46,7 +77,7 @@ export default function(hljs) {
'set_exponent shape size spacing spread sum system_clock tiny transpose trim ubound unpack verify achar iachar transfer ' +
'dble entry dprod cpu_time command_argument_count get_command get_command_argument get_environment_variable is_iostat_end ' +
'ieee_arithmetic ieee_support_underflow_control ieee_get_underflow_mode ieee_set_underflow_mode ' +
'is_iostat_eor move_alloc new_line selected_char_kind same_type_as extends_type_of' +
'is_iostat_eor move_alloc new_line selected_char_kind same_type_as extends_type_of ' +
'acosh asinh atanh bessel_j0 bessel_j1 bessel_jn bessel_y0 bessel_y1 bessel_yn erf erfc erfc_scaled gamma log_gamma hypot norm2 ' +
'atomic_define atomic_ref execute_command_line leadz trailz storage_size merge_bits ' +
'bge bgt ble blt dshiftl dshiftr findloc iall iany iparity image_index lcobound ucobound maskl maskr ' +
Expand All @@ -56,24 +87,19 @@ export default function(hljs) {
name: 'Fortran',
case_insensitive: true,
aliases: ['f90', 'f95'],
keywords: F_KEYWORDS,
keywords: KEYWORDS,
illegal: /\/\*/,
contains: [
hljs.inherit(hljs.APOS_STRING_MODE, {className: 'string', relevance: 0}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {className: 'string', relevance: 0}),
STRING,
FUNCTION_DEF,
// allow `C = value` for assignments so they aren't misdetected
// as Fortran 77 style comments
{
className: 'function',
beginKeywords: 'subroutine function program',
illegal: '[${=\\n]',
contains: [hljs.UNDERSCORE_TITLE_MODE, PARAMS]
begin: /^C\s*=(?!=)/,
relevance: 0,
},
hljs.COMMENT('!', '$', {relevance: 0}),
{
className: 'number',
// regex in both fortran and irpf90 should match
begin: '(?=\\b|\\+|\\-|\\.)(?:\\.|\\d+\\.?)\\d*([de][+-]?\\d+)?(_[a-z_\\d]+)?',
relevance: 0
}
COMMENT,
NUMBER
]
};
}
13 changes: 13 additions & 0 deletions test/markup/fortran/comments.expect.txt
@@ -0,0 +1,13 @@
<span class="hljs-comment">C</span>
<span class="hljs-comment">C This program in FORTRAN 77 outputs "Hello, World!".</span>
<span class="hljs-comment">C ====================================================</span>

<span class="hljs-comment">!===============================</span>
<span class="hljs-comment">! This is a test subroutine</span>
<span class="hljs-comment">!===============================</span>

<span class="hljs-comment">! another comment</span>

C=<span class="hljs-number">2</span>
C = <span class="hljs-number">2</span> <span class="hljs-comment">!correct</span>
C =<span class="hljs-string">'boo'</span>
13 changes: 13 additions & 0 deletions test/markup/fortran/comments.txt
@@ -0,0 +1,13 @@
C
C This program in FORTRAN 77 outputs "Hello, World!".
C ====================================================

!===============================
! This is a test subroutine
!===============================

! another comment

C=2
C = 2 !correct
C ='boo'

0 comments on commit d2e7493

Please sign in to comment.