0

I put together a lexer that should ideally work for psuedocode, but when I test it using python3 -m pygments -x -l ./psuedo.py:PseudoLexer test.pseudo, I keep getting the following error: "no valid PseudoLexer class found in ./psuedo.py".

I looked through my lexer and I pretty much just took a lot of the content from examples from C++'s comment lexer and Python's keyword lexer, so I don't understand why I am getting this error.

Here is the code, if it is needed:

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

__all__ = ['PsuedoLexer']

class PsuedoLexer(RegexLexer):
    """
    Lexer for minted highlighting in psuedocode
    """
    name = 'Pseudo'
    aliases = ['psuedo']
    filenames = ['*.pseudo']
    
    tokens = {
        'root' : [
            # comments from cpp 
            (r'[^/]+', Text),
            (r'/\*', Comment.Multiline, 'comment'),
            (r'//.*?$', Comment.Singleline),
            (r'/', Text),
            # operators from python
            (r'!=|==|<<|>>|:=|[-~+/*%=<>&^|.]', Operator),
            (r'[]{}:(),;[]', Punctuation),
            (r'(in|is|and|or|not)\b', Operator.Word),
            # keywords from python (modified)
            (words((
                'assert', 'break', 'continue', 'del', 'elif',
                'else', 'except', 'finally', 'for', 'if', 'lambda',
                'pass', 'return', 'try', 'while', 'as', 'with',
                'end', 'repeat', 'do', 'then'), suffix=r'\b'),
             Keyword),
            (words(('True', 'False', 'None'), suffix=r'\b'), Keyword.Constant)
        ],
        'comment': [
            (r'[^*/]+', Comment.Multiline),
            (r'/\*', Comment.Multiline, '#push'),
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[*/]', Comment.Multiline)
        ]
    }

Additionally, once I get this lexer to work, how can I use it globally/in minted environments in LaTeX?

Revise
  • 229
  • 4
  • 11

1 Answers1

0

The command you're using expects your class to be named CustomLexer:

From the documentation:

Then you can load and test the lexer from the command line with the additional flag -x:

python -m pygments -x -l your_lexer_file.py <inputfile>

To specify a class name other than CustomLexer, append it with a colon:

python -m pygments -x -l your_lexer.py:SomeLexer <inputfile>

Which means you should rename your PseudoLexer class to CustomLexer or call python3 -m pygments -x -l your_lexer.py:PseudoLexer test.pseudo

dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • I thought I did? > when I test it using `python3 -m pygments -x -l ./psuedo.py:PseudoLexer test.pseudo`, I keep getting the following error: "no valid PseudoLexer class found in ./psuedo.py". – Revise Dec 14 '22 at 02:25