可以使用
Javascript编写可以支持其他类型的语言作为脚本的编译器吗?
假设我有一块HTML.
<script language="cpp" id="cppScriptBlock" EntryPoint="main"> int main() { cout << "<h1>CPPHeader</h1>"; } </script> <script language="java" id="javaScriptBlock" EntryPoint="MyJavaClass"> public class MyJavaClass { public final void main() { java.lang.System.out.println("<h1>JavaHeader</h1>"); } } </script> <script language="csharp" id="csharpScriptBlock" EntryPoint="MyCSharpClass "> public class MyCSharpClass { public static void Main() { System.Console.WriteLine("<h1>CSharpHeader</h1>"); } } </script> <script language="javascript"> $("#cppScriptBlock").compileAndRun(); $("#javaScriptBlock").compileAndRun(); $("#csharpScriptBlock").compileAndRun(); </script>
最后生成以下HTML
<h1>CPPHeader</h1> <h1>JavaHeader</h1> <h1>CSharpHeader</h1>
可能吗?
亚历克斯
解决方法
是的,这很可能使用
Jison.
它基于您定义的语言结构生成JavaScript解析器.
Jison takes a context-free grammar as input and outputs a JavaScript
file capable of parsing the language described by that grammar. You
can then use the generated script to parse inputs and accept,reject,
or perform actions based on the input.
PS:CoffeeScript!也是使用这个创建的.