ThirdParty/Pygments/pygments/lexers/dotnet.py

changeset 1705
b0fbc9300f2b
parent 808
8f85926125ef
child 2426
da76c71624de
equal deleted inserted replaced
1704:02ae6c55b35b 1705:b0fbc9300f2b
3 pygments.lexers.dotnet 3 pygments.lexers.dotnet
4 ~~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for .net languages. 6 Lexers for .net languages.
7 7
8 :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2012 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 import re 11 import re
12 12
13 from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, using, this 13 from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, include, \
14 using, this
14 from pygments.token import Punctuation, \ 15 from pygments.token import Punctuation, \
15 Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other 16 Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
16 from pygments.util import get_choice_opt 17 from pygments.util import get_choice_opt
17 from pygments import unistring as uni 18 from pygments import unistring as uni
18 19
19 from pygments.lexers.web import XmlLexer 20 from pygments.lexers.web import XmlLexer
20 21
21 __all__ = ['CSharpLexer', 'BooLexer', 'VbNetLexer', 'CSharpAspxLexer', 22 __all__ = ['CSharpLexer', 'NemerleLexer', 'BooLexer', 'VbNetLexer',
22 'VbNetAspxLexer'] 23 'CSharpAspxLexer', 'VbNetAspxLexer', 'FSharpLexer']
23 24
24 25
25 def _escape(st): 26 def _escape(st):
26 return st.replace('\\', r'\\').replace('-', r'\-').\ 27 return st.replace('\\', r'\\').replace('-', r'\-').\
27 replace('[', r'\[').replace(']', r'\]') 28 replace('[', r'\[').replace(']', r'\]')
89 (r'//.*?\n', Comment.Single), 90 (r'//.*?\n', Comment.Single),
90 (r'/[*](.|\n)*?[*]/', Comment.Multiline), 91 (r'/[*](.|\n)*?[*]/', Comment.Multiline),
91 (r'\n', Text), 92 (r'\n', Text),
92 (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation), 93 (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
93 (r'[{}]', Punctuation), 94 (r'[{}]', Punctuation),
94 (r'@"(\\\\|\\"|[^"])*"', String), 95 (r'@"(""|[^"])*"', String),
95 (r'"(\\\\|\\"|[^"\n])*["\n]', String), 96 (r'"(\\\\|\\"|[^"\n])*["\n]', String),
96 (r"'\\.'|'[^\\]'", String.Char), 97 (r"'\\.'|'[^\\]'", String.Char),
97 (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?" 98 (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?"
98 r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number), 99 r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
99 (r'#[ \t]*(if|endif|else|elif|define|undef|' 100 (r'#[ \t]*(if|endif|else|elif|define|undef|'
108 r'internal|is|lock|new|null|operator|' 109 r'internal|is|lock|new|null|operator|'
109 r'out|override|params|private|protected|public|readonly|' 110 r'out|override|params|private|protected|public|readonly|'
110 r'ref|return|sealed|sizeof|stackalloc|static|' 111 r'ref|return|sealed|sizeof|stackalloc|static|'
111 r'switch|this|throw|true|try|typeof|' 112 r'switch|this|throw|true|try|typeof|'
112 r'unchecked|unsafe|virtual|void|while|' 113 r'unchecked|unsafe|virtual|void|while|'
113 r'get|set|new|partial|yield|add|remove|value)\b', Keyword), 114 r'get|set|new|partial|yield|add|remove|value|alias|ascending|'
115 r'descending|from|group|into|orderby|select|where|'
116 r'join|equals)\b', Keyword),
114 (r'(global)(::)', bygroups(Keyword, Punctuation)), 117 (r'(global)(::)', bygroups(Keyword, Punctuation)),
115 (r'(bool|byte|char|decimal|double|float|int|long|object|sbyte|' 118 (r'(bool|byte|char|decimal|double|dynamic|float|int|long|object|'
116 r'short|string|uint|ulong|ushort)\b\??', Keyword.Type), 119 r'sbyte|short|string|uint|ulong|ushort|var)\b\??', Keyword.Type),
117 (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'class'), 120 (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'class'),
118 (r'(namespace|using)(\s+)', bygroups(Keyword, Text), 'namespace'), 121 (r'(namespace|using)(\s+)', bygroups(Keyword, Text), 'namespace'),
119 (cs_ident, Name), 122 (cs_ident, Name),
120 ], 123 ],
121 'class': [ 124 'class': [
127 ] 130 ]
128 } 131 }
129 132
130 def __init__(self, **options): 133 def __init__(self, **options):
131 level = get_choice_opt(options, 'unicodelevel', list(self.tokens.keys()), 'basic') 134 level = get_choice_opt(options, 'unicodelevel', list(self.tokens.keys()), 'basic')
135 if level not in self._all_tokens:
136 # compile the regexes now
137 self._tokens = self.__class__.process_tokendef(level)
138 else:
139 self._tokens = self._all_tokens[level]
140
141 RegexLexer.__init__(self, **options)
142
143
144 class NemerleLexer(RegexLexer):
145 """
146 For `Nemerle <http://nemerle.org>`_ source code.
147
148 Additional options accepted:
149
150 `unicodelevel`
151 Determines which Unicode characters this lexer allows for identifiers.
152 The possible values are:
153
154 * ``none`` -- only the ASCII letters and numbers are allowed. This
155 is the fastest selection.
156 * ``basic`` -- all Unicode characters from the specification except
157 category ``Lo`` are allowed.
158 * ``full`` -- all Unicode characters as specified in the C# specs
159 are allowed. Note that this means a considerable slowdown since the
160 ``Lo`` category has more than 40,000 characters in it!
161
162 The default value is ``basic``.
163
164 *New in Pygments 1.5.*
165 """
166
167 name = 'Nemerle'
168 aliases = ['nemerle']
169 filenames = ['*.n']
170 mimetypes = ['text/x-nemerle'] # inferred
171
172 flags = re.MULTILINE | re.DOTALL | re.UNICODE
173
174 # for the range of allowed unicode characters in identifiers, see
175 # http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
176
177 levels = dict(
178 none = '@?[_a-zA-Z][a-zA-Z0-9_]*',
179 basic = ('@?[_' + uni.Lu + uni.Ll + uni.Lt + uni.Lm + uni.Nl + ']' +
180 '[' + uni.Lu + uni.Ll + uni.Lt + uni.Lm + uni.Nl +
181 uni.Nd + uni.Pc + uni.Cf + uni.Mn + uni.Mc + ']*'),
182 full = ('@?(?:_|[^' + _escape(uni.allexcept('Lu', 'Ll', 'Lt', 'Lm',
183 'Lo', 'Nl')) + '])'
184 + '[^' + _escape(uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo',
185 'Nl', 'Nd', 'Pc', 'Cf', 'Mn',
186 'Mc')) + ']*'),
187 )
188
189 tokens = {}
190 token_variants = True
191
192 for levelname, cs_ident in list(levels.items()):
193 tokens[levelname] = {
194 'root': [
195 # method names
196 (r'^([ \t]*(?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
197 r'(' + cs_ident + ')' # method name
198 r'(\s*)(\()', # signature start
199 bygroups(using(this), Name.Function, Text, Punctuation)),
200 (r'^\s*\[.*?\]', Name.Attribute),
201 (r'[^\S\n]+', Text),
202 (r'\\\n', Text), # line continuation
203 (r'//.*?\n', Comment.Single),
204 (r'/[*].*?[*]/', Comment.Multiline),
205 (r'\n', Text),
206 (r'\$\s*"', String, 'splice-string'),
207 (r'\$\s*<#', String, 'splice-string2'),
208 (r'<#', String, 'recursive-string'),
209
210 (r'(<\[)\s*(' + cs_ident + ':)?', Keyword),
211 (r'\]\>', Keyword),
212
213 # quasiquotation only
214 (r'\$' + cs_ident, Name),
215 (r'(\$)(\()', bygroups(Name, Punctuation),
216 'splice-string-content'),
217
218 (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
219 (r'[{}]', Punctuation),
220 (r'@"(""|[^"])*"', String),
221 (r'"(\\\\|\\"|[^"\n])*["\n]', String),
222 (r"'\\.'|'[^\\]'", String.Char),
223 (r"0[xX][0-9a-fA-F]+[Ll]?", Number),
224 (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?[flFLdD]?", Number),
225 (r'#[ \t]*(if|endif|else|elif|define|undef|'
226 r'line|error|warning|region|endregion|pragma)\b.*?\n',
227 Comment.Preproc),
228 (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Text,
229 Keyword)),
230 (r'(abstract|and|as|base|catch|def|delegate|'
231 r'enum|event|extern|false|finally|'
232 r'fun|implements|interface|internal|'
233 r'is|macro|match|matches|module|mutable|new|'
234 r'null|out|override|params|partial|private|'
235 r'protected|public|ref|sealed|static|'
236 r'syntax|this|throw|true|try|type|typeof|'
237 r'virtual|volatile|when|where|with|'
238 r'assert|assert2|async|break|checked|continue|do|else|'
239 r'ensures|for|foreach|if|late|lock|new|nolate|'
240 r'otherwise|regexp|repeat|requires|return|surroundwith|'
241 r'unchecked|unless|using|while|yield)\b', Keyword),
242 (r'(global)(::)', bygroups(Keyword, Punctuation)),
243 (r'(bool|byte|char|decimal|double|float|int|long|object|sbyte|'
244 r'short|string|uint|ulong|ushort|void|array|list)\b\??',
245 Keyword.Type),
246 (r'(:>?)\s*(' + cs_ident + r'\??)',
247 bygroups(Punctuation, Keyword.Type)),
248 (r'(class|struct|variant|module)(\s+)',
249 bygroups(Keyword, Text), 'class'),
250 (r'(namespace|using)(\s+)', bygroups(Keyword, Text),
251 'namespace'),
252 (cs_ident, Name),
253 ],
254 'class': [
255 (cs_ident, Name.Class, '#pop')
256 ],
257 'namespace': [
258 (r'(?=\()', Text, '#pop'), # using (resource)
259 ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop')
260 ],
261 'splice-string': [
262 (r'[^"$]', String),
263 (r'\$' + cs_ident, Name),
264 (r'(\$)(\()', bygroups(Name, Punctuation),
265 'splice-string-content'),
266 (r'\\"', String),
267 (r'"', String, '#pop')
268 ],
269 'splice-string2': [
270 (r'[^#<>$]', String),
271 (r'\$' + cs_ident, Name),
272 (r'(\$)(\()', bygroups(Name, Punctuation),
273 'splice-string-content'),
274 (r'<#', String, '#push'),
275 (r'#>', String, '#pop')
276 ],
277 'recursive-string': [
278 (r'[^#<>]', String),
279 (r'<#', String, '#push'),
280 (r'#>', String, '#pop')
281 ],
282 'splice-string-content': [
283 (r'if|match', Keyword),
284 (r'[~!%^&*+=|\[\]:;,.<>/?-\\"$ ]', Punctuation),
285 (cs_ident, Name),
286 (r'\d+', Number),
287 (r'\(', Punctuation, '#push'),
288 (r'\)', Punctuation, '#pop')
289 ]
290 }
291
292 def __init__(self, **options):
293 level = get_choice_opt(options, 'unicodelevel', list(self.tokens.keys()),
294 'basic')
132 if level not in self._all_tokens: 295 if level not in self._all_tokens:
133 # compile the regexes now 296 # compile the regexes now
134 self._tokens = self.__class__.process_tokendef(level) 297 self._tokens = self.__class__.process_tokendef(level)
135 else: 298 else:
136 self._tokens = self._all_tokens[level] 299 self._tokens = self._all_tokens[level]
174 (r'(?<!\.)(true|false|null|self|__eval__|__switch__|array|' 337 (r'(?<!\.)(true|false|null|self|__eval__|__switch__|array|'
175 r'assert|checked|enumerate|filter|getter|len|lock|map|' 338 r'assert|checked|enumerate|filter|getter|len|lock|map|'
176 r'matrix|max|min|normalArrayIndexing|print|property|range|' 339 r'matrix|max|min|normalArrayIndexing|print|property|range|'
177 r'rawArrayIndexing|required|typeof|unchecked|using|' 340 r'rawArrayIndexing|required|typeof|unchecked|using|'
178 r'yieldAll|zip)\b', Name.Builtin), 341 r'yieldAll|zip)\b', Name.Builtin),
179 ('"""(\\\\|\\"|.*?)"""', String.Double), 342 (r'"""(\\\\|\\"|.*?)"""', String.Double),
180 ('"(\\\\|\\"|[^"]*?)"', String.Double), 343 (r'"(\\\\|\\"|[^"]*?)"', String.Double),
181 ("'(\\\\|\\'|[^']*?)'", String.Single), 344 (r"'(\\\\|\\'|[^']*?)'", String.Single),
182 ('[a-zA-Z_][a-zA-Z0-9_]*', Name), 345 (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
183 (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float), 346 (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float),
184 (r'[0-9][0-9\.]*(m|ms|d|h|s)', Number), 347 (r'[0-9][0-9\.]*(ms?|d|h|s)', Number),
185 (r'0\d+', Number.Oct), 348 (r'0\d+', Number.Oct),
186 (r'0x[a-fA-F0-9]+', Number.Hex), 349 (r'0x[a-fA-F0-9]+', Number.Hex),
187 (r'\d+L', Number.Integer.Long), 350 (r'\d+L', Number.Integer.Long),
188 (r'\d+', Number.Integer), 351 (r'\d+', Number.Integer),
189 ], 352 ],
300 Keyword, '#pop'), 463 Keyword, '#pop'),
301 (r'', Text, '#pop'), 464 (r'', Text, '#pop'),
302 ] 465 ]
303 } 466 }
304 467
468
305 class GenericAspxLexer(RegexLexer): 469 class GenericAspxLexer(RegexLexer):
306 """ 470 """
307 Lexer for ASP.NET pages. 471 Lexer for ASP.NET pages.
308 """ 472 """
309 473
322 (r'(.+?)(?=<)', using(XmlLexer)), 486 (r'(.+?)(?=<)', using(XmlLexer)),
323 (r'.+', using(XmlLexer)), 487 (r'.+', using(XmlLexer)),
324 ], 488 ],
325 } 489 }
326 490
491
327 #TODO support multiple languages within the same source file 492 #TODO support multiple languages within the same source file
328 class CSharpAspxLexer(DelegatingLexer): 493 class CSharpAspxLexer(DelegatingLexer):
329 """ 494 """
330 Lexer for highligting C# within ASP.NET pages. 495 Lexer for highligting C# within ASP.NET pages.
331 """ 496 """
342 def analyse_text(text): 507 def analyse_text(text):
343 if re.search(r'Page\s*Language="C#"', text, re.I) is not None: 508 if re.search(r'Page\s*Language="C#"', text, re.I) is not None:
344 return 0.2 509 return 0.2
345 elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None: 510 elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None:
346 return 0.15 511 return 0.15
347 return 0.001 # TODO really only for when filename matched... 512
348 513
349 class VbNetAspxLexer(DelegatingLexer): 514 class VbNetAspxLexer(DelegatingLexer):
350 """ 515 """
351 Lexer for highligting Visual Basic.net within ASP.NET pages. 516 Lexer for highligting Visual Basic.net within ASP.NET pages.
352 """ 517 """
363 def analyse_text(text): 528 def analyse_text(text):
364 if re.search(r'Page\s*Language="Vb"', text, re.I) is not None: 529 if re.search(r'Page\s*Language="Vb"', text, re.I) is not None:
365 return 0.2 530 return 0.2
366 elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None: 531 elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None:
367 return 0.15 532 return 0.15
533
534
535 # Very close to functional.OcamlLexer
536 class FSharpLexer(RegexLexer):
537 """
538 For the F# language.
539
540 *New in Pygments 1.5.*
541 """
542
543 name = 'FSharp'
544 aliases = ['fsharp']
545 filenames = ['*.fs', '*.fsi']
546 mimetypes = ['text/x-fsharp']
547
548 keywords = [
549 'abstract', 'and', 'as', 'assert', 'base', 'begin', 'class',
550 'default', 'delegate', 'do', 'do!', 'done', 'downcast',
551 'downto', 'elif', 'else', 'end', 'exception', 'extern',
552 'false', 'finally', 'for', 'fun', 'function', 'global', 'if',
553 'in', 'inherit', 'inline', 'interface', 'internal', 'lazy',
554 'let', 'let!', 'match', 'member', 'module', 'mutable',
555 'namespace', 'new', 'null', 'of', 'open', 'or', 'override',
556 'private', 'public', 'rec', 'return', 'return!', 'sig',
557 'static', 'struct', 'then', 'to', 'true', 'try', 'type',
558 'upcast', 'use', 'use!', 'val', 'void', 'when', 'while',
559 'with', 'yield', 'yield!'
560 ]
561 keyopts = [
562 '!=','#','&&','&','\(','\)','\*','\+',',','-\.',
563 '->','-','\.\.','\.','::',':=',':>',':',';;',';','<-',
564 '<','>]','>','\?\?','\?','\[<','\[>','\[\|','\[',
565 ']','_','`','{','\|\]','\|','}','~','<@','=','@>'
566 ]
567
568 operators = r'[!$%&*+\./:<=>?@^|~-]'
569 word_operators = ['and', 'asr', 'land', 'lor', 'lsl', 'lxor', 'mod', 'not', 'or']
570 prefix_syms = r'[!?~]'
571 infix_syms = r'[=<>@^|&+\*/$%-]'
572 primitives = ['unit', 'int', 'float', 'bool', 'string', 'char', 'list', 'array',
573 'byte', 'sbyte', 'int16', 'uint16', 'uint32', 'int64', 'uint64'
574 'nativeint', 'unativeint', 'decimal', 'void', 'float32', 'single',
575 'double']
576
577 tokens = {
578 'escape-sequence': [
579 (r'\\[\\\"\'ntbr]', String.Escape),
580 (r'\\[0-9]{3}', String.Escape),
581 (r'\\x[0-9a-fA-F]{2}', String.Escape),
582 ],
583 'root': [
584 (r'\s+', Text),
585 (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo),
586 (r'\b([A-Z][A-Za-z0-9_\']*)(?=\s*\.)',
587 Name.Namespace, 'dotted'),
588 (r'\b([A-Z][A-Za-z0-9_\']*)', Name.Class),
589 (r'//.*?\n', Comment.Single),
590 (r'\(\*(?!\))', Comment, 'comment'),
591 (r'\b(%s)\b' % '|'.join(keywords), Keyword),
592 (r'(%s)' % '|'.join(keyopts), Operator),
593 (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator),
594 (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word),
595 (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type),
596
597 (r'#[ \t]*(if|endif|else|line|nowarn|light)\b.*?\n',
598 Comment.Preproc),
599
600 (r"[^\W\d][\w']*", Name),
601
602 (r'\d[\d_]*', Number.Integer),
603 (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
604 (r'0[oO][0-7][0-7_]*', Number.Oct),
605 (r'0[bB][01][01_]*', Number.Binary),
606 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
607
608 (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'",
609 String.Char),
610 (r"'.'", String.Char),
611 (r"'", Keyword), # a stray quote is another syntax element
612
613 (r'"', String.Double, 'string'),
614
615 (r'[~?][a-z][\w\']*:', Name.Variable),
616 ],
617 'comment': [
618 (r'[^(*)]+', Comment),
619 (r'\(\*', Comment, '#push'),
620 (r'\*\)', Comment, '#pop'),
621 (r'[(*)]', Comment),
622 ],
623 'string': [
624 (r'[^\\"]+', String.Double),
625 include('escape-sequence'),
626 (r'\\\n', String.Double),
627 (r'"', String.Double, '#pop'),
628 ],
629 'dotted': [
630 (r'\s+', Text),
631 (r'\.', Punctuation),
632 (r'[A-Z][A-Za-z0-9_\']*(?=\s*\.)', Name.Namespace),
633 (r'[A-Z][A-Za-z0-9_\']*', Name.Class, '#pop'),
634 (r'[a-z_][A-Za-z0-9_\']*', Name, '#pop'),
635 ],
636 }

eric ide

mercurial