Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/dylan.py: 65%

65 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.dylan 

3 ~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for the Dylan language. 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import re 

12 

13from pygments.lexer import Lexer, RegexLexer, bygroups, do_insertions, \ 

14 default, line_re 

15from pygments.token import Comment, Operator, Keyword, Name, String, \ 

16 Number, Punctuation, Generic, Literal, Whitespace 

17 

18__all__ = ['DylanLexer', 'DylanConsoleLexer', 'DylanLidLexer'] 

19 

20 

21class DylanLexer(RegexLexer): 

22 """ 

23 For the Dylan language. 

24 

25 .. versionadded:: 0.7 

26 """ 

27 

28 name = 'Dylan' 

29 url = 'http://www.opendylan.org/' 

30 aliases = ['dylan'] 

31 filenames = ['*.dylan', '*.dyl', '*.intr'] 

32 mimetypes = ['text/x-dylan'] 

33 

34 flags = re.IGNORECASE 

35 

36 builtins = { 

37 'subclass', 'abstract', 'block', 'concrete', 'constant', 'class', 

38 'compiler-open', 'compiler-sideways', 'domain', 'dynamic', 

39 'each-subclass', 'exception', 'exclude', 'function', 'generic', 

40 'handler', 'inherited', 'inline', 'inline-only', 'instance', 

41 'interface', 'import', 'keyword', 'library', 'macro', 'method', 

42 'module', 'open', 'primary', 'required', 'sealed', 'sideways', 

43 'singleton', 'slot', 'thread', 'variable', 'virtual'} 

44 

45 keywords = { 

46 'above', 'afterwards', 'begin', 'below', 'by', 'case', 'cleanup', 

47 'create', 'define', 'else', 'elseif', 'end', 'export', 'finally', 

48 'for', 'from', 'if', 'in', 'let', 'local', 'otherwise', 'rename', 

49 'select', 'signal', 'then', 'to', 'unless', 'until', 'use', 'when', 

50 'while'} 

51 

52 operators = { 

53 '~', '+', '-', '*', '|', '^', '=', '==', '~=', '~==', '<', '<=', 

54 '>', '>=', '&', '|'} 

55 

56 functions = { 

57 'abort', 'abs', 'add', 'add!', 'add-method', 'add-new', 'add-new!', 

58 'all-superclasses', 'always', 'any?', 'applicable-method?', 'apply', 

59 'aref', 'aref-setter', 'as', 'as-lowercase', 'as-lowercase!', 

60 'as-uppercase', 'as-uppercase!', 'ash', 'backward-iteration-protocol', 

61 'break', 'ceiling', 'ceiling/', 'cerror', 'check-type', 'choose', 

62 'choose-by', 'complement', 'compose', 'concatenate', 'concatenate-as', 

63 'condition-format-arguments', 'condition-format-string', 'conjoin', 

64 'copy-sequence', 'curry', 'default-handler', 'dimension', 'dimensions', 

65 'direct-subclasses', 'direct-superclasses', 'disjoin', 'do', 

66 'do-handlers', 'element', 'element-setter', 'empty?', 'error', 'even?', 

67 'every?', 'false-or', 'fill!', 'find-key', 'find-method', 'first', 

68 'first-setter', 'floor', 'floor/', 'forward-iteration-protocol', 

69 'function-arguments', 'function-return-values', 

70 'function-specializers', 'gcd', 'generic-function-mandatory-keywords', 

71 'generic-function-methods', 'head', 'head-setter', 'identity', 

72 'initialize', 'instance?', 'integral?', 'intersection', 

73 'key-sequence', 'key-test', 'last', 'last-setter', 'lcm', 'limited', 

74 'list', 'logand', 'logbit?', 'logior', 'lognot', 'logxor', 'make', 

75 'map', 'map-as', 'map-into', 'max', 'member?', 'merge-hash-codes', 

76 'min', 'modulo', 'negative', 'negative?', 'next-method', 

77 'object-class', 'object-hash', 'odd?', 'one-of', 'pair', 'pop', 

78 'pop-last', 'positive?', 'push', 'push-last', 'range', 'rank', 

79 'rcurry', 'reduce', 'reduce1', 'remainder', 'remove', 'remove!', 

80 'remove-duplicates', 'remove-duplicates!', 'remove-key!', 

81 'remove-method', 'replace-elements!', 'replace-subsequence!', 

82 'restart-query', 'return-allowed?', 'return-description', 

83 'return-query', 'reverse', 'reverse!', 'round', 'round/', 

84 'row-major-index', 'second', 'second-setter', 'shallow-copy', 

85 'signal', 'singleton', 'size', 'size-setter', 'slot-initialized?', 

86 'sort', 'sort!', 'sorted-applicable-methods', 'subsequence-position', 

87 'subtype?', 'table-protocol', 'tail', 'tail-setter', 'third', 

88 'third-setter', 'truncate', 'truncate/', 'type-error-expected-type', 

89 'type-error-value', 'type-for-copy', 'type-union', 'union', 'values', 

90 'vector', 'zero?'} 

91 

92 valid_name = '\\\\?[\\w!&*<>|^$%@\\-+~?/=]+' 

93 

94 def get_tokens_unprocessed(self, text): 

95 for index, token, value in RegexLexer.get_tokens_unprocessed(self, text): 

96 if token is Name: 

97 lowercase_value = value.lower() 

98 if lowercase_value in self.builtins: 

99 yield index, Name.Builtin, value 

100 continue 

101 if lowercase_value in self.keywords: 

102 yield index, Keyword, value 

103 continue 

104 if lowercase_value in self.functions: 

105 yield index, Name.Builtin, value 

106 continue 

107 if lowercase_value in self.operators: 

108 yield index, Operator, value 

109 continue 

110 yield index, token, value 

111 

112 tokens = { 

113 'root': [ 

114 # Whitespace 

115 (r'\s+', Whitespace), 

116 

117 # single line comment 

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

119 

120 # lid header 

121 (r'([a-z0-9-]+)(:)([ \t]*)(.*(?:\n[ \t].+)*)', 

122 bygroups(Name.Attribute, Operator, Whitespace, String)), 

123 

124 default('code') # no header match, switch to code 

125 ], 

126 'code': [ 

127 # Whitespace 

128 (r'\s+', Whitespace), 

129 

130 # single line comment 

131 (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)), 

132 

133 # multi-line comment 

134 (r'/\*', Comment.Multiline, 'comment'), 

135 

136 # strings and characters 

137 (r'"', String, 'string'), 

138 (r"'(\\.|\\[0-7]{1,3}|\\x[a-f0-9]{1,2}|[^\\\'\n])'", String.Char), 

139 

140 # binary integer 

141 (r'#b[01]+', Number.Bin), 

142 

143 # octal integer 

144 (r'#o[0-7]+', Number.Oct), 

145 

146 # floating point 

147 (r'[-+]?(\d*\.\d+(e[-+]?\d+)?|\d+(\.\d*)?e[-+]?\d+)', Number.Float), 

148 

149 # decimal integer 

150 (r'[-+]?\d+', Number.Integer), 

151 

152 # hex integer 

153 (r'#x[0-9a-f]+', Number.Hex), 

154 

155 # Macro parameters 

156 (r'(\?' + valid_name + ')(:)' 

157 r'(token|name|variable|expression|body|case-body|\*)', 

158 bygroups(Name.Tag, Operator, Name.Builtin)), 

159 (r'(\?)(:)(token|name|variable|expression|body|case-body|\*)', 

160 bygroups(Name.Tag, Operator, Name.Builtin)), 

161 (r'\?' + valid_name, Name.Tag), 

162 

163 # Punctuation 

164 (r'(=>|::|#\(|#\[|##|\?\?|\?=|\?|[(){}\[\],.;])', Punctuation), 

165 

166 # Most operators are picked up as names and then re-flagged. 

167 # This one isn't valid in a name though, so we pick it up now. 

168 (r':=', Operator), 

169 

170 # Pick up #t / #f before we match other stuff with #. 

171 (r'#[tf]', Literal), 

172 

173 # #"foo" style keywords 

174 (r'#"', String.Symbol, 'keyword'), 

175 

176 # #rest, #key, #all-keys, etc. 

177 (r'#[a-z0-9-]+', Keyword), 

178 

179 # required-init-keyword: style keywords. 

180 (valid_name + ':', Keyword), 

181 

182 # class names 

183 ('<' + valid_name + '>', Name.Class), 

184 

185 # define variable forms. 

186 (r'\*' + valid_name + r'\*', Name.Variable.Global), 

187 

188 # define constant forms. 

189 (r'\$' + valid_name, Name.Constant), 

190 

191 # everything else. We re-flag some of these in the method above. 

192 (valid_name, Name), 

193 ], 

194 'comment': [ 

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

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

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

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

199 ], 

200 'keyword': [ 

201 (r'"', String.Symbol, '#pop'), 

202 (r'[^\\"]+', String.Symbol), # all other characters 

203 ], 

204 'string': [ 

205 (r'"', String, '#pop'), 

206 (r'\\([\\abfnrtv"\']|x[a-f0-9]{2,4}|[0-7]{1,3})', String.Escape), 

207 (r'[^\\"\n]+', String), # all other characters 

208 (r'\\\n', String), # line continuation 

209 (r'\\', String), # stray backslash 

210 ] 

211 } 

212 

213 

214class DylanLidLexer(RegexLexer): 

215 """ 

216 For Dylan LID (Library Interchange Definition) files. 

217 

218 .. versionadded:: 1.6 

219 """ 

220 

221 name = 'DylanLID' 

222 aliases = ['dylan-lid', 'lid'] 

223 filenames = ['*.lid', '*.hdp'] 

224 mimetypes = ['text/x-dylan-lid'] 

225 

226 flags = re.IGNORECASE 

227 

228 tokens = { 

229 'root': [ 

230 # Whitespace 

231 (r'\s+', Whitespace), 

232 

233 # single line comment 

234 (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)), 

235 

236 # lid header 

237 (r'(.*?)(:)([ \t]*)(.*(?:\n[ \t].+)*)', 

238 bygroups(Name.Attribute, Operator, Whitespace, String)), 

239 ] 

240 } 

241 

242 

243class DylanConsoleLexer(Lexer): 

244 """ 

245 For Dylan interactive console output like: 

246 

247 .. sourcecode:: dylan-console 

248 

249 ? let a = 1; 

250 => 1 

251 ? a 

252 => 1 

253 

254 This is based on a copy of the RubyConsoleLexer. 

255 

256 .. versionadded:: 1.6 

257 """ 

258 name = 'Dylan session' 

259 aliases = ['dylan-console', 'dylan-repl'] 

260 filenames = ['*.dylan-console'] 

261 mimetypes = ['text/x-dylan-console'] 

262 

263 _prompt_re = re.compile(r'\?| ') 

264 

265 def get_tokens_unprocessed(self, text): 

266 dylexer = DylanLexer(**self.options) 

267 

268 curcode = '' 

269 insertions = [] 

270 for match in line_re.finditer(text): 

271 line = match.group() 

272 m = self._prompt_re.match(line) 

273 if m is not None: 

274 end = m.end() 

275 insertions.append((len(curcode), 

276 [(0, Generic.Prompt, line[:end])])) 

277 curcode += line[end:] 

278 else: 

279 if curcode: 

280 yield from do_insertions(insertions, 

281 dylexer.get_tokens_unprocessed(curcode)) 

282 curcode = '' 

283 insertions = [] 

284 yield match.start(), Generic.Output, line 

285 if curcode: 

286 yield from do_insertions(insertions, 

287 dylexer.get_tokens_unprocessed(curcode))