Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(fortran) Add support for FORTRAN 77 style comments #2416

Merged
merged 7 commits into from Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -55,24 +86,19 @@ export default function(hljs) {
return {
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,
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
},
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'