3 pygments.lexers.fortran |
3 pygments.lexers.fortran |
4 ~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for Fortran languages. |
6 Lexers for Fortran languages. |
7 |
7 |
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
14 from pygments.lexer import RegexLexer, include, words |
14 from pygments.lexer import RegexLexer, bygroups, include, words, using |
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
16 Number, Punctuation |
16 Number, Punctuation, Generic |
17 |
17 |
18 __all__ = ['FortranLexer'] |
18 __all__ = ['FortranLexer', 'FortranFixedLexer'] |
19 |
19 |
20 |
20 |
21 class FortranLexer(RegexLexer): |
21 class FortranLexer(RegexLexer): |
22 """ |
22 """ |
23 Lexer for FORTRAN 90 code. |
23 Lexer for FORTRAN 90 code. |
24 |
24 |
25 .. versionadded:: 0.10 |
25 .. versionadded:: 0.10 |
26 """ |
26 """ |
27 name = 'Fortran' |
27 name = 'Fortran' |
28 aliases = ['fortran'] |
28 aliases = ['fortran'] |
29 filenames = ['*.f', '*.f90', '*.F', '*.F90'] |
29 filenames = ['*.f03', '*.f90', '*.F03', '*.F90'] |
30 mimetypes = ['text/x-fortran'] |
30 mimetypes = ['text/x-fortran'] |
31 flags = re.IGNORECASE | re.MULTILINE |
31 flags = re.IGNORECASE | re.MULTILINE |
32 |
32 |
33 # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and DOUBLE PRECISION |
33 # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and DOUBLE PRECISION |
34 # Operators: **, *, +, -, /, <, >, <=, >=, ==, /= |
34 # Operators: **, *, +, -, /, <, >, <=, >=, ==, /= |
71 Keyword), |
71 Keyword), |
72 |
72 |
73 # Data Types |
73 # Data Types |
74 (words(( |
74 (words(( |
75 'CHARACTER', 'COMPLEX', 'DOUBLE PRECISION', 'DOUBLE COMPLEX', 'INTEGER', |
75 'CHARACTER', 'COMPLEX', 'DOUBLE PRECISION', 'DOUBLE COMPLEX', 'INTEGER', |
76 'LOGICAL', 'REAL', 'C_INT', 'C_SHORT', 'C_LONG', 'C_LONG_LONG', 'C_SIGNED_CHAR', |
76 'LOGICAL', 'REAL', 'C_INT', 'C_SHORT', 'C_LONG', 'C_LONG_LONG', |
77 'C_SIZE_T', 'C_INT8_T', 'C_INT16_T', 'C_INT32_T', 'C_INT64_T', 'C_INT_LEAST8_T', |
77 'C_SIGNED_CHAR', 'C_SIZE_T', 'C_INT8_T', 'C_INT16_T', 'C_INT32_T', |
78 'C_INT_LEAST16_T', 'C_INT_LEAST32_T', 'C_INT_LEAST64_T', 'C_INT_FAST8_T', |
78 'C_INT64_T', 'C_INT_LEAST8_T', 'C_INT_LEAST16_T', 'C_INT_LEAST32_T', |
79 'C_INT_FAST16_T', 'C_INT_FAST32_T', 'C_INT_FAST64_T', 'C_INTMAX_T', |
79 'C_INT_LEAST64_T', 'C_INT_FAST8_T', 'C_INT_FAST16_T', 'C_INT_FAST32_T', |
80 'C_INTPTR_T', 'C_FLOAT', 'C_DOUBLE', 'C_LONG_DOUBLE', 'C_FLOAT_COMPLEX', |
80 'C_INT_FAST64_T', 'C_INTMAX_T', 'C_INTPTR_T', 'C_FLOAT', 'C_DOUBLE', |
81 'C_DOUBLE_COMPLEX', 'C_LONG_DOUBLE_COMPLEX', 'C_BOOL', 'C_CHAR', 'C_PTR', |
81 'C_LONG_DOUBLE', 'C_FLOAT_COMPLEX', 'C_DOUBLE_COMPLEX', |
82 'C_FUNPTR'), prefix=r'\b', suffix=r'\s*\b'), |
82 'C_LONG_DOUBLE_COMPLEX', 'C_BOOL', 'C_CHAR', 'C_PTR', 'C_FUNPTR'), |
|
83 prefix=r'\b', suffix=r'\s*\b'), |
83 Keyword.Type), |
84 Keyword.Type), |
84 |
85 |
85 # Operators |
86 # Operators |
86 (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator), |
87 (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator), |
87 |
88 |
157 (r'\d+(?![.e])(_[a-z]\w+)?', Number.Integer), |
158 (r'\d+(?![.e])(_[a-z]\w+)?', Number.Integer), |
158 (r'[+-]?\d*\.\d+(e[-+]?\d+)?(_[a-z]\w+)?', Number.Float), |
159 (r'[+-]?\d*\.\d+(e[-+]?\d+)?(_[a-z]\w+)?', Number.Float), |
159 (r'[+-]?\d+\.\d*(e[-+]?\d+)?(_[a-z]\w+)?', Number.Float), |
160 (r'[+-]?\d+\.\d*(e[-+]?\d+)?(_[a-z]\w+)?', Number.Float), |
160 ], |
161 ], |
161 } |
162 } |
|
163 |
|
164 |
|
165 class FortranFixedLexer(RegexLexer): |
|
166 """ |
|
167 Lexer for fixed format Fortran. |
|
168 |
|
169 .. versionadded:: 2.1 |
|
170 """ |
|
171 name = 'FortranFixed' |
|
172 aliases = ['fortranfixed'] |
|
173 filenames = ['*.f', '*.F'] |
|
174 |
|
175 flags = re.IGNORECASE |
|
176 |
|
177 def _lex_fortran(self, match, ctx=None): |
|
178 """Lex a line just as free form fortran without line break.""" |
|
179 lexer = FortranLexer() |
|
180 text = match.group(0) + "\n" |
|
181 for index, token, value in lexer.get_tokens_unprocessed(text): |
|
182 value = value.replace('\n', '') |
|
183 if value != '': |
|
184 yield index, token, value |
|
185 |
|
186 tokens = { |
|
187 'root': [ |
|
188 (r'[C*].*\n', Comment), |
|
189 (r'#.*\n', Comment.Preproc), |
|
190 (r' {0,4}!.*\n', Comment), |
|
191 (r'(.{5})', Name.Label, 'cont-char'), |
|
192 (r'.*\n', using(FortranLexer)), |
|
193 ], |
|
194 |
|
195 'cont-char': [ |
|
196 (' ', Text, 'code'), |
|
197 ('0', Comment, 'code'), |
|
198 ('.', Generic.Strong, 'code') |
|
199 ], |
|
200 |
|
201 'code': [ |
|
202 (r'(.{66})(.*)(\n)', |
|
203 bygroups(_lex_fortran, Comment, Text), 'root'), |
|
204 (r'(.*)(\n)', bygroups(_lex_fortran, Text), 'root'), |
|
205 (r'', Text, 'root')] |
|
206 } |