1 #Region "Microsoft.VisualBasic::2803be39c8bca883c581fee23cecb813, Microsoft.VisualBasic.Core\Scripting\TokenIcer\Prefix.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 Prefix
35     
36     
37     '         Enum MathTokens
38     
39     
40     
41     
42     '  
43     
44     '     Properties: MathParser
45     
46     '     Function: __getMathParser, IsScientificNotation, MathExpression, MathParserHash
47     
48     
49     ' /********************************************************************************/
50
51 #End Region
52
53 Imports System.Runtime.CompilerServices
54 Imports System.Text.RegularExpressions
55 Imports Microsoft.VisualBasic.Scripting.Runtime
56
57 Namespace Scripting.TokenIcer
58
59     ''' <summary>
60     ''' 预定义的一些脚本的解析程序
61     ''' </summary>
62     Public Module Prefix
63
64         Public Const undefined$ = NameOf(MathTokens.UNDEFINED)
65
66         ' This is our token enumeration. It holds every token defined in the grammar
67         ''' <summary>
68         ''' Tokens is an enumeration of all possible token values.
69         ''' </summary>
70         Public Enum MathTokens
71             UNDEFINED = 0
72             CallFunc = 1
73             Float = 2
74             Factorial = 3
75             [Integer] = 4
76             ArrayType = 5
77             ParamDeli = 6
78             WhiteSpace = 7
79             [Let] = 8
80             Equals = 9
81             LPair = 10
82             RPair = 11
83             Asterisk = 12
84             Slash = 13
85             Plus = 14
86             Minus = 15
87             Power = 16
88             [Mod] = 17
89             Pretend = 18
90             [And] = 19
91             [Not] = 20
92             [Or] = 21
93             var = 22
94             varRef = 23
95             constRef = 24
96         End Enum
97
98         ReadOnly _mathStack As New StackTokens(Of MathTokens)(Function(a, b) a = b) With {
99             .LPair = MathTokens.LPair,
100             .ParamDeli = MathTokens.ParamDeli,
101             .Pretend = MathTokens.Pretend,
102             .RPair = MathTokens.RPair,
103             .WhiteSpace = MathTokens.WhiteSpace
104         }
105
106         Public Function MathExpression(expr As StringAs Func(Of MathTokens)
107             SyncLock MathParser
108                 Return MathParser.TokenParser(expr, _mathStack)
109             End SyncLock
110         End Function
111
112         Public ReadOnly Property MathParser As TokenParser(Of MathTokens) =
113             __getMathParser()
114
115         Private Function __getMathParser() As TokenParser(Of MathTokens)
116             Return New TokenParser(Of MathTokens)(MathParserHash, MathTokens.UNDEFINED)
117         End Function
118
119         ''' <summary>
120         ''' These lines add each grammar rule to the dictionary
121         ''' </summary>
122         ''' <returns></returns>
123         Public Function MathParserHash() As Dictionary(Of MathTokens, String)
124             Dim tokens As New Dictionary(Of MathTokens, String)
125
126             tokens.Add(MathTokens.CallFunc, "->\s*[a-zA-Z_][a-zA-Z0-9_]*")
127             tokens.Add(MathTokens.Float, "[0-9]+\.+[0-9]+")
128             tokens.Add(MathTokens.Factorial, "[0-9]+!")
129             tokens.Add(MathTokens.[Integer], "[0-9]+")
130             tokens.Add(MathTokens.ArrayType, "[a-zA-Z_][a-zA-Z0-9_]*\(\)")
131             tokens.Add(MathTokens.ParamDeli, ",")
132             tokens.Add(MathTokens.WhiteSpace, "[ \t]+")
133             tokens.Add(MathTokens.[Let], "[Ll][Ee][Tt]")
134             tokens.Add(MathTokens.Equals, "=")
135             tokens.Add(MathTokens.LPair, "\(")
136             tokens.Add(MathTokens.RPair, "\)")
137             tokens.Add(MathTokens.Asterisk, "\*")
138             tokens.Add(MathTokens.Slash, "\")
139             tokens.Add(MathTokens.Plus, "\+")
140             tokens.Add(MathTokens.Minus, "\-")
141             tokens.Add(MathTokens.Power, "\^")
142             tokens.Add(MathTokens.[Mod], "%")
143             tokens.Add(MathTokens.Pretend, "Pretend")
144             tokens.Add(MathTokens.[And], "[aA][nN][dD]")
145             tokens.Add(MathTokens.[Not], "[nN][oO][tT]")
146             tokens.Add(MathTokens.[Or], "[oO][rR]")
147             tokens.Add(MathTokens.var, "[a-zA-Z_][a-zA-Z0-9_]*")
148             tokens.Add(MathTokens.varRef, "\$[a-zA-Z0-9_]*")
149             tokens.Add(MathTokens.constRef, "[&][a-zA-Z0-9_]*")
150
151             Return tokens
152         End Function
153
154         ''' <summary>
155         ''' 这个字符串表达式是否是科学记数法的数字?
156         ''' </summary>
157         ''' <param name="s$"></param>
158         ''' <returns></returns>
159         <Extension>
160         Public Function IsScientificNotation(s$) As Boolean
161             Dim n$ = Regex.Match(s, ScientificNotation, RegexOptions.IgnoreCase).Value
162             Dim yes As Boolean = (n = s)
163             Return yes
164         End Function
165     End Module
166 End Namespace