dotnet.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.dotnet
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for .net languages.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, DelegatingLexer, bygroups, include, \
  11. using, this, default, words
  12. from pygments.token import Punctuation, \
  13. Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
  14. from pygments.util import get_choice_opt, iteritems
  15. from pygments import unistring as uni
  16. from pygments.lexers.html import XmlLexer
  17. __all__ = ['CSharpLexer', 'NemerleLexer', 'BooLexer', 'VbNetLexer',
  18. 'CSharpAspxLexer', 'VbNetAspxLexer', 'FSharpLexer']
  19. class CSharpLexer(RegexLexer):
  20. """
  21. For `C# <http://msdn2.microsoft.com/en-us/vcsharp/default.aspx>`_
  22. source code.
  23. Additional options accepted:
  24. `unicodelevel`
  25. Determines which Unicode characters this lexer allows for identifiers.
  26. The possible values are:
  27. * ``none`` -- only the ASCII letters and numbers are allowed. This
  28. is the fastest selection.
  29. * ``basic`` -- all Unicode characters from the specification except
  30. category ``Lo`` are allowed.
  31. * ``full`` -- all Unicode characters as specified in the C# specs
  32. are allowed. Note that this means a considerable slowdown since the
  33. ``Lo`` category has more than 40,000 characters in it!
  34. The default value is ``basic``.
  35. .. versionadded:: 0.8
  36. """
  37. name = 'C#'
  38. aliases = ['csharp', 'c#']
  39. filenames = ['*.cs']
  40. mimetypes = ['text/x-csharp'] # inferred
  41. flags = re.MULTILINE | re.DOTALL | re.UNICODE
  42. # for the range of allowed unicode characters in identifiers, see
  43. # http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
  44. levels = {
  45. 'none': r'@?[_a-zA-Z]\w*',
  46. 'basic': ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  47. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  48. 'Cf', 'Mn', 'Mc') + ']*'),
  49. 'full': ('@?(?:_|[^' +
  50. uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + '])'
  51. + '[^' + uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
  52. 'Nd', 'Pc', 'Cf', 'Mn', 'Mc') + ']*'),
  53. }
  54. tokens = {}
  55. token_variants = True
  56. for levelname, cs_ident in iteritems(levels):
  57. tokens[levelname] = {
  58. 'root': [
  59. # method names
  60. (r'^([ \t]*(?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
  61. r'(' + cs_ident + ')' # method name
  62. r'(\s*)(\()', # signature start
  63. bygroups(using(this), Name.Function, Text, Punctuation)),
  64. (r'^\s*\[.*?\]', Name.Attribute),
  65. (r'[^\S\n]+', Text),
  66. (r'\\\n', Text), # line continuation
  67. (r'//.*?\n', Comment.Single),
  68. (r'/[*].*?[*]/', Comment.Multiline),
  69. (r'\n', Text),
  70. (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
  71. (r'[{}]', Punctuation),
  72. (r'@"(""|[^"])*"', String),
  73. (r'"(\\\\|\\"|[^"\n])*["\n]', String),
  74. (r"'\\.'|'[^\\]'", String.Char),
  75. (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?"
  76. r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
  77. (r'#[ \t]*(if|endif|else|elif|define|undef|'
  78. r'line|error|warning|region|endregion|pragma)\b.*?\n',
  79. Comment.Preproc),
  80. (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Text,
  81. Keyword)),
  82. (r'(abstract|as|async|await|base|break|by|case|catch|'
  83. r'checked|const|continue|default|delegate|'
  84. r'do|else|enum|event|explicit|extern|false|finally|'
  85. r'fixed|for|foreach|goto|if|implicit|in|interface|'
  86. r'internal|is|let|lock|new|null|on|operator|'
  87. r'out|override|params|private|protected|public|readonly|'
  88. r'ref|return|sealed|sizeof|stackalloc|static|'
  89. r'switch|this|throw|true|try|typeof|'
  90. r'unchecked|unsafe|virtual|void|while|'
  91. r'get|set|new|partial|yield|add|remove|value|alias|ascending|'
  92. r'descending|from|group|into|orderby|select|thenby|where|'
  93. r'join|equals)\b', Keyword),
  94. (r'(global)(::)', bygroups(Keyword, Punctuation)),
  95. (r'(bool|byte|char|decimal|double|dynamic|float|int|long|object|'
  96. r'sbyte|short|string|uint|ulong|ushort|var)\b\??', Keyword.Type),
  97. (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'class'),
  98. (r'(namespace|using)(\s+)', bygroups(Keyword, Text), 'namespace'),
  99. (cs_ident, Name),
  100. ],
  101. 'class': [
  102. (cs_ident, Name.Class, '#pop'),
  103. default('#pop'),
  104. ],
  105. 'namespace': [
  106. (r'(?=\()', Text, '#pop'), # using (resource)
  107. ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop'),
  108. ]
  109. }
  110. def __init__(self, **options):
  111. level = get_choice_opt(options, 'unicodelevel', list(self.tokens), 'basic')
  112. if level not in self._all_tokens:
  113. # compile the regexes now
  114. self._tokens = self.__class__.process_tokendef(level)
  115. else:
  116. self._tokens = self._all_tokens[level]
  117. RegexLexer.__init__(self, **options)
  118. class NemerleLexer(RegexLexer):
  119. """
  120. For `Nemerle <http://nemerle.org>`_ source code.
  121. Additional options accepted:
  122. `unicodelevel`
  123. Determines which Unicode characters this lexer allows for identifiers.
  124. The possible values are:
  125. * ``none`` -- only the ASCII letters and numbers are allowed. This
  126. is the fastest selection.
  127. * ``basic`` -- all Unicode characters from the specification except
  128. category ``Lo`` are allowed.
  129. * ``full`` -- all Unicode characters as specified in the C# specs
  130. are allowed. Note that this means a considerable slowdown since the
  131. ``Lo`` category has more than 40,000 characters in it!
  132. The default value is ``basic``.
  133. .. versionadded:: 1.5
  134. """
  135. name = 'Nemerle'
  136. aliases = ['nemerle']
  137. filenames = ['*.n']
  138. mimetypes = ['text/x-nemerle'] # inferred
  139. flags = re.MULTILINE | re.DOTALL | re.UNICODE
  140. # for the range of allowed unicode characters in identifiers, see
  141. # http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
  142. levels = {
  143. 'none': r'@?[_a-zA-Z]\w*',
  144. 'basic': ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  145. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  146. 'Cf', 'Mn', 'Mc') + ']*'),
  147. 'full': ('@?(?:_|[^' +
  148. uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + '])'
  149. + '[^' + uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
  150. 'Nd', 'Pc', 'Cf', 'Mn', 'Mc') + ']*'),
  151. }
  152. tokens = {}
  153. token_variants = True
  154. for levelname, cs_ident in iteritems(levels):
  155. tokens[levelname] = {
  156. 'root': [
  157. # method names
  158. (r'^([ \t]*(?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
  159. r'(' + cs_ident + ')' # method name
  160. r'(\s*)(\()', # signature start
  161. bygroups(using(this), Name.Function, Text, Punctuation)),
  162. (r'^\s*\[.*?\]', Name.Attribute),
  163. (r'[^\S\n]+', Text),
  164. (r'\\\n', Text), # line continuation
  165. (r'//.*?\n', Comment.Single),
  166. (r'/[*].*?[*]/', Comment.Multiline),
  167. (r'\n', Text),
  168. (r'\$\s*"', String, 'splice-string'),
  169. (r'\$\s*<#', String, 'splice-string2'),
  170. (r'<#', String, 'recursive-string'),
  171. (r'(<\[)\s*(' + cs_ident + ':)?', Keyword),
  172. (r'\]\>', Keyword),
  173. # quasiquotation only
  174. (r'\$' + cs_ident, Name),
  175. (r'(\$)(\()', bygroups(Name, Punctuation),
  176. 'splice-string-content'),
  177. (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
  178. (r'[{}]', Punctuation),
  179. (r'@"(""|[^"])*"', String),
  180. (r'"(\\\\|\\"|[^"\n])*["\n]', String),
  181. (r"'\\.'|'[^\\]'", String.Char),
  182. (r"0[xX][0-9a-fA-F]+[Ll]?", Number),
  183. (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?[flFLdD]?", Number),
  184. (r'#[ \t]*(if|endif|else|elif|define|undef|'
  185. r'line|error|warning|region|endregion|pragma)\b.*?\n',
  186. Comment.Preproc),
  187. (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Text,
  188. Keyword)),
  189. (r'(abstract|and|as|base|catch|def|delegate|'
  190. r'enum|event|extern|false|finally|'
  191. r'fun|implements|interface|internal|'
  192. r'is|macro|match|matches|module|mutable|new|'
  193. r'null|out|override|params|partial|private|'
  194. r'protected|public|ref|sealed|static|'
  195. r'syntax|this|throw|true|try|type|typeof|'
  196. r'virtual|volatile|when|where|with|'
  197. r'assert|assert2|async|break|checked|continue|do|else|'
  198. r'ensures|for|foreach|if|late|lock|new|nolate|'
  199. r'otherwise|regexp|repeat|requires|return|surroundwith|'
  200. r'unchecked|unless|using|while|yield)\b', Keyword),
  201. (r'(global)(::)', bygroups(Keyword, Punctuation)),
  202. (r'(bool|byte|char|decimal|double|float|int|long|object|sbyte|'
  203. r'short|string|uint|ulong|ushort|void|array|list)\b\??',
  204. Keyword.Type),
  205. (r'(:>?)\s*(' + cs_ident + r'\??)',
  206. bygroups(Punctuation, Keyword.Type)),
  207. (r'(class|struct|variant|module)(\s+)',
  208. bygroups(Keyword, Text), 'class'),
  209. (r'(namespace|using)(\s+)', bygroups(Keyword, Text),
  210. 'namespace'),
  211. (cs_ident, Name),
  212. ],
  213. 'class': [
  214. (cs_ident, Name.Class, '#pop')
  215. ],
  216. 'namespace': [
  217. (r'(?=\()', Text, '#pop'), # using (resource)
  218. ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop')
  219. ],
  220. 'splice-string': [
  221. (r'[^"$]', String),
  222. (r'\$' + cs_ident, Name),
  223. (r'(\$)(\()', bygroups(Name, Punctuation),
  224. 'splice-string-content'),
  225. (r'\\"', String),
  226. (r'"', String, '#pop')
  227. ],
  228. 'splice-string2': [
  229. (r'[^#<>$]', String),
  230. (r'\$' + cs_ident, Name),
  231. (r'(\$)(\()', bygroups(Name, Punctuation),
  232. 'splice-string-content'),
  233. (r'<#', String, '#push'),
  234. (r'#>', String, '#pop')
  235. ],
  236. 'recursive-string': [
  237. (r'[^#<>]', String),
  238. (r'<#', String, '#push'),
  239. (r'#>', String, '#pop')
  240. ],
  241. 'splice-string-content': [
  242. (r'if|match', Keyword),
  243. (r'[~!%^&*+=|\[\]:;,.<>/?-\\"$ ]', Punctuation),
  244. (cs_ident, Name),
  245. (r'\d+', Number),
  246. (r'\(', Punctuation, '#push'),
  247. (r'\)', Punctuation, '#pop')
  248. ]
  249. }
  250. def __init__(self, **options):
  251. level = get_choice_opt(options, 'unicodelevel', list(self.tokens),
  252. 'basic')
  253. if level not in self._all_tokens:
  254. # compile the regexes now
  255. self._tokens = self.__class__.process_tokendef(level)
  256. else:
  257. self._tokens = self._all_tokens[level]
  258. RegexLexer.__init__(self, **options)
  259. class BooLexer(RegexLexer):
  260. """
  261. For `Boo <http://boo.codehaus.org/>`_ source code.
  262. """
  263. name = 'Boo'
  264. aliases = ['boo']
  265. filenames = ['*.boo']
  266. mimetypes = ['text/x-boo']
  267. tokens = {
  268. 'root': [
  269. (r'\s+', Text),
  270. (r'(#|//).*$', Comment.Single),
  271. (r'/[*]', Comment.Multiline, 'comment'),
  272. (r'[]{}:(),.;[]', Punctuation),
  273. (r'\\\n', Text),
  274. (r'\\', Text),
  275. (r'(in|is|and|or|not)\b', Operator.Word),
  276. (r'/(\\\\|\\/|[^/\s])/', String.Regex),
  277. (r'@/(\\\\|\\/|[^/])*/', String.Regex),
  278. (r'=~|!=|==|<<|>>|[-+/*%=<>&^|]', Operator),
  279. (r'(as|abstract|callable|constructor|destructor|do|import|'
  280. r'enum|event|final|get|interface|internal|of|override|'
  281. r'partial|private|protected|public|return|set|static|'
  282. r'struct|transient|virtual|yield|super|and|break|cast|'
  283. r'continue|elif|else|ensure|except|for|given|goto|if|in|'
  284. r'is|isa|not|or|otherwise|pass|raise|ref|try|unless|when|'
  285. r'while|from|as)\b', Keyword),
  286. (r'def(?=\s+\(.*?\))', Keyword),
  287. (r'(def)(\s+)', bygroups(Keyword, Text), 'funcname'),
  288. (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
  289. (r'(namespace)(\s+)', bygroups(Keyword, Text), 'namespace'),
  290. (r'(?<!\.)(true|false|null|self|__eval__|__switch__|array|'
  291. r'assert|checked|enumerate|filter|getter|len|lock|map|'
  292. r'matrix|max|min|normalArrayIndexing|print|property|range|'
  293. r'rawArrayIndexing|required|typeof|unchecked|using|'
  294. r'yieldAll|zip)\b', Name.Builtin),
  295. (r'"""(\\\\|\\"|.*?)"""', String.Double),
  296. (r'"(\\\\|\\"|[^"]*?)"', String.Double),
  297. (r"'(\\\\|\\'|[^']*?)'", String.Single),
  298. (r'[a-zA-Z_]\w*', Name),
  299. (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float),
  300. (r'[0-9][0-9.]*(ms?|d|h|s)', Number),
  301. (r'0\d+', Number.Oct),
  302. (r'0x[a-fA-F0-9]+', Number.Hex),
  303. (r'\d+L', Number.Integer.Long),
  304. (r'\d+', Number.Integer),
  305. ],
  306. 'comment': [
  307. ('/[*]', Comment.Multiline, '#push'),
  308. ('[*]/', Comment.Multiline, '#pop'),
  309. ('[^/*]', Comment.Multiline),
  310. ('[*/]', Comment.Multiline)
  311. ],
  312. 'funcname': [
  313. (r'[a-zA-Z_]\w*', Name.Function, '#pop')
  314. ],
  315. 'classname': [
  316. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  317. ],
  318. 'namespace': [
  319. (r'[a-zA-Z_][\w.]*', Name.Namespace, '#pop')
  320. ]
  321. }
  322. class VbNetLexer(RegexLexer):
  323. """
  324. For
  325. `Visual Basic.NET <http://msdn2.microsoft.com/en-us/vbasic/default.aspx>`_
  326. source code.
  327. """
  328. name = 'VB.net'
  329. aliases = ['vb.net', 'vbnet']
  330. filenames = ['*.vb', '*.bas']
  331. mimetypes = ['text/x-vbnet', 'text/x-vba'] # (?)
  332. uni_name = '[_' + uni.combine('Ll', 'Lt', 'Lm', 'Nl') + ']' + \
  333. '[' + uni.combine('Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  334. 'Cf', 'Mn', 'Mc') + ']*'
  335. flags = re.MULTILINE | re.IGNORECASE
  336. tokens = {
  337. 'root': [
  338. (r'^\s*<.*?>', Name.Attribute),
  339. (r'\s+', Text),
  340. (r'\n', Text),
  341. (r'rem\b.*?\n', Comment),
  342. (r"'.*?\n", Comment),
  343. (r'#If\s.*?\sThen|#ElseIf\s.*?\sThen|#Else|#End\s+If|#Const|'
  344. r'#ExternalSource.*?\n|#End\s+ExternalSource|'
  345. r'#Region.*?\n|#End\s+Region|#ExternalChecksum',
  346. Comment.Preproc),
  347. (r'[(){}!#,.:]', Punctuation),
  348. (r'Option\s+(Strict|Explicit|Compare)\s+'
  349. r'(On|Off|Binary|Text)', Keyword.Declaration),
  350. (words((
  351. 'AddHandler', 'Alias', 'ByRef', 'ByVal', 'Call', 'Case',
  352. 'Catch', 'CBool', 'CByte', 'CChar', 'CDate', 'CDec', 'CDbl',
  353. 'CInt', 'CLng', 'CObj', 'Continue', 'CSByte', 'CShort', 'CSng',
  354. 'CStr', 'CType', 'CUInt', 'CULng', 'CUShort', 'Declare',
  355. 'Default', 'Delegate', 'DirectCast', 'Do', 'Each', 'Else',
  356. 'ElseIf', 'EndIf', 'Erase', 'Error', 'Event', 'Exit', 'False',
  357. 'Finally', 'For', 'Friend', 'Get', 'Global', 'GoSub', 'GoTo',
  358. 'Handles', 'If', 'Implements', 'Inherits', 'Interface', 'Let',
  359. 'Lib', 'Loop', 'Me', 'MustInherit', 'MustOverride', 'MyBase',
  360. 'MyClass', 'Narrowing', 'New', 'Next', 'Not', 'Nothing',
  361. 'NotInheritable', 'NotOverridable', 'Of', 'On', 'Operator',
  362. 'Option', 'Optional', 'Overloads', 'Overridable', 'Overrides',
  363. 'ParamArray', 'Partial', 'Private', 'Protected', 'Public',
  364. 'RaiseEvent', 'ReadOnly', 'ReDim', 'RemoveHandler', 'Resume',
  365. 'Return', 'Select', 'Set', 'Shadows', 'Shared', 'Single',
  366. 'Static', 'Step', 'Stop', 'SyncLock', 'Then', 'Throw', 'To',
  367. 'True', 'Try', 'TryCast', 'Wend', 'Using', 'When', 'While',
  368. 'Widening', 'With', 'WithEvents', 'WriteOnly'),
  369. prefix=r'(?<!\.)', suffix=r'\b'), Keyword),
  370. (r'(?<!\.)End\b', Keyword, 'end'),
  371. (r'(?<!\.)(Dim|Const)\b', Keyword, 'dim'),
  372. (r'(?<!\.)(Function|Sub|Property)(\s+)',
  373. bygroups(Keyword, Text), 'funcname'),
  374. (r'(?<!\.)(Class|Structure|Enum)(\s+)',
  375. bygroups(Keyword, Text), 'classname'),
  376. (r'(?<!\.)(Module|Namespace|Imports)(\s+)',
  377. bygroups(Keyword, Text), 'namespace'),
  378. (r'(?<!\.)(Boolean|Byte|Char|Date|Decimal|Double|Integer|Long|'
  379. r'Object|SByte|Short|Single|String|Variant|UInteger|ULong|'
  380. r'UShort)\b', Keyword.Type),
  381. (r'(?<!\.)(AddressOf|And|AndAlso|As|GetType|In|Is|IsNot|Like|Mod|'
  382. r'Or|OrElse|TypeOf|Xor)\b', Operator.Word),
  383. (r'&=|[*]=|/=|\\=|\^=|\+=|-=|<<=|>>=|<<|>>|:=|'
  384. r'<=|>=|<>|[-&*/\\^+=<>\[\]]',
  385. Operator),
  386. ('"', String, 'string'),
  387. (r'_\n', Text), # Line continuation (must be before Name)
  388. (uni_name + '[%&@!#$]?', Name),
  389. ('#.*?#', Literal.Date),
  390. (r'(\d+\.\d*|\d*\.\d+)(F[+-]?[0-9]+)?', Number.Float),
  391. (r'\d+([SILDFR]|US|UI|UL)?', Number.Integer),
  392. (r'&H[0-9a-f]+([SILDFR]|US|UI|UL)?', Number.Integer),
  393. (r'&O[0-7]+([SILDFR]|US|UI|UL)?', Number.Integer),
  394. ],
  395. 'string': [
  396. (r'""', String),
  397. (r'"C?', String, '#pop'),
  398. (r'[^"]+', String),
  399. ],
  400. 'dim': [
  401. (uni_name, Name.Variable, '#pop'),
  402. default('#pop'), # any other syntax
  403. ],
  404. 'funcname': [
  405. (uni_name, Name.Function, '#pop'),
  406. ],
  407. 'classname': [
  408. (uni_name, Name.Class, '#pop'),
  409. ],
  410. 'namespace': [
  411. (uni_name, Name.Namespace),
  412. (r'\.', Name.Namespace),
  413. default('#pop'),
  414. ],
  415. 'end': [
  416. (r'\s+', Text),
  417. (r'(Function|Sub|Property|Class|Structure|Enum|Module|Namespace)\b',
  418. Keyword, '#pop'),
  419. default('#pop'),
  420. ]
  421. }
  422. def analyse_text(text):
  423. if re.search(r'^\s*(#If|Module|Namespace)', text, re.MULTILINE):
  424. return 0.5
  425. class GenericAspxLexer(RegexLexer):
  426. """
  427. Lexer for ASP.NET pages.
  428. """
  429. name = 'aspx-gen'
  430. filenames = []
  431. mimetypes = []
  432. flags = re.DOTALL
  433. tokens = {
  434. 'root': [
  435. (r'(<%[@=#]?)(.*?)(%>)', bygroups(Name.Tag, Other, Name.Tag)),
  436. (r'(<script.*?>)(.*?)(</script>)', bygroups(using(XmlLexer),
  437. Other,
  438. using(XmlLexer))),
  439. (r'(.+?)(?=<)', using(XmlLexer)),
  440. (r'.+', using(XmlLexer)),
  441. ],
  442. }
  443. # TODO support multiple languages within the same source file
  444. class CSharpAspxLexer(DelegatingLexer):
  445. """
  446. Lexer for highlighting C# within ASP.NET pages.
  447. """
  448. name = 'aspx-cs'
  449. aliases = ['aspx-cs']
  450. filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
  451. mimetypes = []
  452. def __init__(self, **options):
  453. super(CSharpAspxLexer, self).__init__(CSharpLexer, GenericAspxLexer,
  454. **options)
  455. def analyse_text(text):
  456. if re.search(r'Page\s*Language="C#"', text, re.I) is not None:
  457. return 0.2
  458. elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None:
  459. return 0.15
  460. class VbNetAspxLexer(DelegatingLexer):
  461. """
  462. Lexer for highlighting Visual Basic.net within ASP.NET pages.
  463. """
  464. name = 'aspx-vb'
  465. aliases = ['aspx-vb']
  466. filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
  467. mimetypes = []
  468. def __init__(self, **options):
  469. super(VbNetAspxLexer, self).__init__(VbNetLexer, GenericAspxLexer,
  470. **options)
  471. def analyse_text(text):
  472. if re.search(r'Page\s*Language="Vb"', text, re.I) is not None:
  473. return 0.2
  474. elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None:
  475. return 0.15
  476. # Very close to functional.OcamlLexer
  477. class FSharpLexer(RegexLexer):
  478. """
  479. For the `F# language <https://fsharp.org/>`_ (version 3.0).
  480. .. versionadded:: 1.5
  481. """
  482. name = 'F#'
  483. aliases = ['fsharp', 'f#']
  484. filenames = ['*.fs', '*.fsi']
  485. mimetypes = ['text/x-fsharp']
  486. keywords = [
  487. 'abstract', 'as', 'assert', 'base', 'begin', 'class', 'default',
  488. 'delegate', 'do!', 'do', 'done', 'downcast', 'downto', 'elif', 'else',
  489. 'end', 'exception', 'extern', 'false', 'finally', 'for', 'function',
  490. 'fun', 'global', 'if', 'inherit', 'inline', 'interface', 'internal',
  491. 'in', 'lazy', 'let!', 'let', 'match', 'member', 'module', 'mutable',
  492. 'namespace', 'new', 'null', 'of', 'open', 'override', 'private', 'public',
  493. 'rec', 'return!', 'return', 'select', 'static', 'struct', 'then', 'to',
  494. 'true', 'try', 'type', 'upcast', 'use!', 'use', 'val', 'void', 'when',
  495. 'while', 'with', 'yield!', 'yield',
  496. ]
  497. # Reserved words; cannot hurt to color them as keywords too.
  498. keywords += [
  499. 'atomic', 'break', 'checked', 'component', 'const', 'constraint',
  500. 'constructor', 'continue', 'eager', 'event', 'external', 'fixed',
  501. 'functor', 'include', 'method', 'mixin', 'object', 'parallel',
  502. 'process', 'protected', 'pure', 'sealed', 'tailcall', 'trait',
  503. 'virtual', 'volatile',
  504. ]
  505. keyopts = [
  506. '!=', '#', '&&', '&', r'\(', r'\)', r'\*', r'\+', ',', r'-\.',
  507. '->', '-', r'\.\.', r'\.', '::', ':=', ':>', ':', ';;', ';', '<-',
  508. r'<\]', '<', r'>\]', '>', r'\?\?', r'\?', r'\[<', r'\[\|', r'\[', r'\]',
  509. '_', '`', r'\{', r'\|\]', r'\|', r'\}', '~', '<@@', '<@', '=', '@>', '@@>',
  510. ]
  511. operators = r'[!$%&*+\./:<=>?@^|~-]'
  512. word_operators = ['and', 'or', 'not']
  513. prefix_syms = r'[!?~]'
  514. infix_syms = r'[=<>@^|&+\*/$%-]'
  515. primitives = [
  516. 'sbyte', 'byte', 'char', 'nativeint', 'unativeint', 'float32', 'single',
  517. 'float', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32',
  518. 'uint32', 'int64', 'uint64', 'decimal', 'unit', 'bool', 'string',
  519. 'list', 'exn', 'obj', 'enum',
  520. ]
  521. # See http://msdn.microsoft.com/en-us/library/dd233181.aspx and/or
  522. # http://fsharp.org/about/files/spec.pdf for reference. Good luck.
  523. tokens = {
  524. 'escape-sequence': [
  525. (r'\\[\\"\'ntbrafv]', String.Escape),
  526. (r'\\[0-9]{3}', String.Escape),
  527. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  528. (r'\\U[0-9a-fA-F]{8}', String.Escape),
  529. ],
  530. 'root': [
  531. (r'\s+', Text),
  532. (r'\(\)|\[\]', Name.Builtin.Pseudo),
  533. (r'\b(?<!\.)([A-Z][\w\']*)(?=\s*\.)',
  534. Name.Namespace, 'dotted'),
  535. (r'\b([A-Z][\w\']*)', Name),
  536. (r'///.*?\n', String.Doc),
  537. (r'//.*?\n', Comment.Single),
  538. (r'\(\*(?!\))', Comment, 'comment'),
  539. (r'@"', String, 'lstring'),
  540. (r'"""', String, 'tqs'),
  541. (r'"', String, 'string'),
  542. (r'\b(open|module)(\s+)([\w.]+)',
  543. bygroups(Keyword, Text, Name.Namespace)),
  544. (r'\b(let!?)(\s+)(\w+)',
  545. bygroups(Keyword, Text, Name.Variable)),
  546. (r'\b(type)(\s+)(\w+)',
  547. bygroups(Keyword, Text, Name.Class)),
  548. (r'\b(member|override)(\s+)(\w+)(\.)(\w+)',
  549. bygroups(Keyword, Text, Name, Punctuation, Name.Function)),
  550. (r'\b(%s)\b' % '|'.join(keywords), Keyword),
  551. (r'``([^`\n\r\t]|`[^`\n\r\t])+``', Name),
  552. (r'(%s)' % '|'.join(keyopts), Operator),
  553. (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator),
  554. (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word),
  555. (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type),
  556. (r'#[ \t]*(if|endif|else|line|nowarn|light|\d+)\b.*?\n',
  557. Comment.Preproc),
  558. (r"[^\W\d][\w']*", Name),
  559. (r'\d[\d_]*[uU]?[yslLnQRZINGmM]?', Number.Integer),
  560. (r'0[xX][\da-fA-F][\da-fA-F_]*[uU]?[yslLn]?[fF]?', Number.Hex),
  561. (r'0[oO][0-7][0-7_]*[uU]?[yslLn]?', Number.Oct),
  562. (r'0[bB][01][01_]*[uU]?[yslLn]?', Number.Bin),
  563. (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)[fFmM]?',
  564. Number.Float),
  565. (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'B?",
  566. String.Char),
  567. (r"'.'", String.Char),
  568. (r"'", Keyword), # a stray quote is another syntax element
  569. (r'@?"', String.Double, 'string'),
  570. (r'[~?][a-z][\w\']*:', Name.Variable),
  571. ],
  572. 'dotted': [
  573. (r'\s+', Text),
  574. (r'\.', Punctuation),
  575. (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace),
  576. (r'[A-Z][\w\']*', Name, '#pop'),
  577. (r'[a-z_][\w\']*', Name, '#pop'),
  578. # e.g. dictionary index access
  579. default('#pop'),
  580. ],
  581. 'comment': [
  582. (r'[^(*)@"]+', Comment),
  583. (r'\(\*', Comment, '#push'),
  584. (r'\*\)', Comment, '#pop'),
  585. # comments cannot be closed within strings in comments
  586. (r'@"', String, 'lstring'),
  587. (r'"""', String, 'tqs'),
  588. (r'"', String, 'string'),
  589. (r'[(*)@]', Comment),
  590. ],
  591. 'string': [
  592. (r'[^\\"]+', String),
  593. include('escape-sequence'),
  594. (r'\\\n', String),
  595. (r'\n', String), # newlines are allowed in any string
  596. (r'"B?', String, '#pop'),
  597. ],
  598. 'lstring': [
  599. (r'[^"]+', String),
  600. (r'\n', String),
  601. (r'""', String),
  602. (r'"B?', String, '#pop'),
  603. ],
  604. 'tqs': [
  605. (r'[^"]+', String),
  606. (r'\n', String),
  607. (r'"""B?', String, '#pop'),
  608. (r'"', String),
  609. ],
  610. }