-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathConvertToAst.ps1
More file actions
87 lines (82 loc) · 4.16 KB
/
ConvertToAst.ps1
File metadata and controls
87 lines (82 loc) · 4.16 KB
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
function ConvertToAst {
<#
.SYNOPSIS
Parses the given code and returns an object with the AST, Tokens and ParseErrors
#>
[CmdletBinding(DefaultParameterSetName = "Path")]
param(
# The script content, or script or module file path to parse
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "Code", Position = 0)]
[Alias("ScriptBlock")]
$Code,
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = "Command", Position = 0)]
[System.Management.Automation.FunctionInfo]$Command,
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = "Path", Position = 0)]
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = "Command", Position = 1)]
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = "Code", Position = 1)]
[Alias("PSPath", "File", "Definition")]
[string]$Path
)
process {
Write-Debug " ENTER: ConvertToAst $Path $(("$Command$Code" -split "[\r\n]")[0])"
$ParseErrors = $null
$Tokens = $null
switch($PSCmdlet.ParameterSetName) {
"Path" {
$Provider = $null
try {
$Path = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath($Path, [ref]$Provider)[0]
} catch {
Write-Debug (" Exception resolving $Path as Path " + $_.Exception.Message)
}
if ($Path -and $Provider.Name -eq "FileSystem") {
Write-Debug " Parse File Path: $Path"
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$Tokens, [ref]$ParseErrors)
} else {
Write-Debug " Parse Path as Code $(($PSBoundParameters.Path -split "[\r\n]")[0])"
$AST = [System.Management.Automation.Language.Parser]::ParseInput($PSBoundParameters.Path, [ref]$Tokens, [ref]$ParseErrors)
$Path = "scriptblock"
}
}
"Command" {
Write-Debug " Parse Function"
if (!$Path) {
$Path = "function:$($Command.Name)"
}
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Command.Definition, $Path, [ref]$Tokens, [ref]$ParseErrors)
}
"Code" {
Write-Debug " Parse Code as ScriptBlock"
if ($Code -is [System.Management.Automation.ScriptBlock]) {
$Code = $Code.GetNewClosure().ToString()
if (!$Path) {
$Path = "scriptblock"
}
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Code, $Path, [ref]$Tokens, [ref]$ParseErrors)
} else {
$Provider = $null
try {
$Path = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath($Code, [ref]$Provider)[0]
} catch {
Write-Debug (" Failed to resolve Code as Path " + $_.Exception.Message)
}
if ($Path -and $Provider.Name -eq "FileSystem") {
Write-Debug " Parse File Path: $Path"
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$Tokens, [ref]$ParseErrors)
} else {
Write-Debug " Parse Code $(($Code -split "[\r\n]")[0])"
$AST = [System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$Tokens, [ref]$ParseErrors)
}
}
}
}
Write-Debug " EXIT: ConvertToAst $Path"
[PSCustomObject]@{
PSTypeName = "PoshCode.ModuleBuilder.ParseResults"
ParseErrors = $ParseErrors
Tokens = $Tokens
Path = $Path
AST = $AST
}
}
}