VB.NET的文法解析程式

來源:互聯網
上載者:User
程式|文法 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>
    /// 練習Regex
    /// </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;    
        }
    }
}


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。