|
| 1 | +# The code in this file is based on: |
| 2 | +# https://github.com/python/cpython/blob/v3.15.2/Doc/tools/extensions/lexers/asdl_lexer.py |
| 3 | + |
| 4 | +# Licensed under the Python Software Foundation License v2 |
| 5 | +# For the full license text, see: |
| 6 | +# https://github.com/python/cpython/blob/v3.15.2/LICENSE |
| 7 | + |
| 8 | +from pygments.lexer import RegexLexer, bygroups, include |
| 9 | +from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text |
| 10 | + |
| 11 | + |
| 12 | +class ASDLLexer(RegexLexer): |
| 13 | + name = "ASDL" |
| 14 | + aliases = ["asdl"] |
| 15 | + filenames = ["*.asdl"] |
| 16 | + _name = r"([^\W\d]\w*)" |
| 17 | + _text_ws = r"(\s*)" |
| 18 | + |
| 19 | + tokens = { |
| 20 | + "ws": [ |
| 21 | + (r"\n", Text), |
| 22 | + (r"\s+", Text), |
| 23 | + (r"--.*?$", Comment.Singleline), |
| 24 | + ], |
| 25 | + "root": [ |
| 26 | + include("ws"), |
| 27 | + ( |
| 28 | + r"(module)" + _text_ws + _name, |
| 29 | + bygroups(Keyword, Text, Name.Tag), |
| 30 | + ), |
| 31 | + ( |
| 32 | + r"(\w+)([\?\*]*\s)(\w+)", |
| 33 | + bygroups(Name.Builtin.Pseudo, Operator, Name), |
| 34 | + ), |
| 35 | + # Keep in line with ``builtin_types`` from Parser/asdl.py. |
| 36 | + # ASDL's 4 builtin types are |
| 37 | + # constant, identifier, int, string |
| 38 | + ("constant|identifier|int|string", Name.Builtin), |
| 39 | + (r"attributes", Name.Builtin), |
| 40 | + ( |
| 41 | + _name + _text_ws + "(=)", |
| 42 | + bygroups(Name, Text, Operator), |
| 43 | + ), |
| 44 | + (_name, Name.Class), |
| 45 | + (r"\|", Operator), |
| 46 | + (r"{|}|\(|\)", Punctuation), |
| 47 | + (r".", Text), |
| 48 | + ], |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +def register(): |
| 53 | + return [ASDLLexer] |
0 commit comments