• <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>

            用Boost.Spirit寫了一個term下c++詞法高亮

            Posted on 2008-07-08 11:49 RichardHe 閱讀(2188) 評論(1)  編輯 收藏 引用 所屬分類: [轉]
            出自http://hi.baidu.com/yesbaba/blog/item/79c3eb13215da0d2f7039ec9.html

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

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

                轉成html的功能以后不懶的時候再寫吧


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

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

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

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

            論壇可以用它來實現代碼高亮顯示
            以后再改進

            正確的代碼coco應該不會分析錯,能分析正確的不一定是正確的代碼

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

            再看如何分析文件

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

            gnu source highlight就可以實現這個功能了,我做這個一來是學spirit練練手,

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

            ///////////////////////////////////////////////////////////////////////////////
            //
            //       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函數,匹配后輸出成ansi代碼的彩色控制符,
            //可參考各大高校bbs的asciiart版精華區,可以輸出成需要的格式,如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就方便多了,識別函數名還沒實現
            ////////////////////////////////////////////////////////////////////////////
            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)
                     {//把要處理的文件拖到窗口內
                             cout << "drag file to this windows\n";
                             string filepath_input;
                             getline(cin, filepath_input);
                             filepath_input.erase(filepath_input.end()-1); //去掉結尾的'\"'
                             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詞法對應的顏色在這里改就可以
                     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;
            }


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


            Feedback

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

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

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

            Copyright © RichardHe

            99久久免费国产特黄| 人妻精品久久久久中文字幕一冢本 | 26uuu久久五月天| 国内精品久久久久久野外| 久久av高潮av无码av喷吹| 久久久久亚洲AV成人网| 伊人久久亚洲综合影院| 成人国内精品久久久久一区| 久久久WWW成人免费毛片| 日韩人妻无码精品久久久不卡| 国产成人AV综合久久| 热久久视久久精品18| 日本精品久久久中文字幕| 午夜精品久久久内射近拍高清 | 无码精品久久久久久人妻中字| 91性高湖久久久久| 久久亚洲AV成人无码软件| 精品久久人人做人人爽综合| 色欲av伊人久久大香线蕉影院| 久久精品夜色噜噜亚洲A∨| 午夜欧美精品久久久久久久| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 四虎国产精品免费久久| 国产成人久久精品区一区二区| 久久精品一区二区三区AV| 国内精品久久久久久久久| 国产成人精品久久一区二区三区| 日本欧美久久久久免费播放网| 亚洲国产精品一区二区三区久久 | 久久久受www免费人成| 狠狠久久亚洲欧美专区| 久久人妻少妇嫩草AV无码专区| 日韩精品久久无码中文字幕| 国产一区二区久久久| 欧美伊人久久大香线蕉综合| 久久综合久久综合亚洲| 欧美粉嫩小泬久久久久久久 | 看全色黄大色大片免费久久久| 久久久久亚洲AV综合波多野结衣 | 亚洲精品高清国产一线久久| 久久精品aⅴ无码中文字字幕不卡|