QScintilla:如何创建一个新的词法分析器或修改现有的词法分析器?

发布于 2024-10-01 03:28:43 字数 3243 浏览 0 评论 0原文

我发现 C++ 突出显示的默认词法分析器不够具体。

我希望至少能够为以下类型指定不同的颜色:

type 关键字(void、int、float 等) 指令关键字(if while do return for 等) 类相关关键字(模板类虚拟朋友) 类型修饰符关键字(static const extern unsigned 等)

我在 LexerCPP 源中找到了这个:

const char *QsciLexerCPX::keywords(int set) const
{
    if (set == 1)
        return
            "and and_eq asm auto bitand bitor bool break case "
            "catch char class compl const const_cast continue "
            "default delete do double dynamic_cast else enum "
            "explicit export extern false float for friend goto if "
            "inline int long mutable namespace new not not_eq "
            "operator or or_eq private protected public register "
            "reinterpret_cast return short signed sizeof static "
            "static_cast struct switch template this throw true "
            "try typedef typeid typename union unsigned using "
            "virtual void volatile wchar_t while xor xor_eq";

    if (set == 3)
        return
            "a addindex addtogroup anchor arg attention author b "
            "brief bug c class code date def defgroup deprecated "
            "dontinclude e em endcode endhtmlonly endif "
            "endlatexonly endlink endverbatim enum example "
            "exception f$ f[ f] file fn hideinitializer "
            "htmlinclude htmlonly if image include ingroup "
            "internal invariant interface latexonly li line link "
            "mainpage name namespace nosubgrouping note overload "
            "p page par param post pre ref relates remarks return "
            "retval sa section see showinitializer since skip "
            "skipline struct subsection test throw todo typedef "
            "union until var verbatim verbinclude version warning "
            "weakgroup $ @ \\ & < > # { }";

我尝试了这个 - 将 qscilexercpp.cpp 复制/粘贴到新文件名 qscilexercxx.cpp - 将上面的代码替换为适当的开关:

switch(set)
    {
    case Oper:
        //operators
        return
        "and and_eq bitand bitor "
        "catch compl const_cast "
        "delete dynamic_cast "
        "new not not_eq "
        "operator or or_eq "
        "reinterpret_cast sizeof "
        "static_cast throw "
        "try typeid typename "
        "xor xor_eq";

    case BaseType:
        // basic types
        return
        "bool char double enum float int long "
        "short struct union void wchar_t";

    case ClassRelated:
        // class/template-related
        return
        "class inline friend private protected public "
        "template this virtual";

    case Misc:
        // misc
        return
        "asm namespace typedef using";

    case Modifiers:
        // type modifiers
        return
        "auto const explicit extern mutable register "
        "signed static unsigned volatile";

    case Instruct:
        return
        "break case continue default do else "
        "for goto if return switch while";
    }

创建适当的枚举之后:

    Oper = 20,
    BaseType = 21,
    ClassRelated = 22,
    Misc = 23,
    Modifiers = 24,
    Instruct = 25

在现有枚举的末尾。

现在我的大部分文本都是黑色的,我很确定我错过了有关枚举和返回的关键字 char 数组之间的链接的一些信息...

有人可以指导我做某事或帮助我吗?

I find the default lexer for C++ highlighting not very specific enough.

I want to at least be able to specify a different color for:

type keyword (void, int, float etc)
instruction keyword (if while do return for etc)
class-related keyword (template class virtual friend)
type modifiers keywords (static const extern unsigned etc)

I found this in the LexerCPP source:

const char *QsciLexerCPX::keywords(int set) const
{
    if (set == 1)
        return
            "and and_eq asm auto bitand bitor bool break case "
            "catch char class compl const const_cast continue "
            "default delete do double dynamic_cast else enum "
            "explicit export extern false float for friend goto if "
            "inline int long mutable namespace new not not_eq "
            "operator or or_eq private protected public register "
            "reinterpret_cast return short signed sizeof static "
            "static_cast struct switch template this throw true "
            "try typedef typeid typename union unsigned using "
            "virtual void volatile wchar_t while xor xor_eq";

    if (set == 3)
        return
            "a addindex addtogroup anchor arg attention author b "
            "brief bug c class code date def defgroup deprecated "
            "dontinclude e em endcode endhtmlonly endif "
            "endlatexonly endlink endverbatim enum example "
            "exception f$ f[ f] file fn hideinitializer "
            "htmlinclude htmlonly if image include ingroup "
            "internal invariant interface latexonly li line link "
            "mainpage name namespace nosubgrouping note overload "
            "p page par param post pre ref relates remarks return "
            "retval sa section see showinitializer since skip "
            "skipline struct subsection test throw todo typedef "
            "union until var verbatim verbinclude version warning "
            "weakgroup $ @ \\ & < > # { }";

etc

I tried this
- Copy/paste the qscilexercpp.cpp to a new file name qscilexercxx.cpp
- replace the code above by a switch with an appropriate switch:

switch(set)
    {
    case Oper:
        //operators
        return
        "and and_eq bitand bitor "
        "catch compl const_cast "
        "delete dynamic_cast "
        "new not not_eq "
        "operator or or_eq "
        "reinterpret_cast sizeof "
        "static_cast throw "
        "try typeid typename "
        "xor xor_eq";

    case BaseType:
        // basic types
        return
        "bool char double enum float int long "
        "short struct union void wchar_t";

    case ClassRelated:
        // class/template-related
        return
        "class inline friend private protected public "
        "template this virtual";

    case Misc:
        // misc
        return
        "asm namespace typedef using";

    case Modifiers:
        // type modifiers
        return
        "auto const explicit extern mutable register "
        "signed static unsigned volatile";

    case Instruct:
        return
        "break case continue default do else "
        "for goto if return switch while";
    }

After creating the appropriate enum:

    Oper = 20,
    BaseType = 21,
    ClassRelated = 22,
    Misc = 23,
    Modifiers = 24,
    Instruct = 25

At the end of the existing ones.

Now most of my text is black, and I'm pretty sure I missed something about the link between the enum and the char array of keywords returned...

Can somebody direct me to something or help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文