Path: blob/main/Doc/tools/extensions/peg_highlight.py
12 views
from pygments.lexer import RegexLexer, bygroups, include1from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text23from sphinx.highlighting import lexers456class PEGLexer(RegexLexer):7"""Pygments Lexer for PEG grammar (.gram) files89This lexer strips the following elements from the grammar:1011- Meta-tags12- Variable assignments13- Actions14- Lookaheads15- Rule types16- Rule options17- Rules named `invalid_*` or `incorrect_*`18"""1920name = "PEG"21aliases = ["peg"]22filenames = ["*.gram"]23_name = r"([^\W\d]\w*)"24_text_ws = r"(\s*)"2526tokens = {27"ws": [(r"\n", Text), (r"\s+", Text), (r"#.*$", Comment.Singleline),],28"lookaheads": [29# Forced tokens30(r"(&&)(?=\w+\s?)", bygroups(None)),31(r"(&&)(?='.+'\s?)", bygroups(None)),32(r'(&&)(?=".+"\s?)', bygroups(None)),33(r"(&&)(?=\(.+\)\s?)", bygroups(None)),3435(r"(?<=\|\s)(&\w+\s?)", bygroups(None)),36(r"(?<=\|\s)(&'.+'\s?)", bygroups(None)),37(r'(?<=\|\s)(&".+"\s?)', bygroups(None)),38(r"(?<=\|\s)(&\(.+\)\s?)", bygroups(None)),39],40"metas": [41(r"(@\w+ '''(.|\n)+?''')", bygroups(None)),42(r"^(@.*)$", bygroups(None)),43],44"actions": [45(r"{(.|\n)+?}", bygroups(None)),46],47"strings": [48(r"'\w+?'", Keyword),49(r'"\w+?"', Keyword),50(r"'\W+?'", Text),51(r'"\W+?"', Text),52],53"variables": [54(_name + _text_ws + "(=)", bygroups(None, None, None),),55(_name + _text_ws + r"(\[[\w\d_\*]+?\])" + _text_ws + "(=)", bygroups(None, None, None, None, None),),56],57"invalids": [58(r"^(\s+\|\s+.*invalid_\w+.*\n)", bygroups(None)),59(r"^(\s+\|\s+.*incorrect_\w+.*\n)", bygroups(None)),60(r"^(#.*invalid syntax.*(?:.|\n)*)", bygroups(None),),61],62"root": [63include("invalids"),64include("ws"),65include("lookaheads"),66include("metas"),67include("actions"),68include("strings"),69include("variables"),70(r"\b(?!(NULL|EXTRA))([A-Z_]+)\b\s*(?!\()", Text,),71(72r"^\s*" + _name + r"\s*" + r"(\[.*\])?" + r"\s*" + r"(\(.+\))?" + r"\s*(:)",73bygroups(Name.Function, None, None, Punctuation),74),75(_name, Name.Function),76(r"[\||\.|\+|\*|\?]", Operator),77(r"{|}|\(|\)|\[|\]", Punctuation),78(r".", Text),79],80}818283def setup(app):84lexers["peg"] = PEGLexer()85return {"version": "1.0", "parallel_read_safe": True}868788