1"""
2 pygments.lexers.elpi
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for the `Elpi <http://github.com/LPCIC/elpi>`_ programming 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, include, using
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation
14
15__all__ = ['ElpiLexer']
16
17from pygments.lexers.theorem import CoqLexer
18
19class ElpiLexer(RegexLexer):
20 """
21 Lexer for the Elpi programming language.
22 """
23
24 name = 'Elpi'
25 url = 'http://github.com/LPCIC/elpi'
26 aliases = ['elpi']
27 filenames = ['*.elpi']
28 mimetypes = ['text/x-elpi']
29 version_added = '2.11'
30
31 lcase_re = r"[a-z]"
32 ucase_re = r"[A-Z]"
33 digit_re = r"[0-9]"
34 schar2_re = r"([+*^?/<>`'@#~=&!])"
35 schar_re = rf"({schar2_re}|-|\$|_)"
36 idchar_re = rf"({lcase_re}|{ucase_re}|{digit_re}|{schar_re})"
37 idcharstarns_re = rf"({idchar_re}*(\.({lcase_re}|{ucase_re}){idchar_re}*)*)"
38 symbchar_re = rf"({lcase_re}|{ucase_re}|{digit_re}|{schar_re}|:)"
39 constant_re = rf"({ucase_re}{idchar_re}*|{lcase_re}{idcharstarns_re}|{schar2_re}{symbchar_re}*|_{idchar_re}+)"
40 symbol_re = r"(,|<=>|->|:-|;|\?-|->|&|=>|\bas\b|\buvar\b|<|=<|=|==|>=|>|\bi<|\bi=<|\bi>=|\bi>|\bis\b|\br<|\br=<|\br>=|\br>|\bs<|\bs=<|\bs>=|\bs>|@|::|\[\]|`->|`:|`:=|\^|-|\+|\bi-|\bi\+|r-|r\+|/|\*|\bdiv\b|\bi\*|\bmod\b|\br\*|~|\bi~|\br~)"
41 escape_re = rf"\(({constant_re}|{symbol_re})\)"
42 const_sym_re = rf"({constant_re}|{symbol_re}|{escape_re})"
43
44 tokens = {
45 'root': [
46 include('elpi')
47 ],
48
49 'elpi': [
50 include('_elpi-comment'),
51
52 (r"(:before|:after|:if|:name)(\s*)(\")",
53 bygroups(Keyword.Mode, Text.Whitespace, String.Double),
54 'elpi-string'),
55 (r"(:index)(\s*)(\()", bygroups(Keyword.Mode, Text.Whitespace, Punctuation),
56 'elpi-indexing-expr'),
57 (rf"\b(external pred|pred)(\s+)({const_sym_re})",
58 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
59 'elpi-pred-item'),
60 (rf"\b(external type|type)(\s+)(({const_sym_re}(,\s*)?)+)",
61 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
62 'elpi-type'),
63 (rf"\b(kind)(\s+)(({const_sym_re}|,)+)",
64 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
65 'elpi-type'),
66 (rf"\b(typeabbrev)(\s+)({const_sym_re})",
67 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
68 'elpi-type'),
69 (r"\b(typeabbrev)(\s+)(\([^)]+\))",
70 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
71 'elpi-type'),
72 (r"\b(accumulate)(\s+)(\")",
73 bygroups(Keyword.Declaration, Text.Whitespace, String.Double),
74 'elpi-string'),
75 (rf"\b(accumulate|namespace|local)(\s+)({constant_re})",
76 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
77 (rf"\b(shorten)(\s+)({constant_re}\.)",
78 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
79 (r"\b(pi|sigma)(\s+)([a-zA-Z][A-Za-z0-9_ ]*)(\\)",
80 bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable, Text)),
81 (rf"\b(constraint)(\s+)(({const_sym_re}(\s+)?)+)",
82 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
83 'elpi-chr-rule-start'),
84
85 (rf"(?=[A-Z_]){constant_re}", Name.Variable),
86 (rf"(?=[a-z_])({constant_re}|_)\\", Name.Variable),
87 (r"_", Name.Variable),
88 (rf"({symbol_re}|!|=>|;)", Keyword.Declaration),
89 (constant_re, Text),
90 (r"\[|\]|\||=>", Keyword.Declaration),
91 (r'"', String.Double, 'elpi-string'),
92 (r'`', String.Double, 'elpi-btick'),
93 (r'\'', String.Double, 'elpi-tick'),
94 (r'\{\{', Punctuation, 'elpi-quote'),
95 (r'\{[^\{]', Text, 'elpi-spill'),
96 (r"\(", Punctuation, 'elpi-in-parens'),
97 (r'\d[\d_]*', Number.Integer),
98 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
99 (r"[\+\*\-/\^\.]", Operator),
100 ],
101 '_elpi-comment': [
102 (r'%[^\n]*\n', Comment),
103 (r'/(?:\\\n)?[*](?:[^*]|[*](?!(?:\\\n)?/))*[*](?:\\\n)?/', Comment),
104 (r"\s+", Text.Whitespace),
105 ],
106 'elpi-indexing-expr':[
107 (r'[0-9 _]+', Number.Integer),
108 (r'\)', Punctuation, '#pop'),
109 ],
110 'elpi-type': [
111 (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
112 (r'->', Keyword.Type),
113 (constant_re, Keyword.Type),
114 (r"\(|\)", Keyword.Type),
115 (r"\.", Text, '#pop'),
116 include('_elpi-comment'),
117 ],
118 'elpi-chr-rule-start': [
119 (r"\{", Punctuation, 'elpi-chr-rule'),
120 include('_elpi-comment'),
121 ],
122 'elpi-chr-rule': [
123 (r"\brule\b", Keyword.Declaration),
124 (r"\\", Keyword.Declaration),
125 (r"\}", Punctuation, '#pop:2'),
126 include('elpi'),
127 ],
128 'elpi-pred-item': [
129 (r"[io]:", Keyword.Mode, 'elpi-ctype'),
130 (r"\.", Text, '#pop'),
131 include('_elpi-comment'),
132 ],
133 'elpi-ctype': [
134 (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
135 (r'->', Keyword.Type),
136 (constant_re, Keyword.Type),
137 (r"\(|\)", Keyword.Type),
138 (r",", Text, '#pop'),
139 (r"\.", Text, '#pop:2'),
140 include('_elpi-comment'),
141 ],
142 'elpi-btick': [
143 (r'[^` ]+', String.Double),
144 (r'`', String.Double, '#pop'),
145 ],
146 'elpi-tick': [
147 (r'[^\' ]+', String.Double),
148 (r'\'', String.Double, '#pop'),
149 ],
150 'elpi-string': [
151 (r'[^\"]+', String.Double),
152 (r'"', String.Double, '#pop'),
153 ],
154 'elpi-quote': [
155 (r'\}\}', Punctuation, '#pop'),
156 (r"\s+", Text.Whitespace),
157 (r"(lp:)(\{\{)", bygroups(Number, Punctuation), 'elpi-quote-exit'),
158 (rf"(lp:)((?=[A-Z_]){constant_re})", bygroups(Number, Name.Variable)),
159 (r"((?!lp:|\}\}).)+", using(CoqLexer)),
160 ],
161 'elpi-quote-exit': [
162 include('elpi'),
163 (r'\}\}', Punctuation, '#pop'),
164 ],
165 'elpi-spill': [
166 (r'\{[^\{]', Text, '#push'),
167 (r'\}[^\}]', Text, '#pop'),
168 include('elpi'),
169 ],
170 'elpi-in-parens': [
171 (r"\(", Punctuation, '#push'),
172 include('elpi'),
173 (r"\)", Punctuation, '#pop'),
174 ],
175 }