Commits

Gregory Haskins committed 8545dcd

Added FixedFortranLexer for fixed-format Fortran code. Includes preproc support.

  • Participants
  • Parent commits d62d0de

Comments (0)

Files changed (1)

pygments/lexers/compiled.py

     }
 
 
+class FixedFortranLexer(RegexLexer):
+    '''
+    Lexer for fixed-format FORTRAN code.
+
+    '''
+    name = 'FixedFortran'
+    aliases = ['fortran-fixed']
+    filenames = ['*.f', '*.f77', '*.F', '*.F77']
+    mimetypes = ['text/x-fortran']
+    flags = re.IGNORECASE | re.MULTILINE
+
+    _comps = r'eq|ne|lt|le|gt|ge|not|and|or|eqv|neqv'
+    _varname = r'[a-z][a-z0-9_]*'
+
+    # Data Types: INTEGER, REAL, COMPLEX, LOGICAL, CHARACTER and
+    #             DOUBLE PRECISION
+    # Operators: **, *, +, -, /, <, >, <=, >=, ==, /=
+    # Logical (?): NOT, AND, OR, EQV, NEQV
+
+    # Builtins:
+    # http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Table-of-Intrinsic-Functions.html
+
+    tokens = {
+        'whitespace': [
+            # Preprocessor statements
+            (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
+            (r'^\s*#', Comment.Preproc, 'macro'),
+            # Comments
+            (r'^[C\*].*\n', Comment),
+            (r'!.*\n', Comment.Single),
+            # Labels, numeric in column 2-5
+            (r'^( \d{4}|'
+             r'  \d{3}| \d{3} |'
+             r'   \d{2}|  \d{2} | \d{2}   |'
+             r'    \d|   \d |  \d  | \d    )', Name.Label),
+            # Continued line marker, nonzero & nonblank in column 6
+            (r'^( {5})([^0 ])', bygroups(Text, Operator.Word)),
+        ],
+        'root': [
+            include('whitespace'),
+            ('', Text, 'statement'),
+        ],
+        'statement': [
+            include('whitespace'),
+            include('strings'),
+
+            # GOTO and labled DO
+            (r'(GO ?TO )(\d+)', bygroups(Keyword, Name.Label)),
+            (r'(DO )(\d+ )', bygroups(Keyword, Name.Label)),
+
+            # Labeled READ and WRITE formats
+            (r'((?:READ|WRITE) *)(\()', bygroups(Keyword, Punctuation),
+             'readwrite'),
+
+            # Format statements
+            (r'(FORMAT *)(\()', bygroups(Keyword, Punctuation), 'format'),
+
+            # Subroutine/function/program declarations
+            ('((?:SUBROUTINE|FUNCTION|PROGRAM) +)(' + _varname + r')',
+             bygroups(Keyword, Name.Function)),
+
+            # Subroutine calls
+            (r'(CALL +)(' + _varname + r')',
+             bygroups(Keyword, Name.Function)),
+
+            # Statements
+            (r'\b(ACCEPT|ALLOCATABLE|ALLOCATE|ARRAY|ASSIGN|BACKSPACE|'
+             r'BLOCK DATA|BYTE|CALL|CASE|CLOSE|COMMON|CONTAINS|CONTINUE|CYCLE|'
+             r'DATA|DEALLOCATE|DECODE|DIMENSION|DO|ENCODE|END FILE|ENDIF|END|'
+             r'ENTRY|EQUIVALENCE|EXIT|EXTERNAL|EXTRINSIC|FORALL|'
+             r'IF|IMPLICIT|INCLUDE|INQUIRE|INTENT|INTERFACE|INTRINSIC|MODULE|'
+             r'NAMELIST|NULLIFY|NONE|OPEN|OPTIONAL|OPTIONS|PARAMETER|PAUSE|'
+             r'POINTER|PRINT|PRIVATE|PROGRAM|PUBLIC|PURE|RECURSIVE|RETURN|'
+             r'REWIND|SAVE|SELECT|SEQUENCE|STOP|TARGET|TYPE|USE|'
+             r'VOLATILE|WHERE|WHILE|THEN|ELSE|ENDIF)\b',
+             Keyword),
+
+            # Data Types
+            (r'\b(CHARACTER|COMPLEX|DOUBLE PRECISION|DOUBLE COMPLEX|INTEGER|'
+             r'LOGICAL|REAL)\s*\b',
+             Keyword.Type),
+
+            (r'(::)', Keyword.Declaration),
+            include('expression'),
+            (r'\n', Text, '#pop'),
+            (r'\s', Text),
+        ],
+        'expression': [
+            include('strings'),
+
+            # Operators
+            (r'(\*\*|\*|\+|-|\/|<|>|<=|>=|==|\/=|=)', Operator),
+
+            (r'[,:&%;]', Punctuation),
+
+            # Intrinsic functions
+            (r'\b(Abort|Abs|Access|AChar|ACos|AdjustL|AdjustR|AImag|AInt|Alarm|'
+             r'All|Allocated|ALog|AMax|AMin|AMod|And|ANInt|Any|'
+             r'ASin|Associated|ATan|BesJ|BesJN|BesY|BesYN|'
+             r'Bit_Size|BTest|CAbs|CCos|Ceiling|CExp|Char|ChDir|ChMod|CLog|'
+             r'Cmplx|Complex|Conjg|Cos|CosH|Count|CPU_Time|CShift|CSin|CSqRt|'
+             r'CTime|DAbs|DACos|DASin|DATan|Date_and_Time|DbesJ|'
+             r'DbesJ|DbesJN|DbesY|DbesY|DbesYN|Dble|DCos|DCosH|DDiM|DErF|DErFC|'
+             r'DExp|Digits|DiM|DInt|DLog|DLog|DMax|DMin|DMod|DNInt|Dot_Product|'
+             r'DProd|DSign|DSinH|DSin|DSqRt|DTanH|DTan|DTime|EOShift|Epsilon|'
+             r'ErF|ErFC|ETime|Exit|Exp|Exponent|FDate|FGet|FGetC|Float|'
+             r'Floor|Flush|FNum|FPutC|FPut|Fraction|FSeek|FStat|FTell|'
+             r'GError|GetArg|GetCWD|GetEnv|GetGId|GetLog|GetPId|GetUId|'
+             r'GMTime|HostNm|Huge|IAbs|IAChar|IAnd|IArgC|IBClr|IBits|'
+             r'IBSet|IChar|IDate|IDiM|IDInt|IDNInt|IEOr|IErrNo|IFix|Imag|'
+             r'ImagPart|Index|Int|IOr|IRand|IsaTty|IShft|IShftC|ISign|'
+             r'ITime|Kill|Kind|LBound|Len|Len_Trim|LGe|LGt|Link|LLe|LLt|LnBlnk|'
+             r'Loc|Log|Log|Logical|Long|LShift|LStat|LTime|MatMul|Max|'
+             r'MaxExponent|MaxLoc|MaxVal|MClock|Merge|Min|MinExponent|MinLoc|'
+             r'MinVal|Mod|Modulo|MvBits|Nearest|NInt|Not|Or|Pack|PError|'
+             r'Precision|Present|Product|Radix|Rand|Random_Number|Random_Seed|'
+             r'Range|Real|RealPart|Rename|Repeat|Reshape|RRSpacing|RShift|Scale|'
+             r'Scan|Second|Selected_Int_Kind|Selected_Real_Kind|Set_Exponent|'
+             r'Shape|Short|Sign|Signal|SinH|Sin|Sleep|Sngl|Spacing|Spread|SqRt|'
+             r'SRand|Stat|Sum|SymLnk|System|System_Clock|Tan|TanH|Time|'
+             r'Tiny|Transfer|Transpose|Trim|TtyNam|UBound|UMask|Unlink|Unpack|'
+             r'Verify|XOr|ZAbs|ZCos|ZExp|ZLog|ZSin|ZSqRt)[ \t]*(\()',
+             bygroups(Name.Function, Punctuation), '#push'),
+
+            # Booleans
+            (r'\.(true|false)\.', Name.Constant),
+            # Comparison Operators
+            (r'\.(' + _comps + r')\.', Operator.Word),
+            # Variables
+            (_varname, Name.Variable),
+            # Numbers
+            include('nums'),
+            # Blanks
+            (r'[ \t]', Text),
+            # Parentheses begin/end sub-expressions
+            (r'\(', Punctuation, '#push'),
+            (r'\)', Punctuation, '#pop'),
+        ],
+        'strings': [
+            (r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
+            (r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
+        ],
+
+        'nums': [
+            # Integers
+            (r'\d+(?![.Ee])', Number.Integer),
+            (r'[+-]?\d*\.\d+([eE][-+]?\d+)?', Number.Float),
+            # Being mindful of trailing periods preceding comparisons
+            (r'(?:[+-]?\d+\.\d*([eE][-+]?\d+)?)'
+             r'(?!' + _comps + r')', Number.Float),
+            (r'(\d+)(\.(?:' + _comps + r')\.)',
+             bygroups(Number.Integer, Operator.Word)),
+        ],
+        'macro': [
+            (r'[^/\n]+', Comment.Preproc),
+            (r'/[*](.|\n)*?[*]/', Comment.Multiline),
+            (r'//.*?\n', Comment.Single, '#pop'),
+            (r'/', Comment.Preproc),
+            (r'(?<=\\)\n', Comment.Preproc),
+            (r'\n', Comment.Preproc, '#pop'),
+        ],
+        'if0': [
+            (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
+            (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
+            (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
+            (r'.*?\n', Comment),
+        ],
+        'readwrite': [
+            include('whitespace'),
+            (r'\(', Punctuation, '#push'),
+            (r'(\d+)([ \t]*)(\))', bygroups(Name.Label, Text, Punctuation),
+             '#pop'),
+            (r'\)', Punctuation, '#pop'),
+            include('expression'),
+        ],
+        'format': [
+            include('whitespace'),
+            include('strings'),
+            (r'[^\(\),]', String.Regex),
+            (r',', Punctuation),
+            (r'\(', Punctuation, '#push'),
+            (r'\)[ ]*', Punctuation, '#pop'),
+        ],
+    }
+
+
 class GLShaderLexer(RegexLexer):
     """
     GLSL (OpenGL Shader) lexer.