arturo.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """
  2. pygments.lexers.arturo
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Arturo language.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, do_insertions, include, \
  9. this, using, words
  10. from pygments.token import Comment, Error, Keyword, Name, Number, Operator, \
  11. Punctuation, String, Text
  12. from pygments.util import ClassNotFound, get_bool_opt
  13. __all__ = ['ArturoLexer']
  14. class ArturoLexer(RegexLexer):
  15. """
  16. For Arturo source code.
  17. See `Arturo's Github <https://github.com/arturo-lang/arturo>`_
  18. and `Arturo's Website <https://arturo-lang.io/>`_.
  19. .. versionadded:: 2.14.0
  20. """
  21. name = 'Arturo'
  22. aliases = ['arturo', 'art']
  23. filenames = ['*.art']
  24. url = 'https://arturo-lang.io/'
  25. def __init__(self, **options):
  26. self.handle_annotateds = get_bool_opt(options, 'handle_annotateds',
  27. True)
  28. RegexLexer.__init__(self, **options)
  29. def handle_annotated_strings(self, match):
  30. """Adds syntax from another languages inside annotated strings
  31. match args:
  32. 1:open_string,
  33. 2:exclamation_mark,
  34. 3:lang_name,
  35. 4:space_or_newline,
  36. 5:code,
  37. 6:close_string
  38. """
  39. from pygments.lexers import get_lexer_by_name
  40. # Header's section
  41. yield match.start(1), String.Double, match.group(1)
  42. yield match.start(2), String.Interpol, match.group(2)
  43. yield match.start(3), String.Interpol, match.group(3)
  44. yield match.start(4), Text.Whitespace, match.group(4)
  45. lexer = None
  46. if self.handle_annotateds:
  47. try:
  48. lexer = get_lexer_by_name(match.group(3).strip())
  49. except ClassNotFound:
  50. pass
  51. code = match.group(5)
  52. if lexer is None:
  53. yield match.group(5), String, code
  54. else:
  55. yield from do_insertions([], lexer.get_tokens_unprocessed(code))
  56. yield match.start(6), String.Double, match.group(6)
  57. tokens = {
  58. 'root': [
  59. (r';.*?$', Comment.Single),
  60. (r'^((\s#!)|(#!)).*?$', Comment.Hashbang),
  61. # Constants
  62. (words(('false', 'true', 'maybe'), # boolean
  63. suffix=r'\b'), Name.Constant),
  64. (words(('this', 'init'), # class related keywords
  65. prefix=r'\b', suffix=r'\b\??:?'), Name.Builtin.Pseudo),
  66. (r'`.`', String.Char), # character
  67. (r'\\\w+\b\??:?', Name.Property), # array index
  68. (r'#\w+', Name.Constant), # color
  69. (r'\b[0-9]+\.[0-9]+', Number.Float), # float
  70. (r'\b[0-9]+', Number.Integer), # integer
  71. (r'\w+\b\??:', Name.Label), # label
  72. # Note: Literals can be labeled too
  73. (r'\'(?:\w+\b\??:?)', Keyword.Declaration), # literal
  74. (r'\:\w+', Keyword.Type), # type
  75. # Note: Attributes can be labeled too
  76. (r'\.\w+\??:?', Name.Attribute), # attributes
  77. # Switch structure
  78. (r'(\()(.*?)(\)\?)',
  79. bygroups(Punctuation, using(this), Punctuation)),
  80. # Single Line Strings
  81. (r'"', String.Double, 'inside-simple-string'),
  82. (r'»', String.Single, 'inside-smart-string'),
  83. (r'«««', String.Double, 'inside-safe-string'),
  84. (r'\{\/', String.Single, 'inside-regex-string'),
  85. # Multi Line Strings
  86. (r'\{\:', String.Double, 'inside-curly-verb-string'),
  87. (r'(\{)(\!)(\w+)(\s|\n)([\w\W]*?)(^\})', handle_annotated_strings),
  88. (r'\{', String.Single, 'inside-curly-string'),
  89. (r'\-{3,}', String.Single, 'inside-eof-string'),
  90. include('builtin-functions'),
  91. # Operators
  92. (r'[()[\],]', Punctuation),
  93. (words(('->', '==>', '|', '::', '@', '#', # sugar syntax
  94. '$', '&', '!', '!!', './')), Name.Decorator),
  95. (words(('<:', ':>', ':<', '>:', '<\\', '<>', '<', '>',
  96. 'ø', '∞',
  97. '+', '-', '*', '~', '=', '^', '%', '/', '//',
  98. '==>', '<=>', '<==>',
  99. '=>>', '<<=>>', '<<==>>',
  100. '-->', '<->', '<-->',
  101. '=|', '|=', '-:', ':-',
  102. '_', '.', '..', '\\')), Operator),
  103. (r'\b\w+', Name),
  104. (r'\s+', Text.Whitespace),
  105. (r'.+$', Error),
  106. ],
  107. 'inside-interpol': [
  108. (r'\|', String.Interpol, '#pop'),
  109. (r'[^|]+', using(this)),
  110. ],
  111. 'inside-template': [
  112. (r'\|\|\>', String.Interpol, '#pop'),
  113. (r'[^|]+', using(this)),
  114. ],
  115. 'string-escape': [
  116. (words(('\\\\', '\\n', '\\t', '\\"')), String.Escape),
  117. ],
  118. 'inside-simple-string': [
  119. include('string-escape'),
  120. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  121. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  122. (r'"', String.Double, '#pop'), # Closing Quote
  123. (r'[^|"]+', String) # String Content
  124. ],
  125. 'inside-smart-string': [
  126. include('string-escape'),
  127. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  128. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  129. (r'\n', String.Single, '#pop'), # Closing Quote
  130. (r'[^|\n]+', String) # String Content
  131. ],
  132. 'inside-safe-string': [
  133. include('string-escape'),
  134. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  135. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  136. (r'»»»', String.Double, '#pop'), # Closing Quote
  137. (r'[^|»]+', String) # String Content
  138. ],
  139. 'inside-regex-string': [
  140. (r'\\[sSwWdDbBZApPxucItnvfr0]+', String.Escape),
  141. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  142. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  143. (r'\/\}', String.Single, '#pop'), # Closing Quote
  144. (r'[^|\/]+', String.Regex), # String Content
  145. ],
  146. 'inside-curly-verb-string': [
  147. include('string-escape'),
  148. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  149. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  150. (r'\:\}', String.Double, '#pop'), # Closing Quote
  151. (r'[^|<:]+', String), # String Content
  152. ],
  153. 'inside-curly-string': [
  154. include('string-escape'),
  155. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  156. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  157. (r'\}', String.Single, '#pop'), # Closing Quote
  158. (r'[^|<}]+', String), # String Content
  159. ],
  160. 'inside-eof-string': [
  161. include('string-escape'),
  162. (r'\|', String.Interpol, 'inside-interpol'), # Interpolation
  163. (r'\<\|\|', String.Interpol, 'inside-template'), # Templates
  164. (r'\Z', String.Single, '#pop'), # Closing Quote
  165. (r'[^|<]+', String), # String Content
  166. ],
  167. 'builtin-functions': [
  168. (words((
  169. 'all', 'and', 'any', 'ascii', 'attr', 'attribute',
  170. 'attributeLabel', 'binary', 'block' 'char', 'contains',
  171. 'database', 'date', 'dictionary', 'empty', 'equal', 'even',
  172. 'every', 'exists', 'false', 'floatin', 'function', 'greater',
  173. 'greaterOrEqual', 'if', 'in', 'inline', 'integer', 'is',
  174. 'key', 'label', 'leap', 'less', 'lessOrEqual', 'literal',
  175. 'logical', 'lower', 'nand', 'negative', 'nor', 'not',
  176. 'notEqual', 'null', 'numeric', 'odd', 'or', 'path',
  177. 'pathLabel', 'positive', 'prefix', 'prime', 'set', 'some',
  178. 'sorted', 'standalone', 'string', 'subset', 'suffix',
  179. 'superset', 'ymbol', 'true', 'try', 'type', 'unless', 'upper',
  180. 'when', 'whitespace', 'word', 'xnor', 'xor', 'zero',
  181. ), prefix=r'\b', suffix=r'\b\?'), Name.Builtin),
  182. (words((
  183. 'abs', 'acos', 'acosh', 'acsec', 'acsech', 'actan', 'actanh',
  184. 'add', 'after', 'alphabet', 'and', 'angle', 'append', 'arg',
  185. 'args', 'arity', 'array', 'as', 'asec', 'asech', 'asin',
  186. 'asinh', 'atan', 'atan2', 'atanh', 'attr', 'attrs', 'average',
  187. 'before', 'benchmark', 'blend', 'break', 'builtins1',
  188. 'builtins2', 'call', 'capitalize', 'case', 'ceil', 'chop',
  189. 'chunk', 'clear', 'close', 'cluster', 'color', 'combine',
  190. 'conj', 'continue', 'copy', 'cos', 'cosh', 'couple', 'csec',
  191. 'csech', 'ctan', 'ctanh', 'cursor', 'darken', 'dec', 'decode',
  192. 'decouple', 'define', 'delete', 'desaturate', 'deviation',
  193. 'dictionary', 'difference', 'digest', 'digits', 'div', 'do',
  194. 'download', 'drop', 'dup', 'e', 'else', 'empty', 'encode',
  195. 'ensure', 'env', 'epsilon', 'escape', 'execute', 'exit', 'exp',
  196. 'extend', 'extract', 'factors', 'false', 'fdiv', 'filter',
  197. 'first', 'flatten', 'floor', 'fold', 'from', 'function',
  198. 'gamma', 'gcd', 'get', 'goto', 'hash', 'help', 'hypot', 'if',
  199. 'in', 'inc', 'indent', 'index', 'infinity', 'info', 'input',
  200. 'insert', 'inspect', 'intersection', 'invert', 'join', 'keys',
  201. 'kurtosis', 'last', 'let', 'levenshtein', 'lighten', 'list',
  202. 'ln', 'log', 'loop', 'lower', 'mail', 'map', 'match', 'max',
  203. 'maybe', 'median', 'min', 'mod', 'module', 'mul', 'nand',
  204. 'neg', 'new', 'nor', 'normalize', 'not', 'now', 'null', 'open',
  205. 'or', 'outdent', 'pad', 'panic', 'path', 'pause',
  206. 'permissions', 'permutate', 'pi', 'pop', 'pow', 'powerset',
  207. 'powmod', 'prefix', 'print', 'prints', 'process', 'product',
  208. 'query', 'random', 'range', 'read', 'relative', 'remove',
  209. 'rename', 'render', 'repeat', 'replace', 'request', 'return',
  210. 'reverse', 'round', 'sample', 'saturate', 'script', 'sec',
  211. 'sech', 'select', 'serve', 'set', 'shl', 'shr', 'shuffle',
  212. 'sin', 'sinh', 'size', 'skewness', 'slice', 'sort', 'split',
  213. 'sqrt', 'squeeze', 'stack', 'strip', 'sub', 'suffix', 'sum',
  214. 'switch', 'symbols', 'symlink', 'sys', 'take', 'tan', 'tanh',
  215. 'terminal', 'to', 'true', 'truncate', 'try', 'type', 'union',
  216. 'unique', 'unless', 'until', 'unzip', 'upper', 'values', 'var',
  217. 'variance', 'volume', 'webview', 'while', 'with', 'wordwrap',
  218. 'write', 'xnor', 'xor', 'zip'
  219. ), prefix=r'\b', suffix=r'\b'), Name.Builtin)
  220. ],
  221. }