-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSyntaxISEr.ps1
263 lines (227 loc) · 9.96 KB
/
SyntaxISEr.ps1
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#######################################################
#region SyntaxISEr ####################################
## Function Invoke-SyntaxISEr
## BackBone Tool for Invoke-CyberISEr & Co
<#
.Synopsis
ISE ScriptPane Explorer
.DESCRIPTION
ISE ScriptPane Content Exploration Utility.
Returns ASTree Elements, Tokens and Errors from Current ISE ScriptPane.
.EXAMPLE
Invoke-SyntaxISEr -Tree
Returns full Abstract Syntax Tree of Current ScriptPane
.EXAMPLE
Invoke-SyntaxISEr -TokenKindList
Returns a list of tokens present in current ISE Scriptpane
.EXAMPLE
Invoke-SyntaxISEr -TokenKind Comment
Returns all Comment Tokens
.EXAMPLE
(SyntaxISEr -TreeType FunctionDefinitionAst)[1]
Returns Second Function Definition Ast
.EXAMPLE
$ParentTree = (SyntaxISEr -TreeType FunctionDefinitionAst)[1]; SyntaxISEr -TokenKind Comment -ParentTree $ParentTree
Returns all Comment Tokens from the second function in Scriptpane
.EXAMPLE
SyntaxISEr -ErrorList
Returns Errors
.INPUTS
ISE ScriptPane
.OUTPUTS
Language: ASTree Obj | Token Obj | Error obj
Use object .extent props to set Editor caret selection and manipulate content.
Note: Sort Descending before editing multiple tokens (Foreach)
or things will get out of position.
.NOTES
Requires PoSh v3+ (Abstract Syntax Tree)
.ROLE
BackBone for Invoke-CyberISEr.
.FUNCTIONALITY
ISE ScriptPane Content to PoSh Objects for further manipulation.
#>
function Invoke-SyntaxISEr{
[CmdletBinding(DefaultParameterSetName='NoParam')]
[Alias('SyntaxISEr','Syntax')]
Param(
# Show Full Tree
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TreeAll')]
[Switch]$Tree,
# Show Tree with selected TreeType
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TreeByType')]
[ValidateSet('ArrayExpressionAst',
'ArrayLiteralAst',
'AssignmentStatementAst',
'Ast',
'AttributeAst',
'AttributeBaseAst',
'AttributedExpressionAst',
'BaseCtorInvokeMemberExpressionAst',
'BinaryExpressionAst',
'BlockStatementAst',
'BreakStatementAst',
'CatchClauseAst',
'CommandAst',
'CommandBaseAst',
'CommandElementAst',
'CommandExpressionAst',
'CommandParameterAst',
'ConfigurationDefinitionAst',
'ConstantExpressionAst',
'ContinueStatementAst',
'ConvertExpressionAst',
'DataStatementAst',
'DoUntilStatementAst',
'DoWhileStatementAst',
'DynamicKeywordStatementAst',
'ErrorExpressionAst',
'ErrorStatementAst',
'ExitStatementAst',
'ExpandableStringExpressionAst',
'ExpressionAst',
'FileRedirectionAst',
'ForEachStatementAst',
'ForStatementAst',
'FunctionDefinitionAst',
'FunctionMemberAst',
'HashtableAst',
'IfStatementAst',
'IndexExpressionAst',
'InvokeMemberExpressionAst',
'LabeledStatementAst',
'LoopStatementAst',
'MemberAst',
'MemberExpressionAst',
'MergingRedirectionAst',
'NamedAttributeArgumentAst',
'NamedBlockAst',
'ParamBlockAst',
'ParameterAst',
'ParenExpressionAst',
'PipelineAst',
'PipelineBaseAst',
'PropertyMemberAst',
'RedirectionAst',
'ReturnStatementAst',
'ScriptBlockAst',
'ScriptBlockExpressionAst',
'StatementAst',
'StatementBlockAst',
'StringConstantExpressionAst',
'SubExpressionAst',
'SwitchStatementAst',
'ThrowStatementAst',
'TrapStatementAst',
'TryStatementAst',
'TypeConstraintAst',
'TypeDefinitionAst',
'TypeExpressionAst',
'UnaryExpressionAst',
'UsingExpressionAst',
'UsingStatementAst',
'VariableExpressionAst',
'WhileStatementAst')]
[String]$TreeType,
# List Tree Types
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTreeType')]
[Switch]$TreeTypeList,
# Show All Tokens
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenAll')]
[Switch]$Token,
# Show Tokens with selected TokenFlag
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenByFlag')]
[System.Management.Automation.Language.TokenFlags]$TokenFlag,
# List Token Flags
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTokenFlag')]
[Switch]$TokenFlagList,
# Show Tokens with selected TokenKind
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenByKind')]
[System.Management.Automation.Language.TokenKind]$TokenKind,
# List Token Kind
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTokenKind')]
[Switch]$TokenKindList,
# Show Errors
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ErrorAll')]
[Switch]$ErrorList,
# Restrict search to parent AST scope
[Parameter(Mandatory=$false,ParameterSetName='TokenAll')]
[Parameter(Mandatory=$false,ParameterSetName='ListTokenFlag')]
[Parameter(Mandatory=$false,ParameterSetName='TokenByFlag')]
[Parameter(Mandatory=$false,ParameterSetName='ListTokenKind')]
[Parameter(Mandatory=$false,ParameterSetName='TokenByKind')]
[Parameter(Mandatory=$false,ParameterSetName='TreeAll')]
[Parameter(Mandatory=$false,ParameterSetName='ListTreeType')]
[Parameter(Mandatory=$false,ParameterSetName='TreeByType')]
[System.Management.Automation.Language.ast]$ParentTree=$null,
# Do not recurse tree search
[Parameter(Mandatory=$false,ParameterSetName='TreeByType')]
[Parameter(Mandatory=$false,ParameterSetName='ListTreeType')]
[Parameter(Mandatory=$false,ParameterSetName='TreeAll')]
[Switch]$NoRecurse
)
### PREP
# Scriptpane
$ScriptPane = $psISE.CurrentFile.Editor.text
# Init collectors for Tok/Err
$Tokens=$Errors=$null
# Parse ScriptPane
$AST = [System.Management.Automation.Language.Parser]::ParseInput($ScriptPane,[ref]$Tokens,[ref]$Errors)
# Tree Recurse?
[Bool]$Recurse = $True
If($NoRecurse){$Recurse = $false}
# WTF!?
$WTF = '¯\_(ツ)_/¯ "Oups!?"'
### ACTION
## Help
# If NoParam
If($PSCmdlet.ParameterSetName -eq 'NoParam'){Help SyntaxISEr}
## Tree
# If Tree
ElseIf($PSCmdlet.ParameterSetName -eq 'TreeAll'){
$AST.FindAll({$true},$Recurse) | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)}}
# If List TreeType
ElseIf($PSCmdlet.ParameterSetName -eq 'ListTreeType'){
$AST.FindAll({$true},$Recurse) | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)} | %{
$_.gettype().name} | sort -Unique
}
# If TreeType
ElseIf($PSCmdlet.ParameterSetName -eq 'TreeByType'){
$AST.findAll({$args[0] -is ("System.Management.Automation.Language.$TreeType" -as [Type])},$Recurse) | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)}}
## Token
# If Token
ElseIf($PSCmdlet.ParameterSetName -eq 'TokenAll'){
$Tokens | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)}}
# If List TokenFlag
ElseIf($PSCmdlet.ParameterSetName -eq 'ListTokenFlag'){
($Tokens | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)}).TokenFlags | sort -unique
}
# If List TokenKind
ElseIf($PSCmdlet.ParameterSetName -eq 'ListTokenKind'){
($Tokens | where {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)}).Kind | sort -unique
}
# If TokenKind
ElseIf($PSCmdlet.ParameterSetName -eq 'TokenByKind'){$Tokens | where {
$_.Kind -eq "$TokenKind" -and ($ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset))}}
# If TokenFlag
ElseIf($PSCmdlet.ParameterSetName -eq 'TokenByFlag'){$Tokens | where {
$_.TokenFlags -eq "$TokenFlag" -and ($ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset))}}
## Errors
# If ErrorAll
ElseIf($PSCmdlet.ParameterSetName -eq 'ErrorAll'){$Errors}
## DebugParam
# If Something else ?!?!?!
Else{write-host $WTF -ForegroundColor cyan}
}
#EndFunction
#endregion SyntaxISEr
#######################################################