dylan.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.dylan
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the Dylan language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import Lexer, RegexLexer, bygroups, do_insertions, default
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation, Generic, Literal
  13. __all__ = ['DylanLexer', 'DylanConsoleLexer', 'DylanLidLexer']
  14. class DylanLexer(RegexLexer):
  15. """
  16. For the `Dylan <http://www.opendylan.org/>`_ language.
  17. .. versionadded:: 0.7
  18. """
  19. name = 'Dylan'
  20. aliases = ['dylan']
  21. filenames = ['*.dylan', '*.dyl', '*.intr']
  22. mimetypes = ['text/x-dylan']
  23. flags = re.IGNORECASE
  24. builtins = {
  25. 'subclass', 'abstract', 'block', 'concrete', 'constant', 'class',
  26. 'compiler-open', 'compiler-sideways', 'domain', 'dynamic',
  27. 'each-subclass', 'exception', 'exclude', 'function', 'generic',
  28. 'handler', 'inherited', 'inline', 'inline-only', 'instance',
  29. 'interface', 'import', 'keyword', 'library', 'macro', 'method',
  30. 'module', 'open', 'primary', 'required', 'sealed', 'sideways',
  31. 'singleton', 'slot', 'thread', 'variable', 'virtual'}
  32. keywords = {
  33. 'above', 'afterwards', 'begin', 'below', 'by', 'case', 'cleanup',
  34. 'create', 'define', 'else', 'elseif', 'end', 'export', 'finally',
  35. 'for', 'from', 'if', 'in', 'let', 'local', 'otherwise', 'rename',
  36. 'select', 'signal', 'then', 'to', 'unless', 'until', 'use', 'when',
  37. 'while'}
  38. operators = {
  39. '~', '+', '-', '*', '|', '^', '=', '==', '~=', '~==', '<', '<=',
  40. '>', '>=', '&', '|'}
  41. functions = {
  42. 'abort', 'abs', 'add', 'add!', 'add-method', 'add-new', 'add-new!',
  43. 'all-superclasses', 'always', 'any?', 'applicable-method?', 'apply',
  44. 'aref', 'aref-setter', 'as', 'as-lowercase', 'as-lowercase!',
  45. 'as-uppercase', 'as-uppercase!', 'ash', 'backward-iteration-protocol',
  46. 'break', 'ceiling', 'ceiling/', 'cerror', 'check-type', 'choose',
  47. 'choose-by', 'complement', 'compose', 'concatenate', 'concatenate-as',
  48. 'condition-format-arguments', 'condition-format-string', 'conjoin',
  49. 'copy-sequence', 'curry', 'default-handler', 'dimension', 'dimensions',
  50. 'direct-subclasses', 'direct-superclasses', 'disjoin', 'do',
  51. 'do-handlers', 'element', 'element-setter', 'empty?', 'error', 'even?',
  52. 'every?', 'false-or', 'fill!', 'find-key', 'find-method', 'first',
  53. 'first-setter', 'floor', 'floor/', 'forward-iteration-protocol',
  54. 'function-arguments', 'function-return-values',
  55. 'function-specializers', 'gcd', 'generic-function-mandatory-keywords',
  56. 'generic-function-methods', 'head', 'head-setter', 'identity',
  57. 'initialize', 'instance?', 'integral?', 'intersection',
  58. 'key-sequence', 'key-test', 'last', 'last-setter', 'lcm', 'limited',
  59. 'list', 'logand', 'logbit?', 'logior', 'lognot', 'logxor', 'make',
  60. 'map', 'map-as', 'map-into', 'max', 'member?', 'merge-hash-codes',
  61. 'min', 'modulo', 'negative', 'negative?', 'next-method',
  62. 'object-class', 'object-hash', 'odd?', 'one-of', 'pair', 'pop',
  63. 'pop-last', 'positive?', 'push', 'push-last', 'range', 'rank',
  64. 'rcurry', 'reduce', 'reduce1', 'remainder', 'remove', 'remove!',
  65. 'remove-duplicates', 'remove-duplicates!', 'remove-key!',
  66. 'remove-method', 'replace-elements!', 'replace-subsequence!',
  67. 'restart-query', 'return-allowed?', 'return-description',
  68. 'return-query', 'reverse', 'reverse!', 'round', 'round/',
  69. 'row-major-index', 'second', 'second-setter', 'shallow-copy',
  70. 'signal', 'singleton', 'size', 'size-setter', 'slot-initialized?',
  71. 'sort', 'sort!', 'sorted-applicable-methods', 'subsequence-position',
  72. 'subtype?', 'table-protocol', 'tail', 'tail-setter', 'third',
  73. 'third-setter', 'truncate', 'truncate/', 'type-error-expected-type',
  74. 'type-error-value', 'type-for-copy', 'type-union', 'union', 'values',
  75. 'vector', 'zero?'}
  76. valid_name = '\\\\?[\\w!&*<>|^$%@\\-+~?/=]+'
  77. def get_tokens_unprocessed(self, text):
  78. for index, token, value in RegexLexer.get_tokens_unprocessed(self, text):
  79. if token is Name:
  80. lowercase_value = value.lower()
  81. if lowercase_value in self.builtins:
  82. yield index, Name.Builtin, value
  83. continue
  84. if lowercase_value in self.keywords:
  85. yield index, Keyword, value
  86. continue
  87. if lowercase_value in self.functions:
  88. yield index, Name.Builtin, value
  89. continue
  90. if lowercase_value in self.operators:
  91. yield index, Operator, value
  92. continue
  93. yield index, token, value
  94. tokens = {
  95. 'root': [
  96. # Whitespace
  97. (r'\s+', Text),
  98. # single line comment
  99. (r'//.*?\n', Comment.Single),
  100. # lid header
  101. (r'([a-z0-9-]+)(:)([ \t]*)(.*(?:\n[ \t].+)*)',
  102. bygroups(Name.Attribute, Operator, Text, String)),
  103. default('code') # no header match, switch to code
  104. ],
  105. 'code': [
  106. # Whitespace
  107. (r'\s+', Text),
  108. # single line comment
  109. (r'//.*?\n', Comment.Single),
  110. # multi-line comment
  111. (r'/\*', Comment.Multiline, 'comment'),
  112. # strings and characters
  113. (r'"', String, 'string'),
  114. (r"'(\\.|\\[0-7]{1,3}|\\x[a-f0-9]{1,2}|[^\\\'\n])'", String.Char),
  115. # binary integer
  116. (r'#b[01]+', Number.Bin),
  117. # octal integer
  118. (r'#o[0-7]+', Number.Oct),
  119. # floating point
  120. (r'[-+]?(\d*\.\d+(e[-+]?\d+)?|\d+(\.\d*)?e[-+]?\d+)', Number.Float),
  121. # decimal integer
  122. (r'[-+]?\d+', Number.Integer),
  123. # hex integer
  124. (r'#x[0-9a-f]+', Number.Hex),
  125. # Macro parameters
  126. (r'(\?' + valid_name + ')(:)'
  127. r'(token|name|variable|expression|body|case-body|\*)',
  128. bygroups(Name.Tag, Operator, Name.Builtin)),
  129. (r'(\?)(:)(token|name|variable|expression|body|case-body|\*)',
  130. bygroups(Name.Tag, Operator, Name.Builtin)),
  131. (r'\?' + valid_name, Name.Tag),
  132. # Punctuation
  133. (r'(=>|::|#\(|#\[|##|\?\?|\?=|\?|[(){}\[\],.;])', Punctuation),
  134. # Most operators are picked up as names and then re-flagged.
  135. # This one isn't valid in a name though, so we pick it up now.
  136. (r':=', Operator),
  137. # Pick up #t / #f before we match other stuff with #.
  138. (r'#[tf]', Literal),
  139. # #"foo" style keywords
  140. (r'#"', String.Symbol, 'keyword'),
  141. # #rest, #key, #all-keys, etc.
  142. (r'#[a-z0-9-]+', Keyword),
  143. # required-init-keyword: style keywords.
  144. (valid_name + ':', Keyword),
  145. # class names
  146. ('<' + valid_name + '>', Name.Class),
  147. # define variable forms.
  148. (r'\*' + valid_name + r'\*', Name.Variable.Global),
  149. # define constant forms.
  150. (r'\$' + valid_name, Name.Constant),
  151. # everything else. We re-flag some of these in the method above.
  152. (valid_name, Name),
  153. ],
  154. 'comment': [
  155. (r'[^*/]', Comment.Multiline),
  156. (r'/\*', Comment.Multiline, '#push'),
  157. (r'\*/', Comment.Multiline, '#pop'),
  158. (r'[*/]', Comment.Multiline)
  159. ],
  160. 'keyword': [
  161. (r'"', String.Symbol, '#pop'),
  162. (r'[^\\"]+', String.Symbol), # all other characters
  163. ],
  164. 'string': [
  165. (r'"', String, '#pop'),
  166. (r'\\([\\abfnrtv"\']|x[a-f0-9]{2,4}|[0-7]{1,3})', String.Escape),
  167. (r'[^\\"\n]+', String), # all other characters
  168. (r'\\\n', String), # line continuation
  169. (r'\\', String), # stray backslash
  170. ]
  171. }
  172. class DylanLidLexer(RegexLexer):
  173. """
  174. For Dylan LID (Library Interchange Definition) files.
  175. .. versionadded:: 1.6
  176. """
  177. name = 'DylanLID'
  178. aliases = ['dylan-lid', 'lid']
  179. filenames = ['*.lid', '*.hdp']
  180. mimetypes = ['text/x-dylan-lid']
  181. flags = re.IGNORECASE
  182. tokens = {
  183. 'root': [
  184. # Whitespace
  185. (r'\s+', Text),
  186. # single line comment
  187. (r'//.*?\n', Comment.Single),
  188. # lid header
  189. (r'(.*?)(:)([ \t]*)(.*(?:\n[ \t].+)*)',
  190. bygroups(Name.Attribute, Operator, Text, String)),
  191. ]
  192. }
  193. class DylanConsoleLexer(Lexer):
  194. """
  195. For Dylan interactive console output like:
  196. .. sourcecode:: dylan-console
  197. ? let a = 1;
  198. => 1
  199. ? a
  200. => 1
  201. This is based on a copy of the RubyConsoleLexer.
  202. .. versionadded:: 1.6
  203. """
  204. name = 'Dylan session'
  205. aliases = ['dylan-console', 'dylan-repl']
  206. filenames = ['*.dylan-console']
  207. mimetypes = ['text/x-dylan-console']
  208. _line_re = re.compile('.*?\n')
  209. _prompt_re = re.compile(r'\?| ')
  210. def get_tokens_unprocessed(self, text):
  211. dylexer = DylanLexer(**self.options)
  212. curcode = ''
  213. insertions = []
  214. for match in self._line_re.finditer(text):
  215. line = match.group()
  216. m = self._prompt_re.match(line)
  217. if m is not None:
  218. end = m.end()
  219. insertions.append((len(curcode),
  220. [(0, Generic.Prompt, line[:end])]))
  221. curcode += line[end:]
  222. else:
  223. if curcode:
  224. for item in do_insertions(insertions,
  225. dylexer.get_tokens_unprocessed(curcode)):
  226. yield item
  227. curcode = ''
  228. insertions = []
  229. yield match.start(), Generic.Output, line
  230. if curcode:
  231. for item in do_insertions(insertions,
  232. dylexer.get_tokens_unprocessed(curcode)):
  233. yield item