d.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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', 'typedef', 'typeid', 'typeof',
  43. 'union', 'unittest', 'version', 'volatile', 'while', 'with',
  44. '__gshared', '__traits', '__vector', '__parameters'),
  45. suffix=r'\b'),
  46. Keyword),
  47. (words((
  48. 'bool', 'byte', 'cdouble', 'cent', 'cfloat', 'char', 'creal',
  49. 'dchar', 'double', 'float', 'idouble', 'ifloat', 'int', 'ireal',
  50. 'long', 'real', 'short', 'ubyte', 'ucent', 'uint', 'ulong',
  51. 'ushort', 'void', 'wchar'), suffix=r'\b'),
  52. Keyword.Type),
  53. (r'(false|true|null)\b', Keyword.Constant),
  54. (words((
  55. '__FILE__', '__MODULE__', '__LINE__', '__FUNCTION__', '__PRETTY_FUNCTION__'
  56. '', '__DATE__', '__EOF__', '__TIME__', '__TIMESTAMP__', '__VENDOR__',
  57. '__VERSION__'), suffix=r'\b'),
  58. Keyword.Pseudo),
  59. (r'macro\b', Keyword.Reserved),
  60. (r'(string|wstring|dstring|size_t|ptrdiff_t)\b', Name.Builtin),
  61. # FloatLiteral
  62. # -- HexFloat
  63. (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)'
  64. r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float),
  65. # -- DecimalFloat
  66. (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|'
  67. r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float),
  68. (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float),
  69. # IntegerLiteral
  70. # -- Binary
  71. (r'0[Bb][01_]+', Number.Bin),
  72. # -- Octal
  73. (r'0[0-7_]+', Number.Oct),
  74. # -- Hexadecimal
  75. (r'0[xX][0-9a-fA-F_]+', Number.Hex),
  76. # -- Decimal
  77. (r'(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?', Number.Integer),
  78. # CharacterLiteral
  79. (r"""'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
  80. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'""",
  81. String.Char),
  82. # StringLiteral
  83. # -- WysiwygString
  84. (r'r"[^"]*"[cwd]?', String),
  85. # -- AlternateWysiwygString
  86. (r'`[^`]*`[cwd]?', String),
  87. # -- DoubleQuotedString
  88. (r'"(\\\\|\\"|[^"])*"[cwd]?', String),
  89. # -- EscapeSequence
  90. (r"\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}"
  91. r"|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)",
  92. String),
  93. # -- HexString
  94. (r'x"[0-9a-fA-F_\s]*"[cwd]?', String),
  95. # -- DelimitedString
  96. (r'q"\[', String, 'delimited_bracket'),
  97. (r'q"\(', String, 'delimited_parenthesis'),
  98. (r'q"<', String, 'delimited_angle'),
  99. (r'q"\{', String, 'delimited_curly'),
  100. (r'q"([a-zA-Z_]\w*)\n.*?\n\1"', String),
  101. (r'q"(.).*?\1"', String),
  102. # -- TokenString
  103. (r'q\{', String, 'token_string'),
  104. # Attributes
  105. (r'@([a-zA-Z_]\w*)?', Name.Decorator),
  106. # Tokens
  107. (r'(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>='
  108. r'|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)'
  109. r'|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]', Punctuation),
  110. # Identifier
  111. (r'[a-zA-Z_]\w*', Name),
  112. # Line
  113. (r'#line\s.*\n', Comment.Special),
  114. ],
  115. 'nested_comment': [
  116. (r'[^+/]+', Comment.Multiline),
  117. (r'/\+', Comment.Multiline, '#push'),
  118. (r'\+/', Comment.Multiline, '#pop'),
  119. (r'[+/]', Comment.Multiline),
  120. ],
  121. 'token_string': [
  122. (r'\{', Punctuation, 'token_string_nest'),
  123. (r'\}', String, '#pop'),
  124. include('root'),
  125. ],
  126. 'token_string_nest': [
  127. (r'\{', Punctuation, '#push'),
  128. (r'\}', Punctuation, '#pop'),
  129. include('root'),
  130. ],
  131. 'delimited_bracket': [
  132. (r'[^\[\]]+', String),
  133. (r'\[', String, 'delimited_inside_bracket'),
  134. (r'\]"', String, '#pop'),
  135. ],
  136. 'delimited_inside_bracket': [
  137. (r'[^\[\]]+', String),
  138. (r'\[', String, '#push'),
  139. (r'\]', String, '#pop'),
  140. ],
  141. 'delimited_parenthesis': [
  142. (r'[^()]+', String),
  143. (r'\(', String, 'delimited_inside_parenthesis'),
  144. (r'\)"', String, '#pop'),
  145. ],
  146. 'delimited_inside_parenthesis': [
  147. (r'[^()]+', String),
  148. (r'\(', String, '#push'),
  149. (r'\)', String, '#pop'),
  150. ],
  151. 'delimited_angle': [
  152. (r'[^<>]+', String),
  153. (r'<', String, 'delimited_inside_angle'),
  154. (r'>"', String, '#pop'),
  155. ],
  156. 'delimited_inside_angle': [
  157. (r'[^<>]+', String),
  158. (r'<', String, '#push'),
  159. (r'>', String, '#pop'),
  160. ],
  161. 'delimited_curly': [
  162. (r'[^{}]+', String),
  163. (r'\{', String, 'delimited_inside_curly'),
  164. (r'\}"', String, '#pop'),
  165. ],
  166. 'delimited_inside_curly': [
  167. (r'[^{}]+', String),
  168. (r'\{', String, '#push'),
  169. (r'\}', String, '#pop'),
  170. ],
  171. }
  172. class CrocLexer(RegexLexer):
  173. """
  174. For `Croc <http://jfbillingsley.com/croc>`_ source.
  175. """
  176. name = 'Croc'
  177. filenames = ['*.croc']
  178. aliases = ['croc']
  179. mimetypes = ['text/x-crocsrc']
  180. tokens = {
  181. 'root': [
  182. (r'\n', Text),
  183. (r'\s+', Text),
  184. # Comments
  185. (r'//(.*?)\n', Comment.Single),
  186. (r'/\*', Comment.Multiline, 'nestedcomment'),
  187. # Keywords
  188. (words((
  189. 'as', 'assert', 'break', 'case', 'catch', 'class', 'continue',
  190. 'default', 'do', 'else', 'finally', 'for', 'foreach', 'function',
  191. 'global', 'namespace', 'if', 'import', 'in', 'is', 'local',
  192. 'module', 'return', 'scope', 'super', 'switch', 'this', 'throw',
  193. 'try', 'vararg', 'while', 'with', 'yield'), suffix=r'\b'),
  194. Keyword),
  195. (r'(false|true|null)\b', Keyword.Constant),
  196. # FloatLiteral
  197. (r'([0-9][0-9_]*)(?=[.eE])(\.[0-9][0-9_]*)?([eE][+\-]?[0-9_]+)?',
  198. Number.Float),
  199. # IntegerLiteral
  200. # -- Binary
  201. (r'0[bB][01][01_]*', Number.Bin),
  202. # -- Hexadecimal
  203. (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*', Number.Hex),
  204. # -- Decimal
  205. (r'([0-9][0-9_]*)(?![.eE])', Number.Integer),
  206. # CharacterLiteral
  207. (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-9]{1,3}"""
  208. r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""",
  209. String.Char),
  210. # StringLiteral
  211. # -- WysiwygString
  212. (r'@"(""|[^"])*"', String),
  213. (r'@`(``|[^`])*`', String),
  214. (r"@'(''|[^'])*'", String),
  215. # -- DoubleQuotedString
  216. (r'"(\\\\|\\"|[^"])*"', String),
  217. # Tokens
  218. (r'(~=|\^=|%=|\*=|==|!=|>>>=|>>>|>>=|>>|>=|<=>|\?=|-\>'
  219. r'|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.|/=)'
  220. r'|[-/.&$@|\+<>!()\[\]{}?,;:=*%^~#\\]', Punctuation),
  221. # Identifier
  222. (r'[a-zA-Z_]\w*', Name),
  223. ],
  224. 'nestedcomment': [
  225. (r'[^*/]+', Comment.Multiline),
  226. (r'/\*', Comment.Multiline, '#push'),
  227. (r'\*/', Comment.Multiline, '#pop'),
  228. (r'[*/]', Comment.Multiline),
  229. ],
  230. }
  231. class MiniDLexer(CrocLexer):
  232. """
  233. For MiniD source. MiniD is now known as Croc.
  234. """
  235. name = 'MiniD'
  236. filenames = [] # don't lex .md as MiniD, reserve for Markdown
  237. aliases = ['minid']
  238. mimetypes = ['text/x-minidsrc']