fortran.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """
  2. pygments.lexers.fortran
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Fortran languages.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups, include, words, using, default
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Generic
  12. __all__ = ['FortranLexer', 'FortranFixedLexer']
  13. class FortranLexer(RegexLexer):
  14. """
  15. Lexer for FORTRAN 90 code.
  16. .. versionadded:: 0.10
  17. """
  18. name = 'Fortran'
  19. aliases = ['fortran']
  20. filenames = ['*.f03', '*.f90', '*.F03', '*.F90']
  21. mimetypes = ['text/x-fortran']
  22. flags = re.IGNORECASE | re.MULTILINE
  23. # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and DOUBLE PRECISION
  24. # Operators: **, *, +, -, /, <, >, <=, >=, ==, /=
  25. # Logical (?): NOT, AND, OR, EQV, NEQV
  26. # Builtins:
  27. # http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Table-of-Intrinsic-Functions.html
  28. tokens = {
  29. 'root': [
  30. (r'^#.*\n', Comment.Preproc),
  31. (r'!.*\n', Comment),
  32. include('strings'),
  33. include('core'),
  34. (r'[a-z][\w$]*', Name),
  35. include('nums'),
  36. (r'[\s]+', Text),
  37. ],
  38. 'core': [
  39. # Statements
  40. (words((
  41. 'ABSTRACT', 'ACCEPT', 'ALL', 'ALLSTOP', 'ALLOCATABLE', 'ALLOCATE',
  42. 'ARRAY', 'ASSIGN', 'ASSOCIATE', 'ASYNCHRONOUS', 'BACKSPACE', 'BIND',
  43. 'BLOCK', 'BLOCKDATA', 'BYTE', 'CALL', 'CASE', 'CLASS', 'CLOSE',
  44. 'CODIMENSION', 'COMMON', 'CONCURRRENT', 'CONTIGUOUS', 'CONTAINS',
  45. 'CONTINUE', 'CRITICAL', 'CYCLE', 'DATA', 'DEALLOCATE', 'DECODE',
  46. 'DEFERRED', 'DIMENSION', 'DO', 'ELEMENTAL', 'ELSE', 'ENCODE', 'END',
  47. 'ENDASSOCIATE', 'ENDBLOCK', 'ENDDO', 'ENDENUM', 'ENDFORALL',
  48. 'ENDFUNCTION', 'ENDIF', 'ENDINTERFACE', 'ENDMODULE', 'ENDPROGRAM',
  49. 'ENDSELECT', 'ENDSUBMODULE', 'ENDSUBROUTINE', 'ENDTYPE', 'ENDWHERE',
  50. 'ENTRY', 'ENUM', 'ENUMERATOR', 'EQUIVALENCE', 'ERROR STOP', 'EXIT',
  51. 'EXTENDS', 'EXTERNAL', 'EXTRINSIC', 'FILE', 'FINAL', 'FORALL', 'FORMAT',
  52. 'FUNCTION', 'GENERIC', 'GOTO', 'IF', 'IMAGES', 'IMPLICIT',
  53. 'IMPORT', 'IMPURE', 'INCLUDE', 'INQUIRE', 'INTENT', 'INTERFACE',
  54. 'INTRINSIC', 'IS', 'LOCK', 'MEMORY', 'MODULE', 'NAMELIST', 'NULLIFY',
  55. 'NONE', 'NON_INTRINSIC', 'NON_OVERRIDABLE', 'NOPASS', 'ONLY', 'OPEN',
  56. 'OPTIONAL', 'OPTIONS', 'PARAMETER', 'PASS', 'PAUSE', 'POINTER', 'PRINT',
  57. 'PRIVATE', 'PROGRAM', 'PROCEDURE', 'PROTECTED', 'PUBLIC', 'PURE', 'READ',
  58. 'RECURSIVE', 'RESULT', 'RETURN', 'REWIND', 'SAVE', 'SELECT', 'SEQUENCE',
  59. 'STOP', 'SUBMODULE', 'SUBROUTINE', 'SYNC', 'SYNCALL', 'SYNCIMAGES',
  60. 'SYNCMEMORY', 'TARGET', 'THEN', 'TYPE', 'UNLOCK', 'USE', 'VALUE',
  61. 'VOLATILE', 'WHERE', 'WRITE', 'WHILE'), prefix=r'\b', suffix=r'\s*\b'),
  62. Keyword),
  63. # Data Types
  64. (words((
  65. 'CHARACTER', 'COMPLEX', 'DOUBLE PRECISION', 'DOUBLE COMPLEX', 'INTEGER',
  66. 'LOGICAL', 'REAL', 'C_INT', 'C_SHORT', 'C_LONG', 'C_LONG_LONG',
  67. 'C_SIGNED_CHAR', 'C_SIZE_T', 'C_INT8_T', 'C_INT16_T', 'C_INT32_T',
  68. 'C_INT64_T', 'C_INT_LEAST8_T', 'C_INT_LEAST16_T', 'C_INT_LEAST32_T',
  69. 'C_INT_LEAST64_T', 'C_INT_FAST8_T', 'C_INT_FAST16_T', 'C_INT_FAST32_T',
  70. 'C_INT_FAST64_T', 'C_INTMAX_T', 'C_INTPTR_T', 'C_FLOAT', 'C_DOUBLE',
  71. 'C_LONG_DOUBLE', 'C_FLOAT_COMPLEX', 'C_DOUBLE_COMPLEX',
  72. 'C_LONG_DOUBLE_COMPLEX', 'C_BOOL', 'C_CHAR', 'C_PTR', 'C_FUNPTR'),
  73. prefix=r'\b', suffix=r'\s*\b'),
  74. Keyword.Type),
  75. # Operators
  76. (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator),
  77. (r'(::)', Keyword.Declaration),
  78. (r'[()\[\],:&%;.]', Punctuation),
  79. # Intrinsics
  80. (words((
  81. 'Abort', 'Abs', 'Access', 'AChar', 'ACos', 'ACosH', 'AdjustL',
  82. 'AdjustR', 'AImag', 'AInt', 'Alarm', 'All', 'Allocated', 'ALog',
  83. 'AMax', 'AMin', 'AMod', 'And', 'ANInt', 'Any', 'ASin', 'ASinH',
  84. 'Associated', 'ATan', 'ATanH', 'Atomic_Define', 'Atomic_Ref',
  85. 'BesJ', 'BesJN', 'Bessel_J0', 'Bessel_J1', 'Bessel_JN', 'Bessel_Y0',
  86. 'Bessel_Y1', 'Bessel_YN', 'BesY', 'BesYN', 'BGE', 'BGT', 'BLE',
  87. 'BLT', 'Bit_Size', 'BTest', 'CAbs', 'CCos', 'Ceiling', 'CExp',
  88. 'Char', 'ChDir', 'ChMod', 'CLog', 'Cmplx', 'Command_Argument_Count',
  89. 'Complex', 'Conjg', 'Cos', 'CosH', 'Count', 'CPU_Time', 'CShift',
  90. 'CSin', 'CSqRt', 'CTime', 'C_Loc', 'C_Associated',
  91. 'C_Null_Ptr', 'C_Null_Funptr', 'C_F_Pointer', 'C_F_ProcPointer',
  92. 'C_Null_Char', 'C_Alert', 'C_Backspace', 'C_Form_Feed', 'C_FunLoc',
  93. 'C_Sizeof', 'C_New_Line', 'C_Carriage_Return',
  94. 'C_Horizontal_Tab', 'C_Vertical_Tab', 'DAbs', 'DACos', 'DASin',
  95. 'DATan', 'Date_and_Time', 'DbesJ', 'DbesJN', 'DbesY',
  96. 'DbesYN', 'Dble', 'DCos', 'DCosH', 'DDiM', 'DErF',
  97. 'DErFC', 'DExp', 'Digits', 'DiM', 'DInt', 'DLog', 'DMax',
  98. 'DMin', 'DMod', 'DNInt', 'Dot_Product', 'DProd', 'DSign', 'DSinH',
  99. 'DShiftL', 'DShiftR', 'DSin', 'DSqRt', 'DTanH', 'DTan', 'DTime',
  100. 'EOShift', 'Epsilon', 'ErF', 'ErFC', 'ErFC_Scaled', 'ETime',
  101. 'Execute_Command_Line', 'Exit', 'Exp', 'Exponent', 'Extends_Type_Of',
  102. 'FDate', 'FGet', 'FGetC', 'FindLoc', 'Float', 'Floor', 'Flush',
  103. 'FNum', 'FPutC', 'FPut', 'Fraction', 'FSeek', 'FStat', 'FTell',
  104. 'Gamma', 'GError', 'GetArg', 'Get_Command', 'Get_Command_Argument',
  105. 'Get_Environment_Variable', 'GetCWD', 'GetEnv', 'GetGId', 'GetLog',
  106. 'GetPId', 'GetUId', 'GMTime', 'HostNm', 'Huge', 'Hypot', 'IAbs',
  107. 'IAChar', 'IAll', 'IAnd', 'IAny', 'IArgC', 'IBClr', 'IBits',
  108. 'IBSet', 'IChar', 'IDate', 'IDiM', 'IDInt', 'IDNInt', 'IEOr',
  109. 'IErrNo', 'IFix', 'Imag', 'ImagPart', 'Image_Index', 'Index',
  110. 'Int', 'IOr', 'IParity', 'IRand', 'IsaTty', 'IShft', 'IShftC',
  111. 'ISign', 'Iso_C_Binding', 'Is_Contiguous', 'Is_Iostat_End',
  112. 'Is_Iostat_Eor', 'ITime', 'Kill', 'Kind', 'LBound', 'LCoBound',
  113. 'Len', 'Len_Trim', 'LGe', 'LGt', 'Link', 'LLe', 'LLt', 'LnBlnk',
  114. 'Loc', 'Log', 'Log_Gamma', 'Logical', 'Long', 'LShift', 'LStat',
  115. 'LTime', 'MaskL', 'MaskR', 'MatMul', 'Max', 'MaxExponent',
  116. 'MaxLoc', 'MaxVal', 'MClock', 'Merge', 'Merge_Bits', 'Move_Alloc',
  117. 'Min', 'MinExponent', 'MinLoc', 'MinVal', 'Mod', 'Modulo', 'MvBits',
  118. 'Nearest', 'New_Line', 'NInt', 'Norm2', 'Not', 'Null', 'Num_Images',
  119. 'Or', 'Pack', 'Parity', 'PError', 'Precision', 'Present', 'Product',
  120. 'Radix', 'Rand', 'Random_Number', 'Random_Seed', 'Range', 'Real',
  121. 'RealPart', 'Rename', 'Repeat', 'Reshape', 'RRSpacing', 'RShift',
  122. 'Same_Type_As', 'Scale', 'Scan', 'Second', 'Selected_Char_Kind',
  123. 'Selected_Int_Kind', 'Selected_Real_Kind', 'Set_Exponent', 'Shape',
  124. 'ShiftA', 'ShiftL', 'ShiftR', 'Short', 'Sign', 'Signal', 'SinH',
  125. 'Sin', 'Sleep', 'Sngl', 'Spacing', 'Spread', 'SqRt', 'SRand',
  126. 'Stat', 'Storage_Size', 'Sum', 'SymLnk', 'System', 'System_Clock',
  127. 'Tan', 'TanH', 'Time', 'This_Image', 'Tiny', 'TrailZ', 'Transfer',
  128. 'Transpose', 'Trim', 'TtyNam', 'UBound', 'UCoBound', 'UMask',
  129. 'Unlink', 'Unpack', 'Verify', 'XOr', 'ZAbs', 'ZCos', 'ZExp',
  130. 'ZLog', 'ZSin', 'ZSqRt'), prefix=r'\b', suffix=r'\s*\b'),
  131. Name.Builtin),
  132. # Booleans
  133. (r'\.(true|false)\.', Name.Builtin),
  134. # Comparing Operators
  135. (r'\.(eq|ne|lt|le|gt|ge|not|and|or|eqv|neqv)\.', Operator.Word),
  136. ],
  137. 'strings': [
  138. (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
  139. (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
  140. ],
  141. 'nums': [
  142. (r'\d+(?![.e])(_([1-9]|[a-z]\w*))?', Number.Integer),
  143. (r'[+-]?\d*\.\d+([ed][-+]?\d+)?(_([1-9]|[a-z]\w*))?', Number.Float),
  144. (r'[+-]?\d+\.\d*([ed][-+]?\d+)?(_([1-9]|[a-z]\w*))?', Number.Float),
  145. (r'[+-]?\d+(\.\d*)?[ed][-+]?\d+(_([1-9]|[a-z]\w*))?', Number.Float),
  146. ],
  147. }
  148. class FortranFixedLexer(RegexLexer):
  149. """
  150. Lexer for fixed format Fortran.
  151. .. versionadded:: 2.1
  152. """
  153. name = 'FortranFixed'
  154. aliases = ['fortranfixed']
  155. filenames = ['*.f', '*.F']
  156. flags = re.IGNORECASE
  157. def _lex_fortran(self, match, ctx=None):
  158. """Lex a line just as free form fortran without line break."""
  159. lexer = FortranLexer()
  160. text = match.group(0) + "\n"
  161. for index, token, value in lexer.get_tokens_unprocessed(text):
  162. value = value.replace('\n', '')
  163. if value != '':
  164. yield index, token, value
  165. tokens = {
  166. 'root': [
  167. (r'[C*].*\n', Comment),
  168. (r'#.*\n', Comment.Preproc),
  169. (r' {0,4}!.*\n', Comment),
  170. (r'(.{5})', Name.Label, 'cont-char'),
  171. (r'.*\n', using(FortranLexer)),
  172. ],
  173. 'cont-char': [
  174. (' ', Text, 'code'),
  175. ('0', Comment, 'code'),
  176. ('.', Generic.Strong, 'code'),
  177. ],
  178. 'code': [
  179. (r'(.{66})(.*)(\n)',
  180. bygroups(_lex_fortran, Comment, Text), 'root'),
  181. (r'(.*)(\n)', bygroups(_lex_fortran, Text), 'root'),
  182. default('root'),
  183. ]
  184. }