eric6/ThirdParty/Pygments/pygments/lexers/prolog.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.prolog
4 ~~~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for Prolog and Prolog-like languages.
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import re
13
14 from pygments.lexer import RegexLexer, bygroups
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
16 Number, Punctuation
17
18 __all__ = ['PrologLexer', 'LogtalkLexer']
19
20
21 class PrologLexer(RegexLexer):
22 """
23 Lexer for Prolog files.
24 """
25 name = 'Prolog'
26 aliases = ['prolog']
27 filenames = ['*.ecl', '*.prolog', '*.pro', '*.pl']
28 mimetypes = ['text/x-prolog']
29
30 flags = re.UNICODE | re.MULTILINE
31
32 tokens = {
33 'root': [
34 (r'/\*', Comment.Multiline, 'nested-comment'),
35 (r'%.*', Comment.Single),
36 # character literal
37 (r'0\'.', String.Char),
38 (r'0b[01]+', Number.Bin),
39 (r'0o[0-7]+', Number.Oct),
40 (r'0x[0-9a-fA-F]+', Number.Hex),
41 # literal with prepended base
42 (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer),
43 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
44 (r'\d+', Number.Integer),
45 (r'[\[\](){}|.,;!]', Punctuation),
46 (r':-|-->', Punctuation),
47 (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
48 r'\\[0-7]+\\|\\["\nabcefnrstv]|[^\\"])*"', String.Double),
49 (r"'(?:''|[^'])*'", String.Atom), # quoted atom
50 # Needs to not be followed by an atom.
51 # (r'=(?=\s|[a-zA-Z\[])', Operator),
52 (r'is\b', Operator),
53 (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])',
54 Operator),
55 (r'(mod|div|not)\b', Operator),
56 (r'_', Keyword), # The don't-care variable
57 (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)),
58 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
59 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
60 r'(\s*)(:-|-->)',
61 bygroups(Name.Function, Text, Operator)), # function defn
62 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
63 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
64 r'(\s*)(\()',
65 bygroups(Name.Function, Text, Punctuation)),
66 (r'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
67 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*',
68 String.Atom), # atom, characters
69 # This one includes !
70 (r'[#&*+\-./:<=>?@\\^~\u00a1-\u00bf\u2010-\u303f]+',
71 String.Atom), # atom, graphics
72 (r'[A-Z_]\w*', Name.Variable),
73 (r'\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text),
74 ],
75 'nested-comment': [
76 (r'\*/', Comment.Multiline, '#pop'),
77 (r'/\*', Comment.Multiline, '#push'),
78 (r'[^*/]+', Comment.Multiline),
79 (r'[*/]', Comment.Multiline),
80 ],
81 }
82
83 def analyse_text(text):
84 return ':-' in text
85
86
87 class LogtalkLexer(RegexLexer):
88 """
89 For `Logtalk <http://logtalk.org/>`_ source code.
90
91 .. versionadded:: 0.10
92 """
93
94 name = 'Logtalk'
95 aliases = ['logtalk']
96 filenames = ['*.lgt', '*.logtalk']
97 mimetypes = ['text/x-logtalk']
98
99 tokens = {
100 'root': [
101 # Directives
102 (r'^\s*:-\s', Punctuation, 'directive'),
103 # Comments
104 (r'%.*?\n', Comment),
105 (r'/\*(.|\n)*?\*/', Comment),
106 # Whitespace
107 (r'\n', Text),
108 (r'\s+', Text),
109 # Numbers
110 (r"0'[\\]?.", Number),
111 (r'0b[01]+', Number.Bin),
112 (r'0o[0-7]+', Number.Oct),
113 (r'0x[0-9a-fA-F]+', Number.Hex),
114 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),
115 # Variables
116 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),
117 # Event handlers
118 (r'(after|before)(?=[(])', Keyword),
119 # Message forwarding handler
120 (r'forward(?=[(])', Keyword),
121 # Execution-context methods
122 (r'(context|parameter|this|se(lf|nder))(?=[(])', Keyword),
123 # Reflection
124 (r'(current_predicate|predicate_property)(?=[(])', Keyword),
125 # DCGs and term expansion
126 (r'(expand_(goal|term)|(goal|term)_expansion|phrase)(?=[(])', Keyword),
127 # Entity
128 (r'(abolish|c(reate|urrent))_(object|protocol|category)(?=[(])', Keyword),
129 (r'(object|protocol|category)_property(?=[(])', Keyword),
130 # Entity relations
131 (r'co(mplements_object|nforms_to_protocol)(?=[(])', Keyword),
132 (r'extends_(object|protocol|category)(?=[(])', Keyword),
133 (r'imp(lements_protocol|orts_category)(?=[(])', Keyword),
134 (r'(instantiat|specializ)es_class(?=[(])', Keyword),
135 # Events
136 (r'(current_event|(abolish|define)_events)(?=[(])', Keyword),
137 # Flags
138 (r'(create|current|set)_logtalk_flag(?=[(])', Keyword),
139 # Compiling, loading, and library paths
140 (r'logtalk_(compile|l(ibrary_path|oad|oad_context)|make(_target_action)?)(?=[(])', Keyword),
141 (r'\blogtalk_make\b', Keyword),
142 # Database
143 (r'(clause|retract(all)?)(?=[(])', Keyword),
144 (r'a(bolish|ssert(a|z))(?=[(])', Keyword),
145 # Control constructs
146 (r'(ca(ll|tch)|throw)(?=[(])', Keyword),
147 (r'(fa(il|lse)|true|(instantiation|system)_error)\b', Keyword),
148 (r'(type|domain|existence|permission|representation|evaluation|resource|syntax)_error(?=[(])', Keyword),
149 # All solutions
150 (r'((bag|set)of|f(ind|or)all)(?=[(])', Keyword),
151 # Multi-threading predicates
152 (r'threaded(_(ca(ll|ncel)|once|ignore|exit|peek|wait|notify))?(?=[(])', Keyword),
153 # Engine predicates
154 (r'threaded_engine(_(create|destroy|self|next|next_reified|yield|post|fetch))?(?=[(])', Keyword),
155 # Term unification
156 (r'(subsumes_term|unify_with_occurs_check)(?=[(])', Keyword),
157 # Term creation and decomposition
158 (r'(functor|arg|copy_term|numbervars|term_variables)(?=[(])', Keyword),
159 # Evaluable functors
160 (r'(div|rem|m(ax|in|od)|abs|sign)(?=[(])', Keyword),
161 (r'float(_(integer|fractional)_part)?(?=[(])', Keyword),
162 (r'(floor|t(an|runcate)|round|ceiling)(?=[(])', Keyword),
163 # Other arithmetic functors
164 (r'(cos|a(cos|sin|tan|tan2)|exp|log|s(in|qrt)|xor)(?=[(])', Keyword),
165 # Term testing
166 (r'(var|atom(ic)?|integer|float|c(allable|ompound)|n(onvar|umber)|ground|acyclic_term)(?=[(])', Keyword),
167 # Term comparison
168 (r'compare(?=[(])', Keyword),
169 # Stream selection and control
170 (r'(curren|se)t_(in|out)put(?=[(])', Keyword),
171 (r'(open|close)(?=[(])', Keyword),
172 (r'flush_output(?=[(])', Keyword),
173 (r'(at_end_of_stream|flush_output)\b', Keyword),
174 (r'(stream_property|at_end_of_stream|set_stream_position)(?=[(])', Keyword),
175 # Character and byte input/output
176 (r'(nl|(get|peek|put)_(byte|c(har|ode)))(?=[(])', Keyword),
177 (r'\bnl\b', Keyword),
178 # Term input/output
179 (r'read(_term)?(?=[(])', Keyword),
180 (r'write(q|_(canonical|term))?(?=[(])', Keyword),
181 (r'(current_)?op(?=[(])', Keyword),
182 (r'(current_)?char_conversion(?=[(])', Keyword),
183 # Atomic term processing
184 (r'atom_(length|c(hars|o(ncat|des)))(?=[(])', Keyword),
185 (r'(char_code|sub_atom)(?=[(])', Keyword),
186 (r'number_c(har|ode)s(?=[(])', Keyword),
187 # Implementation defined hooks functions
188 (r'(se|curren)t_prolog_flag(?=[(])', Keyword),
189 (r'\bhalt\b', Keyword),
190 (r'halt(?=[(])', Keyword),
191 # Message sending operators
192 (r'(::|:|\^\^)', Operator),
193 # External call
194 (r'[{}]', Keyword),
195 # Logic and control
196 (r'(ignore|once)(?=[(])', Keyword),
197 (r'\brepeat\b', Keyword),
198 # Sorting
199 (r'(key)?sort(?=[(])', Keyword),
200 # Bitwise functors
201 (r'(>>|<<|/\\|\\\\|\\)', Operator),
202 # Predicate aliases
203 (r'\bas\b', Operator),
204 # Arithemtic evaluation
205 (r'\bis\b', Keyword),
206 # Arithemtic comparison
207 (r'(=:=|=\\=|<|=<|>=|>)', Operator),
208 # Term creation and decomposition
209 (r'=\.\.', Operator),
210 # Term unification
211 (r'(=|\\=)', Operator),
212 # Term comparison
213 (r'(==|\\==|@=<|@<|@>=|@>)', Operator),
214 # Evaluable functors
215 (r'(//|[-+*/])', Operator),
216 (r'\b(e|pi|div|mod|rem)\b', Operator),
217 # Other arithemtic functors
218 (r'\b\*\*\b', Operator),
219 # DCG rules
220 (r'-->', Operator),
221 # Control constructs
222 (r'([!;]|->)', Operator),
223 # Logic and control
224 (r'\\+', Operator),
225 # Mode operators
226 (r'[?@]', Operator),
227 # Existential quantifier
228 (r'\^', Operator),
229 # Strings
230 (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
231 # Punctuation
232 (r'[()\[\],.|]', Text),
233 # Atoms
234 (r"[a-z][a-zA-Z0-9_]*", Text),
235 (r"'", String, 'quoted_atom'),
236 ],
237
238 'quoted_atom': [
239 (r"''", String),
240 (r"'", String, '#pop'),
241 (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape),
242 (r"[^\\'\n]+", String),
243 (r'\\', String),
244 ],
245
246 'directive': [
247 # Conditional compilation directives
248 (r'(el)?if(?=[(])', Keyword, 'root'),
249 (r'(e(lse|ndif))(?=[.])', Keyword, 'root'),
250 # Entity directives
251 (r'(category|object|protocol)(?=[(])', Keyword, 'entityrelations'),
252 (r'(end_(category|object|protocol))(?=[.])', Keyword, 'root'),
253 # Predicate scope directives
254 (r'(public|protected|private)(?=[(])', Keyword, 'root'),
255 # Other directives
256 (r'e(n(coding|sure_loaded)|xport)(?=[(])', Keyword, 'root'),
257 (r'in(clude|itialization|fo)(?=[(])', Keyword, 'root'),
258 (r'(built_in|dynamic|synchronized|threaded)(?=[.])', Keyword, 'root'),
259 (r'(alias|d(ynamic|iscontiguous)|m(eta_(non_terminal|predicate)|ode|ultifile)|s(et_(logtalk|prolog)_flag|ynchronized))(?=[(])', Keyword, 'root'),
260 (r'op(?=[(])', Keyword, 'root'),
261 (r'(c(alls|oinductive)|module|reexport|use(s|_module))(?=[(])', Keyword, 'root'),
262 (r'[a-z][a-zA-Z0-9_]*(?=[(])', Text, 'root'),
263 (r'[a-z][a-zA-Z0-9_]*(?=[.])', Text, 'root'),
264 ],
265
266 'entityrelations': [
267 (r'(complements|extends|i(nstantiates|mp(lements|orts))|specializes)(?=[(])', Keyword),
268 # Numbers
269 (r"0'[\\]?.", Number),
270 (r'0b[01]+', Number.Bin),
271 (r'0o[0-7]+', Number.Oct),
272 (r'0x[0-9a-fA-F]+', Number.Hex),
273 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),
274 # Variables
275 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),
276 # Atoms
277 (r"[a-z][a-zA-Z0-9_]*", Text),
278 (r"'", String, 'quoted_atom'),
279 # Strings
280 (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
281 # End of entity-opening directive
282 (r'([)]\.)', Text, 'root'),
283 # Scope operator
284 (r'(::)', Operator),
285 # Punctuation
286 (r'[()\[\],.|]', Text),
287 # Comments
288 (r'%.*?\n', Comment),
289 (r'/\*(.|\n)*?\*/', Comment),
290 # Whitespace
291 (r'\n', Text),
292 (r'\s+', Text),
293 ]
294 }
295
296 def analyse_text(text):
297 if ':- object(' in text:
298 return 1.0
299 elif ':- protocol(' in text:
300 return 1.0
301 elif ':- category(' in text:
302 return 1.0
303 elif re.search(r'^:-\s[a-z]', text, re.M):
304 return 0.9
305 else:
306 return 0.0

eric ide

mercurial