count lines of Function that don't consist solely of comments

150 views Asked by At

I want to count lines of Function that don't consist solely of comments.

extrait of .jj file :

    options
{
    ……..
  COMMON_TOKEN_ACTION = true ;
  LOOKAHEAD= 2;
}
PARSER_BEGIN(MyParseur)
……………
PARSER_END(MyParseur)

TOKEN_MGR_DECLS:
{


    static  int interestingLineCount=0;
    static int lineNumberOfLastInterestingLine=0;
    static Map <Integer, Integer> f = new HashMap<Integer, Integer>();
    void CommonTokenAction(Token t)
    {

       if(t.beginLine != lineNumberOfLastInterestingLine)
       {
        interestingLineCount++;

       lineNumberOfLastInterestingLine= t.beginLine;
       f.put(lineNumberOfLastInterestingLine, interestingLineCount);

       }
    }
}


    void  MyFunction : { int firstLine, lastLine;}
{
<begin> <id> "(" (Argument ())* ")"

{m = getToken(1).beginLine ; }

(Statement ())*

{n = getToken(0).beginLine ; }


<end>
}

My question how I can utilized CommonTokenAction defined in TOKEN_MGR_DECLS to calculte the number of lignes of MyFunction(). Thank your in advance.

1

There are 1 answers

5
Theodore Norvell On BEST ANSWER

I'm going to assume that blank lines are considered to only consist of comments. (Because they do.) Secondly, I'll assume that comments are either skipped or special tokens. Let's call the first line that contains a token interesting line 1, the second one interesting line 2, and so on.

What I would do is count the noncomment lines of the file.

  • Create a new field, say interestingLineCount, in the token manager. This counts lines that contain tokens other than comments. Initialize it to 0.

  • Create a second field in the token manager, that records the actual line number the last time interestingLineCount was incremented. Call it lineNumberOfLastInterestingLine. E.g. if interestingLineCount is 10, then lineNumberOfLastInterestingLine is the line number of the 10th interesting line. Initialize it to 0.

  • Create a field in the token manager than represents a mutable map from int to int. I'll call it f.

  • Create a common token action that does the following. If the token starts on lineNumberOfLastInterestingLine, there is nothing to do, as the current line is already known to be interesting. Otherwise: increment interestingLineCount, set lineNumberOfLastInterestingLine to the line number of the token, and add (lineNumberOfLastInterestingLine, interestingLineCount) to the map.

  • Now the first and last token of the function have line numbers, say m and n. The number of lines in the function definition is f(n)-f(m)+1.