如何从文本文件中的代码片段中找到一些语法特征 [英] How to find some syntactic features from a code fragment in a text file

查看:66
本文介绍了如何从文本文件中的代码片段中找到一些语法特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件中有代码片段,并且必须找出哪些特征(例如,一行中有一个getter / mutator)与代码片段中的指定行相关联。例如,在给定的代码片段中。



I have code fragments in a text file and have to find out what features (e.g is there a getter/mutator in a line) are associated with a specified line in a code fragment. For example, in a given code fragment.

1 public HTMLEditor() {
2  //install the source configuration
3  setSourceViewerConfiguration(new HTMLConfiguration());
4  //install the document provider
5  setDocumentProvider(new HTMLDocumentProvider()); }
6 protected void createActions() {
7  super.createActions();
8 //... add other editor actions here
9 }}





第2,4和8行包含注释,我可以通过编写以下源代码来检查它。让我们说





Line 2,4 and 8 contains a comment and i can check it simply by writing following source code. Lets say

public boolean containsComment(String line) {
    // if line contains comment
    if(line.contains("//")){
        return true;            
    }else{
        return false;
    }
}





一行可以包含多个功能,例如上面代码段中的第5行有一套方法和新关键字也是如此。此外,只应在源代码中检查诸如new或get等的单词,而不应在注释部分中检查。同样这样的情况有很多;与我的containsComment相同,它更容易检查其他功能,如异常检查,新的或其他关键字检查有点棘手,需要一些复杂的代码来处理。



我有以这种方式解决了大部分功能,除了这三个......我正在解释每个功能,需要立即和紧急的帮助。



包含一个匿名类



A line can contain more than one feature e.g line 5 in above snippet has a set method and new keyword too. Also, words such as new or get or etc should only be checked in source code and not in comments part. Same like this there are many such cases; Same as my containsComment which is easier to check other features such as exception checking, new or other keywords checking are little trickier and needs some complex code to handle.

I have solved most of the features in this way except these three...I am explaining each with an example and need an immediate and urgent help.

Contains an anonymous class

ObjectInterestedInFooObjects() {
   result = someLookup.lookupResult(Foo.class);
    result.addLookupListener(this);
     resultChanged(null);
 }





算法中没有参数

< b>

putValue(Action.Name,NbBundle.getMessage(FooAction.class, "LBL_Action"));





< b>注释中的保留或反射





Retention or reflection in annotation

@ActionID(
         category = "Build",
        id = "com.foo.bar.OnJavaPackageAction")





我期待着为这些提供即时帮助。我认为一些正则表达式可能会帮助我,但我不确定。期待您的帮助。



I am looking forward to immediate help for these. I think some regex might help me but i am not sure. Looking forward to your help.

推荐答案

这篇关于如何从文本文件中的代码片段中找到一些语法特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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