Re: [isabelle] sloccount for .thy files?

Dear Rafal,

def lines_without_comments(text: String): Int =
   val newlines =
     for {
       token <- Outer_Syntax.empty.scan(text).iterator
       if !token.is_comment
       c <- token.source.iterator
       if c == '\n'
     } yield 1
   val n = (0 /: newlines)(_ + _)
   if (text.endsWith("\n")) n else n + 1
Scala's "for" is more or less like Haskell's list comprehension. The above for-expression does the following:
- iterate over all tokens,
- omit comment tokens
- for every other token, iterate through its characters
- only consider newline characters

Furthermore, I think there is a typo in the script, the last if-then-else should have swapped branches, i.e., add 1 if the file ends with a newline character.

After correcting the typo, the script counts all newlines that are not part of comments. Your attached Scratch.thy had 20 lines. It contains only a single comment (at least what _.is_comment considers as comment), namely (* more comments\n *), which just contains a single new-line, thus the script is right.

To see how the different tokens are categorized you can do the following:

$ isabelle scala
import isabelle._
val text ="Scratch.thy").mkString
val tokens = Outer_Syntax.empty.scan(text).toList
tokens.foreach { x => print(x.toString + "\n") }

hope this helps


This archive was generated by a fusion of Pipermail (Mailman edition) and MHonArc.