You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

csharp6.py 3.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, include, using, this, default
  4. from pygments.token import Punctuation, Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
  5. from pygments.util import get_choice_opt, iteritems
  6. from pygments import unistring as uni
  7. from pygments.lexers.html import XmlLexer
  8. class CSharp6Lexer(RegexLexer):
  9. name = 'C#6'
  10. aliases = ['csharp6', 'c#6']
  11. filenames = ['*.cs']
  12. mimetypes = ['text/x-csharp']
  13. flags = re.MULTILINE | re.DOTALL | re.UNICODE
  14. cs_ident = ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  15. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  16. 'Cf', 'Mn', 'Mc') + ']*')
  17. tokens = {
  18. 'root': [
  19. # method names
  20. (r'^([ \t]*(?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
  21. r'(' + cs_ident + ')' # method name
  22. r'(\s*)(\()', # signature start
  23. bygroups(using(this), Name.Function, Text, Punctuation)),
  24. (r'^\s*\[.*?\]', Name.Attribute),
  25. (r'[^\S\n]+', Text),
  26. (r'\\\n', Text), # line continuation
  27. (r'//.*?\n', Comment.Single),
  28. (r'/[*].*?[*]/', Comment.Multiline),
  29. (r'\n', Text),
  30. (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
  31. (r'[{}]', Punctuation),
  32. (r'@\$?"(""|[^"])*"', String),
  33. (r'"\$?(\\\\|\\"|[^"\n])*["\n]', String),
  34. (r"'\\.'|'[^\\]'", String.Char),
  35. (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?"
  36. r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
  37. (r'#[ \t]*(if|endif|else|elif|define|undef|'
  38. r'line|error|warning|region|endregion|pragma)\b.*?\n',
  39. Comment.Preproc),
  40. (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Text,
  41. Keyword)),
  42. (r'(abstract|as|async|await|base|break|case|catch|'
  43. r'checked|const|continue|default|delegate|'
  44. r'do|else|enum|event|explicit|extern|false|finally|'
  45. r'fixed|for|foreach|goto|if|implicit|in|interface|'
  46. r'internal|is|lock|new|null|operator|'
  47. r'out|override|params|private|protected|public|readonly|'
  48. r'ref|return|sealed|sizeof|stackalloc|static|'
  49. r'switch|this|throw|true|try|typeof|'
  50. r'unchecked|unsafe|virtual|var|void|while|'
  51. r'get|set|new|partial|yield|add|remove|value|alias|ascending|'
  52. r'descending|from|group|into|orderby|select|where|'
  53. r'join|equals)\b', Keyword),
  54. (r'(global)(::)', bygroups(Keyword, Punctuation)),
  55. (r'(bool|byte|char|decimal|double|dynamic|float|int|long|object|'
  56. r'sbyte|short|string|uint|ulong|ushort|var)\b\??', Keyword.Type),
  57. (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'class'),
  58. (r'(namespace|using)(\s+)', bygroups(Keyword, Text), 'namespace'),
  59. (cs_ident, Name),
  60. ],
  61. 'class': [
  62. (cs_ident, Name.Class, '#pop'),
  63. default('#pop'),
  64. ],
  65. 'namespace': [
  66. (r'(?=\()', Text, '#pop'), # using (resource)
  67. ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop'),
  68. ]
  69. }
  70. def setup(app):
  71. from sphinx.highlighting import lexers
  72. lexers['csharp6'] = CSharp6Lexer()