From b7bc2b98c45155c1f79f0d20375fcde8e2222689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Basile=20Cl=C3=A9ment?= Date: Thu, 5 Oct 2023 12:26:49 +0200 Subject: [PATCH] Add a Pygments lexer for the Alt-Ergo language This patch adds a custom Pygments lexer for the Alt-Ergo language and uses it to make the Sphinx documentation prettier. --- .gitignore | 3 ++ docs/sphinx_docs/alt_ergo_lexer.py | 72 ++++++++++++++++++++++++++++++ docs/sphinx_docs/conf.py | 18 ++++++-- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 docs/sphinx_docs/alt_ergo_lexer.py diff --git a/.gitignore b/.gitignore index 861c1cbc1..6a48d9cf7 100644 --- a/.gitignore +++ b/.gitignore @@ -41,5 +41,8 @@ alt-ergo.js alt-ergo-worker.js www/ +# Generated python files +__pycache__ + # Generated nix files /result* diff --git a/docs/sphinx_docs/alt_ergo_lexer.py b/docs/sphinx_docs/alt_ergo_lexer.py new file mode 100644 index 000000000..185883970 --- /dev/null +++ b/docs/sphinx_docs/alt_ergo_lexer.py @@ -0,0 +1,72 @@ +from pygments.lexer import RegexLexer, include +from pygments.token import * + +__globals__ = [ 'AltErgoLexer' ] + +class AltErgoLexer(RegexLexer): + """ + For the Alt-Ergo language. + """ + + name = 'Alt-Ergo' + url = 'https://alt-ergo.ocamlpro.com/' + aliases = ['alt-ergo'] + filenames = ['*.ae'] + mimetypes = ['text/x-alt-ergo'] + + keywords = ( + 'ac', 'axiom', 'bitv', 'case_split', 'check', 'cut', + 'distinct', 'else', 'end', 'exists', 'extends', 'forall', + 'function', 'goal', 'check_sat', 'if', 'let', 'logic', 'not', + 'predicate', 'prop', 'rewriting', 'then', 'theory', + 'type', 'match', 'with', 'of', + ) + keyopts = ( + r',', r';', r'\(', r'\)', r':', r'->', r'<-', r'<->', r'=', r'<', r'<=', + r'>', r'>=', r'<>', r'\+', r'-', r'\*', r'\*\*', r'\*\*\.', r'/', r'%', + r'@', r'\.', r'#', r'\[', r'\]', r'\{', r'\}', 'r\|', r'\^', r'\|->', + ) + + word_operators = ('and', 'in', 'or', 'xor') + primitives = ('bool', 'int', 'real', 'unit', 'void') + + tokens = { + 'escape-sequence': [ + (r'\\[\\"\'ntbr]', String.Escape), + (r'\\[0-9]{3}', String.Escape), + (r'\\x[0-9a-fA-F]{2}', String.Escape), + ], + 'root': [ + (r'\s+', Text), + (r'false|true', Name.Builtin.Pseudo), + (r'\(\*(?![)])', Comment, 'comment'), + (r'\b(%s)\b' % '|'.join(keywords), Keyword), + (r'(%s)' % '|'.join(keyopts[::-1]), Operator), + (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word), + (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), + + (r"\??[^\W\d][\w?']*", Name), + + (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), + (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex), + (r'0[oO][0-7][0-7_]*', Number.Oct), + (r'0[bB][01][01_]*', Number.Bin), + (r'\d[\d_]*', Number.Integer), + + (r"'", Keyword), + + (r'"', String.Double, 'string'), + ], + 'comment': [ + (r'[^(*)]+', Comment), + (r'\(\*', Comment, '#push'), + (r'\*\)', Comment, '#pop'), + (r'[(*)]', Comment), + ], + 'string': [ + (r'[^\\"]+', String.Double), + include('escape-sequence'), + (r'\\\n', String.Double), + (r'"', String.Double, '#pop'), + ], + } diff --git a/docs/sphinx_docs/conf.py b/docs/sphinx_docs/conf.py index cf504c2a3..8dd321104 100644 --- a/docs/sphinx_docs/conf.py +++ b/docs/sphinx_docs/conf.py @@ -9,10 +9,10 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -# -# import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) + +import os +import sys +sys.path.insert(0, os.path.abspath('.')) # -- Project information ----------------------------------------------------- @@ -21,6 +21,16 @@ copyright = '2020 - 2023, Alt-Ergo developers' author = 'Alt-Ergo developers' +# -- Custom lexer ------------------------------------------------------------ + +# Import our custom lexer +from alt_ergo_lexer import AltErgoLexer +from sphinx.highlighting import lexers +lexers['alt-ergo'] = AltErgoLexer() + +# Use alt-ergo as the default language +highlight_language = 'alt-ergo' + # -- Entry point ------------------------------------------------------------- master_doc = 'index'