fortran.py 9.6 KB

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