1 #Region "Microsoft.VisualBasic::e52326c9f906543d245f7cac8deccee0, Microsoft.VisualBasic.Core\Scripting\VBLanguage.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class Patterns
35     
36     '         Constructor: (+1 OverloadsSub New
37     
38     '     Class KeywordProcessor
39     
40     '         Properties: Words
41     
42     '         Constructor: (+1 OverloadsSub New
43     '         Function: AutoEscapeVBKeyword
44     
45     
46     ' /********************************************************************************/
47
48 #End Region
49
50 Imports System.Runtime.CompilerServices
51
52 Namespace Scripting.SymbolBuilder.VBLanguage
53
54     Public NotInheritable Class Patterns
55
56         Private Sub New()
57         End Sub
58
59         ''' <summary>
60         ''' 匹配一个合法的标识符,在正则匹配的时候应该不区分大小写
61         ''' </summary>
62         Public Const Identifer$ = "\[?[_a-z][_a-z0-9]*\]?"
63
64         Public Const Access$ = "((Partial )|(Public )|(Private )|(Friend )|(Protected )|(Shadows )|(Shared )|(Overrides )|(Overloads )|(Overridable )|(MustOverrides )|(NotInheritable )|(MustInherit ))*"
65         Public Const Type$ = "^\s*" & Access & "((Class)|(Module)|(Structure)|(Enum)|(Delegate)|(Interface))\s+" & VBLanguage.Patterns.Identifer
66         Public Const Property$ = "^\s+" & Access & "\s*((ReadOnly )|(WriteOnly )|(Default ))*\s*Property\s+" & VBLanguage.Patterns.Identifer
67         Public Const Method$ = "^\s+" & Access & "\s*((Sub )|(Function )|(Iterator )|(Operator ))+\s*" & VBLanguage.Patterns.Identifer
68         Public Const Operator$ = "^\s+" & Access & "\s*Operator\s+(([<]|[>]|\=|\+|\-|\*|/|\^|\\)+|(" & VBLanguage.Patterns.Identifer & "))"
69         Public Const Close$ = "^\s+End\s((Sub)|(Function)|(Class)|(Structure)|(Enum)|(Interface)|(Operator)|(Module))"
70         Public Const CloseType$ = "^\s*End\s((Class)|(Structure)|(Enum)|(Interface)|(Module))"
71         Public Const Indents$ = "^\s+"
72         Public Const Attribute$ = "<.+?>\s*"
73
74     End Class
75
76     ''' <summary>
77     ''' Keyword processor of the VB.NET language
78     ''' </summary>
79     Public NotInheritable Class KeywordProcessor
80
81         ''' <summary>
82         ''' List of VB.NET language keywords
83         ''' </summary>
84         Public Const VBKeywords$ =
85             "|AddHandler|AddressOf|Alias|And|AndAlso|As|" &
86             "|Boolean|ByRef|Byte|" &
87             "|Call|Case|Catch|CBool|CByte|CChar|CDate|CDec|CDbl|Char|CInt|Class|CLng|CObj|Const|Continue|CSByte|CShort|CSng|CStr|CType|CUInt|CULng|CUShort|" &
88             "|Date|Decimal|Declare|Default|Delegate|Dim|DirectCast|Do|Double|" &
89             "|Each|Else|ElseIf|End|EndIf|Enum|Erase|Error|Event|Exit|" &
90             "|False|Finally|For|Friend|Function|" &
91             "|Get|GetType|GetXMLNamespace|Global|GoSub|GoTo|" &
92             "|Handles|" &
93             "|If|Implements|Imports|In|Inherits|Integer|Interface|Is|IsNot|" &
94             "|Let|Lib|Like|Long|Loop|" &
95             "|Me|Mod|Module|MustInherit|MustOverride|MyBase|MyClass|" &
96             "|Namespace|Narrowing|New|Next|Not|Nothing|NotInheritable|NotOverridable|NameOf|" &
97             "|Object|Of|On|Operator|Option|Optional|Or|OrElse|Overloads|Overridable|Overrides|" &
98             "|ParamArray|Partial|Private|Property|Protected|Public|" &
99             "|RaiseEvent|ReadOnly|ReDim|REM|RemoveHandler|Resume|Return|" &
100             "|SByte|Select|Set|Shadows|Shared|Short|Single|Static|Step|Stop|String|Structure|Sub|SyncLock|" &
101             "|Then|Throw|To|True|Try|TryCast|TypeOf|" &
102             "|Variant|" &
103             "|Wend|" &
104             "|UInteger|ULong|UShort|Using|" &
105             "|When|While|Widening|With|WithEvents|WriteOnly|" &
106             "|Xor|" &
107             "|Yield|"
108
109         ''' <summary>
110         ''' Tokenize of <see cref="VBKeywords"/>
111         ''' </summary>
112         ''' <returns></returns>
113         Public ReadOnly Property Words As String()
114             <MethodImpl(MethodImplOptions.AggressiveInlining)>
115             Get
116                 Return VBKeywords _
117                     .Split("|"c) _
118                     .Where(Function(s) Not s.StringEmpty) _
119                     .ToArray
120             End Get
121         End Property
122
123         Private Sub New()
124         End Sub
125
126         ''' <summary>
127         ''' Escaping the vb variable name when it conflicts with VB keywords name, 
128         ''' this function can be using for the VB.NET related code generator.
129         ''' </summary>
130         ''' <param name="name$">The identifier name.</param>
131         ''' <returns>If the identifier is a VB.NET keyword, then it will be escaping and returns, 
132         ''' otherwise, will do nothing, function returns the raw input identifier.
133         ''' </returns>
134         Public Shared Function AutoEscapeVBKeyword(name$) As String
135             If InStr(VBKeywords, $"|{name}|", CompareMethod.Text) > 0 Then
136                 Return $"[{name}]"
137             Else
138                 Return name
139             End If
140         End Function
141     End Class
142 End Namespace