• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Powershell ISE的抽象语法树编程示例

Powershell ISE的抽象语法树编程示例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了抽象语法树,编译原理抽象语法树,抽象语法,抽象语法书,语法的抽象性等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

有一个让我非常喜欢Windows PowerShell ISE的理由,就是它将它的基础脚本对象模型暴露给用户,这样就允许用户按照自己的方式和需要去自定义脚本体验。
自定义ISE的核心是$psISE对象。$psISE对象允许用户去控制ISE许多方面的功能。你可以从这里获取关于$psISE的分层对象模型的介绍,和与这些对象相关联的功能。

这篇文章会讨论你怎样利用PowerShell公开提供的解释器接口,来结合ISE对象模型魅力,去创建脚本分析和快速定位的工具。

想象一下,你不得不分析一个相对庞大的PowerShell脚本。那这个脚本可能是别人写的,也有可能是你自己几个月前写的,扔了好久了。PowerShell ISE已经做了件非常棒的工作了,它提供了脚本环境。你可以通过添加Add-On(附加工具)来扩充它的功能,让你的脚本体验更好,更高效。从PowerShell 3.0开始,脚本的抽象语法树(AST)就可以使用语法解释器接口非常方便的获取了。下面的脚本行会获取当前打开的ISE中的脚本的AST:
$AbstractSyntaxTree = [System.Management.Automation.Language.Parser]::
ParseInput($psISE.CurrentFile.Editor.Text, [ref]$null, [ref]$null)
</div>

接下来让我们查询脚本中所有的函数:

$functionsInFile = $AbstractSyntaxTree.FindAll({$args[0] -is
 [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
</div>

撇开函数定位的定义,如果我们能回到光标之前出现的位置,那将太漂亮了。实现这个也非常简单。我们所要做的只是存储这些行号,然后按照反转顺序反转他们。(是否有人已经知道了,“堆栈”)

下面的脚本块展示了展示了Go-To Definition的实现。
#Define some useful global variables
 
$global:__ISEGoToAddOncurrLine=1
 
$global:__ISEGoToAddOncurrcol=1
 
$global:__ISEGoToAddOnlineToGoTo=1
 
$global:__ISEGoToAddOncolToGoTo=1
 
#We need two stacks - one each for line and column
 
$global:__ISEGoToAddOnstackOfLine = New-Object System.Collections.Stack
 
$global:__ISEGoToAddOnstackOfCol = New-Object System.Collections.Stack
 
#This script block has the logic for the implementation of the Go-To definition functionality
 
$global:__ISEGoToAddOnscriptBlockGoTo =
 
{
 
$AbstractSyntaxTree =[System.Management.Automation.Language.Parser]::ParseInput($psISE.CurrentFile.Editor.Text,[ref]$null, [ref]$null)
 
$functionsInFile = $AbstractSyntaxTree.FindAll(
 
{$args[0] -is[System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
 
#Get the text of the line where we have the cursor
 
$str = $psISE.CurrentFile.Editor.CaretLineText
 
#Store them on the stack for later use
 
$global:__ISEGoToAddOnstackOfLine.Push($psISE.CurrentFile.Editor.CaretLine)
 
$global:__ISEGoToAddOnstackOfCol.Push($psISE.CurrentFile.Editor.CaretColumn)
 
$global:__ISEGoToAddOncurrLine = $global:__ISEGoToAddOnstackOfLine.Peek()
 
$global:__ISEGoToAddOncurrcol = $global:__ISEGoToAddOnstackOfCol.Peek()
 
#Get the selected text so that it can be used for searching existing functions
 
$selectedFunction = $psISE.CurrentFile.Editor.SelectedText
 
#Ensure that the cursor is somewhere between the word boundaries of the function
 
$functionsInFile | %{if(($str.Contains($_.name)) `
 
–and ($global:__ISEGoToAddOncurrcol -ge
 
$str.IndexOf($_.name)) `
 
-and ($global:__ISEGoToAddOncurrcol -le
 
($str.IndexOf($_.name)+$_.name.length))
 
)
 
{$selectedFunction = $_.name}
 
}
 
if($selectedFunction -ne "")
 
{
 
#See if the selected function exists in the current open file
 
$functionToGoTo = $functionsInFile | ?{$_.name -eq "$selectedFunction"}
 
$global:__ISEGoToAddOnlineToGoTo = $functionToGoTo.Extent.StartLineNumber
 
$global:__ISEGoToAddOncolToGoTo = $functionToGoTo.Extent.StartColumnNumber
 
}
 
if($functionToGoTo -eq $null)
 
{
 
try
 
{
 
$comm = Get-Command -Name "$selectedFunction" -ErrorAction SilentlyContinue
 
$comm.Definition | Out-GridView
 
}
 
catch [System.Exception]
 
{
 
}
 
}
 
else
 
{
 
#Select the function definition, assuming the function name immediately follows the keyword 'function'
 
try
 
{
 
$psise.CurrentFile.Editor.Select($global:__ISEGoToAddOnlineToGoTo,
 
($global:__ISEGoToAddOncolToGoTo+9),
 
$global:__ISEGoToAddOnlineToGoTo,
 
($global:__ISEGoToAddOncolToGoTo+8+$selectedFunction.length+1))
 
}
 
catch [System.Exception]
 
{
 
}
 
}
 
}
</div>

补充一下,Go-To Definition 功能,如果当前Powershell会话中存在的话,以上脚本会显示选中文本的定义。(另外,上面的脚本只是一个简单的例子,假如你的“function”关键字和函数名出现在脚本的同一行。这在PowerShell中并不是必须的,所以如果你的脚本风格不同,你可能需要微调一下逻辑。)

接下来应当是在Add-on(附加工具)菜单上添加这些脚本,并把它作为选中脚本的一个命令。下面两行就可以做这件事。
$global:__ISEGoToAddOnsb1 =
{& $global:__ISEGoToAddOnscriptBlockGoTo | Out-Null}
$null=$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
"Go do definition", $global:__ISEGoToAddOnsb1, "F12")
</div>

现在来看看我们怎样实现Go-Back 功能,使用我们定义的全局堆栈,几行代码即可:

$global:__ISEGoToAddOnscriptBlockGoBack =
 
{
 
try
 
{
 
#Pop the line and column numbers from the stack to do a reverse traversal
 
$global:__ISEGoToAddOncurrLine =
 
$global:__ISEGoToAddOnstackOfLine.Pop()
 
$global:__ISEGoToAddOncurrcol =
 
$global:__ISEGoToAddOnstackOfCol.Pop()
 
$psISE.CurrentFile.Editor.SetCaretPosition(
 
$global:__ISEGoToAddOncurrLine, $global:__ISEGoToAddOncurrcol)
 
$psISE.CurrentFile.Editor.SelectCaretLine();
 
}
 
catch [System.Exception]
 

您可能想查找下面的文章:

  • Powershell ISE的抽象语法树编程示例

相关文章

  • PowerShell中查看当前版本、Windows版本、.NET版本信息的代码
  • PowerShell 4.0实现自动化设置服务器
  • PowerShell查看Windows功能选项的方法
  • PowerShell中正则表达式使用例子
  • Powershell访问SQL Server数据库代码实例
  • PowerShell脚本反引号用法实例:随时随地给代码换行
  • Powershell生成Windows密码算法简单学习
  • PowerShell在控制台输出特殊符号的方法
  • PowerShell小技巧之同时使用可选强制参数
  • Powershell使用WPF技术实现弹窗提示实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell中查看当前版本、Windows版本、.NET版本信息的代码
    • PowerShell中定义多行字符串变量的方法
    • Powershell Profiles配置文件的存放位置介绍
    • Powershell小技巧之通过EventLog查看近期电脑开机和关机时间
    • PowerShell编程中的一些命名规则参考
    • Powershell小技巧之系统运行时间
    • Powershell小技巧之编辑Hosts文件
    • Powershell从注册表中查询默认MAPI客户端的例子
    • PowerShell脚本中查看网卡的高级属性
    • Powershell获取图片名字、文件夹及拍摄时间的例子

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有