Reputation: 33
I built a module to count the lines of code (LOC) of a Java project. For this purpose I had to ignore :
I achieved the first two using the list comprehension on the file lines with regexes and I solved also the third point visiting the whole file strings with the proper pattern matching and substitution. I was wondering, is there a better and/or more performant way to reach the same goal?
PS: I opted for substitution, even if it is heavier than counting and subtraction, due to the fact that multiline comments can be intertwined with actual code in the same line. An example of tricky multiline comments can be:
String test2 = "abc /* fake comment*/";
String cde = "this is a test";//an inline comment
String efg = "ciccio"; /*this is a
weird comment*/ String hil = "pluto";
Upvotes: 1
Views: 681
Reputation: 6696
Yes, you can try different ways.
lexical SingleLineComment = "//" ~[\n] "\n";
and lexical OtherStuff = ![\\]+ !>> ![\\]
. The parse tree that comes out can be visited to count the size of all the comments and you could subtract that from the total amount.lang::java
, and similarly analyze the parse tree^
and $
, so that the visit
you wrote becomes faster.It's advisable to enable the Rascal CPU profiler on the REPL: :set profiling true
and look where the actual bottleneck AST node is in the profile which is printed after running a test.
Upvotes: 1