dotnet.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. """
  2. pygments.lexers.dotnet
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for .net languages.
  5. :copyright: Copyright 2006-2024 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, Text, Comment, Operator, Keyword, \
  12. Name, String, Number, Literal, Other, Whitespace
  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', 'XppLexer']
  18. class CSharpLexer(RegexLexer):
  19. """
  20. For C# source code.
  21. Additional options accepted:
  22. `unicodelevel`
  23. Determines which Unicode characters this lexer allows for identifiers.
  24. The possible values are:
  25. * ``none`` -- only the ASCII letters and numbers are allowed. This
  26. is the fastest selection.
  27. * ``basic`` -- all Unicode characters from the specification except
  28. category ``Lo`` are allowed.
  29. * ``full`` -- all Unicode characters as specified in the C# specs
  30. are allowed. Note that this means a considerable slowdown since the
  31. ``Lo`` category has more than 40,000 characters in it!
  32. The default value is ``basic``.
  33. .. versionadded:: 0.8
  34. """
  35. name = 'C#'
  36. url = 'https://docs.microsoft.com/en-us/dotnet/csharp/'
  37. aliases = ['csharp', 'c#', 'cs']
  38. filenames = ['*.cs']
  39. mimetypes = ['text/x-csharp'] # inferred
  40. version_added = ''
  41. flags = re.MULTILINE | re.DOTALL
  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 levels.items():
  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(Whitespace, using(this), Name.Function, Whitespace,
  64. Punctuation)),
  65. (r'^(\s*)(\[.*?\])', bygroups(Whitespace, Name.Attribute)),
  66. (r'[^\S\n]+', Whitespace),
  67. (r'(\\)(\n)', bygroups(Text, Whitespace)), # line continuation
  68. (r'//.*?\n', Comment.Single),
  69. (r'/[*].*?[*]/', Comment.Multiline),
  70. (r'\n', Whitespace),
  71. (words((
  72. '>>>=', '>>=', '<<=', '<=', '>=', '+=', '-=', '*=', '/=',
  73. '%=', '&=', '|=', '^=', '??=', '=>', '??', '?.', '!=', '==',
  74. '&&', '||', '>>>', '>>', '<<', '++', '--', '+', '-', '*',
  75. '/', '%', '&', '|', '^', '<', '>', '?', '!', '~', '=',
  76. )), Operator),
  77. (r'=~|!=|==|<<|>>|[-+/*%=<>&^|]', Operator),
  78. (r'[()\[\];:,.]', Punctuation),
  79. (r'[{}]', Punctuation),
  80. (r'@"(""|[^"])*"', String),
  81. (r'\$?"(\\\\|\\[^\\]|[^"\\\n])*["\n]', String),
  82. (r"'\\.'|'[^\\]'", String.Char),
  83. (r"[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)?"
  84. r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
  85. (r'(#)([ \t]*)(if|endif|else|elif|define|undef|'
  86. r'line|error|warning|region|endregion|pragma)\b(.*?)(\n)',
  87. bygroups(Comment.Preproc, Whitespace, Comment.Preproc,
  88. Comment.Preproc, Whitespace)),
  89. (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Whitespace,
  90. Keyword)),
  91. (r'(abstract|as|async|await|base|break|by|case|catch|'
  92. r'checked|const|continue|default|delegate|'
  93. r'do|else|enum|event|explicit|extern|false|finally|'
  94. r'fixed|for|foreach|goto|if|implicit|in|interface|'
  95. r'internal|is|let|lock|new|null|on|operator|'
  96. r'out|override|params|private|protected|public|readonly|'
  97. r'ref|return|sealed|sizeof|stackalloc|static|'
  98. r'switch|this|throw|true|try|typeof|'
  99. r'unchecked|unsafe|virtual|void|while|'
  100. r'get|set|new|partial|yield|add|remove|value|alias|ascending|'
  101. r'descending|from|group|into|orderby|select|thenby|where|'
  102. r'join|equals)\b', Keyword),
  103. (r'(global)(::)', bygroups(Keyword, Punctuation)),
  104. (r'(bool|byte|char|decimal|double|dynamic|float|int|long|object|'
  105. r'sbyte|short|string|uint|ulong|ushort|var)\b\??', Keyword.Type),
  106. (r'(class|struct)(\s+)', bygroups(Keyword, Whitespace), 'class'),
  107. (r'(namespace|using)(\s+)', bygroups(Keyword, Whitespace), 'namespace'),
  108. (cs_ident, Name),
  109. ],
  110. 'class': [
  111. (cs_ident, Name.Class, '#pop'),
  112. default('#pop'),
  113. ],
  114. 'namespace': [
  115. (r'(?=\()', Text, '#pop'), # using (resource)
  116. ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop'),
  117. ]
  118. }
  119. def __init__(self, **options):
  120. level = get_choice_opt(options, 'unicodelevel', list(self.tokens), 'basic')
  121. if level not in self._all_tokens:
  122. # compile the regexes now
  123. self._tokens = self.__class__.process_tokendef(level)
  124. else:
  125. self._tokens = self._all_tokens[level]
  126. RegexLexer.__init__(self, **options)
  127. class NemerleLexer(RegexLexer):
  128. """
  129. For Nemerle source code.
  130. Additional options accepted:
  131. `unicodelevel`
  132. Determines which Unicode characters this lexer allows for identifiers.
  133. The possible values are:
  134. * ``none`` -- only the ASCII letters and numbers are allowed. This
  135. is the fastest selection.
  136. * ``basic`` -- all Unicode characters from the specification except
  137. category ``Lo`` are allowed.
  138. * ``full`` -- all Unicode characters as specified in the C# specs
  139. are allowed. Note that this means a considerable slowdown since the
  140. ``Lo`` category has more than 40,000 characters in it!
  141. The default value is ``basic``.
  142. """
  143. name = 'Nemerle'
  144. url = 'http://nemerle.org'
  145. aliases = ['nemerle']
  146. filenames = ['*.n']
  147. mimetypes = ['text/x-nemerle'] # inferred
  148. version_added = '1.5'
  149. flags = re.MULTILINE | re.DOTALL
  150. # for the range of allowed unicode characters in identifiers, see
  151. # http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
  152. levels = {
  153. 'none': r'@?[_a-zA-Z]\w*',
  154. 'basic': ('@?[_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl') + ']' +
  155. '[' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  156. 'Cf', 'Mn', 'Mc') + ']*'),
  157. 'full': ('@?(?:_|[^' +
  158. uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + '])' +
  159. '[^' + uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
  160. 'Nd', 'Pc', 'Cf', 'Mn', 'Mc') + ']*'),
  161. }
  162. tokens = {}
  163. token_variants = True
  164. for levelname, cs_ident in levels.items():
  165. tokens[levelname] = {
  166. 'root': [
  167. # method names
  168. (r'^([ \t]*)((?:' + cs_ident + r'(?:\[\])?\s+)+?)' # return type
  169. r'(' + cs_ident + ')' # method name
  170. r'(\s*)(\()', # signature start
  171. bygroups(Whitespace, using(this), Name.Function, Whitespace, \
  172. Punctuation)),
  173. (r'^(\s*)(\[.*?\])', bygroups(Whitespace, Name.Attribute)),
  174. (r'[^\S\n]+', Whitespace),
  175. (r'(\\)(\n)', bygroups(Text, Whitespace)), # line continuation
  176. (r'//.*?\n', Comment.Single),
  177. (r'/[*].*?[*]/', Comment.Multiline),
  178. (r'\n', Whitespace),
  179. (r'(\$)(\s*)(")', bygroups(String, Whitespace, String),
  180. 'splice-string'),
  181. (r'(\$)(\s*)(<#)', bygroups(String, Whitespace, String),
  182. 'splice-string2'),
  183. (r'<#', String, 'recursive-string'),
  184. (r'(<\[)(\s*)(' + cs_ident + ':)?',
  185. bygroups(Keyword, Whitespace, Keyword)),
  186. (r'\]\>', Keyword),
  187. # quasiquotation only
  188. (r'\$' + cs_ident, Name),
  189. (r'(\$)(\()', bygroups(Name, Punctuation),
  190. 'splice-string-content'),
  191. (r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation),
  192. (r'[{}]', Punctuation),
  193. (r'@"(""|[^"])*"', String),
  194. (r'"(\\\\|\\[^\\]|[^"\\\n])*["\n]', String),
  195. (r"'\\.'|'[^\\]'", String.Char),
  196. (r"0[xX][0-9a-fA-F]+[Ll]?", Number),
  197. (r"[0-9](\.[0-9]*)?([eE][+-][0-9]+)?[flFLdD]?", Number),
  198. (r'(#)([ \t]*)(if|endif|else|elif|define|undef|'
  199. r'line|error|warning|region|endregion|pragma)\b',
  200. bygroups(Comment.Preproc, Whitespace, Comment.Preproc), 'preproc'),
  201. (r'\b(extern)(\s+)(alias)\b', bygroups(Keyword, Whitespace, Keyword)),
  202. (r'(abstract|and|as|base|catch|def|delegate|'
  203. r'enum|event|extern|false|finally|'
  204. r'fun|implements|interface|internal|'
  205. r'is|macro|match|matches|module|mutable|new|'
  206. r'null|out|override|params|partial|private|'
  207. r'protected|public|ref|sealed|static|'
  208. r'syntax|this|throw|true|try|type|typeof|'
  209. r'virtual|volatile|when|where|with|'
  210. r'assert|assert2|async|break|checked|continue|do|else|'
  211. r'ensures|for|foreach|if|late|lock|new|nolate|'
  212. r'otherwise|regexp|repeat|requires|return|surroundwith|'
  213. r'unchecked|unless|using|while|yield)\b', Keyword),
  214. (r'(global)(::)', bygroups(Keyword, Punctuation)),
  215. (r'(bool|byte|char|decimal|double|float|int|long|object|sbyte|'
  216. r'short|string|uint|ulong|ushort|void|array|list)\b\??',
  217. Keyword.Type),
  218. (r'(:>?)(\s*)(' + cs_ident + r'\??)',
  219. bygroups(Punctuation, Whitespace, Keyword.Type)),
  220. (r'(class|struct|variant|module)(\s+)',
  221. bygroups(Keyword, Whitespace), 'class'),
  222. (r'(namespace|using)(\s+)', bygroups(Keyword, Whitespace),
  223. 'namespace'),
  224. (cs_ident, Name),
  225. ],
  226. 'class': [
  227. (cs_ident, Name.Class, '#pop')
  228. ],
  229. 'preproc': [
  230. (r'\w+', Comment.Preproc),
  231. (r'[ \t]+', Whitespace),
  232. (r'\n', Whitespace, '#pop')
  233. ],
  234. 'namespace': [
  235. (r'(?=\()', Text, '#pop'), # using (resource)
  236. ('(' + cs_ident + r'|\.)+', Name.Namespace, '#pop')
  237. ],
  238. 'splice-string': [
  239. (r'[^"$]', String),
  240. (r'\$' + cs_ident, Name),
  241. (r'(\$)(\()', bygroups(Name, Punctuation),
  242. 'splice-string-content'),
  243. (r'\\"', String),
  244. (r'"', String, '#pop')
  245. ],
  246. 'splice-string2': [
  247. (r'[^#<>$]', String),
  248. (r'\$' + cs_ident, Name),
  249. (r'(\$)(\()', bygroups(Name, Punctuation),
  250. 'splice-string-content'),
  251. (r'<#', String, '#push'),
  252. (r'#>', String, '#pop')
  253. ],
  254. 'recursive-string': [
  255. (r'[^#<>]', String),
  256. (r'<#', String, '#push'),
  257. (r'#>', String, '#pop')
  258. ],
  259. 'splice-string-content': [
  260. (r'if|match', Keyword),
  261. (r'[~!%^&*+=|\[\]:;,.<>/?-\\"$ ]', Punctuation),
  262. (cs_ident, Name),
  263. (r'\d+', Number),
  264. (r'\(', Punctuation, '#push'),
  265. (r'\)', Punctuation, '#pop')
  266. ]
  267. }
  268. def __init__(self, **options):
  269. level = get_choice_opt(options, 'unicodelevel', list(self.tokens),
  270. 'basic')
  271. if level not in self._all_tokens:
  272. # compile the regexes now
  273. self._tokens = self.__class__.process_tokendef(level)
  274. else:
  275. self._tokens = self._all_tokens[level]
  276. RegexLexer.__init__(self, **options)
  277. def analyse_text(text):
  278. """Nemerle is quite similar to Python, but @if is relatively uncommon
  279. elsewhere."""
  280. result = 0
  281. if '@if' in text:
  282. result += 0.1
  283. return result
  284. class BooLexer(RegexLexer):
  285. """
  286. For Boo source code.
  287. """
  288. name = 'Boo'
  289. url = 'https://github.com/boo-lang/boo'
  290. aliases = ['boo']
  291. filenames = ['*.boo']
  292. mimetypes = ['text/x-boo']
  293. version_added = ''
  294. tokens = {
  295. 'root': [
  296. (r'\s+', Whitespace),
  297. (r'(#|//).*$', Comment.Single),
  298. (r'/[*]', Comment.Multiline, 'comment'),
  299. (r'[]{}:(),.;[]', Punctuation),
  300. (r'(\\)(\n)', bygroups(Text, Whitespace)),
  301. (r'\\', Text),
  302. (r'(in|is|and|or|not)\b', Operator.Word),
  303. (r'/(\\\\|\\[^\\]|[^/\\\s])/', String.Regex),
  304. (r'@/(\\\\|\\[^\\]|[^/\\])*/', String.Regex),
  305. (r'=~|!=|==|<<|>>|[-+/*%=<>&^|]', Operator),
  306. (r'(as|abstract|callable|constructor|destructor|do|import|'
  307. r'enum|event|final|get|interface|internal|of|override|'
  308. r'partial|private|protected|public|return|set|static|'
  309. r'struct|transient|virtual|yield|super|and|break|cast|'
  310. r'continue|elif|else|ensure|except|for|given|goto|if|in|'
  311. r'is|isa|not|or|otherwise|pass|raise|ref|try|unless|when|'
  312. r'while|from|as)\b', Keyword),
  313. (r'def(?=\s+\(.*?\))', Keyword),
  314. (r'(def)(\s+)', bygroups(Keyword, Whitespace), 'funcname'),
  315. (r'(class)(\s+)', bygroups(Keyword, Whitespace), 'classname'),
  316. (r'(namespace)(\s+)', bygroups(Keyword, Whitespace), 'namespace'),
  317. (r'(?<!\.)(true|false|null|self|__eval__|__switch__|array|'
  318. r'assert|checked|enumerate|filter|getter|len|lock|map|'
  319. r'matrix|max|min|normalArrayIndexing|print|property|range|'
  320. r'rawArrayIndexing|required|typeof|unchecked|using|'
  321. r'yieldAll|zip)\b', Name.Builtin),
  322. (r'"""(\\\\|\\"|.*?)"""', String.Double),
  323. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  324. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  325. (r'[a-zA-Z_]\w*', Name),
  326. (r'(\d+\.\d*|\d*\.\d+)([fF][+-]?[0-9]+)?', Number.Float),
  327. (r'[0-9][0-9.]*(ms?|d|h|s)', Number),
  328. (r'0\d+', Number.Oct),
  329. (r'0x[a-fA-F0-9]+', Number.Hex),
  330. (r'\d+L', Number.Integer.Long),
  331. (r'\d+', Number.Integer),
  332. ],
  333. 'comment': [
  334. ('/[*]', Comment.Multiline, '#push'),
  335. ('[*]/', Comment.Multiline, '#pop'),
  336. ('[^/*]', Comment.Multiline),
  337. ('[*/]', Comment.Multiline)
  338. ],
  339. 'funcname': [
  340. (r'[a-zA-Z_]\w*', Name.Function, '#pop')
  341. ],
  342. 'classname': [
  343. (r'[a-zA-Z_]\w*', Name.Class, '#pop')
  344. ],
  345. 'namespace': [
  346. (r'[a-zA-Z_][\w.]*', Name.Namespace, '#pop')
  347. ]
  348. }
  349. class VbNetLexer(RegexLexer):
  350. """
  351. For Visual Basic.NET source code.
  352. Also LibreOffice Basic, OpenOffice Basic, and StarOffice Basic.
  353. """
  354. name = 'VB.net'
  355. url = 'https://docs.microsoft.com/en-us/dotnet/visual-basic/'
  356. aliases = ['vb.net', 'vbnet', 'lobas', 'oobas', 'sobas', 'visual-basic', 'visualbasic']
  357. filenames = ['*.vb', '*.bas']
  358. mimetypes = ['text/x-vbnet', 'text/x-vba'] # (?)
  359. version_added = ''
  360. uni_name = '[_' + uni.combine('Ll', 'Lt', 'Lm', 'Nl') + ']' + \
  361. '[' + uni.combine('Ll', 'Lt', 'Lm', 'Nl', 'Nd', 'Pc',
  362. 'Cf', 'Mn', 'Mc') + ']*'
  363. flags = re.MULTILINE | re.IGNORECASE
  364. tokens = {
  365. 'root': [
  366. (r'^\s*<.*?>', Name.Attribute),
  367. (r'\s+', Whitespace),
  368. (r'\n', Whitespace),
  369. (r'(rem\b.*?)(\n)', bygroups(Comment, Whitespace)),
  370. (r"('.*?)(\n)", bygroups(Comment, Whitespace)),
  371. (r'#If\s.*?\sThen|#ElseIf\s.*?\sThen|#Else|#End\s+If|#Const|'
  372. r'#ExternalSource.*?\n|#End\s+ExternalSource|'
  373. r'#Region.*?\n|#End\s+Region|#ExternalChecksum',
  374. Comment.Preproc),
  375. (r'[(){}!#,.:]', Punctuation),
  376. (r'(Option)(\s+)(Strict|Explicit|Compare)(\s+)'
  377. r'(On|Off|Binary|Text)',
  378. bygroups(Keyword.Declaration, Whitespace, Keyword.Declaration,
  379. Whitespace, Keyword.Declaration)),
  380. (words((
  381. 'AddHandler', 'Alias', 'ByRef', 'ByVal', 'Call', 'Case',
  382. 'Catch', 'CBool', 'CByte', 'CChar', 'CDate', 'CDec', 'CDbl',
  383. 'CInt', 'CLng', 'CObj', 'Continue', 'CSByte', 'CShort', 'CSng',
  384. 'CStr', 'CType', 'CUInt', 'CULng', 'CUShort', 'Declare',
  385. 'Default', 'Delegate', 'DirectCast', 'Do', 'Each', 'Else',
  386. 'ElseIf', 'EndIf', 'Erase', 'Error', 'Event', 'Exit', 'False',
  387. 'Finally', 'For', 'Friend', 'Get', 'Global', 'GoSub', 'GoTo',
  388. 'Handles', 'If', 'Implements', 'Inherits', 'Interface', 'Let',
  389. 'Lib', 'Loop', 'Me', 'MustInherit', 'MustOverride', 'MyBase',
  390. 'MyClass', 'Narrowing', 'New', 'Next', 'Not', 'Nothing',
  391. 'NotInheritable', 'NotOverridable', 'Of', 'On', 'Operator',
  392. 'Option', 'Optional', 'Overloads', 'Overridable', 'Overrides',
  393. 'ParamArray', 'Partial', 'Private', 'Protected', 'Public',
  394. 'RaiseEvent', 'ReadOnly', 'ReDim', 'RemoveHandler', 'Resume',
  395. 'Return', 'Select', 'Set', 'Shadows', 'Shared', 'Single',
  396. 'Static', 'Step', 'Stop', 'SyncLock', 'Then', 'Throw', 'To',
  397. 'True', 'Try', 'TryCast', 'Wend', 'Using', 'When', 'While',
  398. 'Widening', 'With', 'WithEvents', 'WriteOnly'),
  399. prefix=r'(?<!\.)', suffix=r'\b'), Keyword),
  400. (r'(?<!\.)End\b', Keyword, 'end'),
  401. (r'(?<!\.)(Dim|Const)\b', Keyword, 'dim'),
  402. (r'(?<!\.)(Function|Sub|Property)(\s+)',
  403. bygroups(Keyword, Whitespace), 'funcname'),
  404. (r'(?<!\.)(Class|Structure|Enum)(\s+)',
  405. bygroups(Keyword, Whitespace), 'classname'),
  406. (r'(?<!\.)(Module|Namespace|Imports)(\s+)',
  407. bygroups(Keyword, Whitespace), 'namespace'),
  408. (r'(?<!\.)(Boolean|Byte|Char|Date|Decimal|Double|Integer|Long|'
  409. r'Object|SByte|Short|Single|String|Variant|UInteger|ULong|'
  410. r'UShort)\b', Keyword.Type),
  411. (r'(?<!\.)(AddressOf|And|AndAlso|As|GetType|In|Is|IsNot|Like|Mod|'
  412. r'Or|OrElse|TypeOf|Xor)\b', Operator.Word),
  413. (r'&=|[*]=|/=|\\=|\^=|\+=|-=|<<=|>>=|<<|>>|:=|'
  414. r'<=|>=|<>|[-&*/\\^+=<>\[\]]',
  415. Operator),
  416. ('"', String, 'string'),
  417. (r'(_)(\n)', bygroups(Text, Whitespace)), # Line continuation (must be before Name)
  418. (uni_name + '[%&@!#$]?', Name),
  419. ('#.*?#', Literal.Date),
  420. (r'(\d+\.\d*|\d*\.\d+)(F[+-]?[0-9]+)?', Number.Float),
  421. (r'\d+([SILDFR]|US|UI|UL)?', Number.Integer),
  422. (r'&H[0-9a-f]+([SILDFR]|US|UI|UL)?', Number.Integer),
  423. (r'&O[0-7]+([SILDFR]|US|UI|UL)?', Number.Integer),
  424. ],
  425. 'string': [
  426. (r'""', String),
  427. (r'"C?', String, '#pop'),
  428. (r'[^"]+', String),
  429. ],
  430. 'dim': [
  431. (uni_name, Name.Variable, '#pop'),
  432. default('#pop'), # any other syntax
  433. ],
  434. 'funcname': [
  435. (uni_name, Name.Function, '#pop'),
  436. ],
  437. 'classname': [
  438. (uni_name, Name.Class, '#pop'),
  439. ],
  440. 'namespace': [
  441. (uni_name, Name.Namespace),
  442. (r'\.', Name.Namespace),
  443. default('#pop'),
  444. ],
  445. 'end': [
  446. (r'\s+', Whitespace),
  447. (r'(Function|Sub|Property|Class|Structure|Enum|Module|Namespace)\b',
  448. Keyword, '#pop'),
  449. default('#pop'),
  450. ]
  451. }
  452. def analyse_text(text):
  453. if re.search(r'^\s*(#If|Module|Namespace)', text, re.MULTILINE):
  454. return 0.5
  455. class GenericAspxLexer(RegexLexer):
  456. """
  457. Lexer for ASP.NET pages.
  458. """
  459. name = 'aspx-gen'
  460. filenames = []
  461. mimetypes = []
  462. url = 'https://dotnet.microsoft.com/en-us/apps/aspnet'
  463. flags = re.DOTALL
  464. tokens = {
  465. 'root': [
  466. (r'(<%[@=#]?)(.*?)(%>)', bygroups(Name.Tag, Other, Name.Tag)),
  467. (r'(<script.*?>)(.*?)(</script>)', bygroups(using(XmlLexer),
  468. Other,
  469. using(XmlLexer))),
  470. (r'(.+?)(?=<)', using(XmlLexer)),
  471. (r'.+', using(XmlLexer)),
  472. ],
  473. }
  474. # TODO support multiple languages within the same source file
  475. class CSharpAspxLexer(DelegatingLexer):
  476. """
  477. Lexer for highlighting C# within ASP.NET pages.
  478. """
  479. name = 'aspx-cs'
  480. aliases = ['aspx-cs']
  481. filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
  482. mimetypes = []
  483. url = 'https://dotnet.microsoft.com/en-us/apps/aspnet'
  484. version_added = ''
  485. def __init__(self, **options):
  486. super().__init__(CSharpLexer, GenericAspxLexer, **options)
  487. def analyse_text(text):
  488. if re.search(r'Page\s*Language="C#"', text, re.I) is not None:
  489. return 0.2
  490. elif re.search(r'script[^>]+language=["\']C#', text, re.I) is not None:
  491. return 0.15
  492. class VbNetAspxLexer(DelegatingLexer):
  493. """
  494. Lexer for highlighting Visual Basic.net within ASP.NET pages.
  495. """
  496. name = 'aspx-vb'
  497. aliases = ['aspx-vb']
  498. filenames = ['*.aspx', '*.asax', '*.ascx', '*.ashx', '*.asmx', '*.axd']
  499. mimetypes = []
  500. url = 'https://dotnet.microsoft.com/en-us/apps/aspnet'
  501. version_added = ''
  502. def __init__(self, **options):
  503. super().__init__(VbNetLexer, GenericAspxLexer, **options)
  504. def analyse_text(text):
  505. if re.search(r'Page\s*Language="Vb"', text, re.I) is not None:
  506. return 0.2
  507. elif re.search(r'script[^>]+language=["\']vb', text, re.I) is not None:
  508. return 0.15
  509. # Very close to functional.OcamlLexer
  510. class FSharpLexer(RegexLexer):
  511. """
  512. For the F# language (version 3.0).
  513. """
  514. name = 'F#'
  515. url = 'https://fsharp.org/'
  516. aliases = ['fsharp', 'f#']
  517. filenames = ['*.fs', '*.fsi', '*.fsx']
  518. mimetypes = ['text/x-fsharp']
  519. version_added = '1.5'
  520. keywords = [
  521. 'abstract', 'as', 'assert', 'base', 'begin', 'class', 'default',
  522. 'delegate', 'do!', 'do', 'done', 'downcast', 'downto', 'elif', 'else',
  523. 'end', 'exception', 'extern', 'false', 'finally', 'for', 'function',
  524. 'fun', 'global', 'if', 'inherit', 'inline', 'interface', 'internal',
  525. 'in', 'lazy', 'let!', 'let', 'match', 'member', 'module', 'mutable',
  526. 'namespace', 'new', 'null', 'of', 'open', 'override', 'private', 'public',
  527. 'rec', 'return!', 'return', 'select', 'static', 'struct', 'then', 'to',
  528. 'true', 'try', 'type', 'upcast', 'use!', 'use', 'val', 'void', 'when',
  529. 'while', 'with', 'yield!', 'yield',
  530. ]
  531. # Reserved words; cannot hurt to color them as keywords too.
  532. keywords += [
  533. 'atomic', 'break', 'checked', 'component', 'const', 'constraint',
  534. 'constructor', 'continue', 'eager', 'event', 'external', 'fixed',
  535. 'functor', 'include', 'method', 'mixin', 'object', 'parallel',
  536. 'process', 'protected', 'pure', 'sealed', 'tailcall', 'trait',
  537. 'virtual', 'volatile',
  538. ]
  539. keyopts = [
  540. '!=', '#', '&&', '&', r'\(', r'\)', r'\*', r'\+', ',', r'-\.',
  541. '->', '-', r'\.\.', r'\.', '::', ':=', ':>', ':', ';;', ';', '<-',
  542. r'<\]', '<', r'>\]', '>', r'\?\?', r'\?', r'\[<', r'\[\|', r'\[', r'\]',
  543. '_', '`', r'\{', r'\|\]', r'\|', r'\}', '~', '<@@', '<@', '=', '@>', '@@>',
  544. ]
  545. operators = r'[!$%&*+\./:<=>?@^|~-]'
  546. word_operators = ['and', 'or', 'not']
  547. prefix_syms = r'[!?~]'
  548. infix_syms = r'[=<>@^|&+\*/$%-]'
  549. primitives = [
  550. 'sbyte', 'byte', 'char', 'nativeint', 'unativeint', 'float32', 'single',
  551. 'float', 'double', 'int8', 'uint8', 'int16', 'uint16', 'int32',
  552. 'uint32', 'int64', 'uint64', 'decimal', 'unit', 'bool', 'string',
  553. 'list', 'exn', 'obj', 'enum',
  554. ]
  555. # See http://msdn.microsoft.com/en-us/library/dd233181.aspx and/or
  556. # http://fsharp.org/about/files/spec.pdf for reference. Good luck.
  557. tokens = {
  558. 'escape-sequence': [
  559. (r'\\[\\"\'ntbrafv]', String.Escape),
  560. (r'\\[0-9]{3}', String.Escape),
  561. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  562. (r'\\U[0-9a-fA-F]{8}', String.Escape),
  563. ],
  564. 'root': [
  565. (r'\s+', Whitespace),
  566. (r'\(\)|\[\]', Name.Builtin.Pseudo),
  567. (r'\b(?<!\.)([A-Z][\w\']*)(?=\s*\.)',
  568. Name.Namespace, 'dotted'),
  569. (r'\b([A-Z][\w\']*)', Name),
  570. (r'(///.*?)(\n)', bygroups(String.Doc, Whitespace)),
  571. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  572. (r'\(\*(?!\))', Comment, 'comment'),
  573. (r'@"', String, 'lstring'),
  574. (r'"""', String, 'tqs'),
  575. (r'"', String, 'string'),
  576. (r'\b(open|module)(\s+)([\w.]+)',
  577. bygroups(Keyword, Whitespace, Name.Namespace)),
  578. (r'\b(let!?)(\s+)(\w+)',
  579. bygroups(Keyword, Whitespace, Name.Variable)),
  580. (r'\b(type)(\s+)(\w+)',
  581. bygroups(Keyword, Whitespace, Name.Class)),
  582. (r'\b(member|override)(\s+)(\w+)(\.)(\w+)',
  583. bygroups(Keyword, Whitespace, Name, Punctuation, Name.Function)),
  584. (r'\b({})\b'.format('|'.join(keywords)), Keyword),
  585. (r'``([^`\n\r\t]|`[^`\n\r\t])+``', Name),
  586. (r'({})'.format('|'.join(keyopts)), Operator),
  587. (rf'({infix_syms}|{prefix_syms})?{operators}', Operator),
  588. (r'\b({})\b'.format('|'.join(word_operators)), Operator.Word),
  589. (r'\b({})\b'.format('|'.join(primitives)), Keyword.Type),
  590. (r'(#)([ \t]*)(if|endif|else|line|nowarn|light|\d+)\b(.*?)(\n)',
  591. bygroups(Comment.Preproc, Whitespace, Comment.Preproc,
  592. Comment.Preproc, Whitespace)),
  593. (r"[^\W\d][\w']*", Name),
  594. (r'\d[\d_]*[uU]?[yslLnQRZINGmM]?', Number.Integer),
  595. (r'0[xX][\da-fA-F][\da-fA-F_]*[uU]?[yslLn]?[fF]?', Number.Hex),
  596. (r'0[oO][0-7][0-7_]*[uU]?[yslLn]?', Number.Oct),
  597. (r'0[bB][01][01_]*[uU]?[yslLn]?', Number.Bin),
  598. (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)[fFmM]?',
  599. Number.Float),
  600. (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'B?",
  601. String.Char),
  602. (r"'.'", String.Char),
  603. (r"'", Keyword), # a stray quote is another syntax element
  604. (r'@?"', String.Double, 'string'),
  605. (r'[~?][a-z][\w\']*:', Name.Variable),
  606. ],
  607. 'dotted': [
  608. (r'\s+', Whitespace),
  609. (r'\.', Punctuation),
  610. (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace),
  611. (r'[A-Z][\w\']*', Name, '#pop'),
  612. (r'[a-z_][\w\']*', Name, '#pop'),
  613. # e.g. dictionary index access
  614. default('#pop'),
  615. ],
  616. 'comment': [
  617. (r'[^(*)@"]+', Comment),
  618. (r'\(\*', Comment, '#push'),
  619. (r'\*\)', Comment, '#pop'),
  620. # comments cannot be closed within strings in comments
  621. (r'@"', String, 'lstring'),
  622. (r'"""', String, 'tqs'),
  623. (r'"', String, 'string'),
  624. (r'[(*)@]', Comment),
  625. ],
  626. 'string': [
  627. (r'[^\\"]+', String),
  628. include('escape-sequence'),
  629. (r'\\\n', String),
  630. (r'\n', String), # newlines are allowed in any string
  631. (r'"B?', String, '#pop'),
  632. ],
  633. 'lstring': [
  634. (r'[^"]+', String),
  635. (r'\n', String),
  636. (r'""', String),
  637. (r'"B?', String, '#pop'),
  638. ],
  639. 'tqs': [
  640. (r'[^"]+', String),
  641. (r'\n', String),
  642. (r'"""B?', String, '#pop'),
  643. (r'"', String),
  644. ],
  645. }
  646. def analyse_text(text):
  647. """F# doesn't have that many unique features -- |> and <| are weak
  648. indicators."""
  649. result = 0
  650. if '|>' in text:
  651. result += 0.05
  652. if '<|' in text:
  653. result += 0.05
  654. return result
  655. class XppLexer(RegexLexer):
  656. """
  657. For X++ source code. This is based loosely on the CSharpLexer
  658. """
  659. name = 'X++'
  660. url = 'https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-language-reference'
  661. aliases = ['xpp', 'x++']
  662. filenames = ['*.xpp']
  663. version_added = '2.15'
  664. flags = re.MULTILINE
  665. XPP_CHARS = ('@?(?:_|[^' +
  666. uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + '])' +
  667. '[^' + uni.allexcept('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
  668. 'Nd', 'Pc', 'Cf', 'Mn', 'Mc') + ']*')
  669. # Temporary, see
  670. # https://github.com/thatch/regexlint/pull/49
  671. XPP_CHARS = XPP_CHARS.replace('\x00', '\x01')
  672. OPERATORS = (
  673. '<=', '>=', '+=', '-=', '*=', '/=', '!=', '==',
  674. '&&', '||', '>>', '<<', '++', '--', '+', '-', '*',
  675. '/', '%', '&', '|', '^', '<', '>', '?', '!', '~', '=',
  676. )
  677. KEYWORDS = ('abstract','anytype','as','async','asc','at','avg','break','breakpoint','by','byref','case','catch',
  678. 'changecompany','client','container','continue','count','crosscompany','default','delegate',
  679. 'delete_from','desc','display','div','do','edit','else','element','eventhandler','exists','false','final',
  680. 'firstfast','firstonly','firstonly10','firstonly100','firstonly1000','flush','for','forceliterals',
  681. 'forcenestedloop','forceplaceholders','forceselectorder','forupdate','from','group','if','insert_recordset',
  682. 'interface','is','join','like','maxof','minof','mod','new','next','nofetch','notexists','null','optimisticlock','order',
  683. 'outer','pause','pessimisticlock','print','private','protected','public','repeatableread','retry','return',
  684. 'reverse','select','server','setting','static','sum','super','switch','tablelock','this','throw','true','try','ttsabort','ttsbegin',
  685. 'ttscommit','update_recordset','validtimestate','void','where','while','window')
  686. RUNTIME_FUNCTIONS = ('_duration','abs','acos','any2Date','any2Enum','any2Guid','any2Int','any2Int64','any2Real','any2Str','anytodate',
  687. 'anytoenum','anytoguid','anytoint','anytoint64','anytoreal','anytostr','asin','atan','beep','cTerm','char2Num','classIdGet',
  688. 'corrFlagGet','corrFlagSet','cos','cosh','curExt','curUserId','date2Num','date2Str','datetime2Str','dayName','dayOfMth',
  689. 'dayOfWk','dayOfYr','ddb','decRound','dg','dimOf','endMth','enum2str','exp','exp10','fV','fieldId2Name','fieldId2PName',
  690. 'fieldName2Id','frac','funcName','getCurrentPartition','getCurrentPartitionRecId','getPrefix','guid2Str','idg','indexId2Name',
  691. 'indexName2Id','int2Str','int642Str','intvMax','intvName','intvNo','intvNorm','log10','logN','match','max','min','mkDate','mthName',
  692. 'mthOfYr','newGuid','nextMth','nextQtr','nextYr','num2Char','num2Date','num2Str','pmt','power','prevMth','prevQtr','prevYr',
  693. 'prmIsDefault','pt','pv','rate','refPrintAll','round','runAs','sessionId','setPrefix','sin','sinh','sleep','sln','str2Date',
  694. 'str2Datetime','str2Enum','str2Guid','str2Int','str2Int64','str2Num','str2Time','strAlpha','strCmp','strColSeq','strDel',
  695. 'strFind','strFmt','strIns','strKeep','strLTrim','strLen','strLine','strLwr','strNFind','strPoke','strPrompt','strRTrim',
  696. 'strRem','strRep','strScan','strUpr','subStr','syd','systemDateGet','systemDateSet','tableId2Name',
  697. 'tableId2PName','tableName2Id','tan','tanh','term','time2Str','timeNow','today','trunc','typeOf','uint2Str','wkOfYr','year')
  698. COMPILE_FUNCTIONS = ('attributeStr','classNum','classStr','configurationKeyNum','configurationKeyStr','dataEntityDataSourceStr','delegateStr',
  699. 'dimensionHierarchyLevelStr','dimensionHierarchyStr','dimensionReferenceStr','dutyStr','enumCnt','enumLiteralStr','enumNum','enumStr',
  700. 'extendedTypeNum','extendedTypeStr','fieldNum','fieldPName','fieldStr','formControlStr','formDataFieldStr','formDataSourceStr',
  701. 'formMethodStr','formStr','identifierStr','indexNum','indexStr','licenseCodeNum','licenseCodeStr','literalStr','maxDate','maxInt',
  702. 'measureStr','measurementStr','menuItemActionStr','menuItemDisplayStr','menuItemOutputStr','menuStr','methodStr','minInt','privilegeStr',
  703. 'queryDatasourceStr','queryMethodStr','queryStr','reportStr','resourceStr','roleStr','ssrsReportStr','staticDelegateStr','staticMethodStr',
  704. 'tableCollectionStr','tableFieldGroupStr','tableMethodStr','tableNum','tablePName','tableStaticMethodStr','tableStr','tileStr','varStr',
  705. 'webActionItemStr','webDisplayContentItemStr','webFormStr','webMenuStr','webOutputContentItemStr','webReportStr','webSiteTempStr',
  706. 'webStaticFileStr','webUrlItemStr','webWebPartStr','webletItemStr','webpageDefStr','websiteDefStr','workflowApprovalStr',
  707. 'workflowCategoryStr','workflowTaskStr','workflowTypeStr')
  708. tokens = {}
  709. tokens = {
  710. 'root': [
  711. # method names
  712. (r'(\s*)\b(else|if)\b([^\n])', bygroups(Whitespace, Keyword, using(this))), # ensure that if is not treated like a function
  713. (r'^([ \t]*)((?:' + XPP_CHARS + r'(?:\[\])?\s+)+?)' # return type
  714. r'(' + XPP_CHARS + ')' # method name
  715. r'(\s*)(\()', # signature start
  716. bygroups(Whitespace, using(this), Name.Function, Whitespace,
  717. Punctuation)),
  718. (r'^(\s*)(\[)([^\n]*?)(\])', bygroups(Whitespace, Name.Attribute, Name.Variable.Class, Name.Attribute)),
  719. (r'[^\S\n]+', Whitespace),
  720. (r'(\\)(\n)', bygroups(Text, Whitespace)), # line continuation
  721. (r'//[^\n]*?\n', Comment.Single),
  722. (r'/[*][^\n]*?[*]/', Comment.Multiline),
  723. (r'\n', Whitespace),
  724. (words(OPERATORS), Operator),
  725. (r'=~|!=|==|<<|>>|[-+/*%=<>&^|]', Operator),
  726. (r'[()\[\];:,.#@]', Punctuation),
  727. (r'[{}]', Punctuation),
  728. (r'@"(""|[^"])*"', String),
  729. (r'\$?"(\\\\|\\[^\\]|[^"\\\n])*["\n]', String),
  730. (r"'\\.'|'[^\\]'", String.Char),
  731. (r"[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)?"
  732. r"[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?", Number),
  733. (words(KEYWORDS, suffix=r'\b'), Keyword),
  734. (r'(boolean|int|int64|str|real|guid|date)\b\??', Keyword.Type),
  735. (r'(class|struct|extends|implements)(\s+)', bygroups(Keyword, Whitespace), 'class'),
  736. (r'('+XPP_CHARS+')(::)', bygroups(Name.Variable.Class, Punctuation)),
  737. (r'(\s*)(\w+)(\s+\w+(,|=)?[^\n]*;)', bygroups(Whitespace, Name.Variable.Class, using(this))), # declaration
  738. # x++ specific function to get field should highlight the classname
  739. (r'(fieldNum\()('+XPP_CHARS+r')(\s*,\s*)('+XPP_CHARS+r')(\s*\))',
  740. bygroups(using(this), Name.Variable.Class, using(this), Name.Property, using(this))),
  741. # x++ specific function to get table should highlight the classname
  742. (r'(tableNum\()('+XPP_CHARS+r')(\s*\))',
  743. bygroups(using(this), Name.Variable.Class, using(this))),
  744. (words(RUNTIME_FUNCTIONS, suffix=r'(?=\()'), Name.Function.Magic),
  745. (words(COMPILE_FUNCTIONS, suffix=r'(?=\()'), Name.Function.Magic),
  746. (XPP_CHARS, Name),
  747. ],
  748. 'class': [
  749. (XPP_CHARS, Name.Class, '#pop'),
  750. default('#pop'),
  751. ],
  752. 'namespace': [
  753. (r'(?=\()', Text, '#pop'), # using (resource)
  754. ('(' + XPP_CHARS + r'|\.)+', Name.Namespace, '#pop'),
  755. ]
  756. }