chapel.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. pygments.lexers.chapel
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Chapel 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, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['ChapelLexer']
  12. class ChapelLexer(RegexLexer):
  13. """
  14. For Chapel source.
  15. .. versionadded:: 2.0
  16. """
  17. name = 'Chapel'
  18. url = 'https://chapel-lang.org/'
  19. filenames = ['*.chpl']
  20. aliases = ['chapel', 'chpl']
  21. # mimetypes = ['text/x-chapel']
  22. known_types = ('bool', 'bytes', 'complex', 'imag', 'int', 'locale',
  23. 'nothing', 'opaque', 'range', 'real', 'string', 'uint',
  24. 'void')
  25. type_modifiers_par = ('atomic', 'single', 'sync')
  26. type_modifiers_mem = ('borrowed', 'owned', 'shared', 'unmanaged')
  27. type_modifiers = (*type_modifiers_par, *type_modifiers_mem)
  28. declarations = ('config', 'const', 'in', 'inout', 'out', 'param', 'ref',
  29. 'type', 'var')
  30. constants = ('false', 'nil', 'none', 'true')
  31. other_keywords = ('align', 'as',
  32. 'begin', 'break', 'by',
  33. 'catch', 'cobegin', 'coforall', 'continue',
  34. 'defer', 'delete', 'dmapped', 'do', 'domain',
  35. 'else', 'enum', 'except', 'export', 'extern',
  36. 'for', 'forall', 'foreach', 'forwarding',
  37. 'if', 'implements', 'import', 'index', 'init', 'inline',
  38. 'label', 'lambda', 'let', 'lifetime', 'local',
  39. 'new', 'noinit',
  40. 'on', 'only', 'otherwise', 'override',
  41. 'pragma', 'primitive', 'private', 'prototype', 'public',
  42. 'reduce', 'require', 'return',
  43. 'scan', 'select', 'serial', 'sparse', 'subdomain',
  44. 'then', 'this', 'throw', 'throws', 'try',
  45. 'use',
  46. 'when', 'where', 'while', 'with',
  47. 'yield',
  48. 'zip')
  49. tokens = {
  50. 'root': [
  51. (r'\n', Whitespace),
  52. (r'\s+', Whitespace),
  53. (r'\\\n', Text),
  54. (r'//(.*?)\n', Comment.Single),
  55. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  56. (words(declarations, suffix=r'\b'), Keyword.Declaration),
  57. (words(constants, suffix=r'\b'), Keyword.Constant),
  58. (words(known_types, suffix=r'\b'), Keyword.Type),
  59. (words((*type_modifiers, *other_keywords), suffix=r'\b'), Keyword),
  60. (r'@', Keyword, 'attributename'),
  61. (r'(iter)(\s+)', bygroups(Keyword, Whitespace), 'procname'),
  62. (r'(proc)(\s+)', bygroups(Keyword, Whitespace), 'procname'),
  63. (r'(operator)(\s+)', bygroups(Keyword, Whitespace), 'procname'),
  64. (r'(class|interface|module|record|union)(\s+)', bygroups(Keyword, Whitespace),
  65. 'classname'),
  66. # imaginary integers
  67. (r'\d+i', Number),
  68. (r'\d+\.\d*([Ee][-+]\d+)?i', Number),
  69. (r'\.\d+([Ee][-+]\d+)?i', Number),
  70. (r'\d+[Ee][-+]\d+i', Number),
  71. # reals cannot end with a period due to lexical ambiguity with
  72. # .. operator. See reference for rationale.
  73. (r'(\d*\.\d+)([eE][+-]?[0-9]+)?i?', Number.Float),
  74. (r'\d+[eE][+-]?[0-9]+i?', Number.Float),
  75. # integer literals
  76. # -- binary
  77. (r'0[bB][01]+', Number.Bin),
  78. # -- hex
  79. (r'0[xX][0-9a-fA-F]+', Number.Hex),
  80. # -- octal
  81. (r'0[oO][0-7]+', Number.Oct),
  82. # -- decimal
  83. (r'[0-9]+', Number.Integer),
  84. # strings
  85. (r'"(\\\\|\\"|[^"])*"', String),
  86. (r"'(\\\\|\\'|[^'])*'", String),
  87. # tokens
  88. (r'(=|\+=|-=|\*=|/=|\*\*=|%=|&=|\|=|\^=|&&=|\|\|=|<<=|>>=|'
  89. r'<=>|<~>|\.\.|by|#|\.\.\.|'
  90. r'&&|\|\||!|&|\||\^|~|<<|>>|'
  91. r'==|!=|<=|>=|<|>|'
  92. r'[+\-*/%]|\*\*)', Operator),
  93. (r'[:;,.?()\[\]{}]', Punctuation),
  94. # identifiers
  95. (r'[a-zA-Z_][\w$]*', Name.Other),
  96. ],
  97. 'classname': [
  98. (r'[a-zA-Z_][\w$]*', Name.Class, '#pop'),
  99. ],
  100. 'procname': [
  101. (r'([a-zA-Z_][.\w$]*|' # regular function name, including secondary
  102. r'\~[a-zA-Z_][.\w$]*|' # support for legacy destructors
  103. r'[+*/!~%<>=&^|\-:]{1,2})', # operators
  104. Name.Function, '#pop'),
  105. # allow `proc (atomic T).foo`
  106. (r'\(', Punctuation, "receivertype"),
  107. (r'\)+\.', Punctuation),
  108. ],
  109. 'receivertype': [
  110. (words(type_modifiers, suffix=r'\b'), Keyword),
  111. (words(known_types, suffix=r'\b'), Keyword.Type),
  112. (r'[^()]*', Name.Other, '#pop'),
  113. ],
  114. 'attributename': [
  115. (r'[a-zA-Z_][.\w$]*', Name.Decorator, '#pop'),
  116. ],
  117. }