-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathParse.ahk
127 lines (104 loc) · 4.76 KB
/
Parse.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Function: Parse
Options parser.
O - String with options.
pQ - Query parameter. It is a space separated list of option names you want to extract from the options string. See bellow for
details of extraction.
o1..o10 - Variables to receive requested options. Their number should match the number of variables you want to extract from the option string plus
1 more if you want to get non-consumed options.
Query:
Query is space separated list of variables you want to extract with optional settings prefix .
The query syntax is:
> Query :: [Settings)][Name=]Value {Query}
> Settings :: sC|aC|qC|eC|c01{Settings}
> Value :: SentenceWithoutSpace | 'Sentence With Space'
Examples:
> options = x20 y40 w0 h0 red HWNDvar gLabel
> options = w800 h600 style='Resize ToolWindow' font='s12 bold, Courier New' 'show' dummy=dummy=5
Extracting:
To extract variables from the option string you first use query parameter to specify how to do extraction
then you supply variables to hold the results:
> Parse(O, "x# y# h# red? HWND* style font 1 3", x, y, h, bRed, HWND, style, font, p1, p3)
name - Get the option by the name (style, font). In option string, option must be followed by assignment char (= by default).
N - Get option by its position in the options string, (1 3).
str* - Get option that has str prefix (HWND*).
str# - Get option that holds the number and have str prefix (x# y# h#).
str? - Boolean option, output is true if str exists in the option string or false otherwise (red?).
Settings:
You can change separator(s), assignment(a), escape(e) and quote(q) character and case sensitivity (c) using syntax similar to RegExMatch.
Option value follows the option name without any separator character.
> Parse("x25|y27|Style:FLAT TOOLTIP", "s|a:c1)x# y# style", x, y, style)
In above example, | is used as separator, : is used as assignment char and case sensitivity is turned on (style wont be found as it starts with S in options string).
sC - Set separator char to C (default is space). Parameters are separated by this char except if it is found inside quotes (specified by quote char qC).
aC - Set assignment char to C (default is =). Assigment char is used to name parameter. Everything after aC will be set as parameter value, everything before as parameter name.
qC - Set quote char to C (default is '). Quote char is used to delimit parameter values that contain separator char.
eC - Set escape char to C (default is `). Escape char can be used to specify characters sC, aC, qC and eC inside parameter string.
cN - Set case sensitivity to number N (default is 0 = off, 1 = on).
Remarks:
Currently you can extract maximum 10 options at a time, but this restriction can be removed for up to 29 options.
You can specify one more reference variable in addition to those you want to extract to get all extra options in the option string.
Returns:
Number of options in the string.
About:
o v2.0 by majkinetor.
o Licenced under BSD <http://creativecommons.org/licenses/BSD/>.
*/
Parse(O, pQ, ByRef o1="",ByRef o2="",ByRef o3="",ByRef o4="",ByRef o5="",ByRef o6="",ByRef o7="",ByRef o8="", ByRef o9="", ByRef o10=""){
cS := " ", cA := "=", cQ := "'", cE := "``", cC := 0
if (j := InStr(pQ, ")")) && (opts := SubStr(pQ, 1, j-1), pQ := SubStr(pQ, j+1))
Loop, parse, opts
mod(A_Index, 2) ? f:=A_LoopField : c%f%:=A_LoopField
p__0 := 0, st := "n"
loop, parse, O ; states: next(n), separator(s), quote(q), escape(e)
{
c := A_LoopField
if (c = cS) {
if !InStr("qs", st) ;is state is not q or s.
p__0++, p__%p__0% := token, token := "", st := "s"
}
else if (c = cE)
{
ifNotEqual, st, e, SetEnv, st, e
else st := "n"
}
else if (c = cQ) && (st != "e") {
ifNotEqual, st, q, SetEnv, st, q ;`cA (!) `cQ `cS `cE `` ``
else st := "n"
}
else if (c = cA) && (st != "e") {
p__%p__0%_name := 1
st := "n"
}
else ifNotEqual, st, q, SetEnv, st, n
if st not in s,e
token .= c
}
if (token != "")
p__0++, p__%p__0% := token
loop, parse, pQ, %A_Space%
{
c := SubStr(f := A_LoopField, 0), n := InStr("?#*", c) ? SubStr(f, 1, -1) : f, j := A_Index, o := ""
if n is integer
o := p__%n%, p__%n% := ""
else {
l := StrLen(n)
loop, %p__0% {
p := p__%A_Index%
if (!cC && !(SubStr(p, 1, l) = n)) || (cC & !(SubStr(p, 1, l) == n))
continue
v := SubStr(p,l+1,1) = cA ? SubStr(p,l+2) : SubStr(p, l+1)
ifEqual, c, ?, SetEnv, v, 1
if (c="#") && (v+0 = "")
continue
o := v, p__%A_Index% := ""
break
}
}
o%j% := SubStr(o, 1, 1) = cQ ? SubStr(o, 2, -1) : o
}
j++
loop, %p__0%
o%j% .= p__%A_Index% != "" ? p__%A_Index% cS : ""
o%j% := SubStr(o%j%, 1, -1)
return p__0
}