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