dotnet.py 37 KB

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