首页 > 编程 > .NET > 正文

VB.NET的语法解析程序

2024-07-10 13:02:36
字体:
来源:转载
供稿:网友
using system;
using system.text;
using system.text.regularexpressions;

namespace com.osleague.component
{
    /// <summary>
    /// 语法分析器,将所有code根据语法进行变色
    /// <list type="vb">支持vb.net</list>
    /// <list type="cs">支持cs</list>
    /// <author>掉掉</author>
    /// <date>2002年5月14日</date>
    /// <memo>
    /// 练习正则表达式
    /// </memo>
    /// </summary>
    public class codeanalysis
    {

        //
        //定义html开始和结束的语句,用于语法变色
        //

        const string tag_fntred    = @"<font color=""red"">";
        const string tag_fntblue   = @"<font color=""blue"">" ;
        const string tag_fntgrn    = @"<font color=""green"">" ;
        const string tag_fntmrn    = @"<font color=""maroon"">" ;
        const string tag_fntblack  = @"<font color=""black"">" ;
        const string tag_efont     = @"</font>" ;
        const string tag_spnyellow = @"<span style=""background-color: yellow;"">";
        const string tag_espan     = @"</span>";
        const string tag_b         = @"<b>";
        const string tag_eb        = @"</b>";
        const string tag_comment   = @"<font colr=#008200>";
        const string tag_ecomment   = @"</font>";

        //



        public codeanalysis()
        {
            //
            // todo: 在此处添加构造函数逻辑
            //
        }

    
        
        /// <summary>
        /// 处理vb.net代码,彩色化..
        /// </summary>
        /// <param name="code">传入的code</param>
        /// <returns>处理过后的代码</returns>
        public string parsevb(string code)
        {
            //
            //定义vb.net中关键字,将其存为数组
            //

            string[] vb_keyword = new string[]
            {
                "addhandler","addressof","andalso","alias","and","ansi","as","assembly","auto","boolean",
                "byref","byte","byval","call","case","catch","cbool","cbyte","cchar",
                "cdate","cdec","cdbl","char","cint","class","clng","cobj","const",
                "cshort","csng","cstr","ctype","date","decimal","declare","default",
                "delegate","dim","directcast","do","double","each","else","elseif","end",
                "enum","erase","error","event","exit","false",
                "finally","for","friend","function","get","gettype","goto","handles","if",
                "implements","imports","in","inherits","integer","interface",
                "is","let","lib","like","long","loop","me","mod","module",
                "mustinherit","mustoverride","mybase","myclass","namespace","new","next","not","nothing",
                "notinheritable","notoverridable","object","on","option","optional","or","orelse",
                "overloads","overridable","overrides","paramarray","preserve","private","property","protected","public",
                "raiseevent","readonly","redim","removehandler","resume","return",
                "select","set","shadows","shared","short","single","static","step","stop",
                "string","structure","sub","synclock","then","throw",
                "to","true","try","typeof","unicode","until","variant","when","while",
                "with","withevents","writeonly","xor"
            };

            

            //
            //设定转换代码颜色
            //

            string replacevbcomment = tag_comment + "$1" + tag_ecomment;
            string replacevbkeyword = tag_fntblue + "${char}" + tag_efont;
            //开始转换
            for (int i=0;i<vb_keyword.length;i++)
            {
                string tempdirectives = @"(?<char>(/s" + vb_keyword[i] + "|" + vb_keyword[i] + @"/s))";
                code = regex.replace(code,tempdirectives,replacevbkeyword,regexoptions.ignorecase);
                code = regex.replace(code,@"'(?<x>[^/r/n]*)",replacevbcomment);
                code = regex.replace(code,@"rem (?<x>[^/r/n]*)",replacevbcomment);
            }
            return code;    
        }
    }
}
国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表