chapel.py 4.9 KB

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