完整的VB.NET的语法解析程序


原文地址:http://www.chinaitpower.com/A200508/2005-08-07/186412.html

比较长,不过支持全部的关键字,直接就可以用了。

  1using System;
  2using System.Text;
  3using System.Text.RegularExpressions;
  4
  5namespace Com.OSLeague.Component
  6{
  7/// <summary>
  8/// 语法分析器,将所有Code根据语法进行变色
  9/// <list type="VB">支持VB.NET</list>
 10/// <list type="CS">支持CS</list>
 11/// <author>掉掉</author>
 12/// <date>2002年5月14日</date>
 13/// <Memo>
 14/// 练习正则表达式
 15/// </Memo>
 16/// </summary>

 17public class CodeAnalysis
 18{
 19
 20//
 21//定义HTML开始和结束的语句,用于语法变色
 22//
 23
 24const string TAG_FNTRED = @"<font color=""red"">";
 25const string TAG_FNTBLUE = @"<font color=""blue"">" ;
 26const string TAG_FNTGRN = @"<font color=""green"">" ;
 27const string TAG_FNTMRN = @"<font color=""maroon"">" ;
 28const string TAG_FNTBLACK = @"<font color=""black"">" ;
 29const string TAG_EFONT = @"</font>" ;
 30const string TAG_SPNYELLOW = @"<span style=""background-color: yellow;"">";
 31const string TAG_ESPAN = @"</span>";
 32const string TAG_B = @"<b>";
 33const string TAG_EB = @"</b>";
 34const string TAG_COMMENT = @"<font colr=#008200>";
 35const string TAG_ECOMMENT = @"</font>";
 36
 37//
 38
 39
 40
 41public CodeAnalysis()
 42{
 43//
 44// TODO: 在此处添加构造函数逻辑
 45//
 46}

 47
 48
 49
 50/// <summary>
 51/// 处理VB.NET代码,彩色化..
 52/// </summary>
 53/// <param name="Code">传入的Code</param>
 54/// <returns>处理过后的代码</returns>

 55public string ParseVB(string Code)
 56{
 57//
 58//定义VB.NET中关键字,将其存为数组
 59//
 60
 61string[] VB_Keyword = new string[]
 62{
 63"AddHandler","AddressOf","AndAlso","Alias","And","Ansi","As","Assembly","Auto","Boolean",
 64"ByRef","Byte","ByVal","Call","Case","Catch","CBool","CByte","CChar",
 65"CDate","CDec","CDbl","Char","CInt","Class","CLng","CObj","Const",
 66"CShort","CSng","CStr","CType","Date","Decimal","Declare","Default",
 67"Delegate","Dim","DirectCast","Do","Double","Each","Else","ElseIf","End",
 68"Enum","Erase","Error","Event","Exit","False",
 69"Finally","For","Friend","Function","Get","GetType","GoTo","Handles","If",
 70"Implements","Imports","In","Inherits","Integer","Interface",
 71"Is","Let","Lib","Like","Long","Loop","Me","Mod","Module",
 72"MustInherit","MustOverride","MyBase","MyClass","Namespace","New","Next","Not","Nothing",
 73"NotInheritable","NotOverridable","Object","On","Option","Optional","Or","OrElse",
 74"Overloads","Overridable","Overrides","ParamArray","Preserve","Private","Property","Protected","Public",
 75"RaiseEvent","ReadOnly","ReDim","RemoveHandler","Resume","Return",
 76"Select","Set","Shadows","Shared","Short","Single","Static","Step","Stop",
 77"String","Structure","Sub","SyncLock","Then","Throw",
 78"To","True","Try","TypeOf","Unicode","Until","Variant","When","While",
 79"With","WithEvents","WriteOnly","Xor"
 80}
;
 81
 82
 83
 84//
 85//设定转换代码颜色
 86//
 87
 88string ReplaceVBComment = TAG_COMMENT + "$1" + TAG_ECOMMENT;
 89string ReplaceVBKeyword = TAG_FNTBLUE + "${char}" + TAG_EFONT;
 90//开始转换
 91for (int i=0;i<VB_Keyword.Length;i++)
 92{
 93string TempDirectives = @"(?<char>(\s" + VB_Keyword[i] + "|" + VB_Keyword[i] + @"\s))";
 94Code = Regex.Replace(Code,TempDirectives,ReplaceVBKeyword,RegexOptions.IgnoreCase);
 95Code = Regex.Replace(Code,@"'(?<x>[^\r\n]*)",ReplaceVBComment);
 96Code = Regex.Replace(Code,@"REM (?<x>[^\r\n]*)",ReplaceVBComment);
 97}

 98return Code; 
 99}

100}

101}
posted @ 2006-04-25 21:42  ahwey  阅读(298)  评论(0)    收藏  举报