8.18
top
← prev up next →

Counting Source Lines of CodeπŸ”— i

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

1Of a Syntax ObjectπŸ”— i

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.

>

(syntax-sloc #' (define (distancex1y1x2y2)
;thedistancebetweenthetwopointsisthelength
;ofthehypotenuse,whichissqrt[(Δx)^2+(Δy)^2]
(sqrt (+ (* (- x2x1)(- x2x1))
(* (- y2y1)(- y2y1))))))

3

2Of a #lang fileπŸ”— i

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.

>

(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

3Of a directoryπŸ”— i

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.

>

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

421

>

;;extisabytestringcontainingtheexpectedextension,withoutthedot
(define ((has-extension?ext)path)

>

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

316

>

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

105

4Of a packageπŸ”— i

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.

5On the command line: raco slocπŸ”— i

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.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /