MCPcopy Index your code
hub / github.com/python/cpython / PEGLexer

Class PEGLexer

Doc/tools/extensions/lexers/peg_lexer.py:5–79  ·  view source on GitHub ↗

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_*`

Source from the content-addressed store, hash-verified

3
4
5class PEGLexer(RegexLexer):
6 """Pygments Lexer for PEG grammar (.gram) files
7
8 This lexer strips the following elements from the grammar:
9
10 - Meta-tags
11 - Variable assignments
12 - Actions
13 - Lookaheads
14 - Rule types
15 - Rule options
16 - Rules named `invalid_*` or `incorrect_*`
17 """
18
19 name = "PEG"
20 aliases = ["peg"]
21 filenames = ["*.gram"]
22 _name = r"([^\W\d]\w*)"
23 _text_ws = r"(\s*)"
24
25 tokens = {
26 "ws": [(r"\n", Text), (r"\s+", Text), (r"#.*$", Comment.Singleline),],
27 "lookaheads": [
28 # Forced tokens
29 (r"(&&)(?=\w+\s?)", bygroups(None)),
30 (r"(&&)(?='.+'\s?)", bygroups(None)),
31 (r'(&&)(?=".+"\s?)', bygroups(None)),
32 (r"(&&)(?=\(.+\)\s?)", bygroups(None)),
33
34 (r"(?<=\|\s)(&\w+\s?)", bygroups(None)),
35 (r"(?<=\|\s)(&'.+'\s?)", bygroups(None)),
36 (r'(?<=\|\s)(&".+"\s?)', bygroups(None)),
37 (r"(?<=\|\s)(&\(.+\)\s?)", bygroups(None)),
38 ],
39 "metas": [
40 (r"(@\w+ '''(.|\n)+?''')", bygroups(None)),
41 (r"^(@.*)$", bygroups(None)),
42 ],
43 "actions": [
44 (r"{(.|\n)+?}", bygroups(None)),
45 ],
46 "strings": [
47 (r"'\w+?'", Keyword),
48 (r'"\w+?"', Keyword),
49 (r"'\W+?'", Text),
50 (r'"\W+?"', Text),
51 ],
52 "variables": [
53 (_name + _text_ws + "(=)", bygroups(None, None, None),),
54 (_name + _text_ws + r"(\[[\w\d_\*]+?\])" + _text_ws + "(=)", bygroups(None, None, None, None, None),),
55 ],
56 "invalids": [
57 (r"^(\s+\|\s+.*invalid_\w+.*\n)", bygroups(None)),
58 (r"^(\s+\|\s+.*incorrect_\w+.*\n)", bygroups(None)),
59 (r"^(#.*invalid syntax.*(?:.|\n)*)", bygroups(None),),
60 ],
61 "root": [
62 include("invalids"),

Callers

nothing calls this directly

Calls 1

includeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…