1 | #Region "Microsoft.VisualBasic::c4d7eb501c6878ec67a094d8ee87992b, Microsoft.VisualBasic.Core\CommandLine\Parsers\CLIParser.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 CLIParser |
35 | ' |
36 | ' Function: GetTokens |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Runtime.CompilerServices |
44 | Imports Microsoft.VisualBasic.Emit.Marshal |
45 | Imports Microsoft.VisualBasic.Language |
46 | Imports Microsoft.VisualBasic.Scripting.TokenIcer |
47 | Imports Microsoft.VisualBasic.Text |
48 | |
49 | Namespace CommandLine.Parsers |
50 | |
51 | ''' <summary> |
52 | ''' 命令行单词解析器 |
53 | ''' </summary> |
54 | Public Module CLIParser |
55 | |
56 | ''' <summary> |
57 | ''' 非正则表达式命令行解析引擎 |
58 | ''' </summary> |
59 | ''' <param name="CLI$"></param> |
60 | ''' <returns></returns> |
61 | ''' <remarks> |
62 | ''' + 双引号表示一个完整的token |
63 | ''' + 空格为分隔符 |
64 | ''' </remarks> |
65 | <Extension> Public Function GetTokens(CLI$) As String() |
66 | Dim buffer As New Pointer(Of Char)(CLI) |
67 | Dim tokens As New List(Of String) |
68 | Dim tmp As New List(Of Char) |
69 | Dim c As Char |
70 | Dim quotOpen As Boolean = False |
71 | |
72 | Do While Not buffer.EndRead |
73 | c = (+buffer) |
74 | |
75 | If quotOpen Then |
76 | |
77 | ' 双引号是结束符,但是可以使用\"进行转义 |
78 | If c <> ASCII.Quot Then |
79 | tmp += c |
80 | Else |
81 | If tmp.StartEscaping Then |
82 | tmp.RemoveLast |
83 | tmp += c |
84 | Else |
85 | ' 结束 |
86 | tokens += tmp.CharString |
87 | tmp *= 0 |
88 | quotOpen = False |
89 | |
90 | End If |
91 | End If |
92 | |
93 | Else |
94 | If c = ASCII.Quot AndAlso tmp = 0 Then |
95 | quotOpen = True |
96 | ElseIf c = " "c Then |
97 | ' 分隔符 |
98 | If tmp <> 0 Then |
99 | tokens += tmp.CharString |
100 | tmp *= 0 |
101 | End If |
102 | Else |
103 | tmp += c |
104 | End If |
105 | End If |
106 | Loop |
107 | |
108 | If tmp <> 0 Then |
109 | tokens += New String(tmp) |
110 | End If |
111 | |
112 | Return tokens |
113 | End Function |
114 | End Module |
115 | End Namespace |