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