Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTParser having problems with line comments(problem parsing source string with comments)
ASTParser having problems with line comments [message #631959] Mon, 11 October 2010 02:45 Go to next message
Aniket Dahotre is currently offline Aniket DahotreFriend
Messages: 2
Registered: October 2010
Junior Member
When I run the following code, the CompilationUnit object cUnit does not have any contents. And this happens only in case of the source string having line comments ( // ). The code works fine in absence of comments.

void TestParser(String currFile) {
    ASTParser _astParser;
    _astParser = ASTParser.newParser(AST.JLS3);
    _astParser.setKind(ASTParser.K_COMPILATION_UNIT);
    _astParser.setSource(currFile.toCharArray());
    _astParser.setStatementsRecovery(true);
    CompilationUnit cUnit = (CompilationUnit) _astParser.createAST(null);
    cUnit.imports(); /*To test whether cUnit has any data*/
}


Following is the String currFile

//Java Examples 
package org.kodejava.example.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {
    public static void main(String[] args) {
        //
        // Create an instance of File for data.txt file.
        //
        File file = new File("data.txt");
        
        try {
            //
            // Create a new Scanner object which will read the data from the 
            // file passed in. To check if there are more line to read from it
            // we check by calling the scanner.hasNextLine() method. We then
            // read line one by one till all line is read.
            //
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}
 //More examples on java.util
Re: ASTParser having problems with line comments [message #631973 is a reply to message #631959] Mon, 11 October 2010 06:17 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
I may be missing somethings, but this test case works for me.
Re: ASTParser having problems with line comments [message #633725 is a reply to message #631973] Tue, 19 October 2010 06:47 Go to previous messageGo to next message
Daniela da Cruz is currently offline Daniela da CruzFriend
Messages: 1
Registered: October 2010
Junior Member
Hello.

I am having a problem or an issue with the ASTParser.
I have the following very basic example:

public class Hello {
    public static void main(String[] args) {
        /* some comment inside method
         *  to test,
         */
        int a;
    }
}


And the AST parser doesn't show it in the tree.
When printing the AST tree it shows:

  CompilationUnit {
    TypeDeclaration {
      Modifier {
      }
      SimpleName {
      }
      MethodDeclaration {
        Modifier {
        }
        Modifier {
        }
        PrimitiveType {
        }
        SimpleName {
        }
        SingleVariableDeclaration {
          ArrayType {
            SimpleType {
              SimpleName {
              }
            }
          }
          SimpleName {
          }
        }
        Block {
          VariableDeclarationStatement {
            PrimitiveType {
            }
            VariableDeclarationFragment {
              SimpleName {
              }
            }
          }
        }
      }
    }
  }


Can anyone give me an hint about what is going one?

thanks
daniela

[Updated on: Tue, 19 October 2010 06:48]

Report message to a moderator

Re: ASTParser having problems with line comments [message #634676 is a reply to message #633725] Fri, 22 October 2010 17:23 Go to previous message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Comments are not part of the tree, but they can be retrieved from the compilation unit.
--
Olivier
Previous Topic:How to find current file from editor in Project Explorer?
Next Topic:Using ASTParser outside Eclipse
Goto Forum:
  


Current Time: Wed Apr 24 18:08:26 GMT 2024

Powered by FUDForum. Page generated in 0.03198 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top