~ubuntu-branches/ubuntu/precise/moin/precise-updates

« back to all changes in this revision

Viewing changes to MoinMoin/support/pygments/lexers/compiled.py

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-08-11 12:35:34 UTC
  • mfrom: (0.1.19 sid)
  • Revision ID: james.westby@ubuntu.com-20100811123534-q8zu7qrwqul6cvec
Tags: 1.9.3-1ubuntu1
* Merge from Debian unstable (LP: #586518). Based on work by Stefan Ebner.
  Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason 
   for us to pull this in by default currently. Note: fckeditor has a
   number of security problems and so this change probably needs to be
   carried indefinitely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
"""
11
11
 
12
12
import re
13
 
try:
14
 
    set
15
 
except NameError:
16
 
    from sets import Set as set
17
13
 
18
14
from pygments.scanner import Scanner
19
15
from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
29
25
__all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer',
30
26
           'ScalaLexer', 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer',
31
27
           'FortranLexer', 'GLShaderLexer', 'PrologLexer', 'CythonLexer',
32
 
           'ValaLexer', 'OocLexer', 'GoLexer']
 
28
           'ValaLexer', 'OocLexer', 'GoLexer', 'FelixLexer', 'AdaLexer',
 
29
           'Modula2Lexer']
33
30
 
34
31
 
35
32
class CLexer(RegexLexer):
63
60
            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
64
61
            (r'0[0-7]+[Ll]?', Number.Oct),
65
62
            (r'\d+[Ll]?', Number.Integer),
 
63
            (r'\*/', Error),
66
64
            (r'[~!%^&*+=|?:<>/-]', Operator),
67
65
            (r'[()\[\],.]', Punctuation),
68
66
            (r'\b(case)(.+?)(:)', bygroups(Keyword, using(this), Text)),
187
185
            (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
188
186
            (r'0[0-7]+[Ll]?', Number.Oct),
189
187
            (r'\d+[Ll]?', Number.Integer),
 
188
            (r'\*/', Error),
190
189
            (r'[~!%^&*+=|?:<>/-]', Operator),
191
190
            (r'[()\[\],.;]', Punctuation),
192
191
            (r'(asm|auto|break|case|catch|const|const_cast|continue|'
929
928
        ],
930
929
    }
931
930
 
 
931
 
932
932
class ScalaLexer(RegexLexer):
933
933
    """
934
934
    For `Scala <http://www.scala-lang.org>`_ source code.
1122
1122
            (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
1123
1123
             r'declspec|finally|int64|try|leave)\b', Keyword.Reserved),
1124
1124
            (r'(TRUE|FALSE|nil|NULL)\b', Name.Builtin),
1125
 
            ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label),
1126
 
            ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
 
1125
            ('[a-zA-Z$_][a-zA-Z0-9$_]*:(?!:)', Name.Label),
 
1126
            ('[a-zA-Z$_][a-zA-Z0-9$_]*', Name),
1127
1127
        ],
1128
1128
        'root': [
1129
1129
            include('whitespace'),
1130
1130
            # functions
1131
1131
            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
1132
 
             r'([a-zA-Z_][a-zA-Z0-9_]*)'             # method name
 
1132
             r'([a-zA-Z$_][a-zA-Z0-9$_]*)'           # method name
1133
1133
             r'(\s*\([^;]*?\))'                      # signature
1134
1134
             r'(' + _ws + r')({)',
1135
1135
             bygroups(using(this), Name.Function,
1137
1137
             'function'),
1138
1138
            # function declarations
1139
1139
            (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))'    # return arguments
1140
 
             r'([a-zA-Z_][a-zA-Z0-9_]*)'             # method name
 
1140
             r'([a-zA-Z$_][a-zA-Z0-9$_]*)'           # method name
1141
1141
             r'(\s*\([^;]*?\))'                      # signature
1142
1142
             r'(' + _ws + r')(;)',
1143
1143
             bygroups(using(this), Name.Function,
1151
1151
        ],
1152
1152
        'classname' : [
1153
1153
            # interface definition that inherits
1154
 
            ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*:\s*)([a-zA-Z_][a-zA-Z0-9_]*)?',
 
1154
            ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*:\s*)([a-zA-Z$_][a-zA-Z0-9$_]*)?',
1155
1155
             bygroups(Name.Class, Text, Name.Class), '#pop'),
1156
1156
            # interface definition for a category
1157
 
            ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\([a-zA-Z_][a-zA-Z0-9_]*\))',
 
1157
            ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*)(\([a-zA-Z$_][a-zA-Z0-9$_]*\))',
1158
1158
             bygroups(Name.Class, Text, Name.Label), '#pop'),
1159
1159
            # simple interface / implementation
1160
 
            ('([a-zA-Z_][a-zA-Z0-9_]*)', Name.Class, '#pop')
 
1160
            ('([a-zA-Z$_][a-zA-Z0-9$_]*)', Name.Class, '#pop')
1161
1161
        ],
1162
1162
        'forward_classname' : [
1163
 
          ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*,\s*)',
 
1163
          ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*,\s*)',
1164
1164
           bygroups(Name.Class, Text), 'forward_classname'),
1165
 
          ('([a-zA-Z_][a-zA-Z0-9_]*)(\s*;?)',
 
1165
          ('([a-zA-Z$_][a-zA-Z0-9$_]*)(\s*;?)',
1166
1166
           bygroups(Name.Class, Text), '#pop')
1167
1167
        ],
1168
1168
        'statement' : [
1790
1790
            (r'[a-zA-Z_]\w*', Name),
1791
1791
        ]
1792
1792
    }
 
1793
 
 
1794
 
 
1795
class FelixLexer(RegexLexer):
 
1796
    """
 
1797
    For `Felix <http://www.felix-lang.org>`_ source code.
 
1798
 
 
1799
    *New in Pygments 1.2.*
 
1800
    """
 
1801
 
 
1802
    name = 'Felix'
 
1803
    aliases = ['felix', 'flx']
 
1804
    filenames = ['*.flx', '*.flxh']
 
1805
    mimetypes = ['text/x-felix']
 
1806
 
 
1807
    preproc = [
 
1808
        'elif', 'else', 'endif', 'if', 'ifdef', 'ifndef',
 
1809
    ]
 
1810
 
 
1811
    keywords = [
 
1812
        '_', '_deref', 'all', 'as',
 
1813
        'assert', 'attempt', 'call', 'callback', 'case', 'caseno', 'cclass',
 
1814
        'code', 'compound', 'ctypes', 'do', 'done', 'downto', 'elif', 'else',
 
1815
        'endattempt', 'endcase', 'endif', 'endmatch', 'enum', 'except',
 
1816
        'exceptions', 'expect', 'finally', 'for', 'forall', 'forget', 'fork',
 
1817
        'functor', 'goto', 'ident', 'if', 'incomplete', 'inherit', 'instance',
 
1818
        'interface', 'jump', 'lambda', 'loop', 'match', 'module', 'namespace',
 
1819
        'new', 'noexpand', 'nonterm', 'obj', 'of', 'open', 'parse', 'raise',
 
1820
        'regexp', 'reglex', 'regmatch', 'rename', 'return', 'the', 'then',
 
1821
        'to', 'type', 'typecase', 'typedef', 'typematch', 'typeof', 'upto',
 
1822
        'when', 'whilst', 'with', 'yield',
 
1823
    ]
 
1824
 
 
1825
    keyword_directives = [
 
1826
        '_gc_pointer', '_gc_type', 'body', 'comment', 'const', 'export',
 
1827
        'header', 'inline', 'lval', 'macro', 'noinline', 'noreturn',
 
1828
        'package', 'private', 'pod', 'property', 'public', 'publish',
 
1829
        'requires', 'todo', 'virtual', 'use',
 
1830
    ]
 
1831
 
 
1832
    keyword_declarations = [
 
1833
        'def', 'let', 'ref', 'val', 'var',
 
1834
    ]
 
1835
 
 
1836
    keyword_types = [
 
1837
        'unit', 'void', 'any', 'bool',
 
1838
        'byte',  'offset',
 
1839
        'address', 'caddress', 'cvaddress', 'vaddress',
 
1840
        'tiny', 'short', 'int', 'long', 'vlong',
 
1841
        'utiny', 'ushort', 'vshort', 'uint', 'ulong', 'uvlong',
 
1842
        'int8', 'int16', 'int32', 'int64',
 
1843
        'uint8', 'uint16', 'uint32', 'uint64',
 
1844
        'float', 'double', 'ldouble',
 
1845
        'complex', 'dcomplex', 'lcomplex',
 
1846
        'imaginary', 'dimaginary', 'limaginary',
 
1847
        'char', 'wchar', 'uchar',
 
1848
        'charp', 'charcp', 'ucharp', 'ucharcp',
 
1849
        'string', 'wstring', 'ustring',
 
1850
        'cont',
 
1851
        'array', 'varray', 'list',
 
1852
        'lvalue', 'opt', 'slice',
 
1853
    ]
 
1854
 
 
1855
    keyword_constants = [
 
1856
        'false', 'true',
 
1857
    ]
 
1858
 
 
1859
    operator_words = [
 
1860
        'and', 'not', 'in', 'is', 'isin', 'or', 'xor',
 
1861
    ]
 
1862
 
 
1863
    name_builtins = [
 
1864
        '_svc', 'while',
 
1865
    ]
 
1866
 
 
1867
    name_pseudo = [
 
1868
        'root', 'self', 'this',
 
1869
    ]
 
1870
 
 
1871
    decimal_suffixes = '([tTsSiIlLvV]|ll|LL|([iIuU])(8|16|32|64))?'
 
1872
 
 
1873
    tokens = {
 
1874
        'root': [
 
1875
            include('whitespace'),
 
1876
 
 
1877
            # Keywords
 
1878
            (r'(axiom|ctor|fun|gen|proc|reduce|union)\b', Keyword,
 
1879
             'funcname'),
 
1880
            (r'(class|cclass|cstruct|obj|struct)\b', Keyword, 'classname'),
 
1881
            (r'(instance|module|typeclass)\b', Keyword, 'modulename'),
 
1882
 
 
1883
            (r'(%s)\b' % '|'.join(keywords), Keyword),
 
1884
            (r'(%s)\b' % '|'.join(keyword_directives), Name.Decorator),
 
1885
            (r'(%s)\b' % '|'.join(keyword_declarations), Keyword.Declaration),
 
1886
            (r'(%s)\b' % '|'.join(keyword_types), Keyword.Type),
 
1887
            (r'(%s)\b' % '|'.join(keyword_constants), Keyword.Constant),
 
1888
 
 
1889
            # Operators
 
1890
            include('operators'),
 
1891
 
 
1892
            # Float Literal
 
1893
            # -- Hex Float
 
1894
            (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
 
1895
             r'[pP][+\-]?[0-9_]+[lLfFdD]?', Number.Float),
 
1896
            # -- DecimalFloat
 
1897
            (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
 
1898
             r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[lLfFdD]?', Number.Float),
 
1899
            (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[lLfFdD]?',
 
1900
             Number.Float),
 
1901
 
 
1902
            # IntegerLiteral
 
1903
            # -- Binary
 
1904
            (r'0[Bb][01_]+%s' % decimal_suffixes, Number),
 
1905
            # -- Octal
 
1906
            (r'0[0-7_]+%s' % decimal_suffixes, Number.Oct),
 
1907
            # -- Hexadecimal
 
1908
            (r'0[xX][0-9a-fA-F_]+%s' % decimal_suffixes, Number.Hex),
 
1909
            # -- Decimal
 
1910
            (r'(0|[1-9][0-9_]*)%s' % decimal_suffixes, Number.Integer),
 
1911
 
 
1912
            # Strings
 
1913
            ('([rR][cC]?|[cC][rR])"""', String, 'tdqs'),
 
1914
            ("([rR][cC]?|[cC][rR])'''", String, 'tsqs'),
 
1915
            ('([rR][cC]?|[cC][rR])"', String, 'dqs'),
 
1916
            ("([rR][cC]?|[cC][rR])'", String, 'sqs'),
 
1917
            ('[cCfFqQwWuU]?"""', String, combined('stringescape', 'tdqs')),
 
1918
            ("[cCfFqQwWuU]?'''", String, combined('stringescape', 'tsqs')),
 
1919
            ('[cCfFqQwWuU]?"', String, combined('stringescape', 'dqs')),
 
1920
            ("[cCfFqQwWuU]?'", String, combined('stringescape', 'sqs')),
 
1921
 
 
1922
            # Punctuation
 
1923
            (r'[\[\]{}:(),;?]', Punctuation),
 
1924
 
 
1925
            # Labels
 
1926
            (r'[a-zA-Z_]\w*:>', Name.Label),
 
1927
 
 
1928
            # Identifiers
 
1929
            (r'(%s)\b' % '|'.join(name_builtins), Name.Builtin),
 
1930
            (r'(%s)\b' % '|'.join(name_pseudo), Name.Builtin.Pseudo),
 
1931
            (r'[a-zA-Z_]\w*', Name),
 
1932
        ],
 
1933
        'whitespace': [
 
1934
            (r'\n', Text),
 
1935
            (r'\s+', Text),
 
1936
 
 
1937
            include('comment'),
 
1938
 
 
1939
            # Preprocessor
 
1940
            (r'#\s*if\s+0', Comment.Preproc, 'if0'),
 
1941
            (r'#', Comment.Preproc, 'macro'),
 
1942
        ],
 
1943
        'operators': [
 
1944
            (r'(%s)\b' % '|'.join(operator_words), Operator.Word),
 
1945
            (r'!=|==|<<|>>|\|\||&&|[-~+/*%=<>&^|.$]', Operator),
 
1946
        ],
 
1947
        'comment': [
 
1948
            (r'//(.*?)\n', Comment.Single),
 
1949
            (r'/[*]', Comment.Multiline, 'comment2'),
 
1950
        ],
 
1951
        'comment2': [
 
1952
            (r'[^\/*]', Comment.Multiline),
 
1953
            (r'/[*]', Comment.Multiline, '#push'),
 
1954
            (r'[*]/', Comment.Multiline, '#pop'),
 
1955
            (r'[\/*]', Comment.Multiline),
 
1956
        ],
 
1957
        'if0': [
 
1958
            (r'^\s*#if.*?(?<!\\)\n', Comment, '#push'),
 
1959
            (r'^\s*#endif.*?(?<!\\)\n', Comment, '#pop'),
 
1960
            (r'.*?\n', Comment),
 
1961
        ],
 
1962
        'macro': [
 
1963
            include('comment'),
 
1964
            (r'(import|include)(\s+)(<[^>]*?>)',
 
1965
             bygroups(Comment.Preproc, Text, String), '#pop'),
 
1966
            (r'(import|include)(\s+)("[^"]*?")',
 
1967
             bygroups(Comment.Preproc, Text, String), '#pop'),
 
1968
            (r"(import|include)(\s+)('[^']*?')",
 
1969
             bygroups(Comment.Preproc, Text, String), '#pop'),
 
1970
            (r'[^/\n]+', Comment.Preproc),
 
1971
            ##(r'/[*](.|\n)*?[*]/', Comment),
 
1972
            ##(r'//.*?\n', Comment, '#pop'),
 
1973
            (r'/', Comment.Preproc),
 
1974
            (r'(?<=\\)\n', Comment.Preproc),
 
1975
            (r'\n', Comment.Preproc, '#pop'),
 
1976
        ],
 
1977
        'funcname': [
 
1978
            include('whitespace'),
 
1979
            (r'[a-zA-Z_]\w*', Name.Function, '#pop'),
 
1980
            # anonymous functions
 
1981
            (r'(?=\()', Text, '#pop'),
 
1982
        ],
 
1983
        'classname': [
 
1984
            include('whitespace'),
 
1985
            (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
 
1986
            # anonymous classes
 
1987
            (r'(?=\{)', Text, '#pop'),
 
1988
        ],
 
1989
        'modulename': [
 
1990
            include('whitespace'),
 
1991
            (r'\[', Punctuation, ('modulename2', 'tvarlist')),
 
1992
            (r'', Error, 'modulename2'),
 
1993
        ],
 
1994
        'modulename2': [
 
1995
            include('whitespace'),
 
1996
            (r'([a-zA-Z_]\w*)', Name.Namespace, '#pop:2'),
 
1997
        ],
 
1998
        'tvarlist': [
 
1999
            include('whitespace'),
 
2000
            include('operators'),
 
2001
            (r'\[', Punctuation, '#push'),
 
2002
            (r'\]', Punctuation, '#pop'),
 
2003
            (r',', Punctuation),
 
2004
            (r'(with|where)\b', Keyword),
 
2005
            (r'[a-zA-Z_]\w*', Name),
 
2006
        ],
 
2007
        'stringescape': [
 
2008
            (r'\\([\\abfnrtv"\']|\n|N{.*?}|u[a-fA-F0-9]{4}|'
 
2009
             r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
 
2010
        ],
 
2011
        'strings': [
 
2012
            (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
 
2013
             '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
 
2014
            (r'[^\\\'"%\n]+', String),
 
2015
            # quotes, percents and backslashes must be parsed one at a time
 
2016
            (r'[\'"\\]', String),
 
2017
            # unhandled string formatting sign
 
2018
            (r'%', String)
 
2019
            # newlines are an error (use "nl" state)
 
2020
        ],
 
2021
        'nl': [
 
2022
            (r'\n', String)
 
2023
        ],
 
2024
        'dqs': [
 
2025
            (r'"', String, '#pop'),
 
2026
            # included here again for raw strings
 
2027
            (r'\\\\|\\"|\\\n', String.Escape),
 
2028
            include('strings')
 
2029
        ],
 
2030
        'sqs': [
 
2031
            (r"'", String, '#pop'),
 
2032
            # included here again for raw strings
 
2033
            (r"\\\\|\\'|\\\n", String.Escape),
 
2034
            include('strings')
 
2035
        ],
 
2036
        'tdqs': [
 
2037
            (r'"""', String, '#pop'),
 
2038
            include('strings'),
 
2039
            include('nl')
 
2040
        ],
 
2041
        'tsqs': [
 
2042
            (r"'''", String, '#pop'),
 
2043
            include('strings'),
 
2044
            include('nl')
 
2045
        ],
 
2046
     }
 
2047
 
 
2048
 
 
2049
class AdaLexer(RegexLexer):
 
2050
    """
 
2051
    For Ada source code.
 
2052
 
 
2053
    *New in Pygments 1.3.*
 
2054
    """
 
2055
 
 
2056
    name = 'Ada'
 
2057
    aliases = ['ada', 'ada95' 'ada2005']
 
2058
    filenames = ['*.adb', '*.ads', '*.ada']
 
2059
    mimetypes = ['text/x-ada']
 
2060
 
 
2061
    flags = re.MULTILINE | re.I  # Ignore case
 
2062
 
 
2063
    _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
 
2064
 
 
2065
    tokens = {
 
2066
        'root': [
 
2067
            (r'[^\S\n]+', Text),
 
2068
            (r'--.*?\n', Comment.Single),
 
2069
            (r'[^\S\n]+', Text),
 
2070
            (r'function|procedure|entry', Keyword.Declaration, 'subprogram'),
 
2071
            (r'(subtype|type)(\s+)([a-z0-9_]+)',
 
2072
             bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
 
2073
            (r'task|protected', Keyword.Declaration),
 
2074
            (r'(subtype)(\s+)', bygroups(Keyword.Declaration, Text)),
 
2075
            (r'(end)(\s+)', bygroups(Keyword.Reserved, Text), 'end'),
 
2076
            (r'(pragma)(\s+)([a-zA-Z0-9_]+)', bygroups(Keyword.Reserved, Text,
 
2077
                                                       Comment.Preproc)),
 
2078
            (r'(true|false|null)\b', Keyword.Constant),
 
2079
            (r'(Byte|Character|Float|Integer|Long_Float|Long_Integer|'
 
2080
             r'Long_Long_Float|Long_Long_Integer|Natural|Positive|Short_Float|'
 
2081
             r'Short_Integer|Short_Short_Float|Short_Short_Integer|String|'
 
2082
             r'Wide_String|Duration)\b', Keyword.Type),
 
2083
            (r'(and(\s+then)?|in|mod|not|or(\s+else)|rem)\b', Operator.Word),
 
2084
            (r'generic|private', Keyword.Declaration),
 
2085
            (r'package', Keyword.Declaration, 'package'),
 
2086
            (r'array\b', Keyword.Reserved, 'array_def'),
 
2087
            (r'(with|use)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
 
2088
            (r'([a-z0-9_]+)(\s*)(:)(\s*)(constant)',
 
2089
             bygroups(Name.Constant, Text, Punctuation, Text,
 
2090
                      Keyword.Reserved)),
 
2091
            (r'<<[a-z0-9_]+>>', Name.Label),
 
2092
            (r'([a-z0-9_]+)(\s*)(:)(\s*)(declare|begin|loop|for|while)',
 
2093
             bygroups(Name.Label, Text, Punctuation, Text, Keyword.Reserved)),
 
2094
            (r'\b(abort|abs|abstract|accept|access|aliased|all|array|at|begin|'
 
2095
             r'body|case|constant|declare|delay|delta|digits|do|else|elsif|end|'
 
2096
             r'entry|exception|exit|interface|for|goto|if|is|limited|loop|new|'
 
2097
             r'null|of|or|others|out|overriding|pragma|protected|raise|range|'
 
2098
             r'record|renames|requeue|return|reverse|select|separate|subtype|'
 
2099
             r'synchronized|task|tagged|terminate|then|type|until|when|while|'
 
2100
             r'xor)\b',
 
2101
             Keyword.Reserved),
 
2102
            (r'"[^"]*"', String),
 
2103
            include('attribute'),
 
2104
            include('numbers'),
 
2105
            (r"'[^']'", String.Character),
 
2106
            (r'([a-z0-9_]+)(\s*|[(,])', bygroups(Name, using(this))),
 
2107
            (r"(<>|=>|:=|[\(\)\|:;,.'])", Punctuation),
 
2108
            (r'[*<>+=/&-]', Operator),
 
2109
            (r'\n+', Text),
 
2110
        ],
 
2111
        'numbers' : [
 
2112
            (r'[0-9_]+#[0-9a-f]+#', Number.Hex),
 
2113
            (r'[0-9_]+\.[0-9_]*', Number.Float),
 
2114
            (r'[0-9_]+', Number.Integer),
 
2115
        ],
 
2116
        'attribute' : [
 
2117
            (r"(')([a-zA-Z0-9_]+)", bygroups(Punctuation, Name.Attribute)),
 
2118
        ],
 
2119
        'subprogram' : [
 
2120
            (r'\(', Punctuation, ('#pop', 'formal_part')),
 
2121
            (r';', Punctuation, '#pop'),
 
2122
            (r'is\b', Keyword.Reserved, '#pop'),
 
2123
            (r'"[^"]+"|[a-z0-9_]+', Name.Function),
 
2124
            include('root'),
 
2125
        ],
 
2126
        'end' : [
 
2127
            ('(if|case|record|loop|select)', Keyword.Reserved),
 
2128
            ('"[^"]+"|[a-zA-Z0-9_]+', Name.Function),
 
2129
            ('[\n\s]+', Text),
 
2130
            (';', Punctuation, '#pop'),
 
2131
        ],
 
2132
        'type_def': [
 
2133
            (r';', Punctuation, '#pop'),
 
2134
            (r'\(', Punctuation, 'formal_part'),
 
2135
            (r'with|and|use', Keyword.Reserved),
 
2136
            (r'array\b', Keyword.Reserved, ('#pop', 'array_def')),
 
2137
            (r'record\b', Keyword.Reserved, ('formal_part')),
 
2138
            include('root'),
 
2139
        ],
 
2140
        'array_def' : [
 
2141
            (r';', Punctuation, '#pop'),
 
2142
            (r'([a-z0-9_]+)(\s+)(range)', bygroups(Keyword.Type, Text,
 
2143
                                                   Keyword.Reserved)),
 
2144
            include('root'),
 
2145
        ],
 
2146
        'import': [
 
2147
            (r'[a-z0-9_.]+', Name.Namespace, '#pop'),
 
2148
        ],
 
2149
        'formal_part' : [
 
2150
            (r'\)', Punctuation, '#pop'),
 
2151
            (r'([a-z0-9_]+)(\s*)(,|:[^=])', bygroups(Name.Variable,
 
2152
                                                     Text, Punctuation)),
 
2153
            (r'(in|not|null|out|access)\b', Keyword.Reserved),
 
2154
            include('root'),
 
2155
        ],
 
2156
        'package': [
 
2157
            ('body', Keyword.Declaration),
 
2158
            ('is\s+new|renames', Keyword.Reserved),
 
2159
            ('is', Keyword.Reserved, '#pop'),
 
2160
            (';', Punctuation, '#pop'),
 
2161
            ('\(', Punctuation, 'package_instantiation'),
 
2162
            ('([a-zA-Z0-9_.]+)', Name.Class),
 
2163
            include('root'),
 
2164
        ],
 
2165
        'package_instantiation': [
 
2166
            (r'("[^"]+"|[a-z0-9_]+)(\s+)(=>)', bygroups(Name.Variable,
 
2167
                                                        Text, Punctuation)),
 
2168
            (r'[a-z0-9._\'"]', Text),
 
2169
            (r'\)', Punctuation, '#pop'),
 
2170
            include('root'),
 
2171
        ],
 
2172
    }
 
2173
 
 
2174
 
 
2175
class Modula2Lexer(RegexLexer):
 
2176
    """
 
2177
    For `Modula-2 <http://www.modula2.org/>`_ source code.
 
2178
 
 
2179
    Additional options that determine which keywords are highlighted:
 
2180
 
 
2181
    `pim`
 
2182
        Select PIM Modula-2 dialect (default: True).
 
2183
    `iso`
 
2184
        Select ISO Modula-2 dialect (default: False).
 
2185
    `objm2`
 
2186
        Select Objective Modula-2 dialect (default: False).
 
2187
    `gm2ext`
 
2188
        Also highlight GNU extensions (default: False).
 
2189
 
 
2190
    *New in Pygments 1.3.*
 
2191
    """
 
2192
    name = 'Modula-2'
 
2193
    aliases = ['modula2', 'm2']
 
2194
    filenames = ['*.def', '*.mod']
 
2195
    mimetypes = ['text/x-modula2']
 
2196
 
 
2197
    flags = re.MULTILINE | re.DOTALL
 
2198
 
 
2199
    tokens = {
 
2200
        'whitespace': [
 
2201
            (r'\n+', Text), # blank lines
 
2202
            (r'\s+', Text), # whitespace
 
2203
        ],
 
2204
        'identifiers': [
 
2205
            (r'([a-zA-Z_\$][a-zA-Z0-9_\$]*)', Name),
 
2206
        ],
 
2207
        'numliterals': [
 
2208
            (r'[01]+B', Number.Binary),        # binary number (ObjM2)
 
2209
            (r'[0-7]+B', Number.Oct),          # octal number (PIM + ISO)
 
2210
            (r'[0-7]+C', Number.Oct),          # char code (PIM + ISO)
 
2211
            (r'[0-9A-F]+C', Number.Hex),       # char code (ObjM2)
 
2212
            (r'[0-9A-F]+H', Number.Hex),       # hexadecimal number
 
2213
            (r'[0-9]+\.[0-9]+E[+-][0-9]+', Number.Float), # real number
 
2214
            (r'[0-9]+\.[0-9]+', Number.Float), # real number
 
2215
            (r'[0-9]+', Number.Integer),       # decimal whole number
 
2216
        ],
 
2217
        'strings': [
 
2218
            (r"'(\\\\|\\'|[^'])*'", String), # single quoted string
 
2219
            (r'"(\\\\|\\"|[^"])*"', String), # double quoted string
 
2220
        ],
 
2221
        'operators': [
 
2222
            (r'[*/+=#~&<>\^-]', Operator),
 
2223
            (r':=', Operator),   # assignment
 
2224
            (r'@', Operator),    # pointer deref (ISO)
 
2225
            (r'\.\.', Operator), # ellipsis or range
 
2226
            (r'`', Operator),    # Smalltalk message (ObjM2)
 
2227
            (r'::', Operator),   # type conversion (ObjM2)
 
2228
        ],
 
2229
        'punctuation': [
 
2230
            (r'[\(\)\[\]{},.:;|]', Punctuation),
 
2231
        ],
 
2232
        'comments': [
 
2233
            (r'//.*?\n', Comment.Single),       # ObjM2
 
2234
            (r'/\*(.*?)\*/', Comment.Multiline), # ObjM2
 
2235
            (r'\(\*([^\$].*?)\*\)', Comment.Multiline),
 
2236
            # TO DO: nesting of (* ... *) comments
 
2237
        ],
 
2238
        'pragmas': [
 
2239
            (r'\(\*\$(.*?)\*\)', Comment.Preproc), # PIM
 
2240
            (r'<\*(.*?)\*>', Comment.Preproc),     # ISO + ObjM2
 
2241
        ],
 
2242
        'root': [
 
2243
            include('whitespace'),
 
2244
            include('comments'),
 
2245
            include('pragmas'),
 
2246
            include('identifiers'),
 
2247
            include('numliterals'),
 
2248
            include('strings'),
 
2249
            include('operators'),
 
2250
            include('punctuation'),
 
2251
        ]
 
2252
    }
 
2253
 
 
2254
    pim_reserved_words = [
 
2255
        # 40 reserved words
 
2256
        'AND', 'ARRAY', 'BEGIN', 'BY', 'CASE', 'CONST', 'DEFINITION',
 
2257
        'DIV', 'DO', 'ELSE', 'ELSIF', 'END', 'EXIT', 'EXPORT', 'FOR',
 
2258
        'FROM', 'IF', 'IMPLEMENTATION', 'IMPORT', 'IN', 'LOOP', 'MOD',
 
2259
        'MODULE', 'NOT', 'OF', 'OR', 'POINTER', 'PROCEDURE', 'QUALIFIED',
 
2260
        'RECORD', 'REPEAT', 'RETURN', 'SET', 'THEN', 'TO', 'TYPE',
 
2261
        'UNTIL', 'VAR', 'WHILE', 'WITH',
 
2262
    ]
 
2263
 
 
2264
    pim_pervasives = [
 
2265
        # 31 pervasives
 
2266
        'ABS', 'BITSET', 'BOOLEAN', 'CAP', 'CARDINAL', 'CHAR', 'CHR', 'DEC',
 
2267
        'DISPOSE', 'EXCL', 'FALSE', 'FLOAT', 'HALT', 'HIGH', 'INC', 'INCL',
 
2268
        'INTEGER', 'LONGINT', 'LONGREAL', 'MAX', 'MIN', 'NEW', 'NIL', 'ODD',
 
2269
        'ORD', 'PROC', 'REAL', 'SIZE', 'TRUE', 'TRUNC', 'VAL',
 
2270
    ]
 
2271
 
 
2272
    iso_reserved_words = [
 
2273
        # 46 reserved words
 
2274
        'AND', 'ARRAY', 'BEGIN', 'BY', 'CASE', 'CONST', 'DEFINITION', 'DIV',
 
2275
        'DO', 'ELSE', 'ELSIF', 'END', 'EXCEPT', 'EXIT', 'EXPORT', 'FINALLY',
 
2276
        'FOR', 'FORWARD', 'FROM', 'IF', 'IMPLEMENTATION', 'IMPORT', 'IN',
 
2277
        'LOOP', 'MOD', 'MODULE', 'NOT', 'OF', 'OR', 'PACKEDSET', 'POINTER',
 
2278
        'PROCEDURE', 'QUALIFIED', 'RECORD', 'REPEAT', 'REM', 'RETRY',
 
2279
        'RETURN', 'SET', 'THEN', 'TO', 'TYPE', 'UNTIL', 'VAR', 'WHILE',
 
2280
        'WITH',
 
2281
    ]
 
2282
 
 
2283
    iso_pervasives = [
 
2284
        # 42 pervasives
 
2285
        'ABS', 'BITSET', 'BOOLEAN', 'CAP', 'CARDINAL', 'CHAR', 'CHR', 'CMPLX',
 
2286
        'COMPLEX', 'DEC', 'DISPOSE', 'EXCL', 'FALSE', 'FLOAT', 'HALT', 'HIGH',
 
2287
        'IM', 'INC', 'INCL', 'INT', 'INTEGER', 'INTERRUPTIBLE', 'LENGTH',
 
2288
        'LFLOAT', 'LONGCOMPLEX', 'LONGINT', 'LONGREAL', 'MAX', 'MIN', 'NEW',
 
2289
        'NIL', 'ODD', 'ORD', 'PROC', 'PROTECTION', 'RE', 'REAL', 'SIZE',
 
2290
        'TRUE', 'TRUNC', 'UNINTERRUBTIBLE', 'VAL',
 
2291
    ]
 
2292
 
 
2293
    objm2_reserved_words = [
 
2294
        # base language, 42 reserved words
 
2295
        'AND', 'ARRAY', 'BEGIN', 'BY', 'CASE', 'CONST', 'DEFINITION', 'DIV',
 
2296
        'DO', 'ELSE', 'ELSIF', 'END', 'ENUM', 'EXIT', 'FOR', 'FROM', 'IF',
 
2297
        'IMMUTABLE', 'IMPLEMENTATION', 'IMPORT', 'IN', 'IS', 'LOOP', 'MOD',
 
2298
        'MODULE', 'NOT', 'OF', 'OPAQUE', 'OR', 'POINTER', 'PROCEDURE',
 
2299
        'RECORD', 'REPEAT', 'RETURN', 'SET', 'THEN', 'TO', 'TYPE',
 
2300
        'UNTIL', 'VAR', 'VARIADIC', 'WHILE',
 
2301
        # OO extensions, 16 reserved words
 
2302
        'BYCOPY', 'BYREF', 'CLASS', 'CONTINUE', 'CRITICAL', 'INOUT', 'METHOD',
 
2303
        'ON', 'OPTIONAL', 'OUT', 'PRIVATE', 'PROTECTED', 'PROTOCOL', 'PUBLIC',
 
2304
        'SUPER', 'TRY',
 
2305
    ]
 
2306
 
 
2307
    objm2_pervasives = [
 
2308
        # base language, 38 pervasives
 
2309
        'ABS', 'BITSET', 'BOOLEAN', 'CARDINAL', 'CHAR', 'CHR', 'DISPOSE',
 
2310
        'FALSE', 'HALT', 'HIGH', 'INTEGER', 'INRANGE', 'LENGTH', 'LONGCARD',
 
2311
        'LONGINT', 'LONGREAL', 'MAX', 'MIN', 'NEG', 'NEW', 'NEXTV', 'NIL',
 
2312
        'OCTET', 'ODD', 'ORD', 'PRED', 'PROC', 'READ', 'REAL', 'SUCC', 'TMAX',
 
2313
        'TMIN', 'TRUE', 'TSIZE', 'UNICHAR', 'VAL', 'WRITE', 'WRITEF',
 
2314
        # OO extensions, 3 pervasives
 
2315
        'OBJECT', 'NO', 'YES',
 
2316
    ]
 
2317
 
 
2318
    gnu_reserved_words = [
 
2319
        # 10 additional reserved words
 
2320
        'ASM', '__ATTRIBUTE__', '__BUILTIN__', '__COLUMN__', '__DATE__',
 
2321
        '__FILE__', '__FUNCTION__', '__LINE__', '__MODULE__', 'VOLATILE',
 
2322
    ]
 
2323
 
 
2324
    gnu_pervasives = [
 
2325
        # 21 identifiers, actually from pseudo-module SYSTEM
 
2326
        # but we will highlight them as if they were pervasives
 
2327
        'BITSET8', 'BITSET16', 'BITSET32', 'CARDINAL8', 'CARDINAL16',
 
2328
        'CARDINAL32', 'CARDINAL64', 'COMPLEX32', 'COMPLEX64', 'COMPLEX96',
 
2329
        'COMPLEX128', 'INTEGER8', 'INTEGER16', 'INTEGER32', 'INTEGER64',
 
2330
        'REAL8', 'REAL16', 'REAL32', 'REAL96', 'REAL128', 'THROW',
 
2331
    ]
 
2332
 
 
2333
    def __init__(self, **options):
 
2334
        self.reserved_words = set()
 
2335
        self.pervasives = set()
 
2336
        # ISO Modula-2
 
2337
        if get_bool_opt(options, 'iso', False):
 
2338
            self.reserved_words.update(self.iso_reserved_words)
 
2339
            self.pervasives.update(self.iso_pervasives)
 
2340
        # Objective Modula-2
 
2341
        elif get_bool_opt(options, 'objm2', False):
 
2342
            self.reserved_words.update(self.objm2_reserved_words)
 
2343
            self.pervasives.update(self.objm2_pervasives)
 
2344
        # PIM Modula-2 (DEFAULT)
 
2345
        else:
 
2346
            self.reserved_words.update(self.pim_reserved_words)
 
2347
            self.pervasives.update(self.pim_pervasives)
 
2348
        # GNU extensions
 
2349
        if get_bool_opt(options, 'gm2ext', False):
 
2350
            self.reserved_words.update(self.gnu_reserved_words)
 
2351
            self.pervasives.update(self.gnu_pervasives)
 
2352
        # initialise
 
2353
        RegexLexer.__init__(self, **options)
 
2354
 
 
2355
    def get_tokens_unprocessed(self, text):
 
2356
        for index, token, value in \
 
2357
            RegexLexer.get_tokens_unprocessed(self, text):
 
2358
            # check for reserved words and pervasives
 
2359
            if token is Name:
 
2360
                if value in self.reserved_words:
 
2361
                    token = Keyword.Reserved
 
2362
                elif value in self.pervasives:
 
2363
                    token = Keyword.Pervasive
 
2364
            # return result
 
2365
            yield index, token, value