轻量级javascript到javascript解析器 [英] lightweight javascript to javascript parser

查看:181
本文介绍了轻量级javascript到javascript解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去写一个轻量级javascript到javascript解析器。一些简单的东西可以转换一些代码片段。

How would I go about writing a lightweight javascript to javascript parser. Something simple that can convert some snippets of code.

我想基本上使内部范围对象在函数中public。

I would like to basically make the internal scope objects in functions public.

这样的东西

var outer = 42;
window.addEventListener('load', function() {
   var inner = 42;
   function magic() {
       var in_magic = inner + outer;
       console.log(in_magic);
   }
   magic();
}, false);

将编译为

__Scope__.set('outer', 42);
__Scope__.set('console', console);
window.addEventListener('load', constructScopeWrapper(__Scope__, function(__Scope__) {
    __Scope__.set('inner', 42);
    __Scope__.set('magic',constructScopeWrapper(__Scope__, function _magic(__Scope__) {
        __Scope__.set('in_magic', __Scope__.get('inner') + __Scope__.get('outer'));
        __Scope__.get('console').log(__Scope__.get('in_magic'));
    }));
    __Scope__.get('magic')();
}), false);

Demonstation示例

这里的动机是序列化函数和闭包的状态,并使它们在不同的机器(客户端,服务器,多个服务器)。为此,我需要 [[Scope]]

Motivation behind this is to serialize the state of functions and closures and keep them synchronized across different machines (client, server, multiple servers). For this I would need a representation of [[Scope]]

问题:


  1. 无需编写完整的JavaScript - >(略有不同)的JavaScript编译器,我可以做这种编译器吗?

  2. 我如何编写这样的编译器?

  3. 我可以重用现有的js - > js编译器吗?


推荐答案

如果你想要一个简单的界面,你可以尝试node-burrito: https://github.com/substack/node-burrito

If you want something with a simple interface, you could try node-burrito: https://github.com/substack/node-burrito

它使用uglify生成AST -js解析器,然后递归地遍历节点。所有你需要做的是给单个回调测试每个节点。您可以更改需要更改的那些,并输出结果代码。

It generates an AST using the uglify-js parser and then recursively walks the nodes. All you have to do is give a single callback which tests each node. You can alter the ones you need to change, and it outputs the resulting code.

这篇关于轻量级javascript到javascript解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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