Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/elpi.py: 100%
22 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
1"""
2 pygments.lexers.elpi
3 ~~~~~~~~~~~~~~~~~~~~
5 Lexer for the `Elpi <http://github.com/LPCIC/elpi>`_ programming language.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer, bygroups, include
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation
15__all__ = ['ElpiLexer']
18class ElpiLexer(RegexLexer):
19 """
20 Lexer for the Elpi programming language.
22 .. versionadded:: 2.11
23 """
25 name = 'Elpi'
26 url = 'http://github.com/LPCIC/elpi'
27 aliases = ['elpi']
28 filenames = ['*.elpi']
29 mimetypes = ['text/x-elpi']
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 = r"({}|-|\$|_)".format(schar2_re)
36 idchar_re = r"({}|{}|{}|{})".format(lcase_re,ucase_re,digit_re,schar_re)
37 idcharstarns_re = r"({}*(\.({}|{}){}*)*)".format(idchar_re, lcase_re, ucase_re, idchar_re)
38 symbchar_re = r"({}|{}|{}|{}|:)".format(lcase_re, ucase_re, digit_re, schar_re)
39 constant_re = r"({}{}*|{}{}|{}{}*|_{}+)".format(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 = r"\(({}|{})\)".format(constant_re,symbol_re)
42 const_sym_re = r"({}|{}|{})".format(constant_re,symbol_re,escape_re)
44 tokens = {
45 'root': [
46 include('elpi')
47 ],
49 'elpi': [
50 include('_elpi-comment'),
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),
56 'elpi-indexing-expr'),
57 (r"\b(external pred|pred)(\s+)({})".format(const_sym_re),
58 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
59 'elpi-pred-item'),
60 (r"\b(external type|type)(\s+)(({}(,\s*)?)+)".format(const_sym_re),
61 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
62 'elpi-type'),
63 (r"\b(kind)(\s+)(({}|,)+)".format(const_sym_re),
64 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
65 'elpi-type'),
66 (r"\b(typeabbrev)(\s+)({})".format(const_sym_re),
67 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
68 'elpi-type'),
69 (r"\b(accumulate)(\s+)(\")",
70 bygroups(Keyword.Declaration, Text.Whitespace, String.Double),
71 'elpi-string'),
72 (r"\b(accumulate|namespace|local)(\s+)({})".format(constant_re),
73 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
74 (r"\b(shorten)(\s+)({}\.)".format(constant_re),
75 bygroups(Keyword.Declaration, Text.Whitespace, Text)),
76 (r"\b(pi|sigma)(\s+)([a-zA-Z][A-Za-z0-9_ ]*)(\\)",
77 bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable, Text)),
78 (r"\b(constraint)(\s+)(({}(\s+)?)+)".format(const_sym_re),
79 bygroups(Keyword.Declaration, Text.Whitespace, Name.Function),
80 'elpi-chr-rule-start'),
82 (r"(?=[A-Z_]){}".format(constant_re), Name.Variable),
83 (r"(?=[a-z_]){}\\".format(constant_re), Name.Variable),
84 (r"_", Name.Variable),
85 (r"({}|!|=>|;)".format(symbol_re), Keyword.Declaration),
86 (constant_re, Text),
87 (r"\[|\]|\||=>", Keyword.Declaration),
88 (r'"', String.Double, 'elpi-string'),
89 (r'`', String.Double, 'elpi-btick'),
90 (r'\'', String.Double, 'elpi-tick'),
91 (r'\{\{', Punctuation, 'elpi-quote'),
92 (r'\{[^\{]', Text, 'elpi-spill'),
93 (r"\(", Text, 'elpi-in-parens'),
94 (r'\d[\d_]*', Number.Integer),
95 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
96 (r"[\+\*\-/\^\.]", Operator),
97 ],
98 '_elpi-comment': [
99 (r'%[^\n]*\n', Comment),
100 (r'/\*', Comment, 'elpi-multiline-comment'),
101 (r"\s+", Text.Whitespace),
102 ],
103 'elpi-multiline-comment': [
104 (r'\*/', Comment, '#pop'),
105 (r'.', Comment)
106 ],
107 'elpi-indexing-expr':[
108 (r'[0-9 _]+', Number.Integer),
109 (r'\)', Text, '#pop'),
110 ],
111 'elpi-type': [
112 (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
113 (r'->', Keyword.Type),
114 (constant_re, Keyword.Type),
115 (r"\(|\)", Keyword.Type),
116 (r"\.", Text, '#pop'),
117 include('_elpi-comment'),
118 ],
119 'elpi-chr-rule-start': [
120 (r"\{", Text, 'elpi-chr-rule'),
121 include('_elpi-comment'),
122 ],
123 'elpi-chr-rule': [
124 (r"\brule\b", Keyword.Declaration),
125 (r"\\", Keyword.Declaration),
126 (r"\}", Text, '#pop:2'),
127 include('elpi'),
128 ],
129 'elpi-pred-item': [
130 (r"[io]:", Keyword.Mode, 'elpi-ctype'),
131 (r"\.", Text, '#pop'),
132 include('_elpi-comment'),
133 ],
134 'elpi-ctype': [
135 (r"(ctype\s+)(\")", bygroups(Keyword.Type, String.Double), 'elpi-string'),
136 (r'->', Keyword.Type),
137 (constant_re, Keyword.Type),
138 (r"\(|\)", Keyword.Type),
139 (r",", Text, '#pop'),
140 (r"\.", Text, '#pop:2'),
141 include('_elpi-comment'),
142 ],
143 'elpi-btick': [
144 (r'[^` ]+', String.Double),
145 (r'`', String.Double, '#pop'),
146 ],
147 'elpi-tick': [
148 (r'[^\' ]+', String.Double),
149 (r'\'', String.Double, '#pop'),
150 ],
151 'elpi-string': [
152 (r'[^\"]+', String.Double),
153 (r'"', String.Double, '#pop'),
154 ],
155 'elpi-quote': [
156 (r'\{\{', Punctuation, '#push'),
157 (r'\}\}', Punctuation, '#pop'),
158 (r"(lp:)((?=[A-Z_]){})".format(constant_re), bygroups(Keyword, Name.Variable)),
159 (r"[^l\}]+", Text),
160 (r"l|\}", Text),
161 ],
162 'elpi-spill': [
163 (r'\{[^\{]', Text, '#push'),
164 (r'\}[^\}]', Text, '#pop'),
165 include('elpi'),
166 ],
167 'elpi-in-parens': [
168 (r"\(", Operator, '#push'),
169 (r"\)", Operator, '#pop'),
170 include('elpi'),
171 ],
173 }