1 | #Region "Microsoft.VisualBasic::ab1e97816e7d7b62eb29ebd7b0246cc8, Microsoft.VisualBasic.Core\Scripting\TokenIcer\ParserCommon.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 | ' Module ParserCommon |
35 | ' |
36 | ' Function: [As], [CType], [TryCast], GetCodeComment, GetTokens |
37 | ' StartEscaping, TokenParser |
38 | ' |
39 | ' |
40 | ' /********************************************************************************/ |
41 | |
42 | #End Region |
43 | |
44 | Imports System.Runtime.CompilerServices |
45 | Imports Microsoft.VisualBasic.Language |
46 | |
47 | Namespace Scripting.TokenIcer |
48 | |
49 | ''' <summary> |
50 | ''' Generally expression parser codes |
51 | ''' </summary> |
52 | Public Module ParserCommon |
53 | |
54 | ''' <summary> |
55 | ''' 当前的token对象之中是否是转义的起始,即当前的token之中的最后一个符号是否是转移符<paramref name="escape"/>? |
56 | ''' </summary> |
57 | ''' <param name="buffer"></param> |
58 | ''' <param name="escape"></param> |
59 | ''' <returns></returns> |
60 | <Extension> |
61 | Public Function StartEscaping(buffer As List(Of Char), Optional escape As Char = "\"c) As Boolean |
62 | If buffer.IsNullOrEmpty Then |
63 | Return False |
64 | Else |
65 | Return buffer.Last = escape |
66 | End If |
67 | End Function |
68 | |
69 | ''' <summary> |
70 | ''' 假若返回来的是空字符串,则说明不是注释行 |
71 | ''' </summary> |
72 | ''' <param name="line$"></param> |
73 | ''' <param name="prefix$">The prefix of the code comment character/string</param> |
74 | ''' <returns></returns> |
75 | <Extension> |
76 | Public Function GetCodeComment(line$, ParamArray prefix$()) As String |
77 | Dim code As String = Trim(line) |
78 | |
79 | For Each s As String In prefix |
80 | If InStr(code, s, CompareMethod.Text) = 1 Then |
81 | Return Mid(code, s.Length) |
82 | End If |
83 | Next |
84 | |
85 | Return Nothing |
86 | End Function |
87 | |
88 | <Extension> |
89 | Public Function GetTokens(Of Tokens As IComparable)(parser As TokenParser(Of Tokens), expr As String) As Token(Of Tokens)() |
90 | Dim lstToken As New List(Of Token(Of Tokens)) |
91 | Dim tmp As New Value(Of Token(Of Tokens)) |
92 | |
93 | parser.InputString = expr |
94 | Do While Not (tmp = parser.GetToken) Is Nothing |
95 | Call lstToken.Add(+tmp) |
96 | Loop |
97 | |
98 | Return lstToken.ToArray |
99 | End Function |
100 | |
101 | ''' <summary> |
102 | ''' |
103 | ''' </summary> |
104 | ''' <typeparam name="Tokens"></typeparam> |
105 | ''' <param name="parser"></param> |
106 | ''' <param name="expr">表达式字符串</param> |
107 | ''' <param name="stackT"></param> |
108 | ''' <returns></returns> |
109 | <Extension> |
110 | Public Function TokenParser(Of Tokens As IComparable)(parser As TokenParser(Of Tokens), |
111 | expr$, |
112 | stackT As StackTokens(Of Tokens)) As Func(Of Tokens) |
113 | |
114 | Dim lstToken As Token(Of Tokens)() = parser.GetTokens(expr) |
115 | Dim whiteSpace As Tokens = stackT.WhiteSpace |
116 | Dim source As Token(Of Tokens)() = LinqAPI.Exec(Of Token(Of Tokens)) <= |
117 | _ |
118 | From x As Token(Of Tokens) |
119 | In lstToken |
120 | Where Not stackT.Equals(x.name, whiteSpace) |
121 | Select x |
122 | |
123 | Dim func As Func(Of Tokens) = |
124 | StackParser.Parsing(Of Tokens)(source, stackT) |
125 | Return func |
126 | End Function |
127 | |
128 | ''' <summary> |
129 | ''' Dynamics casting the token value expression as target type object. |
130 | ''' </summary> |
131 | ''' <typeparam name="Tokens"></typeparam> |
132 | ''' <typeparam name="T"></typeparam> |
133 | ''' <param name="x"></param> |
134 | ''' <returns></returns> |
135 | <Extension> Public Function [As](Of Tokens As IComparable, T)(x As Token(Of Tokens)) As T |
136 | Dim obj As T = InputHandler.CTypeDynamic(Of T)(x.Value) |
137 | Return obj |
138 | End Function |
139 | |
140 | ''' <summary> |
141 | ''' Dynamics casting the token value expression as target type object. |
142 | ''' </summary> |
143 | ''' <typeparam name="Tokens"></typeparam> |
144 | ''' <param name="x"></param> |
145 | ''' <param name="type"></param> |
146 | ''' <returns></returns> |
147 | <Extension> Public Function [CType](Of Tokens As IComparable)(x As Token(Of Tokens), type As Type) As Object |
148 | Dim obj As Object = InputHandler.CTypeDynamic(x.Value, type) |
149 | Return obj |
150 | End Function |
151 | |
152 | ''' <summary> |
153 | ''' Try cast the token value to a .NET object based on the token type name. |
154 | ''' </summary> |
155 | ''' <typeparam name="Tokens"></typeparam> |
156 | ''' <param name="x"></param> |
157 | ''' <returns></returns> |
158 | <Extension> Public Function [TryCast](Of Tokens As IComparable)(x As Token(Of Tokens)) As Object |
159 | Dim typeName As String = Scripting.ToString(x.name) |
160 | Dim type As New Value(Of Type) |
161 | |
162 | If type = Scripting.GetType(typeName, False) Is Nothing Then |
163 | Return x.Value |
164 | Else |
165 | Return CTypeDynamic(x.Value, +type) |
166 | End If |
167 | End Function |
168 | End Module |
169 | End Namespace |