Writing a lexer of .java source files in Java. I have a stream of characters and I trying to make the lexer skip single-line comments.
I loop through each char and my hypothesis is that it should be possible to first detect the // of the comment and then skip subsequent chars until the next new line character. But it cannot work and I cannot detect any new line character. This is my code:
//is it a single line comment?
if(currentChar == '/') {
//loop through char:s until next new line
while(inComment == true) {
//increment loop
i++;
//extract next char
currentChar = stringInput.charAt(i);
//check if current character is a new line
if(( currentChar == '\n' ) || ( currentChar == '\r' )) {
inComment = false;
System.out.println("End Of Line Comment.");
}
}
}
So, does .java source files have new line characters? Is it possible to detect them using the Character class or in any other way?
Many thanks in advance!
SOLUTION:
The new line characters seem to been lost while reading the code from the .java source file using a BufferedReader and appending the lines to a StringBuilder. The problem was solved by instead reading the .java file using readFileToString() from org.apache.commons.io.FileUtils which worked a charm!
That should work, the comparison currentChar == ‘\n’ should work fine and return true when you reached the end of the line.
Are you sure that your line breaks don’t get lost already when reading in the file, e.g. by using BufferedReader.readLine()? If that could be the case, try another way to read the file into a String, e.g. use FileUtils.readFileToString from the jakarta commons-io.