Skip to content

Help in understanding the creation of custom lexers #2539

Answered by Andre601
Andre601 asked this question in Q&A
Discussion options

You must be logged in to vote

To give a complete answer to this for everyone, I managed to create the following lexer:

"""A Pygments lexer for the command examples"""
import re

from pygments.lexer import RegexLexer, bygroups
from pygments.token import *

__all__ = ("CommandLexer",)

class CommandLexer(RegexLexer):
    """Simple lexer to highlight arguments of commands."""
    
    name = 'Command'
    aliases = ['command']
    filenames = ['*.command']
    
    tokens = {
        'root': [
            (r'^(\/)([^\S]+)', bygroups(Name.Tag, Text)),
            (r'[\[\{<]', Punctuation, 'option'),
            (r'\w', Text)
        ],
        'option': [
            (r'\|', Punctuation),
            (r'[^\[\{<>\}\|\]]+', N…

Replies: 4 comments 10 replies

Comment options

You must be logged in to vote
5 replies
@Andre601
Comment options

@Andre601
Comment options

@jeanas
Comment options

@Andre601
Comment options

@Marrin
Comment options

Comment options

You must be logged in to vote
3 replies
@Andre601
Comment options

@birkenfeld
Comment options

@Andre601
Comment options

Comment options

You must be logged in to vote
2 replies
@jeanas
Comment options

@Andre601
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Andre601
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants