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?
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?
>
(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)
>
(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)
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.
--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.