eric6/ThirdParty/Pygments/pygments/lexers/theorem.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.theorem
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 Lexers for theorem-proving 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, default, words
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
16 Number, Punctuation, Generic
17
18 __all__ = ['CoqLexer', 'IsabelleLexer', 'LeanLexer']
19
20
21 class CoqLexer(RegexLexer):
22 """
23 For the `Coq <http://coq.inria.fr/>`_ theorem prover.
24
25 .. versionadded:: 1.5
26 """
27
28 name = 'Coq'
29 aliases = ['coq']
30 filenames = ['*.v']
31 mimetypes = ['text/x-coq']
32
33 flags = re.UNICODE
34
35 keywords1 = (
36 # Vernacular commands
37 'Section', 'Module', 'End', 'Require', 'Import', 'Export', 'Variable',
38 'Variables', 'Parameter', 'Parameters', 'Axiom', 'Hypothesis',
39 'Hypotheses', 'Notation', 'Local', 'Tactic', 'Reserved', 'Scope',
40 'Open', 'Close', 'Bind', 'Delimit', 'Definition', 'Let', 'Ltac',
41 'Fixpoint', 'CoFixpoint', 'Morphism', 'Relation', 'Implicit',
42 'Arguments', 'Set', 'Unset', 'Contextual', 'Strict', 'Prenex',
43 'Implicits', 'Inductive', 'CoInductive', 'Record', 'Structure',
44 'Canonical', 'Coercion', 'Theorem', 'Lemma', 'Corollary',
45 'Proposition', 'Fact', 'Remark', 'Example', 'Proof', 'Goal', 'Save',
46 'Qed', 'Defined', 'Hint', 'Resolve', 'Rewrite', 'View', 'Search',
47 'Show', 'Print', 'Printing', 'All', 'Graph', 'Projections', 'inside',
48 'outside', 'Check', 'Global', 'Instance', 'Class', 'Existing',
49 'Universe', 'Polymorphic', 'Monomorphic', 'Context'
50 )
51 keywords2 = (
52 # Gallina
53 'forall', 'exists', 'exists2', 'fun', 'fix', 'cofix', 'struct',
54 'match', 'end', 'in', 'return', 'let', 'if', 'is', 'then', 'else',
55 'for', 'of', 'nosimpl', 'with', 'as',
56 )
57 keywords3 = (
58 # Sorts
59 'Type', 'Prop',
60 )
61 keywords4 = (
62 # Tactics
63 'pose', 'set', 'move', 'case', 'elim', 'apply', 'clear', 'hnf', 'intro',
64 'intros', 'generalize', 'rename', 'pattern', 'after', 'destruct',
65 'induction', 'using', 'refine', 'inversion', 'injection', 'rewrite',
66 'congr', 'unlock', 'compute', 'ring', 'field', 'replace', 'fold',
67 'unfold', 'change', 'cutrewrite', 'simpl', 'have', 'suff', 'wlog',
68 'suffices', 'without', 'loss', 'nat_norm', 'assert', 'cut', 'trivial',
69 'revert', 'bool_congr', 'nat_congr', 'symmetry', 'transitivity', 'auto',
70 'split', 'left', 'right', 'autorewrite', 'tauto', 'setoid_rewrite',
71 'intuition', 'eauto', 'eapply', 'econstructor', 'etransitivity',
72 'constructor', 'erewrite', 'red', 'cbv', 'lazy', 'vm_compute',
73 'native_compute', 'subst',
74 )
75 keywords5 = (
76 # Terminators
77 'by', 'done', 'exact', 'reflexivity', 'tauto', 'romega', 'omega',
78 'assumption', 'solve', 'contradiction', 'discriminate',
79 'congruence',
80 )
81 keywords6 = (
82 # Control
83 'do', 'last', 'first', 'try', 'idtac', 'repeat',
84 )
85 # 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done',
86 # 'downto', 'else', 'end', 'exception', 'external', 'false',
87 # 'for', 'fun', 'function', 'functor', 'if', 'in', 'include',
88 # 'inherit', 'initializer', 'lazy', 'let', 'match', 'method',
89 # 'module', 'mutable', 'new', 'object', 'of', 'open', 'private',
90 # 'raise', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try',
91 # 'type', 'val', 'virtual', 'when', 'while', 'with'
92 keyopts = (
93 '!=', '#', '&', '&&', r'\(', r'\)', r'\*', r'\+', ',', '-', r'-\.',
94 '->', r'\.', r'\.\.', ':', '::', ':=', ':>', ';', ';;', '<', '<-',
95 '<->', '=', '>', '>]', r'>\}', r'\?', r'\?\?', r'\[', r'\[<', r'\[>',
96 r'\[\|', ']', '_', '`', r'\{', r'\{<', r'\|', r'\|]', r'\}', '~', '=>',
97 r'/\\', r'\\/', r'\{\|', r'\|\}',
98 'Π', 'λ',
99 )
100 operators = r'[!$%&*+\./:<=>?@^|~-]'
101 prefix_syms = r'[!?~]'
102 infix_syms = r'[=<>@^|&+\*/$%-]'
103
104 tokens = {
105 'root': [
106 (r'\s+', Text),
107 (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo),
108 (r'\(\*', Comment, 'comment'),
109 (words(keywords1, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
110 (words(keywords2, prefix=r'\b', suffix=r'\b'), Keyword),
111 (words(keywords3, prefix=r'\b', suffix=r'\b'), Keyword.Type),
112 (words(keywords4, prefix=r'\b', suffix=r'\b'), Keyword),
113 (words(keywords5, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
114 (words(keywords6, prefix=r'\b', suffix=r'\b'), Keyword.Reserved),
115 # (r'\b([A-Z][\w\']*)(\.)', Name.Namespace, 'dotted'),
116 (r'\b([A-Z][\w\']*)', Name),
117 (r'(%s)' % '|'.join(keyopts[::-1]), Operator),
118 (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator),
119
120 (r"[^\W\d][\w']*", Name),
121
122 (r'\d[\d_]*', Number.Integer),
123 (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
124 (r'0[oO][0-7][0-7_]*', Number.Oct),
125 (r'0[bB][01][01_]*', Number.Bin),
126 (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
127
128 (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", String.Char),
129
130 (r"'.'", String.Char),
131 (r"'", Keyword), # a stray quote is another syntax element
132
133 (r'"', String.Double, 'string'),
134
135 (r'[~?][a-z][\w\']*:', Name),
136 (r'\S', Name.Builtin.Pseudo),
137 ],
138 'comment': [
139 (r'[^(*)]+', Comment),
140 (r'\(\*', Comment, '#push'),
141 (r'\*\)', Comment, '#pop'),
142 (r'[(*)]', Comment),
143 ],
144 'string': [
145 (r'[^"]+', String.Double),
146 (r'""', String.Double),
147 (r'"', String.Double, '#pop'),
148 ],
149 'dotted': [
150 (r'\s+', Text),
151 (r'\.', Punctuation),
152 (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace),
153 (r'[A-Z][\w\']*', Name.Class, '#pop'),
154 (r'[a-z][a-z0-9_\']*', Name, '#pop'),
155 default('#pop')
156 ],
157 }
158
159 def analyse_text(text):
160 if 'Qed' in text and 'Proof' in text:
161 return 1
162
163
164 class IsabelleLexer(RegexLexer):
165 """
166 For the `Isabelle <http://isabelle.in.tum.de/>`_ proof assistant.
167
168 .. versionadded:: 2.0
169 """
170
171 name = 'Isabelle'
172 aliases = ['isabelle']
173 filenames = ['*.thy']
174 mimetypes = ['text/x-isabelle']
175
176 keyword_minor = (
177 'and', 'assumes', 'attach', 'avoids', 'binder', 'checking',
178 'class_instance', 'class_relation', 'code_module', 'congs',
179 'constant', 'constrains', 'datatypes', 'defines', 'file', 'fixes',
180 'for', 'functions', 'hints', 'identifier', 'if', 'imports', 'in',
181 'includes', 'infix', 'infixl', 'infixr', 'is', 'keywords', 'lazy',
182 'module_name', 'monos', 'morphisms', 'no_discs_sels', 'notes',
183 'obtains', 'open', 'output', 'overloaded', 'parametric', 'permissive',
184 'pervasive', 'rep_compat', 'shows', 'structure', 'type_class',
185 'type_constructor', 'unchecked', 'unsafe', 'where',
186 )
187
188 keyword_diag = (
189 'ML_command', 'ML_val', 'class_deps', 'code_deps', 'code_thms',
190 'display_drafts', 'find_consts', 'find_theorems', 'find_unused_assms',
191 'full_prf', 'help', 'locale_deps', 'nitpick', 'pr', 'prf',
192 'print_abbrevs', 'print_antiquotations', 'print_attributes',
193 'print_binds', 'print_bnfs', 'print_bundles',
194 'print_case_translations', 'print_cases', 'print_claset',
195 'print_classes', 'print_codeproc', 'print_codesetup',
196 'print_coercions', 'print_commands', 'print_context',
197 'print_defn_rules', 'print_dependencies', 'print_facts',
198 'print_induct_rules', 'print_inductives', 'print_interps',
199 'print_locale', 'print_locales', 'print_methods', 'print_options',
200 'print_orders', 'print_quot_maps', 'print_quotconsts',
201 'print_quotients', 'print_quotientsQ3', 'print_quotmapsQ3',
202 'print_rules', 'print_simpset', 'print_state', 'print_statement',
203 'print_syntax', 'print_theorems', 'print_theory', 'print_trans_rules',
204 'prop', 'pwd', 'quickcheck', 'refute', 'sledgehammer', 'smt_status',
205 'solve_direct', 'spark_status', 'term', 'thm', 'thm_deps', 'thy_deps',
206 'try', 'try0', 'typ', 'unused_thms', 'value', 'values', 'welcome',
207 'print_ML_antiquotations', 'print_term_bindings', 'values_prolog',
208 )
209
210 keyword_thy = ('theory', 'begin', 'end')
211
212 keyword_section = ('header', 'chapter')
213
214 keyword_subsection = (
215 'section', 'subsection', 'subsubsection', 'sect', 'subsect',
216 'subsubsect',
217 )
218
219 keyword_theory_decl = (
220 'ML', 'ML_file', 'abbreviation', 'adhoc_overloading', 'arities',
221 'atom_decl', 'attribute_setup', 'axiomatization', 'bundle',
222 'case_of_simps', 'class', 'classes', 'classrel', 'codatatype',
223 'code_abort', 'code_class', 'code_const', 'code_datatype',
224 'code_identifier', 'code_include', 'code_instance', 'code_modulename',
225 'code_monad', 'code_printing', 'code_reflect', 'code_reserved',
226 'code_type', 'coinductive', 'coinductive_set', 'consts', 'context',
227 'datatype', 'datatype_new', 'datatype_new_compat', 'declaration',
228 'declare', 'default_sort', 'defer_recdef', 'definition', 'defs',
229 'domain', 'domain_isomorphism', 'domaindef', 'equivariance',
230 'export_code', 'extract', 'extract_type', 'fixrec', 'fun',
231 'fun_cases', 'hide_class', 'hide_const', 'hide_fact', 'hide_type',
232 'import_const_map', 'import_file', 'import_tptp', 'import_type_map',
233 'inductive', 'inductive_set', 'instantiation', 'judgment', 'lemmas',
234 'lifting_forget', 'lifting_update', 'local_setup', 'locale',
235 'method_setup', 'nitpick_params', 'no_adhoc_overloading',
236 'no_notation', 'no_syntax', 'no_translations', 'no_type_notation',
237 'nominal_datatype', 'nonterminal', 'notation', 'notepad', 'oracle',
238 'overloading', 'parse_ast_translation', 'parse_translation',
239 'partial_function', 'primcorec', 'primrec', 'primrec_new',
240 'print_ast_translation', 'print_translation', 'quickcheck_generator',
241 'quickcheck_params', 'realizability', 'realizers', 'recdef', 'record',
242 'refute_params', 'setup', 'setup_lifting', 'simproc_setup',
243 'simps_of_case', 'sledgehammer_params', 'spark_end', 'spark_open',
244 'spark_open_siv', 'spark_open_vcg', 'spark_proof_functions',
245 'spark_types', 'statespace', 'syntax', 'syntax_declaration', 'text',
246 'text_raw', 'theorems', 'translations', 'type_notation',
247 'type_synonym', 'typed_print_translation', 'typedecl', 'hoarestate',
248 'install_C_file', 'install_C_types', 'wpc_setup', 'c_defs', 'c_types',
249 'memsafe', 'SML_export', 'SML_file', 'SML_import', 'approximate',
250 'bnf_axiomatization', 'cartouche', 'datatype_compat',
251 'free_constructors', 'functor', 'nominal_function',
252 'nominal_termination', 'permanent_interpretation',
253 'binds', 'defining', 'smt2_status', 'term_cartouche',
254 'boogie_file', 'text_cartouche',
255 )
256
257 keyword_theory_script = ('inductive_cases', 'inductive_simps')
258
259 keyword_theory_goal = (
260 'ax_specification', 'bnf', 'code_pred', 'corollary', 'cpodef',
261 'crunch', 'crunch_ignore',
262 'enriched_type', 'function', 'instance', 'interpretation', 'lemma',
263 'lift_definition', 'nominal_inductive', 'nominal_inductive2',
264 'nominal_primrec', 'pcpodef', 'primcorecursive',
265 'quotient_definition', 'quotient_type', 'recdef_tc', 'rep_datatype',
266 'schematic_corollary', 'schematic_lemma', 'schematic_theorem',
267 'spark_vc', 'specification', 'subclass', 'sublocale', 'termination',
268 'theorem', 'typedef', 'wrap_free_constructors',
269 )
270
271 keyword_qed = ('by', 'done', 'qed')
272 keyword_abandon_proof = ('sorry', 'oops')
273
274 keyword_proof_goal = ('have', 'hence', 'interpret')
275
276 keyword_proof_block = ('next', 'proof')
277
278 keyword_proof_chain = (
279 'finally', 'from', 'then', 'ultimately', 'with',
280 )
281
282 keyword_proof_decl = (
283 'ML_prf', 'also', 'include', 'including', 'let', 'moreover', 'note',
284 'txt', 'txt_raw', 'unfolding', 'using', 'write',
285 )
286
287 keyword_proof_asm = ('assume', 'case', 'def', 'fix', 'presume')
288
289 keyword_proof_asm_goal = ('guess', 'obtain', 'show', 'thus')
290
291 keyword_proof_script = (
292 'apply', 'apply_end', 'apply_trace', 'back', 'defer', 'prefer',
293 )
294
295 operators = (
296 '::', ':', '(', ')', '[', ']', '_', '=', ',', '|',
297 '+', '-', '!', '?',
298 )
299
300 proof_operators = ('{', '}', '.', '..')
301
302 tokens = {
303 'root': [
304 (r'\s+', Text),
305 (r'\(\*', Comment, 'comment'),
306 (r'\{\*', Comment, 'text'),
307
308 (words(operators), Operator),
309 (words(proof_operators), Operator.Word),
310
311 (words(keyword_minor, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
312
313 (words(keyword_diag, prefix=r'\b', suffix=r'\b'), Keyword.Type),
314
315 (words(keyword_thy, prefix=r'\b', suffix=r'\b'), Keyword),
316 (words(keyword_theory_decl, prefix=r'\b', suffix=r'\b'), Keyword),
317
318 (words(keyword_section, prefix=r'\b', suffix=r'\b'), Generic.Heading),
319 (words(keyword_subsection, prefix=r'\b', suffix=r'\b'), Generic.Subheading),
320
321 (words(keyword_theory_goal, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
322 (words(keyword_theory_script, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
323
324 (words(keyword_abandon_proof, prefix=r'\b', suffix=r'\b'), Generic.Error),
325
326 (words(keyword_qed, prefix=r'\b', suffix=r'\b'), Keyword),
327 (words(keyword_proof_goal, prefix=r'\b', suffix=r'\b'), Keyword),
328 (words(keyword_proof_block, prefix=r'\b', suffix=r'\b'), Keyword),
329 (words(keyword_proof_decl, prefix=r'\b', suffix=r'\b'), Keyword),
330
331 (words(keyword_proof_chain, prefix=r'\b', suffix=r'\b'), Keyword),
332 (words(keyword_proof_asm, prefix=r'\b', suffix=r'\b'), Keyword),
333 (words(keyword_proof_asm_goal, prefix=r'\b', suffix=r'\b'), Keyword),
334
335 (words(keyword_proof_script, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
336
337 (r'\\<\w*>', Text.Symbol),
338
339 (r"[^\W\d][.\w']*", Name),
340 (r"\?[^\W\d][.\w']*", Name),
341 (r"'[^\W\d][.\w']*", Name.Type),
342
343 (r'\d[\d_]*', Name), # display numbers as name
344 (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
345 (r'0[oO][0-7][0-7_]*', Number.Oct),
346 (r'0[bB][01][01_]*', Number.Bin),
347
348 (r'"', String, 'string'),
349 (r'`', String.Other, 'fact'),
350 ],
351 'comment': [
352 (r'[^(*)]+', Comment),
353 (r'\(\*', Comment, '#push'),
354 (r'\*\)', Comment, '#pop'),
355 (r'[(*)]', Comment),
356 ],
357 'text': [
358 (r'[^*}]+', Comment),
359 (r'\*\}', Comment, '#pop'),
360 (r'\*', Comment),
361 (r'\}', Comment),
362 ],
363 'string': [
364 (r'[^"\\]+', String),
365 (r'\\<\w*>', String.Symbol),
366 (r'\\"', String),
367 (r'\\', String),
368 (r'"', String, '#pop'),
369 ],
370 'fact': [
371 (r'[^`\\]+', String.Other),
372 (r'\\<\w*>', String.Symbol),
373 (r'\\`', String.Other),
374 (r'\\', String.Other),
375 (r'`', String.Other, '#pop'),
376 ],
377 }
378
379
380 class LeanLexer(RegexLexer):
381 """
382 For the `Lean <https://github.com/leanprover/lean>`_
383 theorem prover.
384
385 .. versionadded:: 2.0
386 """
387 name = 'Lean'
388 aliases = ['lean']
389 filenames = ['*.lean']
390 mimetypes = ['text/x-lean']
391
392 flags = re.MULTILINE | re.UNICODE
393
394 tokens = {
395 'root': [
396 (r'\s+', Text),
397 (r'/--', String.Doc, 'docstring'),
398 (r'/-', Comment, 'comment'),
399 (r'--.*?$', Comment.Single),
400 (words((
401 'import', 'renaming', 'hiding',
402 'namespace',
403 'local',
404 'private', 'protected', 'section',
405 'include', 'omit', 'section',
406 'protected', 'export',
407 'open',
408 'attribute',
409 ), prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
410 (words((
411 'lemma', 'theorem', 'def', 'definition', 'example',
412 'axiom', 'axioms', 'constant', 'constants',
413 'universe', 'universes',
414 'inductive', 'coinductive', 'structure', 'extends',
415 'class', 'instance',
416 'abbreviation',
417
418 'noncomputable theory',
419
420 'noncomputable', 'mutual', 'meta',
421
422 'attribute',
423
424 'parameter', 'parameters',
425 'variable', 'variables',
426
427 'reserve', 'precedence',
428 'postfix', 'prefix', 'notation', 'infix', 'infixl', 'infixr',
429
430 'begin', 'by', 'end',
431
432 'set_option',
433 'run_cmd',
434 ), prefix=r'\b', suffix=r'\b'), Keyword.Declaration),
435 (r'@\[[^\]]*\]', Keyword.Declaration),
436 (words((
437 'forall', 'fun', 'Pi', 'from', 'have', 'show', 'assume', 'suffices',
438 'let', 'if', 'else', 'then', 'in', 'with', 'calc', 'match',
439 'do'
440 ), prefix=r'\b', suffix=r'\b'), Keyword),
441 (words(('sorry', 'admit'), prefix=r'\b', suffix=r'\b'), Generic.Error),
442 (words(('Sort', 'Prop', 'Type'), prefix=r'\b', suffix=r'\b'), Keyword.Type),
443 (words((
444 '#eval', '#check', '#reduce', '#exit',
445 '#print', '#help',
446 ), suffix=r'\b'), Keyword),
447 (words((
448 '(', ')', ':', '{', '}', '[', ']', '⟨', '⟩', '‹', '›', '⦃', '⦄', ':=', ',',
449 )), Operator),
450 (r'[A-Za-z_\u03b1-\u03ba\u03bc-\u03fb\u1f00-\u1ffe\u2100-\u214f]'
451 r'[.A-Za-z_\'\u03b1-\u03ba\u03bc-\u03fb\u1f00-\u1ffe\u2070-\u2079'
452 r'\u207f-\u2089\u2090-\u209c\u2100-\u214f0-9]*', Name),
453 (r'0x[A-Za-z0-9]+', Number.Integer),
454 (r'0b[01]+', Number.Integer),
455 (r'\d+', Number.Integer),
456 (r'"', String.Double, 'string'),
457 (r"'(?:(\\[\\\"'nt])|(\\x[0-9a-fA-F]{2})|(\\u[0-9a-fA-F]{4})|.)'", String.Char),
458 (r'[~?][a-z][\w\']*:', Name.Variable),
459 (r'\S', Name.Builtin.Pseudo),
460 ],
461 'comment': [
462 (r'[^/-]', Comment.Multiline),
463 (r'/-', Comment.Multiline, '#push'),
464 (r'-/', Comment.Multiline, '#pop'),
465 (r'[/-]', Comment.Multiline)
466 ],
467 'docstring': [
468 (r'[^/-]', String.Doc),
469 (r'-/', String.Doc, '#pop'),
470 (r'[/-]', String.Doc)
471 ],
472 'string': [
473 (r'[^\\"]+', String.Double),
474 (r"(?:(\\[\\\"'nt])|(\\x[0-9a-fA-F]{2})|(\\u[0-9a-fA-F]{4}))", String.Escape),
475 ('"', String.Double, '#pop'),
476 ],
477 }

eric ide

mercurial