From bf073e1004a3508323fa6f3b438ef16ee31b2a82 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 22 Jul 2016 15:17:20 +0100 Subject: [PATCH 1/7] Add support for Fortran --- src/lang-fortran.js | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/lang-fortran.js diff --git a/src/lang-fortran.js b/src/lang-fortran.js new file mode 100644 index 00000000..5abe0873 --- /dev/null +++ b/src/lang-fortran.js @@ -0,0 +1,61 @@ +/** + * @license + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview + * Registers a language handler for Fortran + * + * To use, include prettify.js and this file in your HTML page. + * Then put your code in an HTML tag like + *
(fortran code)
+ * + * I used lang-lisp.js as a template and The Fortran + * Wiki for the list of keywords + * + * Should work for the majority of Fortran versions + * + * @author zed.three@gmail.com + */ + +PR['registerLangHandler']( + PR['createSimpleLexer']( + [ + // A line comment that starts with ! + [PR['PR_COMMENT'], /^![^\r\n]*/, null, '!'], + // Whitespace + [PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], + // Strings may use embedded double delimiters to represent + // a single character of that delimiter + [PR['PR_STRING'], /^\"(?:([^\\"\r\n]|\"\")*\")/, null, '\"'], + [PR['PR_STRING'], /^\'(?:([^\'\r\n]|\'\')*\')/, null, '\''] + ], + [ + // Keywords from all versions of Fortran + [PR['PR_KEYWORD'], /^(?:abstract|allocatable|allocate|all *stop|assign|asynchronous|backspace|bind|call|case|class|close|codimension|common|contains|contiguous|continue|cycle|data|deallocate|deferred|dimension|do *,? *concurrent|elemental|entry|enumerator|equivalence|error stop|exit|extends|external|final|flush|format|generic|go *to|implicit|import|in *out|include|inquire|intent|intrinsic|lock|namelist|nopass|nullify|only|open|operator|optional|overridable|parameter|pass|pause|pointer|print|private|protected|public|pure|read|recursive|result|return|rewind|rewrite|non_save|sequence|stop|target|then|unlock|use|value|volatile|wait|where|while|write)\b/i], + // Block beginning/end statements (optional spaces) + [PR['PR_KEYWORD'], /^(?:end *)?(?:associate|block|block *data|critical|do|enum|file|forall|function|if|interface|module|procedure|program|select|submodule|subroutine|type|where)\b/i], + [PR['PR_KEYWORD'], /^(?:sync *)(?:all|images|memory)/i], + [PR['PR_KEYWORD'], /^(?:else *)(?:if|where)?/i], + // User defined .operators. + [PR['PR_KEYWORD'], /^\.\w*\./i], + [PR['PR_TYPE'], /^(?:character|complex|double *precision|integer|real|type)\b/i], + [PR['PR_LITERAL'], /^[+\-]?\.?\d+(?:\.\d*)?(?:[EeDd][+\-]?\d+)?/], + [PR['PR_PUNCTUATION'], /^[+\-/*=^&|<>%[\]()?:.,]/ ] + ] + ), + ['f', 'f90', 'F', 'F90', 'fortran']); From 4338010e4fbe649d4a2d494c366bdab7dadf71a3 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Fri, 22 Jul 2016 17:51:54 +0100 Subject: [PATCH 2/7] Basic Fortran test case --- tests/prettify_test_2.html | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/prettify_test_2.html b/tests/prettify_test_2.html index 4abef9e8..c589eb8b 100644 --- a/tests/prettify_test_2.html +++ b/tests/prettify_test_2.html @@ -12,6 +12,7 @@ // Language extensions tested. "lang-clj.js", "lang-dart.js", + "lang-fortran.js", "lang-lisp.js", "lang-llvm.js", "lang-matlab.js", @@ -1066,6 +1067,51 @@

Issue 32 : Matlab

%} +

Fortran

+
program foo
+  use, intrinsic :: iso_c_binding
+  implicit none
+  type, bind(C) :: ball
+    integer(kind=c_int) :: x
+    integer(kind=c_int) :: y
+    real(kind=c_float) :: mass
+  end type ball
+
+  integer, parameter :: foo = 12 ! inline comment
+  real bar = 4.0e0
+  complex, dimension(4, 5) :: zing
+  character(len=5), dimension(:), allocatable :: ding
+  ! In this context, 'type()' is a type, not a keyword
+  type(ball) :: football
+
+  football%x = foo
+
+  ! Allocate only if not allocated
+  if (.not. allocated(ding)) then
+    allocate(ding(12))
+  else
+    write(*,'(A)') 'can''t allocate ding: ding already allocated'
+  end if
+  ding(1) = "hello"
+  ding(2) = "world"
+  print*, ding(1), ", ", ding(2)
+contains
+  ! Fortran is not case-sensitive
+  FUNCTION HERP(A, B) RESULT(DERP)
+    INTEGER, INTENT(IN) :: A, B
+    INTEGER :: DERP
+    IF (A > B) THEN
+       DERP = A .MYOP. B
+    ! Some pairs of keywords may omit separating whitespace
+    ELSEIF (A < B) THEN
+      DERP = B .MYOP. A
+    ELSE
+      DERP = 0
+    ENDIF
+  END FUNCTION HERP
+ENDPROGRAM FOO
+
+