ThirdParty/Pygments/pygments/lexers/prolog.py

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

eric ide

mercurial