d.py 9.4 KB

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