Counting Source Lines of Code
1 Of a Syntax Object
syntax-sloc
2 Of a #lang file
lang-file-sloc
3 Of a directory
directory-sloc
4 Of a package
pkg-sloc
5 On the command line:   raco sloc
7.1

Counting Source Lines of Code

source code: https://github.com/AlexKnauth/syntax-sloc

 (require syntax-sloc) package: syntax-sloc

1 Of a Syntax Object

 (require syntax-sloc/syntax-sloc) package: syntax-sloc

procedure

(syntax-sloc stx)  natural-number/c

  stx : syntax?
Counts the number of source lines of code in the syntax object stx, not counting whitespace or comments.

It does this by going through every syntax object within it, including every sub-expression, looking at the syntax-line of each one, and counting how many different lines are there.

>

 

(require syntax-sloc)

>

 

(syntax-sloc #'(define (distance x1 y1 x2 y2)
                 ; the distance between the two points is the length
                 ; of the hypotenuse, which is sqrt[(Δx)^2 + (Δy)^2]
                 (sqrt (+ (* (- x2 x1) (- x2 x1))
                          (* (- y2 y1) (- y2 y1))))))

3

2 Of a #lang file

 (require syntax-sloc/lang-file-sloc)
  package: syntax-sloc

procedure

(lang-file-sloc path-string)  natural-number/c

  path-string : path-string?
Counts the number of source lines of code in the file that path-string points to. This file must start with either a valid #lang line or a racket module form.

>

 

(require syntax-sloc)

>

 

(current-directory (path-only (collection-file-path "lang-file-sloc.rkt" "syntax-sloc")))

>

 

(lang-file-sloc "lang-file-sloc.rkt")

14

>

 

(lang-file-sloc "scribblings/syntax-sloc.scrbl")

105

3 Of a directory

 (require syntax-sloc/directory-sloc)
  package: syntax-sloc

procedure

(directory-sloc path-string    
  [#:use-file? use-file?])  natural-number/c
  path-string : path-string?
  use-file? : (-> path? boolean?) = (λ (path) #t)
Counts the number of source lines of code in all #lang files recursively contained inside the directory that path-string points to, except for the ones that use-file? returns false for.

>

 

(require syntax-sloc)

>

 

(current-directory (path-only (collection-file-path "lang-file-sloc.rkt" "syntax-sloc")))

>

 

(directory-sloc (current-directory))

421

>

 

;; ext is a byte string containing the expected extension, without the dot
(define ((has-extension? ext) path)
  (equal? (filename-extension path) ext))

>

 

(directory-sloc (current-directory)
                #:use-file? (has-extension? #"rkt"))

316

>

 

(directory-sloc (current-directory)
                #:use-file? (has-extension? #"scrbl"))

105

4 Of a package

 (require syntax-sloc/pkg-sloc) package: syntax-sloc

procedure

(pkg-sloc name [#:use-file? use-file?])  natural-number/c

  name : string?
  use-file? : (-> path? boolean?) = (λ (path) #t)
Counts the number of source lines of code in all #lang files provided by the package with the given name, except the ones that use-file? returns false for.

5 On the command line: raco sloc

The raco sloc command counts source lines of code in files or directories named on the command line and prints results. If an argument to raco sloc is not a #lang file or a directory, its line count is not computed.

Available flags:
  • --lang lang-pregexp Only count lines for files whose #lang string exactly matches the lang-pregexp regular expression. For example --lang racket will match #lang racket and #!racket but not #lang racket/base or #lang sweet-exp racket.