Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ISE Script Explorer - Returns AST/Token/Error
#requires -version 3
#######################################################
#region SyntaxISEr ####################################
## Function Invoke-SyntaxISEr
## BackBone Tool for Invoke-CyberISEr - Exported (?)
<#
.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 -Error
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(
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TreeAll',HelpMessage='Show Full Tree')]
[Switch]$Tree,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TreeByType',HelpMessage='Show Tree with selected TreeType')]
[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,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTreeType',HelpMessage='List Tree Types')]
[Switch]$TreeTypeList,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenAll',HelpMessage='Show All Tokens')]
[Switch]$Token,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenByFlag',HelpMessage='Show Tokens with selected TokenFlag')]
[System.Management.Automation.Language.TokenFlags]$TokenFlag,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTokenFlag',HelpMessage='List Token Flags')]
[Switch]$TokenFlagList,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='TokenByKind',HelpMessage='Show Tokens with selected TokenKind')]
[System.Management.Automation.Language.TokenKind]$TokenKind,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ListTokenKind',HelpMessage='List Token Kind')]
[Switch]$TokenKindList,
[Parameter(Position=0,Mandatory=$true,ParameterSetName='ErrorAll',HelpMessage='Show Errors')]
[Switch]$Errors,
[Parameter(Mandatory=$false,HelpMessage='Restrict search to parent AST scope')]
[System.Management.Automation.Language.ast]$ParentTree=$null,
# Recurse tree search
[Parameter(Mandatory=$false,ParameterSetName='TreeByType',HelpMessage='Recurse tree search (default behavior)')]
[Parameter(Mandatory=$false,ParameterSetName='ListTreeType',HelpMessage='Recurse tree search (default behavior)')]
[Parameter(Mandatory=$false,ParameterSetName='TreeAll',HelpMessage='Recurse tree search (default behavior)')]
[Switch]$Recurse = $true
)
### PREP
# Scriptpane
$ScriptPane = $psISE.CurrentFile.Editor.text
# Init collectors for Tok/Err
$Tokens = $ParsingErrors = $null
# Parse ScriptPane
$AST = [System.Management.Automation.Language.Parser]::ParseInput($ScriptPane,[ref]$Tokens,[ref]$ParsingErrors)
# WTF!?
$WTF = '¯\_(ツ)_/¯ "Oups!?"'
if($PSCmdlet.ParameterSetName -eq 'TreeByType')
{
$AstFunc = {param($ChildAst) $ChildAst -is ("System.Management.Automation.Language.$TreeType" -as [Type])}
}
else
{
$AstFunc = {$true}
}
$ExtentBoundaryFilter = {
$ParentTree -eq $Null -or ($_.Extent.StartOffset -ge $ParentTree.Extent.StartOffset -and $_.Extent.EndOffset -le $ParentTree.Extent.EndOffset)
}
$TreesFiltered = $AST.FindAll($AstFunc,$Recurse) | Where-Object $ExtentBoundaryFilter
  $TokensFiltered = $Tokens |Where-Object $ExtentBoundaryFilter
### ACTION
if($PSCmdlet.ParameterSetName -eq 'NoParam')
{
Help SyntaxISEr
}
elseif($PSCmdlet.ParameterSetName -in 'TreeAll','TreeByType')
{
return $TreesFiltered
}
elseif($PSCmdlet.ParameterSetName -eq 'ListTreeType')
{
return $TreesFiltered | ForEach-Object { $_.GetType().Name } | Sort-Object -Unique
}
elseif($PSCmdlet.ParameterSetName -eq 'TokenAll')
{
return $TokensFiltered
}
elseif($PSCmdlet.ParameterSetName -eq 'ListTokenFlag')
{
return $TokensFiltered.TokenFlags | Sort-Object -Unique
}
elseif($PSCmdlet.ParameterSetName -eq 'ListTokenKind')
{
return $TokensFiltered.Kind | Sort-Object -Unique
}
elseif($PSCmdlet.ParameterSetName -eq 'TokenByKind')
{
return $TokensFiltered | Where-Object { $_.Kind -eq "$TokenKind" }
}
elseif($PSCmdlet.ParameterSetName -eq 'TokenByFlag')
{
return $TokensFiltered | Where-Object { $_.TokenFlags -eq "$TokenFlag"}
}
elseif($PSCmdlet.ParameterSetName -eq 'ErrorAll')
{
return $ParsingErrors
}
else
{
Write-Host $WTF -ForegroundColor Cyan
}
}
#endregion SyntaxISEr
#######################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.