2020-07-27 15:20:36 -03:00
|
|
|
from pygments.lexer import RegexLexer, bygroups, include
|
2022-06-22 14:14:27 -03:00
|
|
|
from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text
|
2020-07-27 15:20:36 -03:00
|
|
|
|
|
|
|
from sphinx.highlighting import lexers
|
|
|
|
|
|
|
|
|
|
|
|
class PEGLexer(RegexLexer):
|
|
|
|
"""Pygments Lexer for PEG grammar (.gram) files
|
|
|
|
|
|
|
|
This lexer strips the following elements from the grammar:
|
|
|
|
|
|
|
|
- Meta-tags
|
|
|
|
- Variable assignments
|
|
|
|
- Actions
|
|
|
|
- Lookaheads
|
|
|
|
- Rule types
|
|
|
|
- Rule options
|
|
|
|
- Rules named `invalid_*` or `incorrect_*`
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = "PEG"
|
|
|
|
aliases = ["peg"]
|
|
|
|
filenames = ["*.gram"]
|
|
|
|
_name = r"([^\W\d]\w*)"
|
|
|
|
_text_ws = r"(\s*)"
|
|
|
|
|
|
|
|
tokens = {
|
|
|
|
"ws": [(r"\n", Text), (r"\s+", Text), (r"#.*$", Comment.Singleline),],
|
|
|
|
"lookaheads": [
|
2021-02-02 15:54:22 -04:00
|
|
|
# Forced tokens
|
|
|
|
(r"(&&)(?=\w+\s?)", bygroups(None)),
|
|
|
|
(r"(&&)(?='.+'\s?)", bygroups(None)),
|
|
|
|
(r'(&&)(?=".+"\s?)', bygroups(None)),
|
|
|
|
(r"(&&)(?=\(.+\)\s?)", bygroups(None)),
|
|
|
|
|
2020-07-27 15:20:36 -03:00
|
|
|
(r"(?<=\|\s)(&\w+\s?)", bygroups(None)),
|
|
|
|
(r"(?<=\|\s)(&'.+'\s?)", bygroups(None)),
|
|
|
|
(r'(?<=\|\s)(&".+"\s?)', bygroups(None)),
|
|
|
|
(r"(?<=\|\s)(&\(.+\)\s?)", bygroups(None)),
|
|
|
|
],
|
|
|
|
"metas": [
|
|
|
|
(r"(@\w+ '''(.|\n)+?''')", bygroups(None)),
|
|
|
|
(r"^(@.*)$", bygroups(None)),
|
|
|
|
],
|
2021-04-17 20:18:13 -03:00
|
|
|
"actions": [
|
|
|
|
(r"{(.|\n)+?}", bygroups(None)),
|
|
|
|
],
|
2020-07-27 15:20:36 -03:00
|
|
|
"strings": [
|
|
|
|
(r"'\w+?'", Keyword),
|
|
|
|
(r'"\w+?"', Keyword),
|
|
|
|
(r"'\W+?'", Text),
|
|
|
|
(r'"\W+?"', Text),
|
|
|
|
],
|
2021-04-17 20:18:13 -03:00
|
|
|
"variables": [
|
|
|
|
(_name + _text_ws + "(=)", bygroups(None, None, None),),
|
|
|
|
(_name + _text_ws + r"(\[[\w\d_\*]+?\])" + _text_ws + "(=)", bygroups(None, None, None, None, None),),
|
|
|
|
],
|
2020-07-27 15:20:36 -03:00
|
|
|
"invalids": [
|
2022-01-04 06:42:15 -04:00
|
|
|
(r"^(\s+\|\s+.*invalid_\w+.*\n)", bygroups(None)),
|
|
|
|
(r"^(\s+\|\s+.*incorrect_\w+.*\n)", bygroups(None)),
|
2020-07-27 15:20:36 -03:00
|
|
|
(r"^(#.*invalid syntax.*(?:.|\n)*)", bygroups(None),),
|
|
|
|
],
|
|
|
|
"root": [
|
|
|
|
include("invalids"),
|
|
|
|
include("ws"),
|
|
|
|
include("lookaheads"),
|
|
|
|
include("metas"),
|
|
|
|
include("actions"),
|
|
|
|
include("strings"),
|
|
|
|
include("variables"),
|
|
|
|
(r"\b(?!(NULL|EXTRA))([A-Z_]+)\b\s*(?!\()", Text,),
|
|
|
|
(
|
2020-09-02 11:29:38 -03:00
|
|
|
r"^\s*" + _name + r"\s*" + r"(\[.*\])?" + r"\s*" + r"(\(.+\))?" + r"\s*(:)",
|
2020-07-27 15:20:36 -03:00
|
|
|
bygroups(Name.Function, None, None, Punctuation),
|
|
|
|
),
|
|
|
|
(_name, Name.Function),
|
|
|
|
(r"[\||\.|\+|\*|\?]", Operator),
|
|
|
|
(r"{|}|\(|\)|\[|\]", Punctuation),
|
|
|
|
(r".", Text),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
lexers["peg"] = PEGLexer()
|
|
|
|
return {"version": "1.0", "parallel_read_safe": True}
|