ProjectKivy/KivyLexer.py

Tue, 10 Dec 2024 15:48:59 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Dec 2024 15:48:59 +0100
branch
eric7
changeset 70
2c0a54bf5b4c
parent 68
20cdd6e03807
permissions
-rw-r--r--

Updated copyright for 2025.

# -*- coding: utf-8 -*-

# Copyright (c) 2013 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a Pygments lexer for the Kivy language.
"""

#
# Lexer class is derived from the highlighter contained in the Kivy package.
# Copyright of the original by the Kivy Team.
#

from pygments.lexer import RegexLexer, bygroups, using
from pygments.lexers.agile import PythonLexer
from pygments.token import Comment, Name, Operator, Punctuation, Text

__all__ = ["KivyLexer"]


class KivyLexer(RegexLexer):
    """
    Class implementing a Pygments lexer for the Kivy language.
    """

    name = "Kivy"
    aliases = ["kivy", "kv"]
    filenames = ["*.kv", "*.kivy"]
    mimetypes = ["application/x-kivy"]
    tokens = {
        "root": [
            (r"#:.*?$", Comment.Preproc),
            (r"#.*?$", using(PythonLexer)),
            (r"\s+", Text),
            (r"<.+>", Name.Namespace),
            (
                r"(\[)(\s*)(.*?)(\s*)(@)",
                bygroups(Punctuation, Text, Name.Class, Text, Operator),
                "classList",
            ),
            (r"[A-Za-z][A-Za-z0-9]*$", Name.Attribute),
            (r"(.*?)(\s*)(:)(\s*)$", bygroups(Name.Class, Text, Punctuation, Text)),
            (
                r"(.*?)(\s*)(:)(\s*)(.*?)$",
                bygroups(Name.Attribute, Text, Punctuation, Text, using(PythonLexer)),
            ),
            (r"[^:]+?$", using(PythonLexer)),
        ],
        "classList": [
            (r"(,)(\s*)([A-Z][A-Za-z0-9]*)", bygroups(Punctuation, Text, Name.Class)),
            (r"(\+)(\s*)([A-Z][A-Za-z0-9]*)", bygroups(Operator, Text, Name.Class)),
            (r"\s+", Text),
            (r"[A-Z][A-Za-z0-9]*", Name.Class),
            (r"\]", Punctuation, "#pop"),
        ],
    }


#
# eflag: noqa = M702

eric ide

mercurial