Back to index

moin  1.9.0~rc2
dotnet.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.lexers.dotnet
00004     ~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     Lexers for .net languages.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 import re
00012 
00013 from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, using, this
00014 from pygments.token import Punctuation, \
00015      Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
00016 from pygments.util import get_choice_opt
00017 from pygments import unistring as uni
00018 
00019 from pygments.lexers.web import XmlLexer
00020 
00021 __all__ = ['CSharpLexer', 'BooLexer', 'VbNetLexer', 'CSharpAspxLexer',
00022            'VbNetAspxLexer']
00023 
00024 
00025 def _escape(st):
00026     return st.replace(u'\\', ur'\\').replace(u'-', ur'\-').\
00027            replace(u'[', ur'\[').replace(u']', ur'\]')
00028 
00029 class CSharpLexer(RegexLexer):
00030     """
00031     For `C# <http://msdn2.microsoft.com/en-us/vcsharp/default.aspx>`_
00032     source code.
00033 
00034     Additional options accepted:
00035 
00036     `unicodelevel`
00037       Determines which Unicode characters this lexer allows for identifiers.
00038       The possible values are:
00039 
00040       * ``none`` -- only the ASCII letters and numbers are allowed. This
00041         is the fastest selection.
00042       * ``basic`` -- all Unicode characters from the specification except
00043         category ``Lo`` are allowed.
00044       * ``full`` -- all Unicode characters as specified in the C# specs
00045         are allowed.  Note that this means a considerable slowdown since the
00046         ``Lo`` category has more than 40,000 characters in it!
00047 
00048       The default value is ``basic``.
00049 
00050       *New in Pygments 0.8.*
00051     """
00052 
00053     name = 'C#'
00054     aliases = ['csharp', 'c#']
00055     filenames = ['*.cs']
00056     mimetypes = ['text/x-csharp'] # inferred
00057 
00058     flags = re.MULTILINE | re.DOTALL | re.UNICODE
00059 
00060     # for the range of allowed unicode characters in identifiers,
00061     # see http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
00062 
00063     levels = {
00064         'none': '@?[_a-zA-Z][a-zA-Z0-9_]*',
00065         'basic': ('@?[_' + uni.Lu + uni.Ll + uni.Lt + uni.Lm + uni.Nl + ']' +
00066                   '[' + uni.Lu + uni.Ll + uni.Lt + uni.Lm + uni.Nl +
00067                   uni.Nd + uni.Pc + uni.Cf + uni.Mn + uni.Mc + ']*'),
00068         'full': ('@?(?:_|[^' +
00069                  _escape(uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl')) + '])'
00070                  + '[^' + _escape(uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo',
00071                                                 'Nl', 'Nd', 'Pc', 'Cf', 'Mn',
00072                                                 'Mc')) + ']*'),
00073     }
00074 
00075     tokens = {}
00076     token_variants = True
00077 
00078     for levelname, cs_ident in levels.items():
00079         tokens[levelname] = {
00080             'root': [
00081                 # method names
00082                 (r'^([ \t]*(?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
00083                  r'(' + cs_ident + ')'                           # method name
00084                  r'(\s*)(\()',                               # signature start
00085                  bygroups(using(this), Name.Function, Text, Punctuation)),
00086                 (r'^\s*\[.*?\]', Name.Attribute),
00087                 (r'[^\S\n]+', Text),
00088                 (r'\\\n', Text), # line continuation
00089                 (r'//.*?\n', Comment.Single),
00090                 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
00091                 (r'\n', Text),
00092                 (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
00093                 (r'[{}]', Punctuation),
00094                 (r'@"(\\\\|\\"|[^"])*"', String),
00095                 (r'"(\\\\|\\"|[^"\n])*["\n]', String),
00096                 (r"'\\.'|'[^\\]'", String.Char),
00097                 (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?"
00098                  r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
00099                 (r'#[ \t]*(if|endif|else|elif|define|undef|'
00100                  r'line|error|warning|region|endregion|pragma)\b.*?\n',
00101                  Comment.Preproc),
00102                 (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Text,
00103                  Keyword)),
00104                 (r'(abstract|as|base|break|case|catch|'
00105                  r'checked|const|continue|default|delegate|'
00106                  r'do|else|enum|event|explicit|extern|false|finally|'
00107                  r'fixed|for|foreach|goto|if|implicit|in|interface|'
00108                  r'internal|is|lock|new|null|operator|'
00109                  r'out|override|params|private|protected|public|readonly|'
00110                  r'ref|return|sealed|sizeof|stackalloc|static|'
00111                  r'switch|this|throw|true|try|typeof|'
00112                  r'unchecked|unsafe|virtual|void|while|'
00113                  r'get|set|new|partial|yield|add|remove|value)\b', Keyword),
00114                 (r'(global)(::)', bygroups(Keyword, Punctuation)),
00115                 (r'(bool|byte|char|decimal|double|float|int|long|object|sbyte|'
00116                  r'short|string|uint|ulong|ushort)\b\??', Keyword.Type),
00117                 (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'class'),
00118                 (r'(namespace|using)(\s+)', bygroups(Keyword, Text), 'namespace'),
00119                 (cs_ident, Name),
00120             ],
00121             'class': [
00122                 (cs_ident, Name.Class, '#pop')
00123             ],
00124             'namespace': [
00125                 (r'(?=\()', Text, '#pop'), # using (resource)
00126                 ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop')
00127             ]
00128         }
00129 
00130     def __init__(self, **options):
00131         level = get_choice_opt(options, 'unicodelevel', self.tokens.keys(), 'basic')
00132         if level not in self._all_tokens:
00133             # compile the regexes now
00134             self._tokens = self.__class__.process_tokendef(level)
00135         else:
00136             self._tokens = self._all_tokens[level]
00137 
00138         RegexLexer.__init__(self, **options)
00139 
00140 
00141 class BooLexer(RegexLexer):
00142     """
00143     For `Boo <http://boo.codehaus.org/>`_ source code.
00144     """
00145 
00146     name = 'Boo'
00147     aliases = ['boo']
00148     filenames = ['*.boo']
00149     mimetypes = ['text/x-boo']
00150 
00151     tokens = {
00152         'root': [
00153             (r'\s+', Text),
00154             (r'(#|//).*$', Comment.Single),
00155             (r'/[*]', Comment.Multiline, 'comment'),
00156             (r'[]{}:(),.;[]', Punctuation),
00157             (r'\\\n', Text),
00158             (r'\\', Text),
00159             (r'(in|is|and|or|not)\b', Operator.Word),
00160             (r'/(\\\\|\\/|[^/\s])/', String.Regex),
00161             (r'@/(\\\\|\\/|[^/])*/', String.Regex),
00162             (r'=~|!=|==|<<|>>|[-+/*%=<>&^|]', Operator),
00163             (r'(as|abstract|callable|constructor|destructor|do|import|'
00164              r'enum|event|final|get|interface|internal|of|override|'
00165              r'partial|private|protected|public|return|set|static|'
00166              r'struct|transient|virtual|yield|super|and|break|cast|'
00167              r'continue|elif|else|ensure|except|for|given|goto|if|in|'
00168              r'is|isa|not|or|otherwise|pass|raise|ref|try|unless|when|'
00169              r'while|from|as)\b', Keyword),
00170             (r'def(?=\s+\(.*?\))', Keyword),
00171             (r'(def)(\s+)', bygroups(Keyword, Text), 'funcname'),
00172             (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
00173             (r'(namespace)(\s+)', bygroups(Keyword, Text), 'namespace'),
00174             (r'(?<!\.)(true|false|null|self|__eval__|__switch__|array|'
00175              r'assert|checked|enumerate|filter|getter|len|lock|map|'
00176              r'matrix|max|min|normalArrayIndexing|print|property|range|'
00177              r'rawArrayIndexing|required|typeof|unchecked|using|'
00178              r'yieldAll|zip)\b', Name.Builtin),
00179             ('"""(\\\\|\\"|.*?)"""', String.Double),
00180             ('"(\\\\|\\"|[^"]*?)"', String.Double),
00181             ("'(\\\\|\\'|[^']*?)'", String.Single),
00182             ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
00183             (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float),
00184             (r'[0-9][0-9\.]*(m|ms|d|h|s)', Number),
00185             (r'0\d+', Number.Oct),
00186             (r'0x[a-fA-F0-9]+', Number.Hex),
00187             (r'\d+L', Number.Integer.Long),
00188             (r'\d+', Number.Integer),
00189         ],
00190         'comment': [
00191             ('/[*]', Comment.Multiline, '#push'),
00192             ('[*]/', Comment.Multiline, '#pop'),
00193             ('[^/*]', Comment.Multiline),
00194             ('[*/]', Comment.Multiline)
00195         ],
00196         'funcname': [
00197             ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop')
00198         ],
00199         'classname': [
00200             ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop')
00201         ],
00202         'namespace': [
00203             ('[a-zA-Z_][a-zA-Z0-9_.]*', Name.Namespace, '#pop')
00204         ]
00205     }
00206 
00207 
00208 class VbNetLexer(RegexLexer):
00209     """
00210     For
00211     `Visual Basic.NET <http://msdn2.microsoft.com/en-us/vbasic/default.aspx>`_
00212     source code.
00213     """
00214 
00215     name = 'VB.net'
00216     aliases = ['vb.net', 'vbnet']
00217     filenames = ['*.vb', '*.bas']
00218     mimetypes = ['text/x-vbnet', 'text/x-vba'] # (?)
00219 
00220     flags = re.MULTILINE | re.IGNORECASE
00221     tokens = {
00222         'root': [
00223             (r'^\s*<.*?>', Name.Attribute),
00224             (r'\s+', Text),
00225             (r'\n', Text),
00226             (r'rem\b.*?\n', Comment),
00227             (r"'.*?\n", Comment),
00228             (r'#If\s.*?\sThen|#ElseIf\s.*?\sThen|#End\s+If|#Const|'
00229              r'#ExternalSource.*?\n|#End\s+ExternalSource|'
00230              r'#Region.*?\n|#End\s+Region|#ExternalChecksum',
00231              Comment.Preproc),
00232             (r'[\(\){}!#,.:]', Punctuation),
00233             (r'Option\s+(Strict|Explicit|Compare)\s+'
00234              r'(On|Off|Binary|Text)', Keyword.Declaration),
00235             (r'(?<!\.)(AddHandler|Alias|'
00236              r'ByRef|ByVal|Call|Case|Catch|CBool|CByte|CChar|CDate|'
00237              r'CDec|CDbl|CInt|CLng|CObj|Const|Continue|CSByte|CShort|'
00238              r'CSng|CStr|CType|CUInt|CULng|CUShort|Declare|'
00239              r'Default|Delegate|Dim|DirectCast|Do|Each|Else|ElseIf|'
00240              r'End|EndIf|Enum|Erase|Error|Event|Exit|False|Finally|For|'
00241              r'Friend|Function|Get|Global|GoSub|GoTo|Handles|If|'
00242              r'Implements|Imports|Inherits|Interface|'
00243              r'Let|Lib|Loop|Me|Module|MustInherit|'
00244              r'MustOverride|MyBase|MyClass|Namespace|Narrowing|New|Next|'
00245              r'Not|Nothing|NotInheritable|NotOverridable|Of|On|'
00246              r'Operator|Option|Optional|Overloads|Overridable|'
00247              r'Overrides|ParamArray|Partial|Private|Property|Protected|'
00248              r'Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler|Resume|'
00249              r'Return|Select|Set|Shadows|Shared|Single|'
00250              r'Static|Step|Stop|Structure|Sub|SyncLock|Then|'
00251              r'Throw|To|True|Try|TryCast|Wend|'
00252              r'Using|When|While|Widening|With|WithEvents|'
00253              r'WriteOnly)\b', Keyword),
00254             (r'(?<!\.)(Function|Sub|Property)(\s+)',
00255              bygroups(Keyword, Text), 'funcname'),
00256             (r'(?<!\.)(Class|Structure|Enum)(\s+)',
00257              bygroups(Keyword, Text), 'classname'),
00258             (r'(?<!\.)(Namespace|Imports)(\s+)',
00259              bygroups(Keyword, Text), 'namespace'),
00260             (r'(?<!\.)(Boolean|Byte|Char|Date|Decimal|Double|Integer|Long|'
00261              r'Object|SByte|Short|Single|String|Variant|UInteger|ULong|'
00262              r'UShort)\b', Keyword.Type),
00263             (r'(?<!\.)(AddressOf|And|AndAlso|As|GetType|In|Is|IsNot|Like|Mod|'
00264              r'Or|OrElse|TypeOf|Xor)\b', Operator.Word),
00265             (r'&=|[*]=|/=|\\=|\^=|\+=|-=|<<=|>>=|<<|>>|:=|'
00266              r'<=|>=|<>|[-&*/\\^+=<>]',
00267              Operator),
00268             ('"', String, 'string'),
00269             ('[a-zA-Z_][a-zA-Z0-9_]*[%&@!#$]?', Name),
00270             ('#.*?#', Literal.Date),
00271             (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float),
00272             (r'\d+([SILDFR]|US|UI|UL)?', Number.Integer),
00273             (r'&H[0-9a-f]+([SILDFR]|US|UI|UL)?', Number.Integer),
00274             (r'&O[0-7]+([SILDFR]|US|UI|UL)?', Number.Integer),
00275             (r'_\n', Text), # Line continuation
00276         ],
00277         'string': [
00278             (r'""', String),
00279             (r'"C?', String, '#pop'),
00280             (r'[^"]+', String),
00281         ],
00282         'funcname': [
00283             (r'[a-z_][a-z0-9_]*', Name.Function, '#pop')
00284         ],
00285         'classname': [
00286             (r'[a-z_][a-z0-9_]*', Name.Class, '#pop')
00287         ],
00288         'namespace': [
00289             (r'[a-z_][a-z0-9_.]*', Name.Namespace, '#pop')
00290         ],
00291     }
00292 
00293 class GenericAspxLexer(RegexLexer):
00294     """
00295     Lexer for ASP.NET pages.
00296     """
00297 
00298     name = 'aspx-gen'
00299     filenames = []
00300     mimetypes = []
00301 
00302     flags = re.DOTALL
00303 
00304     tokens = {
00305         'root': [
00306             (r'(<%[@=#]?)(.*?)(%>)', bygroups(Name.Tag, Other, Name.Tag)),
00307             (r'(<script.*?>)(.*?)(</script>)', bygroups(using(XmlLexer),
00308                                                         Other,
00309                                                         using(XmlLexer))),
00310             (r'(.+?)(?=<)', using(XmlLexer)),
00311             (r'.+', using(XmlLexer)),
00312         ],
00313     }
00314 
00315 #TODO support multiple languages within the same source file
00316 class CSharpAspxLexer(DelegatingLexer):
00317     """
00318     Lexer for highligting C# within ASP.NET pages.
00319     """
00320 
00321     name = 'aspx-cs'
00322     aliases = ['aspx-cs']
00323     filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
00324     mimetypes = []
00325 
00326     def __init__(self, **options):
00327         super(CSharpAspxLexer, self).__init__(CSharpLexer,GenericAspxLexer,
00328                                               **options)
00329 
00330     def analyse_text(text):
00331         if re.search(r'Page\s*Language="C#"', text, re.I) is not None:
00332             return 0.2
00333         elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None:
00334             return 0.15
00335         return 0.001 # TODO really only for when filename matched...
00336 
00337 class VbNetAspxLexer(DelegatingLexer):
00338     """
00339     Lexer for highligting Visual Basic.net within ASP.NET pages.
00340     """
00341 
00342     name = 'aspx-vb'
00343     aliases = ['aspx-vb']
00344     filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
00345     mimetypes = []
00346 
00347     def __init__(self, **options):
00348         super(VbNetAspxLexer, self).__init__(VbNetLexer,GenericAspxLexer,
00349                                               **options)
00350 
00351     def analyse_text(text):
00352         if re.search(r'Page\s*Language="Vb"', text, re.I) is not None:
00353             return 0.2
00354         elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None:
00355             return 0.15