1 #Region "Microsoft.VisualBasic::41c12ad31e1a171ea9928ac4b9638f9a, Microsoft.VisualBasic.Core\Scripting\TokenIcer\LangModels\Token.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 Token
35     
36     '         Properties: Arguments, Closure, IsClosure, IsFunction, IsNumeric
37     '                     IsObject, name, Text, Type, UNDEFINED
38     '                     Value
39     
40     '         Constructor: (+3 OverloadsSub New
41     '         FunctionGetValue, ToString
42     
43     '     Class Statement
44     
45     '         Properties: tokens, Trace
46     
47     '         FunctionToString
48     
49     '     Class Main
50     
51     '         Properties: program
52     
53     
54     ' /********************************************************************************/
55
56 #End Region
57
58 Imports System.Xml.Serialization
59 Imports Microsoft.VisualBasic.Language
60 Imports Microsoft.VisualBasic.Text.Xml.Models
61
62 Namespace Scripting.TokenIcer
63
64     ''' <summary>
65     ''' a Token object class, This defines the Token object
66     ''' </summary>
67     ''' <typeparam name="Tokens">应该是枚举类型</typeparam>
68     ''' <remarks>
69     ''' A Token object holds the token and token value.
70     ''' </remarks>
71     Public Class Token(Of Tokens As IComparable) : Implements Value(Of String).IValueOf
72
73         ''' <summary>
74         ''' Token type
75         ''' </summary>
76         ''' <returns></returns>
77         <XmlAttribute("name")> Public Property name As Tokens
78
79         ''' <summary>
80         ''' 函数参数列表
81         ''' </summary>
82         ''' <returns></returns>
83         Public Property Arguments As Statement(Of Tokens)()
84         ''' <summary>
85         ''' 这个token所拥有的闭包下一级代码,这个属性的值可以看作为具体的函数体
86         ''' </summary>
87         ''' <returns></returns>
88         Public Property Closure As Main(Of Tokens)
89
90         ''' <summary>
91         ''' The text that makes up the token.
92         ''' </summary>
93         ''' <returns></returns>
94         <XmlText> Public Property Value As String Implements Value(Of String).IValueOf.value
95
96         ''' <summary>
97         ''' You must keep the UNDEFINED type equals to ZERO!.
98         ''' (务必要保持0为未定义,如果不是使用零来定义未定义类型,则不能够使用这个属性来判断)
99         ''' </summary>
100         ''' <returns></returns>
101         Public ReadOnly Property UNDEFINED(Optional int% = 0) As Boolean
102             Get
103                 If TypeOf name Is [Enum] OrElse TypeOf name Is Integer Then
104                     Dim o As Object = name
105                     Dim i As Integer = CInt(o)
106                     If i = int Then
107                         Return True
108                     End If
109                 End If
110
111                 Return name Is Nothing OrElse
112                     String.IsNullOrEmpty(Value)
113             End Get
114         End Property
115
116         ''' <summary>
117         ''' The token type, this property is an alias of <see cref="name"/> property
118         ''' </summary>
119         ''' <returns></returns>
120         Public ReadOnly Property Type As Tokens
121             Get
122                 Return name
123             End Get
124         End Property
125
126         ''' <summary>
127         ''' Gets the text value that make up this token value, this readonly property 
128         ''' is an alias of the <see cref="Value"/> property.
129         ''' </summary>
130         ''' <returns></returns>
131         Public ReadOnly Property Text As String
132             Get
133                 Return Value
134             End Get
135         End Property
136
137         ''' <summary>
138         ''' Returns a Boolean value indicating whether an expression can be evaluated as
139         ''' a number.
140         ''' </summary>
141         ''' <returns></returns>
142         Public ReadOnly Property IsNumeric As Boolean
143             Get
144                 Return Information.IsNumeric(Text)
145             End Get
146         End Property
147
148         Public ReadOnly Property IsFunction As Boolean
149             Get
150                 Return Not Arguments.IsNullOrEmpty
151             End Get
152         End Property
153
154         Public ReadOnly Property IsClosure As Boolean
155             Get
156                 Return Not Closure Is Nothing
157             End Get
158         End Property
159
160         ''' <summary>
161         ''' This token object eigther not a function and not a closure.
162         ''' </summary>
163         ''' <returns></returns>
164         Public ReadOnly Property IsObject As Boolean
165             Get
166                 Return Not IsClosure AndAlso Not IsFunction
167             End Get
168         End Property
169
170         Public Sub New(name As Tokens, value$)
171             Me.name = name
172             Me.Value = value
173         End Sub
174
175         Sub New(name As Tokens)
176             Me.name = name
177         End Sub
178
179         Sub New()
180         End Sub
181
182         Public Overrides Function ToString() As String
183             If UNDEFINED Then
184                 Return "* " & Value
185             Else
186                 Return $"[{name}] {Value}"
187             End If
188         End Function
189
190         Public Function GetValue() As Object
191             Return Me.TryCast
192         End Function
193     End Class
194
195     ''' <summary>
196     ''' The statement line that parsing from the script text
197     ''' </summary>
198     Public Class Statement(Of T As IComparable)
199
200         <XmlElement("t")> Public Property tokens As Token(Of T)()
201         ''' <summary>
202         ''' The script text raw data?
203         ''' </summary>
204         ''' <returns></returns>
205         Public Property Trace As LineValue
206
207         Public Overrides Function ToString() As String
208             Return tokens _
209                 .Select(Function(t) t.ToString) _
210                 .JoinBy(" ")
211         End Function
212     End Class
213
214     ''' <summary>
215     ''' inner closure or program main
216     ''' </summary>
217     ''' <typeparam name="T"></typeparam>
218     Public Class Main(Of T As IComparable)
219         Public Property program As Statement(Of T)()
220     End Class
221 End Namespace