dotnet.py 27 KB

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