• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            出自http://hi.baidu.com/yesbaba/blog/item/79c3eb13215da0d2f7039ec9.html

            1.1版,命令行下使用如1.0版,

                增加更易用的方法:點擊coco.exe,將文件拖入窗口,高亮文件輸出到同名txt文件中

                轉(zhuǎn)成html的功能以后不懶的時候再寫吧


            1.0版:命令行下使用,輸出到stdout,

            可以重定向到文件,彩色復(fù)制就行了
            coco.exe xxx.cpp > whateverfile
            記事本打開whateverfile,復(fù)制,在bbs上彩色粘貼
            編譯這個大概需要2百多M內(nèi)存

            想直接現(xiàn)在用的,可以在這下載,這是水木的附件,不知道能有效到什么時候

            http://www.newsmth.net/att.php?p.335.193566.17417.exe

            論壇可以用它來實現(xiàn)代碼高亮顯示
            以后再改進(jìn)

            正確的代碼coco應(yīng)該不會分析錯,能分析正確的不一定是正確的代碼

            讀代碼可以先看spirit最基礎(chǔ)的ch_p,str_p等,還有spirit文檔中的utility

            再看如何分析文件

            http://hi.baidu.com/yesbaba/blog/item/091ca995d0fe6e49d1135e8b.html

            gnu source highlight就可以實現(xiàn)這個功能了,我做這個一來是學(xué)spirit練練手,

            二來是要做個課程設(shè)計,三來這個很實用,平常在水木帖代碼也能用的上

            ///////////////////////////////////////////////////////////////////////////////
            //
            //       highlight c++ source code in ansi code format
            //       COCO (COlorful COde) version 1.1
            //       ibe@newsmth.net
            //       [ 2007-08-13 ]
            //
            ///////////////////////////////////////////////////////////////////////////////

            #include <boost/spirit/core.hpp>
            #include <boost/spirit/iterator/file_iterator.hpp>
            #include <boost/spirit/utility/confix.hpp>
            #include <boost/spirit/utility/escape_char.hpp>
            #include <iostream>
            #include <fstream>
            ///////////////////////////////////////////////////////////////////////////////
            using namespace boost::spirit;
            using namespace std;

            ////////////////////////////////////////////////////////////////////////////
            //
            //       Types
            //types needed for file parsing
            ////////////////////////////////////////////////////////////////////////////
            typedef char char_t;
            typedef file_iterator < char_t > iterator_t;
            typedef scanner < iterator_t > scanner_t;
            typedef rule < scanner_t > rule_t;

            ////////////////////////////////////////////////////////////////////////////
            //To ansi,action函數(shù),匹配后輸出成ansi代碼的彩色控制符,
            //可參考各大高校bbs的asciiart版精華區(qū),可以輸出成需要的格式,如html
            //
            ////////////////////////////////////////////////////////////////////////////
            ofstream outfile;
            namespace to_ansi
            {
              void
               black (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;30m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               red (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;31m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               green (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;32m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               yellow (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;33m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               blue (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;34m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               magenta (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;35m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               cyan (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;36m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               white (iterator_t first, iterator_t const &last)
               {
                 outfile << "\033[1;37m";
                while (first != last)
                   outfile << *first++;
                 outfile << "\033[m";
               }

              void
               echo (iterator_t first, iterator_t const &last)
               {
                while (first != last)
                   outfile << *first++;
               }
            }
            ////////////////////////////////////////////////////////////////////////////
            //
            //       cpp lex
            //c++的詞法描述,有了comment_p就方便多了,識別函數(shù)名還沒實現(xiàn)
            ////////////////////////////////////////////////////////////////////////////
            namespace cpp_lex
            {
                 rule_t comment = comment_p ("/*", "*/")
                                 | comment_p ("http://")
                                 ;
                 rule_t whitespace = space_p
                                 ;
                 rule_t include = str_p ("#include") >> *space_p >>
                                 (comment_p ("<", ">") | comment_p ("\"", "\""))
                                 ;
                 rule_t preprocessor = (include | "##" | "#define" | "#error" | ("#if" >> space_p)
                                     | "#ifdef" | "#ifndef" | "#else" | "#elif"
                                     | "#endif" | "#line" | "#pragma" | "#undef" | "#"
                                     | "__LINE__" | "__FILE__" | "__DATE__" | "__TIME__"
                                     | "_cplusplus" | "__STDC__")
                                     >> space_p
                                     ;
                 rule_t keyword_ = str_p ("asm") | "auto" | "bool" | "break" | "case"
                                 | "catch" | "char" | "class" | "const" | "const_cast"
                                 | "continue" | "default" | "delete" | "do" | "double"
                                 | "dynamic_cast" | "else" | "enum" | "explicit"
                                 | "extern" | "false" | "float" | "for" | "friend"
                                 | "goto" | "if" | "inline" | "int" | "long" | "mutable"
                                 | "namespace" | "new" | "operator" | "private"
                                 | "protected" | "public" | "register" | "reinterpret_cast"
                                 | "return" | "short" | "signed" | "sizeof" | "static"
                                 | "static_cast" | "struct" | "switch" | "template"
                                 | "this" | "throw" | "true" | "try" | "typedef" | "typeid"
                                 | "typename" | "union" | "unsighed" | "using" | "virtual"
                                 | "void" | "volatile" | "wchar_t" | "while"
                                 ;
                 rule_t keyword = keyword_ >> space_p
                                 ;
                 rule_t identifer = (alpha_p | '_') >> *(alnum_p | '_')
                                 ;
                     rule_t operators = punct_p - '`' - '@' - '$' - '\\'
                                     ;
                 rule_t number = real_p
                                 ;
                 rule_t str = confix_p ("\"", *c_escape_ch_p, "\"")
                                 ;
                 rule_t charcter = confix_p("\'", *c_escape_ch_p, "\'")
                                 ;
                 rule_t constant = number
                                 | str
                                 | charcter
                                 ;

            };



            ////////////////////////////////////////////////////////////////////////////
            //
            //       Main program
            //
            ////////////////////////////////////////////////////////////////////////////
            int
            main (int argc, char *argv[])
            {
                     string filepath;
                    if (2 > argc)
                     {//把要處理的文件拖到窗口內(nèi)
                             cout << "drag file to this windows\n";
                             string filepath_input;
                             getline(cin, filepath_input);
                             filepath_input.erase(filepath_input.end()-1); //去掉結(jié)尾的'\"'
                             filepath_input.erase(filepath_input.begin());   //去掉開頭"
                            for (int i = 0; filepath_input[i] != 0; i++) {
                                     filepath.push_back(filepath_input[i]);
                                    if (filepath_input[i] == '\\')
                                             filepath.push_back('\\');
                             }
                     }else{
                            // for console usage
                             filepath = argv[1];
                     }
                     iterator_t first (filepath);
                    if (!first)   {
                             std::cerr << "Unable to open file!\n";
                            return -1;
                     }

                    // Create an EOF iterator
                     iterator_t last = first.make_end ();

                     string filepath_output = filepath+".txt";
                     outfile.open(filepath_output.c_str());
                    // A simple rule詞法對應(yīng)的顏色在這里改就可以
                     rule_t r = *(
                             cpp_lex::comment[&to_ansi::cyan]
                             | cpp_lex::constant[&to_ansi::yellow]
                             | cpp_lex::preprocessor[&to_ansi::red]
                             | cpp_lex::keyword[&to_ansi::green]
                             | cpp_lex::whitespace[&to_ansi::echo]
                             | cpp_lex::operators[&to_ansi::magenta]
                             | cpp_lex::identifer[&to_ansi::white]
                             )
                             ;
                    //  
                    // Parse
                    /*The parse_info structure
                     The functions above return a parse_info structure parameterized by the iterator type passed in.
                     The parse_info struct has these members:parse_info
                     stop     Points to the final parse position (i.e The parser recognized and processed the input up to this point)
                     hit     True if parsing is successful. This may be full: the parser consumed all the input, or partial: the parser consumed only a portion of the input.
                     full     True when we have a full match (i.e The parser consumed all the input).
                     length     The number of characters consumed by the parser. This is valid only if we have a successful match (either partial or full).
                    */
                     parse_info < iterator_t > info = parse (first, last, r);

                    // This really shouldn't fail...
                    if (info.full)
                             std::cout << "\nParse succeeded!\n";
                    else
                             std::cout << "\nParse failed!\n";
                    
                    std::cout << "highlight file saved in " << filepath_output << endl;
                     string end;
                     getline (cin, end);//按回車鍵推出窗口

                    return 0;
            }


            這只是個詞法高亮程序,還不能識別語法,如函數(shù)等,用正則也能做,但boost.spirit更簡單


            Feedback

            # re: 用Boost.Spirit寫了一個term下c++詞法高亮  回復(fù)  更多評論   

            2012-03-10 03:43 by 王小賤
            你好,當(dāng)程序文件第一行以哈哈開頭,沒有注釋符號,會解析出錯。請問怎么只對定義的那些符號進(jìn)行著色,而別的字符只是照原本的方式輸出呢?

            posts - 94, comments - 138, trackbacks - 0, articles - 94

            Copyright © RichardHe

            热RE99久久精品国产66热| 91久久精品视频| 日韩人妻无码一区二区三区久久99| 久久久久久精品成人免费图片 | 久久99国产综合精品女同| 久久AⅤ人妻少妇嫩草影院| 久久人人爽人人爽人人片AV不 | 99久久综合国产精品免费| 亚洲午夜久久久久妓女影院 | 久久综合噜噜激激的五月天| 粉嫩小泬无遮挡久久久久久| 久久国产香蕉视频| 久久精品国产只有精品2020| 久久人人爽人人爽人人AV东京热| 国产精品99久久久久久www| 国产精品va久久久久久久| 久久AV无码精品人妻糸列| 草草久久久无码国产专区| 国内精品久久久久影院优| 蜜桃麻豆www久久国产精品| 亚洲精品无码久久不卡| 久久99这里只有精品国产| 免费国产99久久久香蕉| 久久精品成人免费观看97| 久久精品人人槡人妻人人玩AV| 麻豆一区二区99久久久久| 青青青青久久精品国产h久久精品五福影院1421| 无遮挡粉嫩小泬久久久久久久| 国产精品18久久久久久vr | 国内精品久久人妻互换| 久久人妻少妇嫩草AV蜜桃| 久久久久亚洲AV无码专区桃色 | 久久综合久久久| 国产成人精品白浆久久69| 久久久久亚洲Av无码专| 2021国产精品午夜久久| 久久99国产精品久久99小说| 久久九九久精品国产免费直播| 久久综合久久综合亚洲| 久久精品国产亚洲av水果派| 亚洲va久久久噜噜噜久久|