C# .NET 4.5 中的 JavaScript 解析器和分析器 [英] JavaScript Parser and Analyzer in C# .NET 4.5

查看:29
本文介绍了C# .NET 4.5 中的 JavaScript 解析器和分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定标题是否正确解释了它.

无论如何,我正在构建一个 .NET WPF 应用程序,它应该通过 JavaScript 并识别诸如

之类的问题

  1. 如果定义的变量在最后被取消
  2. 如果正在使用 try/catch/finally 块.
  3. 函数调用

我在这里浏览了所有围绕 c/c++ 的问题.现在我后悔把我的编译器类搞砸了.

我想知道如何在 C# 中验证第 1-3 点.有没有图书馆可以做到这一点?

解决方案

您正在寻找的是 抽象语法树用C#编写的Javascript解析器.

我知道有几个选择:

Microsoft 的 Ajax Minifier 库带有自己的 AST 解析器(用于缩小/优化 Javascript 文件).您可以在 Codeplex 上找到其源代码.>

Esprima.net 是另一种选择.它是流行的 Javascript 库 Esprima 的移植版.

Esprima 的好处在于它以通用格式输出 AST(由 Mozilla 定义 此处),它在几个解析器中使用,使得移植用于遍历树等的实用程序变得非常容易,因为它们都使用相同的底层数据结构.

Not sure if the title explains it correctly.

Anyways, I'm building a .NET WPF application which should go through the JavaScript and identify issues such as

  1. If the variables defined are being nullified at the end
  2. If try/catch/finally blocks are being used.
  3. Function calls

I went through the questions over here which were all revolving around c/c++. Now I regret bunking my compilers classes.

I wanted to know how to verify points 1-3 in C#. Any library out there which does this?

解决方案

What you're looking for is an abstract syntax tree parser for Javascript written in C#.

There are a few choices I know of:

Microsoft's Ajax Minifier library comes with its own AST parser (used to minify / optimize Javascript files). You can find the source code for that on Codeplex.

Esprima.net is another option. It's a port of the popular Javascript library Esprima.

The good thing about Esprima is it outputs the AST in a common format (defined by Mozilla here) that's used across a few parsers, making it really easy to port utilities for walking the tree, etc. since they all use the same underlying data structure.

这篇关于C# .NET 4.5 中的 JavaScript 解析器和分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆