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