完整的VB.NET的语法解析程序
原文地址:http://www.chinaitpower.com/A200508/2005-08-07/186412.html
比较长,不过支持全部的关键字,直接就可以用了。
1
using System;
2
using System.Text;
3
using System.Text.RegularExpressions;
4
5
namespace 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>
17
public class CodeAnalysis
18
{
19
20
//
21
//定义HTML开始和结束的语句,用于语法变色
22
//
23
24
const string TAG_FNTRED = @"<font color=""red"">";
25
const string TAG_FNTBLUE = @"<font color=""blue"">" ;
26
const string TAG_FNTGRN = @"<font color=""green"">" ;
27
const string TAG_FNTMRN = @"<font color=""maroon"">" ;
28
const string TAG_FNTBLACK = @"<font color=""black"">" ;
29
const string TAG_EFONT = @"</font>" ;
30
const string TAG_SPNYELLOW = @"<span style=""background-color: yellow;"">";
31
const string TAG_ESPAN = @"</span>";
32
const string TAG_B = @"<b>";
33
const string TAG_EB = @"</b>";
34
const string TAG_COMMENT = @"<font colr=#008200>";
35
const string TAG_ECOMMENT = @"</font>";
36
37
//
38
39
40
41
public 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>
55
public string ParseVB(string Code)
56
{
57
//
58
//定义VB.NET中关键字,将其存为数组
59
//
60
61
string[] 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
88
string ReplaceVBComment = TAG_COMMENT + "$1" + TAG_ECOMMENT;
89
string ReplaceVBKeyword = TAG_FNTBLUE + "${char}" + TAG_EFONT;
90
//开始转换
91
for (int i=0;i<VB_Keyword.Length;i++)
92
{
93
string TempDirectives = @"(?<char>(\s" + VB_Keyword[i] + "|" + VB_Keyword[i] + @"\s))";
94
Code = Regex.Replace(Code,TempDirectives,ReplaceVBKeyword,RegexOptions.IgnoreCase);
95
Code = Regex.Replace(Code,@"'(?<x>[^\r\n]*)",ReplaceVBComment);
96
Code = Regex.Replace(Code,@"REM (?<x>[^\r\n]*)",ReplaceVBComment);
97
}
98
return Code;
99
}
100
}
101
}
using System;2
using System.Text;3
using System.Text.RegularExpressions;4

5
namespace Com.OSLeague.Component6
{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>17
public class CodeAnalysis18
{19

20
//21
//定义HTML开始和结束的语句,用于语法变色22
//23

24
const string TAG_FNTRED = @"<font color=""red"">";25
const string TAG_FNTBLUE = @"<font color=""blue"">" ;26
const string TAG_FNTGRN = @"<font color=""green"">" ;27
const string TAG_FNTMRN = @"<font color=""maroon"">" ;28
const string TAG_FNTBLACK = @"<font color=""black"">" ;29
const string TAG_EFONT = @"</font>" ;30
const string TAG_SPNYELLOW = @"<span style=""background-color: yellow;"">";31
const string TAG_ESPAN = @"</span>";32
const string TAG_B = @"<b>";33
const string TAG_EB = @"</b>";34
const string TAG_COMMENT = @"<font colr=#008200>";35
const string TAG_ECOMMENT = @"</font>";36

37
//38

39

40

41
public 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>55
public string ParseVB(string Code)56
{57
//58
//定义VB.NET中关键字,将其存为数组59
//60

61
string[] 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

88
string ReplaceVBComment = TAG_COMMENT + "$1" + TAG_ECOMMENT;89
string ReplaceVBKeyword = TAG_FNTBLUE + "${char}" + TAG_EFONT;90
//开始转换91
for (int i=0;i<VB_Keyword.Length;i++)92
{93
string TempDirectives = @"(?<char>(\s" + VB_Keyword[i] + "|" + VB_Keyword[i] + @"\s))";94
Code = Regex.Replace(Code,TempDirectives,ReplaceVBKeyword,RegexOptions.IgnoreCase);95
Code = Regex.Replace(Code,@"'(?<x>[^\r\n]*)",ReplaceVBComment);96
Code = Regex.Replace(Code,@"REM (?<x>[^\r\n]*)",ReplaceVBComment);97
}98
return Code; 99
}100
}101
}


浙公网安备 33010602011771号