d.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.d
  4. ~~~~~~~~~~~~~~~~~
  5. Lexers for D languages.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, include, words
  10. from pygments.token import Text, Comment, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['DLexer', 'CrocLexer', 'MiniDLexer']
  13. class DLexer(RegexLexer):
  14. """
  15. For D source.
  16. .. versionadded:: 1.2
  17. """
  18. name = 'D'
  19. filenames = ['*.d', '*.di']
  20. aliases = ['d']
  21. mimetypes = ['text/x-dsrc']
  22. tokens = {
  23. 'root': [
  24. (r'\n', Text),
  25. (r'\s+', Text),
  26. # (r'\\\n', Text), # line continuations
  27. # Comments
  28. (r'//(.*?)\n', Comment.Single),
  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', Comment.Special),
  119. ],
  120. 'nested_comment': [
  121. (r'[^+/]+', Comment.Multiline),
  122. (r'/\+', Comment.Multiline, '#push'),
  123. (r'\+/', Comment.Multiline, '#pop'),
  124. (r'[+/]', Comment.Multiline),
  125. ],
  126. 'token_string': [
  127. (r'\{', Punctuation, 'token_string_nest'),
  128. (r'\}', String, '#pop'),
  129. include('root'),
  130. ],
  131. 'token_string_nest': [
  132. (r'\{', Punctuation, '#push'),
  133. (r'\}', Punctuation, '#pop'),
  134. include('root'),
  135. ],
  136. 'delimited_bracket': [
  137. (r'[^\[\]]+', String),
  138. (r'\[', String, 'delimited_inside_bracket'),
  139. (r'\]"', String, '#pop'),
  140. ],
  141. 'delimited_inside_bracket': [
  142. (r'[^\[\]]+', String),
  143. (r'\[', String, '#push'),
  144. (r'\]', String, '#pop'),
  145. ],
  146. 'delimited_parenthesis': [
  147. (r'[^()]+', String),
  148. (r'\(', String, 'delimited_inside_parenthesis'),
  149. (r'\)"', String, '#pop'),
  150. ],
  151. 'delimited_inside_parenthesis': [
  152. (r'[^()]+', String),
  153. (r'\(', String, '#push'),
  154. (r'\)', String, '#pop'),
  155. ],
  156. 'delimited_angle': [
  157. (r'[^<>]+', String),
  158. (r'<', String, 'delimited_inside_angle'),
  159. (r'>"', String, '#pop'),
  160. ],
  161. 'delimited_inside_angle': [
  162. (r'[^<>]+', String),
  163. (r'<', String, '#push'),
  164. (r'>', String, '#pop'),
  165. ],
  166. 'delimited_curly': [
  167. (r'[^{}]+', String),
  168. (r'\{', String, 'delimited_inside_curly'),
  169. (r'\}"', String, '#pop'),
  170. ],
  171. 'delimited_inside_curly': [
  172. (r'[^{}]+', String),
  173. (r'\{', String, '#push'),
  174. (r'\}', String, '#pop'),
  175. ],
  176. }
  177. class CrocLexer(RegexLexer):
  178. """
  179. For `Croc <http://jfbillingsley.com/croc>`_ source.
  180. """
  181. name = 'Croc'
  182. filenames = ['*.croc']
  183. aliases = ['croc']
  184. mimetypes = ['text/x-crocsrc']
  185. tokens = {
  186. 'root': [
  187. (r'\n', Text),
  188. (r'\s+', Text),
  189. # Comments
  190. (r'//(.*?)\n', Comment.Single),
  191. (r'/\*', Comment.Multiline, 'nestedcomment'),
  192. # Keywords
  193. (words((
  194. 'as', 'assert', 'break', 'case', 'catch', 'class', 'continue',
  195. 'default', 'do', 'else', 'finally', 'for', 'foreach', 'function',
  196. 'global', 'namespace', 'if', 'import', 'in', 'is', 'local',
  197. 'module', 'return', 'scope', 'super', 'switch', 'this', 'throw',
  198. 'try', 'vararg', 'while', 'with', 'yield'), suffix=r'\b'),
  199. Keyword),
  200. (r'(false|true|null)\b', Keyword.Constant),
  201. # FloatLiteral
  202. (r'([0-9][0-9_]*)(?=[.eE])(\.[0-9][0-9_]*)?([eE][+\-]?[0-9_]+)?',
  203. Number.Float),
  204. # IntegerLiteral
  205. # -- Binary
  206. (r'0[bB][01][01_]*', Number.Bin),
  207. # -- Hexadecimal
  208. (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*', Number.Hex),
  209. # -- Decimal
  210. (r'([0-9][0-9_]*)(?![.eE])', Number.Integer),
  211. # CharacterLiteral
  212. (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-9]{1,3}"""
  213. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""",
  214. String.Char),
  215. # StringLiteral
  216. # -- WysiwygString
  217. (r'@"(""|[^"])*"', String),
  218. (r'@`(``|[^`])*`', String),
  219. (r"@'(''|[^'])*'", String),
  220. # -- DoubleQuotedString
  221. (r'"(\\\\|\\"|[^"])*"', String),
  222. # Tokens
  223. (r'(~=|\^=|%=|\*=|==|!=|>>>=|>>>|>>=|>>|>=|<=>|\?=|-\>'
  224. r'|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.|/=)'
  225. r'|[-/.&$@|\+<>!()\[\]{}?,;:=*%^~#\\]', Punctuation),
  226. # Identifier
  227. (r'[a-zA-Z_]\w*', Name),
  228. ],
  229. 'nestedcomment': [
  230. (r'[^*/]+', Comment.Multiline),
  231. (r'/\*', Comment.Multiline, '#push'),
  232. (r'\*/', Comment.Multiline, '#pop'),
  233. (r'[*/]', Comment.Multiline),
  234. ],
  235. }
  236. class MiniDLexer(CrocLexer):
  237. """
  238. For MiniD source. MiniD is now known as Croc.
  239. """
  240. name = 'MiniD'
  241. filenames = [] # don't lex .md as MiniD, reserve for Markdown
  242. aliases = ['minid']
  243. mimetypes = ['text/x-minidsrc']