我正在研究用 Java 实现的 JavaScript 整理器/合成器。它有效,但必须有更好的方法来实现它,我认为 Lexer 可能是前进的方向,但我有点模糊。
我为合成器开发了一种元语法,它是 JavaScript 语言的一个子集。就典型的 JavaScript 解释器而言,合成器元语法是合法的,只是不起作用(我使用保留字的同义词作为标签,后跟合成器应该解释的代码块)。现在,我正在使用扫描仪和正则表达式来查找源文件中的元语法,然后根据合法表达式的检测进行浅层词法转换。
重写的 javascript 和我不满意的扫描器/解析器之间存在紧密耦合,因为重写的 javascript 使用了专门为此目的编写的对象支持库的功能,并且该库可能会发生变化。
我希望我可以只在 Backaus-Naur 或 EBNF 中声明元语法,将其提供给词法分析器(ANTRL?),并根据在源文件中检测到的元语法表达式,将合成器定向到某些操作,例如将所需的脚本附加到另一个脚本,声明变量,为适当参数化的库函数调用生成文本,甚至压缩脚本。
这是制作合成器的合适方法吗?我是否应该使用 Scanner/Parser/Lexer 方法来合成 JavaScript?任何反馈表示赞赏-我不太确定从哪里开始:)
更新:这是一个更具体的示例——使用元语法的示例对象声明:
namespace: ie.ondevice
{
use: ie.ondevice.lang.Mixin;
use: ie.ondevice.TraitsDeclaration;
declare: Example < Mixin | TraitsDeclaration
{
include: "path/to/file.extension";
// implementation here
}
}
这描述了对象 ie.ondevice.Example,它继承了 Mixin 并类似于(即“实现相同的功能和特征”)TraitsDeclaration。合成器将检测 use 语句,如果命名空间没有映射到有效的文件位置,则合成器会失败,或者在对象声明所在的脚本之前添加,在排序之前预处理元语法。
用我提到的对象支持库表示的重写规则将导致文件看起来像这样(我已经开发了多种表示对象的方法):
module("ie.ondevice.Example", function (mScope)
{
// mScope is a delegate
mScope.use("ie.ondevice.lang.Mixin");
mScope.use("ie.ondevice.TraitsDeclaration");
// As a result of two use statements, the mScope.localVars string would
// would look like this: "var Mixin= ie.ondevice.lang.Mixin, TraitsDeclaration= ie.ondevice.TraitsDeclaration
// by evaling we introduce 'imported' objects with their 'local'names
eval(mScope.localVars);
// Function.prototype has been extended with the functions
// inherits, define, defineStatic, resembles and getName
// Prototypal inheritance using an anonymous bridge constructor
Example.inherits(Mixin);
// named methods and properties are added to Example.prototype
Example.define
(
// functions and other properties
);
// ensures that Example.prototype has all the same
// property names and types as TraitsDeclaration.prototype
// throwing an exception if not the case.
// This is optionally turned off for production- these
// operations are only performed when the object is declared
// - instantiation incurs no additional overhead
Example.resembles(TraitsDeclaration);
// constructor
function Example ()
{
Mixin.call(this);
};
// will generate the ie.ondevice object hierarchy if required
// and avail the constructor to it
mScope.exports(Example);
});
也许我过度构建了我的需求,但我真正想要的是一个事件驱动的整理器——然后侦听器可以松散地耦合到指令检测。