用java语言添加或修改关键字 [英] Add or modify keywords in java language

查看:182
本文介绍了用java语言添加或修改关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的问题似乎不合适,但它是真实的。在编写java时,我必须使用单词 import ,以便从classpath导入类。用户 new 需要在java中启动某些对象和其他关键字。我的问题是,我们是否有最轻微的改进能力和这种优秀的语言,通过定义新的关键词和他们做什么或修改现有的关键词来做同样的事情。例如,而不是写:

I know my question does not seem valid, but it is genuine. When writing java I must use the word import so as to import classes from classpath. It is required to user new to initiate certain objects and other keywords in java. My question is whether we have even the slightest ability to improve and this great language by way of defining new keywords and what they do or modifying the exisiting keyword to do the same thing. For example instead of writing:

import java.io.File;

有什么可能修改 import 以bosnian为例:

What possibility is there to modify the import word to bosnian for example:

uvoziti java.io.File;

它们的工作原理相同。在我得到想法之前请不要关闭。

and it all works the same way. Please do not close before I get ideas.

推荐答案

使用相当复杂的工具链的一种方法可被视为过度杀伤力 ,但不如编写自己的编译器那么多:

One approach that uses a rather sophisticated toolchain and could be considered as an "overkill", but is not as much effort as writing an own compiler or so:

  • Download ANTLR4 from http://www.antlr.org/download.html
  • Download the Java Grammar at https://github.com/antlr/grammars-v4/blob/master/java/Java.g4
  • Modify the Java Grammar according to your needs...
  • Run

java -classpath "antlr-4.4-complete.jar" org.antlr.v4.Tool Java.g4

这将生成一些文件,其中一个是 JavaLexer.java

This will generate some files, one of them being JavaLexer.java.

创建一个如下所示的类,进行翻译:

Create a class like the following, which does the translation:

import java.io.IOException;

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;

public class Main {
    public static void main(String[] args) throws IOException {
        CharStream s = new ANTLRFileStream("Input.javaX");
        JavaLexer lexer = new JavaLexer(s);
        TokenStream t = new CommonTokenStream(lexer);
        int i = 1;
        while (true) {
            if (t.LA(i) == -1) {
                break;
            }
            if (t.LA(i) == JavaLexer.IMPORT) {
                System.out.print("import ");
            } else {
                System.out.print(t.LT(i).getText() + " ");
            }
            i++;
        }
    }
}

(当然,这是只有转换 IMPORT 令牌的示例,该令牌在语法文件中定义为uvoziti。对于更通用和灵活的翻译,可以在外部文件中定义翻译,并且可能会读取此文件以创建地图 Map< Integer,String> JavaLexer.IMPORT 映射到import等......)

(of course, this is only an example that only translates the IMPORT token, which was defined in the grammar file to be "uvoziti". For a more general and flexible translation, one would define the translation in an external file, and probably read this file to create a map Map<Integer, String> that maps JavaLexer.IMPORT to "import" etc...)

从示例中创建输入文件: Input.javaX

Create the input file from the example: Input.javaX:

uvoziti java.io.File;

public class Input
{
    public static void main(String args[])
    {
        File file = null;
        System.out.println("done");
    }
}

当你运行,它将读取此输入文件,最终找到 IMPORT 标记,而不是原始文本( uvoziti )它将打印 import

When you then run the Main, it will read this input file, eventually find the IMPORT token, and instead of the original text (uvoziti) it will print import.

结果将是Java文件的内容,格式错误......

The result will be the contents of a Java file, with an awful formatting...

import java . io . File ; public class Input { public static void main ( String args [ ] ) { File file = null ; System . out . println ( "done" ) ; } } 

但是很有必要,编译器不关心格式化:您可以直接将此输出写入一个 .java 文件,编译器将吞下它。

but fortuntately, the compiler does not care about the formatting: You may write this output directly to a .java file, and the compiler will swallow it.

正如这里所描述的那样,它只是一个概念证明。对于许多(所有)关键字的灵活和通用的翻译,人们将不得不围绕所有关键字构建一些基础设施。应自动读取输入文件( File.listFiles(),递归读取包)。他们每个人都必须翻译(使用前面提到的 Map< Integer,String> )。然后必须使用运行时 JavaCompiler ,或者用 javac /api/java/lang/Runtime.html#exec-java.lang.String-rel =nofollow noreferrer>运行时#exec

As it is described here, it is only a proof of concept. For a flexible and generic translation of many (all) keywords, one would have to build some infrastructure around all that. The input files should be read automatically (File.listFiles(), recursively for packages). Each of them would have to be translated (using the Map<Integer, String> that I mentioned earlier). Then the output files would have to be written and compiled, either with the runtime JavaCompiler, or by manually invoking the javac with Runtime#exec.

但总的来说,我认为在最好的情况下,这应该在几个小时内完成,并且在考虑所有事情都需要比你想象的更长时间的一周内。

But in general, I think that this should be doable within a few hours in the best case, and within one week when considering that everything takes longer than you think.

另一方面,编写自己的Java编译器可能需要更长的时间,即使你认为所有事情都比你想象的还要长......

Writing an own Java compiler, on the other hand, might take a bit longer, even when you consider that everything takes longer than you think...

这篇关于用java语言添加或修改关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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