From 1335ccd36768196189d9229aff5437f424e2e3c8 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 07:25:23 -0500 Subject: [PATCH 1/6] enh(fortran) Add support for FORTRAN 77 style comments Closes #2310. --- src/languages/fortran.js | 16 +++++++++++++++- test/markup/fortran/comments.expect.txt | 13 +++++++++++++ test/markup/fortran/comments.txt | 13 +++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/markup/fortran/comments.expect.txt create mode 100644 test/markup/fortran/comments.txt diff --git a/src/languages/fortran.js b/src/languages/fortran.js index cbe33fe642..e2ddbe8492 100644 --- a/src/languages/fortran.js +++ b/src/languages/fortran.js @@ -11,6 +11,14 @@ export default function(hljs) { begin: '\\(', end: '\\)' }; + var COMMENT = { + variants: [ + hljs.COMMENT('!', '$', {relevance: 0}), + // allow Fortran 77 style comments + hljs.COMMENT('^C', '$', {relevance: 0}) + ] + } + var F_KEYWORDS = { literal: '.False. .True.', keyword: 'kind do concurrent local shared while private call intrinsic where elsewhere ' + @@ -66,7 +74,13 @@ export default function(hljs) { illegal: '[${=\\n]', contains: [hljs.UNDERSCORE_TITLE_MODE, PARAMS] }, - hljs.COMMENT('!', '$', {relevance: 0}), + // allow C = value for assignments so they aren't misdetected + // as Fortran 77 style comments + { + begin: /^C\s*=(?!=)/, + relevance:0, + }, + COMMENT, { className: 'number', // regex in both fortran and irpf90 should match diff --git a/test/markup/fortran/comments.expect.txt b/test/markup/fortran/comments.expect.txt new file mode 100644 index 0000000000..ba78a37f63 --- /dev/null +++ b/test/markup/fortran/comments.expect.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' diff --git a/test/markup/fortran/comments.txt b/test/markup/fortran/comments.txt new file mode 100644 index 0000000000..6127dbc32e --- /dev/null +++ b/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' From 6554a56b8de3cc464bd2cb782baef49bd621b98b Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 07:30:09 -0500 Subject: [PATCH 2/6] cleanup grammar --- src/languages/fortran.js | 50 +++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/languages/fortran.js b/src/languages/fortran.js index e2ddbe8492..c10c52e448 100644 --- a/src/languages/fortran.js +++ b/src/languages/fortran.js @@ -6,20 +6,43 @@ Category: scientific */ export default function(hljs) { - var PARAMS = { + const PARAMS = { className: 'params', begin: '\\(', end: '\\)' }; - var COMMENT = { + 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 + ] + }; - var F_KEYWORDS = { + 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 ' + @@ -63,17 +86,11 @@ 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}), - { - className: 'function', - beginKeywords: 'subroutine function program', - illegal: '[${=\\n]', - contains: [hljs.UNDERSCORE_TITLE_MODE, PARAMS] - }, + STRING, + FUNCTION_DEF, // allow C = value for assignments so they aren't misdetected // as Fortran 77 style comments { @@ -81,12 +98,7 @@ export default function(hljs) { relevance:0, }, COMMENT, - { - className: 'number', - // regex in both fortran and irpf90 should match - begin: '(?=\\b|\\+|\\-|\\.)(?:\\.|\\d+\\.?)\\d*([de][+-]?\\d+)?(_[a-z_\\d]+)?', - relevance: 0 - } + NUMBER ] }; } From 3408bd46d76985e91f9709e036fa1b356c61d044 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 07:30:42 -0500 Subject: [PATCH 3/6] fixup first commit --- src/languages/fortran.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/fortran.js b/src/languages/fortran.js index c10c52e448..bdfa9db167 100644 --- a/src/languages/fortran.js +++ b/src/languages/fortran.js @@ -91,7 +91,7 @@ export default function(hljs) { contains: [ STRING, FUNCTION_DEF, - // allow C = value for assignments so they aren't misdetected + // allow `C = value` for assignments so they aren't misdetected // as Fortran 77 style comments { begin: /^C\s*=(?!=)/, From 847c981bb50596b0c885087c337efc7c445ecd06 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Thu, 20 Feb 2020 07:31:47 -0500 Subject: [PATCH 4/6] add missing space --- src/languages/fortran.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/fortran.js b/src/languages/fortran.js index bdfa9db167..5ecc8301a5 100644 --- a/src/languages/fortran.js +++ b/src/languages/fortran.js @@ -77,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 ' + From e4d71e63d305a26fab42ada9c2362ff2a76e6d9d Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 21 Feb 2020 20:55:58 -0500 Subject: [PATCH 5/6] changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 3bb58c9569..b02fc8fc40 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Core Changes: Language Improvements: +- enh(fortran) Support Fortran 77 style comments (#2416) [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][] - enh(apache) add `deny` and `allow` keywords [Josh Goebel][] From ae362536c512b2e7cc72d9fb309237c8c6ae104a Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 23 Feb 2020 01:05:38 -0500 Subject: [PATCH 6/6] Update src/languages/fortran.js Co-Authored-By: Vladimir Jimenez --- src/languages/fortran.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/fortran.js b/src/languages/fortran.js index 5ecc8301a5..5be3d3a89c 100644 --- a/src/languages/fortran.js +++ b/src/languages/fortran.js @@ -95,7 +95,7 @@ export default function(hljs) { // as Fortran 77 style comments { begin: /^C\s*=(?!=)/, - relevance:0, + relevance: 0, }, COMMENT, NUMBER