Skip to content

Commit

Permalink
Fix Coq-related bug #678 (#1648)
Browse files Browse the repository at this point in the history
* Unicode support for Coq

Catch-all lexing for `Name.Builtin.Pseudo`, as in the lean lexer.

This fixes #678.

* Coq lexer: improve `analyse_text`

* Add a test for Coq
  • Loading branch information
Maximilian Wuttke committed Jan 4, 2021
1 parent 039ba3f commit c01ccfe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pygments/lexers/theorem.py
Expand Up @@ -30,6 +30,8 @@ class CoqLexer(RegexLexer):
filenames = ['*.v']
mimetypes = ['text/x-coq']

flags = re.UNICODE

keywords1 = (
# Vernacular commands
'Section', 'Module', 'End', 'Require', 'Import', 'Export', 'Variable',
Expand Down Expand Up @@ -123,14 +125,15 @@ class CoqLexer(RegexLexer):
(r'0[bB][01][01_]*', Number.Bin),
(r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),

(r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'",
String.Char),
(r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", String.Char),

(r"'.'", String.Char),
(r"'", Keyword), # a stray quote is another syntax element

(r'"', String.Double, 'string'),

(r'[~?][a-z][\w\']*:', Name),
(r'\S', Name.Builtin.Pseudo),
],
'comment': [
(r'[^(*)]+', Comment),
Expand All @@ -154,7 +157,7 @@ class CoqLexer(RegexLexer):
}

def analyse_text(text):
if 'qed' in text and 'tauto' in text:
if 'Qed' in text and 'Proof' in text:
return 1


Expand Down
33 changes: 33 additions & 0 deletions tests/test_coq.py
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
Coq Tests
~~~~~~~~~~~~~
:copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

import pytest

from pygments.lexers import CoqLexer
from pygments.token import Token

@pytest.fixture(scope='module')
def lexer():
yield CoqLexer()

def test_coq_unicode(lexer):
fragment = 'Check (α ≻ β).\n'
tokens = [
(Token.Keyword.Namespace, 'Check'),
(Token.Text, ' '),
(Token.Operator, '('),
(Token.Name, 'α'),
(Token.Text, ' '),
(Token.Name.Builtin.Pseudo, '≻'),
(Token.Text, ' '),
(Token.Name, 'β'),
(Token.Operator, ')'),
(Token.Operator, '.'),
(Token.Text, '\n'),
]
assert list(lexer.get_tokens(fragment)) == tokens

0 comments on commit c01ccfe

Please sign in to comment.