Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3 pygments.lexers.d 

4 ~~~~~~~~~~~~~~~~~ 

5 

6 Lexers for D languages. 

7 

8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. 

9 :license: BSD, see LICENSE for details. 

10""" 

11 

12from pygments.lexer import RegexLexer, include, words 

13from pygments.token import Text, Comment, Keyword, Name, String, \ 

14 Number, Punctuation 

15 

16__all__ = ['DLexer', 'CrocLexer', 'MiniDLexer'] 

17 

18 

19class DLexer(RegexLexer): 

20 """ 

21 For D source. 

22 

23 .. versionadded:: 1.2 

24 """ 

25 name = 'D' 

26 filenames = ['*.d', '*.di'] 

27 aliases = ['d'] 

28 mimetypes = ['text/x-dsrc'] 

29 

30 tokens = { 

31 'root': [ 

32 (r'\n', Text), 

33 (r'\s+', Text), 

34 # (r'\\\n', Text), # line continuations 

35 # Comments 

36 (r'//(.*?)\n', Comment.Single), 

37 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), 

38 (r'/\+', Comment.Multiline, 'nested_comment'), 

39 # Keywords 

40 (words(( 

41 'abstract', 'alias', 'align', 'asm', 'assert', 'auto', 'body', 

42 'break', 'case', 'cast', 'catch', 'class', 'const', 'continue', 

43 'debug', 'default', 'delegate', 'delete', 'deprecated', 'do', 'else', 

44 'enum', 'export', 'extern', 'finally', 'final', 'foreach_reverse', 

45 'foreach', 'for', 'function', 'goto', 'if', 'immutable', 'import', 

46 'interface', 'invariant', 'inout', 'in', 'is', 'lazy', 'mixin', 

47 'module', 'new', 'nothrow', 'out', 'override', 'package', 'pragma', 

48 'private', 'protected', 'public', 'pure', 'ref', 'return', 'scope', 

49 'shared', 'static', 'struct', 'super', 'switch', 'synchronized', 

50 'template', 'this', 'throw', 'try', 'typeid', 'typeof', 

51 'union', 'unittest', 'version', 'volatile', 'while', 'with', 

52 '__gshared', '__traits', '__vector', '__parameters'), 

53 suffix=r'\b'), 

54 Keyword), 

55 (words(( 

56 # Removed in 2.072 

57 'typedef', ), 

58 suffix=r'\b'), 

59 Keyword.Removed), 

60 (words(( 

61 'bool', 'byte', 'cdouble', 'cent', 'cfloat', 'char', 'creal', 

62 'dchar', 'double', 'float', 'idouble', 'ifloat', 'int', 'ireal', 

63 'long', 'real', 'short', 'ubyte', 'ucent', 'uint', 'ulong', 

64 'ushort', 'void', 'wchar'), suffix=r'\b'), 

65 Keyword.Type), 

66 (r'(false|true|null)\b', Keyword.Constant), 

67 (words(( 

68 '__FILE__', '__FILE_FULL_PATH__', '__MODULE__', '__LINE__', '__FUNCTION__', 

69 '__PRETTY_FUNCTION__', '__DATE__', '__EOF__', '__TIME__', '__TIMESTAMP__', 

70 '__VENDOR__', '__VERSION__'), suffix=r'\b'), 

71 Keyword.Pseudo), 

72 (r'macro\b', Keyword.Reserved), 

73 (r'(string|wstring|dstring|size_t|ptrdiff_t)\b', Name.Builtin), 

74 # FloatLiteral 

75 # -- HexFloat 

76 (r'0[xX]([0-9a-fA-F_]*\.[0-9a-fA-F_]+|[0-9a-fA-F_]+)' 

77 r'[pP][+\-]?[0-9_]+[fFL]?[i]?', Number.Float), 

78 # -- DecimalFloat 

79 (r'[0-9_]+(\.[0-9_]+[eE][+\-]?[0-9_]+|' 

80 r'\.[0-9_]*|[eE][+\-]?[0-9_]+)[fFL]?[i]?', Number.Float), 

81 (r'\.(0|[1-9][0-9_]*)([eE][+\-]?[0-9_]+)?[fFL]?[i]?', Number.Float), 

82 # IntegerLiteral 

83 # -- Binary 

84 (r'0[Bb][01_]+', Number.Bin), 

85 # -- Octal 

86 (r'0[0-7_]+', Number.Oct), 

87 # -- Hexadecimal 

88 (r'0[xX][0-9a-fA-F_]+', Number.Hex), 

89 # -- Decimal 

90 (r'(0|[1-9][0-9_]*)([LUu]|Lu|LU|uL|UL)?', Number.Integer), 

91 # CharacterLiteral 

92 (r"""'(\\['"?\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" 

93 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\&\w+;|.)'""", 

94 String.Char), 

95 # StringLiteral 

96 # -- WysiwygString 

97 (r'r"[^"]*"[cwd]?', String), 

98 # -- AlternateWysiwygString 

99 (r'`[^`]*`[cwd]?', String), 

100 # -- DoubleQuotedString 

101 (r'"(\\\\|\\[^\\]|[^"\\])*"[cwd]?', String), 

102 # -- EscapeSequence 

103 (r"\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}" 

104 r"|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)", 

105 String), 

106 # -- HexString 

107 (r'x"[0-9a-fA-F_\s]*"[cwd]?', String), 

108 # -- DelimitedString 

109 (r'q"\[', String, 'delimited_bracket'), 

110 (r'q"\(', String, 'delimited_parenthesis'), 

111 (r'q"<', String, 'delimited_angle'), 

112 (r'q"\{', String, 'delimited_curly'), 

113 (r'q"([a-zA-Z_]\w*)\n.*?\n\1"', String), 

114 (r'q"(.).*?\1"', String), 

115 # -- TokenString 

116 (r'q\{', String, 'token_string'), 

117 # Attributes 

118 (r'@([a-zA-Z_]\w*)?', Name.Decorator), 

119 # Tokens 

120 (r'(~=|\^=|%=|\*=|==|!>=|!<=|!<>=|!<>|!<|!>|!=|>>>=|>>>|>>=|>>|>=' 

121 r'|<>=|<>|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.\.|\.\.|/=)' 

122 r'|[/.&|\-+<>!()\[\]{}?,;:$=*%^~]', Punctuation), 

123 # Identifier 

124 (r'[a-zA-Z_]\w*', Name), 

125 # Line 

126 (r'#line\s.*\n', Comment.Special), 

127 ], 

128 'nested_comment': [ 

129 (r'[^+/]+', Comment.Multiline), 

130 (r'/\+', Comment.Multiline, '#push'), 

131 (r'\+/', Comment.Multiline, '#pop'), 

132 (r'[+/]', Comment.Multiline), 

133 ], 

134 'token_string': [ 

135 (r'\{', Punctuation, 'token_string_nest'), 

136 (r'\}', String, '#pop'), 

137 include('root'), 

138 ], 

139 'token_string_nest': [ 

140 (r'\{', Punctuation, '#push'), 

141 (r'\}', Punctuation, '#pop'), 

142 include('root'), 

143 ], 

144 'delimited_bracket': [ 

145 (r'[^\[\]]+', String), 

146 (r'\[', String, 'delimited_inside_bracket'), 

147 (r'\]"', String, '#pop'), 

148 ], 

149 'delimited_inside_bracket': [ 

150 (r'[^\[\]]+', String), 

151 (r'\[', String, '#push'), 

152 (r'\]', String, '#pop'), 

153 ], 

154 'delimited_parenthesis': [ 

155 (r'[^()]+', String), 

156 (r'\(', String, 'delimited_inside_parenthesis'), 

157 (r'\)"', String, '#pop'), 

158 ], 

159 'delimited_inside_parenthesis': [ 

160 (r'[^()]+', String), 

161 (r'\(', String, '#push'), 

162 (r'\)', String, '#pop'), 

163 ], 

164 'delimited_angle': [ 

165 (r'[^<>]+', String), 

166 (r'<', String, 'delimited_inside_angle'), 

167 (r'>"', String, '#pop'), 

168 ], 

169 'delimited_inside_angle': [ 

170 (r'[^<>]+', String), 

171 (r'<', String, '#push'), 

172 (r'>', String, '#pop'), 

173 ], 

174 'delimited_curly': [ 

175 (r'[^{}]+', String), 

176 (r'\{', String, 'delimited_inside_curly'), 

177 (r'\}"', String, '#pop'), 

178 ], 

179 'delimited_inside_curly': [ 

180 (r'[^{}]+', String), 

181 (r'\{', String, '#push'), 

182 (r'\}', String, '#pop'), 

183 ], 

184 } 

185 

186 

187class CrocLexer(RegexLexer): 

188 """ 

189 For `Croc <http://jfbillingsley.com/croc>`_ source. 

190 """ 

191 name = 'Croc' 

192 filenames = ['*.croc'] 

193 aliases = ['croc'] 

194 mimetypes = ['text/x-crocsrc'] 

195 

196 tokens = { 

197 'root': [ 

198 (r'\n', Text), 

199 (r'\s+', Text), 

200 # Comments 

201 (r'//(.*?)\n', Comment.Single), 

202 (r'/\*', Comment.Multiline, 'nestedcomment'), 

203 # Keywords 

204 (words(( 

205 'as', 'assert', 'break', 'case', 'catch', 'class', 'continue', 

206 'default', 'do', 'else', 'finally', 'for', 'foreach', 'function', 

207 'global', 'namespace', 'if', 'import', 'in', 'is', 'local', 

208 'module', 'return', 'scope', 'super', 'switch', 'this', 'throw', 

209 'try', 'vararg', 'while', 'with', 'yield'), suffix=r'\b'), 

210 Keyword), 

211 (r'(false|true|null)\b', Keyword.Constant), 

212 # FloatLiteral 

213 (r'([0-9][0-9_]*)(?=[.eE])(\.[0-9][0-9_]*)?([eE][+\-]?[0-9_]+)?', 

214 Number.Float), 

215 # IntegerLiteral 

216 # -- Binary 

217 (r'0[bB][01][01_]*', Number.Bin), 

218 # -- Hexadecimal 

219 (r'0[xX][0-9a-fA-F][0-9a-fA-F_]*', Number.Hex), 

220 # -- Decimal 

221 (r'([0-9][0-9_]*)(?![.eE])', Number.Integer), 

222 # CharacterLiteral 

223 (r"""'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-9]{1,3}""" 

224 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|.)'""", 

225 String.Char), 

226 # StringLiteral 

227 # -- WysiwygString 

228 (r'@"(""|[^"])*"', String), 

229 (r'@`(``|[^`])*`', String), 

230 (r"@'(''|[^'])*'", String), 

231 # -- DoubleQuotedString 

232 (r'"(\\\\|\\[^\\]|[^"\\])*"', String), 

233 # Tokens 

234 (r'(~=|\^=|%=|\*=|==|!=|>>>=|>>>|>>=|>>|>=|<=>|\?=|-\>' 

235 r'|<<=|<<|<=|\+\+|\+=|--|-=|\|\||\|=|&&|&=|\.\.|/=)' 

236 r'|[-/.&$@|\+<>!()\[\]{}?,;:=*%^~#\\]', Punctuation), 

237 # Identifier 

238 (r'[a-zA-Z_]\w*', Name), 

239 ], 

240 'nestedcomment': [ 

241 (r'[^*/]+', Comment.Multiline), 

242 (r'/\*', Comment.Multiline, '#push'), 

243 (r'\*/', Comment.Multiline, '#pop'), 

244 (r'[*/]', Comment.Multiline), 

245 ], 

246 } 

247 

248 

249class MiniDLexer(CrocLexer): 

250 """ 

251 For MiniD source. MiniD is now known as Croc. 

252 """ 

253 name = 'MiniD' 

254 filenames = [] # don't lex .md as MiniD, reserve for Markdown 

255 aliases = ['minid'] 

256 mimetypes = ['text/x-minidsrc']