⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cxxcodestate.cpp

📁 语法检查程序
💻 CPP
字号:
/***************************************************************************/
/* NOTE:                                                                   */
/* This document is copyright (c) by Oz Solomon and Yonat Sharon, and is   */
/* bound by the MIT open source license.                                   */ 
/* See License.txt or visit www.opensource.org/licenses/mit-license.html   */
/***************************************************************************/

#include "CodeState.h"

const CodeState* CxxCodeState()
{
    // states
    enum {
        cNormal,
        cString,    // in double quotes
        cChar,      // in single quotes
        cEscString, // "\" in a string
        cEscChar,   // "\" in a char
        cBegComment,
        cLineComment,   // "// bla"
        cBlockComment,  // "/* bla */"
        cStar,          // "*" in a block comment
        cStatesNum
    };
    static CodeState states[cStatesNum];
    static bool initialized = false;

    if (initialized)
        return &states[0];

#ifndef NDEBUG
    states[cNormal].itsName = "Normal";
    states[cString].itsName = "String";
    states[cChar].itsName = "Char";
    states[cEscString].itsName = "EscString";
    states[cEscChar].itsName = "EscChar";
    states[cBegComment].itsName = "BegComment";
    states[cLineComment].itsName = "LineComment";
    states[cBlockComment].itsName = "BlockComment";
    states[cStar].itsName = "Star";
#endif // NDEBUG

    CodeEffect e;

    //
    // strings and chars
    //
    e.markPosition = true;

    // begin string
    e.action = CodeParser::Context::BegString;
    e.next = &states[cString];
    states[cNormal]['"'] = e;
    states[cBegComment]['"'] = e;

    // begin char
    e.action = CodeParser::Context::BegChar;
    e.next = &states[cChar];
    states[cNormal]['\''] = e;
    states[cBegComment]['\''] = e;

    // end string and char
    e.next = &states[cNormal];
    e.action = CodeParser::Context::EndString;
    states[cString]['"'] = e;
    e.action = CodeParser::Context::EndChar;
    states[cChar]['\''] = e;

    //
    // comments
    //

    // begin and end comment
    e.next = &states[cBegComment];
    e.action = 0;
    states[cNormal]['/'] = e;
    e.next = &states[cNormal];
    e.action = CodeParser::Context::EndBlockComment;
    states[cStar]['/'] = e;
    e.action = CodeParser::Context::EndLineComment;
    states[cLineComment]['\n'] = e;

    // identify comment type
    e.markPosition = false;
    e.next = &states[cBlockComment];
    e.action = CodeParser::Context::BegBlockComment;
    states[cBegComment]['*'] = e;
    e.next = &states[cLineComment];
    e.action = CodeParser::Context::BegLineComment;
    states[cBegComment]['/'] = e;

    //
    // "do nothing" effects
    //
    e.action = 0;

    // escpae charactes
    e.next = &states[cEscString];
    states[cString]['\\'] = e;
    e.next = &states[cEscChar];
    states[cChar]['\\'] = e;
    e.next = &states[cString];
    states[cEscString][CodeParser::cDefaultDatum] = e;
    e.next = &states[cChar];
    states[cEscChar][CodeParser::cDefaultDatum] = e;

    // comment related
    e.next = &states[cNormal];
    states[cBegComment][CodeParser::cDefaultDatum] = e;
    e.next = &states[cStar];
    states[cBlockComment]['*'] = e;
    states[cStar]['*'] = e;
    e.next = &states[cBlockComment];
    states[cStar][CodeParser::cDefaultDatum] = e;

    initialized = true;

    return &states[0];
}

#ifdef TEST_CXX_CODE_STATE

#include "TestContext.h"

char str[] =
    "#include \"CodeState.h\"\n"
    "\n"
    "const CodeState* CxxCodeState()\n"
    "{\n"
    "    // states\n"
    "    enum {\n"
    "        cNormal,\n"
    "        cString,    // in double quotes\n"
    "        cChar,      // in single quotes\n"
    "        cEscString, // \"\\\" in a string\n"
    "        cEscChar,   // \"\\\" in a char\n"
    "        cBegComment,\n"
    "        cLineComment,   // \"// bla\"\n"
    "        cBlockComment,  // \"/* bla */\"\n"
    "        cStar,          // \"*\" in a block comment\n"
    "        cStatesNum\n"
    "    };\n"
    "\n"
    "    /*\n"
    "     * strings and chars\n"
    "     */\n"
    "    e.markPosition = true;\n"
    "\n"
    "    // begin string\n"
    "    states[cNormal][\'\\\"\'] = e;\n"
    "    states[cNormal][\'\\\'\'] = e;\n";

int main()
{
    TestContext t(str);
    CodeParser cp(CxxCodeState(), &t);
    cp.Process(str, sizeof(str));

    return 0;
}

#endif // TEST_CXX_CODE_STATE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -