1"""
2 pygments.lexers.elpi
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for the `Elpi <http://github.com/LPCIC/elpi>`_ programming language.
6
7 :copyright: Copyright 2006-present 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 RocqLexer
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(func)(\s+)({const_sym_re})",
61 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
62 'elpi-func-item'),
63 (rf"\b(external type|type)(\s+)(({const_sym_re}(,\s*)?)+)",
64 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
65 'elpi-type'),
66 (rf"\b(kind)(\s+)(({const_sym_re}|,)+)",
67 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
68 'elpi-type'),
69 (rf"\b(typeabbrev)(\s+)({const_sym_re})",
70 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
71 'elpi-type'),
72 (r"\b(typeabbrev)(\s+)(\([^)]+\))",
73 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
74 'elpi-type'),
75 (r"\b(accumulate)(\s+)(\")",
76 bygroups(Keyword.Declaration, Text.Whitespace, String.Double),
77 'elpi-string'),
78 (rf"\b(accumulate|namespace|local)(\s+)({constant_re})",
79 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
80 (rf"\b(shorten)(\s+)({constant_re}\.)",
81 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
82 (r"\b(pi|sigma)(\s+)([a-zA-Z][A-Za-z0-9_ ]*)(\\)",
83 bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable, Text)),
84 (rf"\b(constraint)(\s+)(({const_sym_re}(\s+)?)+)",
85 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
86 'elpi-chr-rule-start'),
87
88 (rf"(?=[A-Z_]){constant_re}", Name.Variable),
89 (rf"(?=[a-z_])({constant_re}|_)\\", Name.Variable),
90 (r"_", Name.Variable),
91 (rf"({symbol_re}|!|=>|;)", Keyword.Declaration),
92 (constant_re, Text),
93 (r"\[|\]|\||=>", Keyword.Declaration),
94 (r'"', String.Double, 'elpi-string'),
95 (r'`', String.Double, 'elpi-btick'),
96 (r'\'', String.Double, 'elpi-tick'),
97 (r'\{\{', Punctuation, 'elpi-quote'),
98 (r'\{[^\{]', Text, 'elpi-spill'),
99 (r"\(", Punctuation, 'elpi-in-parens'),
100 (r'\d[\d_]*', Number.Integer),
101 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
102 (r"[\+\*\-/\^\.]", Operator),
103 ],
104 '_elpi-comment': [
105 (r'%[^\n]*\n', Comment),
106 (r'/(?:\\\n)?[*](?:[^*]|[*](?!(?:\\\n)?/))*[*](?:\\\n)?/', Comment),
107 (r"\s+", Text.Whitespace),
108 ],
109 'elpi-indexing-expr':[
110 (r'[0-9 _]+', Number.Integer),
111 (r'\)', Punctuation, '#pop'),
112 ],
113 'elpi-type': [
114 (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
115 (r'->', Keyword.Type),
116 (r'prop', Keyword.Mode),
117 (constant_re, Keyword.Type),
118 (r"\(|\)", Keyword.Type),
119 (r"\.", Text, '#pop'),
120 include('_elpi-comment'),
121 ],
122 'elpi-chr-rule-start': [
123 (r"\{", Punctuation, 'elpi-chr-rule'),
124 include('_elpi-comment'),
125 ],
126 'elpi-chr-rule': [
127 (r"\brule\b", Keyword.Declaration),
128 (r"\\", Keyword.Declaration),
129 (r"\}", Punctuation, '#pop:2'),
130 include('elpi'),
131 ],
132 'elpi-pred-item': [
133 (r"[io]:", Keyword.Mode),
134 (r"\.", Text, '#pop'),
135 (r",", Keyword.Mode),
136 include('_elpi-inner-pred-fun'),
137 (r"\)", Keyword.Mode, '#pop'),
138 (r"\(", Keyword.Type, '_elpi-type-item'),
139 include('_elpi-comment'),
140 include('_elpi-type-item'),
141 ],
142 'elpi-func-item': [
143 (constant_re, Keyword.Type),
144 (r"\.", Text, '#pop'),
145 (r",", Keyword.Mode),
146 (r'->', Keyword.Mode),
147 include('_elpi-inner-pred-fun'),
148 (r"\)", Keyword.Mode, '#pop'),
149 (r"\(", Keyword.Type, '_elpi-type-item'),
150 include('_elpi-comment'),
151 include('_elpi-type-item'),
152 ],
153 '_elpi-inner-pred-fun': [
154 (r"(\()(\s*)(pred)", bygroups(Keyword.Mode,Text.Whitespace,Keyword.Declaration), 'elpi-pred-item'),
155 (r"(\()(\s*)(func)", bygroups(Keyword.Mode,Text.Whitespace,Keyword.Declaration), 'elpi-func-item'),
156 ],
157 '_elpi-type-item': [
158 (r'->', Keyword.Type),
159 (constant_re, Keyword.Type),
160 include('_elpi-inner-pred-fun'),
161 (r"\(", Keyword.Type, '#push'),
162 (r"\)", Keyword.Type, '#pop'),
163 include('_elpi-comment'),
164 ],
165
166 ''
167
168 'elpi-btick': [
169 (r'[^` ]+', String.Double),
170 (r'`', String.Double, '#pop'),
171 ],
172 'elpi-tick': [
173 (r'[^\' ]+', String.Double),
174 (r'\'', String.Double, '#pop'),
175 ],
176 'elpi-string': [
177 (r'[^\"]+', String.Double),
178 (r'"', String.Double, '#pop'),
179 ],
180 'elpi-quote': [
181 (r'\}\}', Punctuation, '#pop'),
182 (r"\s+", Text.Whitespace),
183 (r"(lp:)(\{\{)", bygroups(Number, Punctuation), 'elpi-quote-exit'),
184 (rf"(lp:)((?=[A-Z_]){constant_re})", bygroups(Number, Name.Variable)),
185 (r"((?!lp:|\}\}).)+", using(RocqLexer)),
186 ],
187 'elpi-quote-exit': [
188 include('elpi'),
189 (r'\}\}', Punctuation, '#pop'),
190 ],
191 'elpi-spill': [
192 (r'\{[^\{]', Text, '#push'),
193 (r'\}[^\}]', Text, '#pop'),
194 include('elpi'),
195 ],
196 'elpi-in-parens': [
197 (r"\(", Punctuation, '#push'),
198 include('elpi'),
199 (r"\)", Punctuation, '#pop'),
200 ],
201 }