d.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. pygments.lexers.d
  3. ~~~~~~~~~~~~~~~~~
  4. Lexers for D languages.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, words, bygroups
  9. from pygments.token import Comment, Keyword, Name, String, Number, \
  10. Punctuation, Whitespace
  11. __all__ = ['DLexer', 'CrocLexer', 'MiniDLexer']
  12. class DLexer(RegexLexer):
  13. """
  14. For D source.
  15. .. versionadded:: 1.2
  16. """
  17. name = 'D'
  18. url = 'https://dlang.org/'
  19. filenames = ['*.d', '*.di']
  20. aliases = ['d']
  21. mimetypes = ['text/x-dsrc']
  22. tokens = {
  23. 'root': [
  24. (r'\n', Whitespace),
  25. (r'\s+', Whitespace),
  26. # (r'\\\n', Text), # line continuations
  27. # Comments
  28. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  29. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  30. (r'/\+', Comment.Multiline, 'nested_comment'),
  31. # Keywords
  32. (words((
  33. 'abstract', 'alias', 'align', 'asm', 'assert', 'auto', 'body',
  34. 'break', 'case', 'cast', 'catch', 'class', 'const', 'continue',
  35. 'debug', 'default', 'delegate', 'delete', 'deprecated', 'do', 'else',
  36. 'enum', 'export', 'extern', 'finally', 'final', 'foreach_reverse',
  37. 'foreach', 'for', 'function', 'goto', 'if', 'immutable', 'import',
  38. 'interface', 'invariant', 'inout', 'in', 'is', 'lazy', 'mixin',
  39. 'module', 'new', 'nothrow', 'out', 'override', 'package', 'pragma',
  40. 'private', 'protected', 'public', 'pure', 'ref', 'return', 'scope',
  41. 'shared', 'static', 'struct', 'super', 'switch', 'synchronized',
  42. 'template', 'this', 'throw', 'try', 'typeid', 'typeof',
  43. 'union', 'unittest', 'version', 'volatile', 'while', 'with',
  44. '__gshared', '__traits', '__vector', '__parameters'),
  45. suffix=r'\b'),
  46. Keyword),
  47. (words((
  48. # Removed in 2.072
  49. 'typedef', ),
  50. suffix=r'\b'),
  51. Keyword.Removed),
  52. (words((
  53. 'bool', 'byte', 'cdouble', 'cent', 'cfloat', 'char', 'creal',
  54. 'dchar', 'double', 'float', 'idouble', 'ifloat', 'int', 'ireal',
  55. 'long', 'real', 'short', 'ubyte', 'ucent', 'uint', 'ulong',
  56. 'ushort', 'void', 'wchar'), suffix=r'\b'),
  57. Keyword.Type),
  58. (r'(false|true|null)\b', Keyword.Constant),
  59. (words((
  60. '__FILE__', '__FILE_FULL_PATH__', '__MODULE__', '__LINE__', '__FUNCTION__',
  61. '__PRETTY_FUNCTION__', '__DATE__', '__EOF__', '__TIME__', '__TIMESTAMP__',
  62. '__VENDOR__', '__VERSION__'), suffix=r'\b'),
  63. Keyword.Pseudo),
  64. (r'macro\b', Keyword.Reserved),
  65. (r'(string|wstring|dstring|size_t|ptrdiff_t)\b', Name.Builtin),
  66. # FloatLiteral
  67. # -- HexFloat
  68. (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
  69. r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float),
  70. # -- DecimalFloat
  71. (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
  72. r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float),
  73. (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float),
  74. # IntegerLiteral
  75. # -- Binary
  76. (r'0[Bb][01_]+', Number.Bin),
  77. # -- Octal
  78. (r'0[0-7_]+', Number.Oct),
  79. # -- Hexadecimal
  80. (r'0[xX][0-9a-fA-F_]+', Number.Hex),
  81. # -- Decimal
  82. (r'(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?', Number.Integer),
  83. # CharacterLiteral
  84. (r"""'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
  85. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'""",
  86. String.Char),
  87. # StringLiteral
  88. # -- WysiwygString
  89. (r'r"[^"]*"[cwd]?', String),
  90. # -- AlternateWysiwygString
  91. (r'`[^`]*`[cwd]?', String),
  92. # -- DoubleQuotedString
  93. (r'"(\\\\|\\[^\\]|[^"\\])*"[cwd]?', String),
  94. # -- EscapeSequence
  95. (r"\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}"
  96. r"|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)",
  97. String),
  98. # -- HexString
  99. (r'x"[0-9a-fA-F_\s]*"[cwd]?', String),
  100. # -- DelimitedString
  101. (r'q"\[', String, 'delimited_bracket'),
  102. (r'q"\(', String, 'delimited_parenthesis'),
  103. (r'q"<', String, 'delimited_angle'),
  104. (r'q"\{', String, 'delimited_curly'),
  105. (r'q"([a-zA-Z_]\w*)\n.*?\n\1"', String),
  106. (r'q"(.).*?\1"', String),
  107. # -- TokenString
  108. (r'q\{', String, 'token_string'),
  109. # Attributes
  110. (r'@([a-zA-Z_]\w*)?', Name.Decorator),
  111. # Tokens
  112. (r'(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>='
  113. r'|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)'
  114. r'|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]', Punctuation),
  115. # Identifier
  116. (r'[a-zA-Z_]\w*', Name),
  117. # Line
  118. (r'(#line)(\s)(.*)(\n)', bygroups(Comment.Special, Whitespace,
  119. Comment.Special, Whitespace)),
  120. ],
  121. 'nested_comment': [
  122. (r'[^+/]+', Comment.Multiline),
  123. (r'/\+', Comment.Multiline, '#push'),
  124. (r'\+/', Comment.Multiline, '#pop'),
  125. (r'[+/]', Comment.Multiline),
  126. ],
  127. 'token_string': [
  128. (r'\{', Punctuation, 'token_string_nest'),
  129. (r'\}', String, '#pop'),
  130. include('root'),
  131. ],
  132. 'token_string_nest': [
  133. (r'\{', Punctuation, '#push'),
  134. (r'\}', Punctuation, '#pop'),
  135. include('root'),
  136. ],
  137. 'delimited_bracket': [
  138. (r'[^\[\]]+', String),
  139. (r'\[', String, 'delimited_inside_bracket'),
  140. (r'\]"', String, '#pop'),
  141. ],
  142. 'delimited_inside_bracket': [
  143. (r'[^\[\]]+', String),
  144. (r'\[', String, '#push'),
  145. (r'\]', String, '#pop'),
  146. ],
  147. 'delimited_parenthesis': [
  148. (r'[^()]+', String),
  149. (r'\(', String, 'delimited_inside_parenthesis'),
  150. (r'\)"', String, '#pop'),
  151. ],
  152. 'delimited_inside_parenthesis': [
  153. (r'[^()]+', String),
  154. (r'\(', String, '#push'),
  155. (r'\)', String, '#pop'),
  156. ],
  157. 'delimited_angle': [
  158. (r'[^<>]+', String),
  159. (r'<', String, 'delimited_inside_angle'),
  160. (r'>"', String, '#pop'),
  161. ],
  162. 'delimited_inside_angle': [
  163. (r'[^<>]+', String),
  164. (r'<', String, '#push'),
  165. (r'>', String, '#pop'),
  166. ],
  167. 'delimited_curly': [
  168. (r'[^{}]+', String),
  169. (r'\{', String, 'delimited_inside_curly'),
  170. (r'\}"', String, '#pop'),
  171. ],
  172. 'delimited_inside_curly': [
  173. (r'[^{}]+', String),
  174. (r'\{', String, '#push'),
  175. (r'\}', String, '#pop'),
  176. ],
  177. }
  178. class CrocLexer(RegexLexer):
  179. """
  180. For Croc source.
  181. """
  182. name = 'Croc'
  183. url = 'http://jfbillingsley.com/croc'
  184. filenames = ['*.croc']
  185. aliases = ['croc']
  186. mimetypes = ['text/x-crocsrc']
  187. tokens = {
  188. 'root': [
  189. (r'\n', Whitespace),
  190. (r'\s+', Whitespace),
  191. # Comments
  192. (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
  193. (r'/\*', Comment.Multiline, 'nestedcomment'),
  194. # Keywords
  195. (words((
  196. 'as', 'assert', 'break', 'case', 'catch', 'class', 'continue',
  197. 'default', 'do', 'else', 'finally', 'for', 'foreach', 'function',
  198. 'global', 'namespace', 'if', 'import', 'in', 'is', 'local',
  199. 'module', 'return', 'scope', 'super', 'switch', 'this', 'throw',
  200. 'try', 'vararg', 'while', 'with', 'yield'), suffix=r'\b'),
  201. Keyword),
  202. (r'(false|true|null)\b', Keyword.Constant),
  203. # FloatLiteral
  204. (r'([0-9][0-9_]*)(?=[.eE])(\.[0-9][0-9_]*)?([eE][+\-]?[0-9_]+)?',
  205. Number.Float),
  206. # IntegerLiteral
  207. # -- Binary
  208. (r'0[bB][01][01_]*', Number.Bin),
  209. # -- Hexadecimal
  210. (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*', Number.Hex),
  211. # -- Decimal
  212. (r'([0-9][0-9_]*)(?![.eE])', Number.Integer),
  213. # CharacterLiteral
  214. (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-9]{1,3}"""
  215. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""",
  216. String.Char),
  217. # StringLiteral
  218. # -- WysiwygString
  219. (r'@"(""|[^"])*"', String),
  220. (r'@`(``|[^`])*`', String),
  221. (r"@'(''|[^'])*'", String),
  222. # -- DoubleQuotedString
  223. (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
  224. # Tokens
  225. (r'(~=|\^=|%=|\*=|==|!=|>>>=|>>>|>>=|>>|>=|<=>|\?=|-\>'
  226. r'|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.|/=)'
  227. r'|[-/.&$@|\+<>!()\[\]{}?,;:=*%^~#\\]', Punctuation),
  228. # Identifier
  229. (r'[a-zA-Z_]\w*', Name),
  230. ],
  231. 'nestedcomment': [
  232. (r'[^*/]+', Comment.Multiline),
  233. (r'/\*', Comment.Multiline, '#push'),
  234. (r'\*/', Comment.Multiline, '#pop'),
  235. (r'[*/]', Comment.Multiline),
  236. ],
  237. }
  238. class MiniDLexer(CrocLexer):
  239. """
  240. For MiniD source. MiniD is now known as Croc.
  241. """
  242. name = 'MiniD'
  243. filenames = [] # don't lex .md as MiniD, reserve for Markdown
  244. aliases = ['minid']
  245. mimetypes = ['text/x-minidsrc']