如何使用V8或Rhino进行JS语法检查?

3

我想使用像V8或Rhino这样的JS引擎来执行语法检查,而不实际执行代码。是否可以使用命令行版本或相应的库实现?有没有相关的文档可以帮助我?


你是否排除了使用像JSHintJSLint这样的工具的可能性? - Jonathan Lonowski
@JonathanLonowski 不,它们是否捕获所有语法错误还是只有一些?我不需要样式强制器,因为涉及的代码是机器生成的。 - Alexei Averchenko
1
请查看:https://dev59.com/yXE85IYBdhLWcg3w436o - jcubic
@jcubic 我不需要一个用JS编写的JS编译器,特别是如果它没有像v8或rhino那样经过充分测试。 - Alexei Averchenko
@AlexeiAverchenko 不是JS编译器,而是JS解析器。但如果你想使用V8,那么你需要阅读代码并在其中找到解析器。我认为使用JavaScript编写的解析器更简单。 - jcubic
1个回答

0

我在v8方面取得了一些有限的成功:

/*
 * main.cpp
 *
 *  Created on: Aug 22, 2013
 *      Author: kallikanzarid
 */

#include <v8.h>
#include <iostream>

int main() {
    using namespace std;
    using namespace v8;

    //
    // See https://developers.google.com/v8/get_started
    //

    // Create a stack-allocated handle scope.
    HandleScope handle_scope;

    // Create a new context.
    Handle<Context> context = Context::New();

    // Here's how you could create a Persistent handle to the context, if needed.
    Persistent<Context> persistent_context(context);

    // Enter the created context for compiling and
    // running the hello world script.
    Context::Scope context_scope(context);

    Local<String> source = String::New("function foo(x { console.log(x); }; foo('bar')");
    Local<Script> script = Script::New(source);

    persistent_context.Dispose();

    return 0;
}

希望你们能够打败这个基本上是二进制语法检查器。如果没有更好的方法,我可能会尝试通过捕获异常并尝试使输出可机读来改进它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接