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
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""
2 pygments.lexers.dylan
3 ~~~~~~~~~~~~~~~~~~~~~
5 Lexers for the Dylan language.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11import re
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
18__all__ = ['DylanLexer', 'DylanConsoleLexer', 'DylanLidLexer']
21class DylanLexer(RegexLexer):
22 """
23 For the Dylan language.
25 .. versionadded:: 0.7
26 """
28 name = 'Dylan'
29 url = 'http://www.opendylan.org/'
30 aliases = ['dylan']
31 filenames = ['*.dylan', '*.dyl', '*.intr']
32 mimetypes = ['text/x-dylan']
34 flags = re.IGNORECASE
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'}
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'}
52 operators = {
53 '~', '+', '-', '*', '|', '^', '=', '==', '~=', '~==', '<', '<=',
54 '>', '>=', '&', '|'}
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?'}
92 valid_name = '\\\\?[\\w!&*<>|^$%@\\-+~?/=]+'
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
112 tokens = {
113 'root': [
114 # Whitespace
115 (r'\s+', Whitespace),
117 # single line comment
118 (r'//.*?\n', Comment.Single),
120 # lid header
121 (r'([a-z0-9-]+)(:)([ \t]*)(.*(?:\n[ \t].+)*)',
122 bygroups(Name.Attribute, Operator, Whitespace, String)),
124 default('code') # no header match, switch to code
125 ],
126 'code': [
127 # Whitespace
128 (r'\s+', Whitespace),
130 # single line comment
131 (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
133 # multi-line comment
134 (r'/\*', Comment.Multiline, 'comment'),
136 # strings and characters
137 (r'"', String, 'string'),
138 (r"'(\\.|\\[0-7]{1,3}|\\x[a-f0-9]{1,2}|[^\\\'\n])'", String.Char),
140 # binary integer
141 (r'#b[01]+', Number.Bin),
143 # octal integer
144 (r'#o[0-7]+', Number.Oct),
146 # floating point
147 (r'[-+]?(\d*\.\d+(e[-+]?\d+)?|\d+(\.\d*)?e[-+]?\d+)', Number.Float),
149 # decimal integer
150 (r'[-+]?\d+', Number.Integer),
152 # hex integer
153 (r'#x[0-9a-f]+', Number.Hex),
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),
163 # Punctuation
164 (r'(=>|::|#\(|#\[|##|\?\?|\?=|\?|[(){}\[\],.;])', Punctuation),
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),
170 # Pick up #t / #f before we match other stuff with #.
171 (r'#[tf]', Literal),
173 # #"foo" style keywords
174 (r'#"', String.Symbol, 'keyword'),
176 # #rest, #key, #all-keys, etc.
177 (r'#[a-z0-9-]+', Keyword),
179 # required-init-keyword: style keywords.
180 (valid_name + ':', Keyword),
182 # class names
183 ('<' + valid_name + '>', Name.Class),
185 # define variable forms.
186 (r'\*' + valid_name + r'\*', Name.Variable.Global),
188 # define constant forms.
189 (r'\$' + valid_name, Name.Constant),
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 }
214class DylanLidLexer(RegexLexer):
215 """
216 For Dylan LID (Library Interchange Definition) files.
218 .. versionadded:: 1.6
219 """
221 name = 'DylanLID'
222 aliases = ['dylan-lid', 'lid']
223 filenames = ['*.lid', '*.hdp']
224 mimetypes = ['text/x-dylan-lid']
226 flags = re.IGNORECASE
228 tokens = {
229 'root': [
230 # Whitespace
231 (r'\s+', Whitespace),
233 # single line comment
234 (r'(//.*?)(\n)', bygroups(Comment.Single, Whitespace)),
236 # lid header
237 (r'(.*?)(:)([ \t]*)(.*(?:\n[ \t].+)*)',
238 bygroups(Name.Attribute, Operator, Whitespace, String)),
239 ]
240 }
243class DylanConsoleLexer(Lexer):
244 """
245 For Dylan interactive console output like:
247 .. sourcecode:: dylan-console
249 ? let a = 1;
250 => 1
251 ? a
252 => 1
254 This is based on a copy of the RubyConsoleLexer.
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']
263 _prompt_re = re.compile(r'\?| ')
265 def get_tokens_unprocessed(self, text):
266 dylexer = DylanLexer(**self.options)
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))